Arduino Servo Control: Sending Position Data Via Serial

by Marco 56 views

Hey folks! Ever wondered how to make your Arduinos talk to each other, especially when it involves something cool like controlling servos? I had this exact question, and I'm excited to share what I've learned about sending servo position data from one Arduino to another using serial communication. It's like teaching your Arduinos to play tag with servo movements! We will be focusing on using two Arduino Uno boards, the serial interface and of course Servos!

Understanding the Basics

Before we dive into the code and connections, let's quickly cover the basics. You know, the stuff that makes this whole thing tick.

  • Arduino Uno: This is our microcontroller of choice. It's like the brain of our operation, executing the code we upload and controlling the servo.
  • Servo Motor: A type of motor that allows for precise angular control. We can tell it to move to a specific position, which is perfect for our project.
  • Serial Communication: This is how our Arduinos will talk to each other. Serial communication involves sending data one bit at a time over a single wire. Think of it as Morse code, but for computers.

Why Serial Communication?

Serial communication is a straightforward method for Arduinos to exchange data. It only requires two pins (TX and RX), making it simple to set up. Plus, Arduino has built-in libraries to handle serial communication, so we don't have to reinvent the wheel.

Components Needed

To follow along with this tutorial, you'll need:

  • Two Arduino Uno boards.
  • One or more servo motors.
  • Jumper wires.
  • A USB cable for each Arduino.

Setting Up the Hardware

Alright, let's get our hands dirty and connect the hardware.

  1. Connect the Servo to the First Arduino:
    • Connect the servo's power (usually red) to the 5V pin on the Arduino.
    • Connect the servo's ground (usually brown or black) to the GND pin on the Arduino.
    • Connect the servo's signal wire (usually yellow or white) to a digital pin on the Arduino (e.g., pin 9).
  2. Connect the Two Arduinos for Serial Communication:
    • Connect the TX pin of the first Arduino to the RX pin of the second Arduino.
    • Connect the RX pin of the first Arduino to the TX pin of the second Arduino.
    • Connect the GND pin of both Arduinos together. This is crucial for establishing a common reference.

Important Notes:

  • Make sure the Arduinos are powered separately via USB or an external power supply. Do not rely on one Arduino to power the other.
  • Double-check your wiring. Incorrect connections can damage your Arduinos or servo.

Writing the Code

Now comes the fun part: writing the code that will make our Arduinos communicate and control the servo.

Arduino 1: Sending the Servo Position

This Arduino will control the servo and send its position to the second Arduino.

#include <Servo.h>

Servo myservo;  // create servo object to control a servo

int pos = 0;    // variable to store the servo position

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  Serial.begin(9600);  // Initialize serial communication at 9600 bps
}

void loop() {
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    Serial.println(pos);             // Send servo position over serial
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    Serial.println(pos);             // Send servo position over serial
    delay(15);                       // waits 15ms for the servo to reach the position
  }
}

Explanation:

  • #include <Servo.h>: Includes the Servo library, which provides functions to control servo motors.
  • Servo myservo;: Creates a Servo object named myservo.
  • int pos = 0;: Declares an integer variable pos to store the servo position.
  • myservo.attach(9);: Attaches the servo to digital pin 9.
  • Serial.begin(9600);: Initializes serial communication at a baud rate of 9600. This is the speed at which the Arduinos will communicate.
  • The loop() function contains two for loops that make the servo sweep from 0 to 180 degrees and back. Inside the loops:
    • myservo.write(pos);: Sets the servo position to the value of pos.
    • Serial.println(pos);: Sends the servo position over serial.
    • delay(15);: Pauses for 15 milliseconds to allow the servo to reach the desired position.

Arduino 2: Receiving the Servo Position

This Arduino will receive the servo position from the first Arduino and control another servo.

#include <Servo.h>

Servo myservo;  // create servo object to control a servo

int pos = 0;    // variable to store the servo position

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  Serial.begin(9600);  // Initialize serial communication at 9600 bps
}

void loop() {
  if (Serial.available() > 0) { // Check if data is available
    pos = Serial.parseInt();      // Read the incoming servo position
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
}

Explanation:

  • The setup() function is the same as in the first Arduino.
  • In the loop() function:
    • if (Serial.available() > 0): Checks if there is any data available to be read from the serial port.
    • pos = Serial.parseInt();: Reads the incoming data from the serial port and converts it to an integer, storing it in the pos variable.
    • myservo.write(pos);: Sets the servo position to the received value.
    • delay(15);: Pauses for 15 milliseconds.

Uploading the Code

  1. Open the Arduino IDE.
  2. Copy and paste the code for Arduino 1 into a new sketch.
  3. Select the correct board and port under the "Tools" menu.
  4. Upload the code to the first Arduino.
  5. Repeat the process for Arduino 2, using the corresponding code.

Testing the Setup

Once you've uploaded the code to both Arduinos, it's time to test the setup. Make sure both Arduinos are powered on and connected as described earlier. You should see the servo on the first Arduino sweep back and forth, and the servo on the second Arduino should follow its movements.

Troubleshooting:

  • If the servos aren't moving, double-check your wiring and make sure the servos are properly connected to the Arduinos.
  • If the servos are moving erratically, make sure the baud rates in both Arduinos are the same (9600 in this example).
  • If you're still having trouble, try adding some debugging code to print the values being sent and received over serial. This can help you identify where the problem lies.

Enhancements and Further Exploration

Now that you've got the basics down, here are some ideas for taking this project to the next level:

  • Multiple Servos: Control multiple servos on both Arduinos.
  • Wireless Communication: Use Bluetooth or other wireless modules to send the servo position data wirelessly.
  • PID Control: Implement a PID controller to improve the accuracy and smoothness of the servo movements.
  • Sensor Feedback: Use sensors to provide feedback to the Arduinos, allowing them to adjust the servo positions based on external conditions.

Conclusion

Sending servo position data from one Arduino to another using serial communication is a fun and educational project. It demonstrates the power of microcontrollers and the versatility of serial communication. With the knowledge you've gained from this tutorial, you can create a wide range of exciting projects, from remote-controlled robots to automated systems. So go ahead, experiment, and have fun building!