Programming 2 Servos Learn to connect and write code to control two servos. Many students who visit the lab and learn how to use a Servo want to use 2 Servos in their project rather than just 1. This lesson will walk you through how to wire the 2 Servos to the Arduino and write code for different servo movements. Getting 2 servos to work together can really open up your options in creating new projects. Think, for example, about a robot that can nod and shake its head!! Note: You may want to look at the instructions on connecting 1 servo before starting this activity. You may also find the Breadboard and Arduino information sheets useful. Arduino 2 Servos (I used one small servo and one big servo, but you could use 2 same sized servos) Note: the color of the wires may be different from mine, but they behave the same. Breadboard 3 red wires 3 black wires 2 white wires
1. Start by connecting one of the red wires from Vin to the positive rail of the breadboard (See Breadboard information sheet). This makes that entire column positive. 2. Next, connect one of the black wires from GND (-) to the negative rail of the breadboard. This makes that entire column negative. 3. Connect the remaining red, and black wires to their respective places on the servos. Connect a white wire to each of the yellow servo wires. 4. Connect both red wires from the servos to the positive rail. 5. Connect both black wires from the servos to the negative rail. 6. Connect the white wire from one servo to pin 9 and the other to pin 10.
1. Start by connecting the Arduino to the computer via the USB cable. Open a new sketch and delete all of the text. Also, don t forget to check that you re connected to the right port. 2. You need to tell Arduino that you are using a Servo. To do this, go to Sketch -> Include Library -> and click on Servo. This brings up #include <Servo.h>. 3. Next you need to name the Servos. Since there are 2, they each need to have a unique name. For example: smallservo and bigservo, if you re using different sized Servos, but you can call them whatever you want.
4. Next is setup. Since there are 2 servos, there will need to be 2 lines in the setup in order to attach our servos to 2 different pins. Reminder: servos always need to be attached to pins on the Arduino with the ~ next to them. (See Arduino information sheet) 5. Now we get to the loop. The following code is for a basic motion where both servos turn 180 degrees back and forth. First tell the both servos to go to 0 degrees (smallservo.write(0);) and (bigservo.write(0);). Next, put a delay so that the Arduino will wait before reading the next line of code and doing a different motion. The delay of 2000 means that the Arduino will wait 2 seconds before reading the next line of code. Next, tell the both servos to move to 180 degrees (smallservo.write(180);) and (bigservo.write(180);). Another delay of 2000 will again tell the Arduino to wait for 2 seconds before reading the next line of code which means that both servos will wait before the loop repeats.
6. If one change is made to the code above, the big servo will spin in only one direction while the small servo spins back and forth. Look at the code for the big servo. Before the delay the big servo was set to 0 degrees (bigservo.write(0);) and then later it was set to 180 degrees (bigservo.write(180);). There is a delay in between those two motions so that the servo has time to rotate before its sent a new motion. However, if you remove one of those commands, then it would only spin to one designated angle (because that s all the code would say). Below is an example of that. Note that you could also change the delays and it would affect the small servo, but the big servo is still only getting one command so it only spins to one designated angle. If you re having trouble with your code, check that you have everything spelled correctly and that you have included all of your semicolons at the end of each line. Use the Troubleshooting document to help. If your code uploaded and it still isn t working, you may have an issue with your wiring. Try pulling everything out and starting over. Sometimes it s difficult to see what the problem is until you start all over. If your wiring looks good and your code is fine, try each servo, one at a time, to see if it is an issue with the servo itself.
To really understand how the delay affects your servos, can you write a code where the small servo goes side to side quickly, say a delay of 200, and the big servo changes directions every 2 seconds? Remember, the delay tells you how long to wait before reading the next line of code and it always reads through the code in order. When it hits a delay, it waits however long you tell it before reading the next line of code. So anything you told it to do before that delay it is going to keep doing. For example, if you write, smallservo.write(0); bigservo.write(0); smallservo.write(180); smallservo.write(0); smallservo.write(180); smallservo.write(0); bigservo.write(180); smallservo.write(180); then the small servo is going to move back and forth from 0 degrees to 180 degrees and stay at each position for 200 milliseconds, but the big servo was only told to spin fast to the left, so it will do that for 800 milliseconds (4 delays of 200 each) until it is told to go fast to the right (bigservo.write(180);) down in line 11).