Programming a Servo Learn to connect wires and write code to program a Servo motor. If you have gone through the LED Circuit and LED Blink exercises, you are ready to move on to programming a Servo. A Servo is a motor that is programmable. There are 2 different types of Servos that we use here in the lab, a full rotation (or continuous) Servo and a regular (or position based) Servo. If you purchased a kit online, it may only have come with a regular Servo, but you could buy a full rotation Servo online for around $10 if you wanted one. Arduino Servo Red Wire Black Wire White Wire 1. Start by opening a new sketch and erasing everything that is already there. Also don t forget to connect to the right port!
2. Coding a Servo is actually quite complicated as it needs a very unique signal in order to make it work. However, others before us have made some shortcuts for us to make it a bit easier. They made a library that we can reference Programming that controls our Servo a Servo more easily. To use this library, go to Sketch -> Include Library and find and click on Servo. It should come up on your screen as #include <Servo.h>. This means that we are including the Servo library in this project. 3. Use your arrow key to arrow down so you are on the next line, and then type in Servo myservo;. This names your servo myservo but you really could call it whatever you want. Please note, if you decide to change the name of your servo, be sure to change it everywhere and not just here! 4. Next press Enter twice to move your cursor down, then type in void setup(){ and press enter. When you press enter, a closed bracket should appear at the bottom of the code. These brackets are like hamburger buns, you can decide what you want to put on your burger, but you always need a top and bottom bun. Anything you type between the open bracket and the closed bracket will be part of the setup.
5. Our set up has one line of code, type in myservo.attach(9);. This means that we will attach the signal wire of our servo to pin 9. Keep this in mind later when we go to connect our wires! Note that we use pin 9 because Programming it has the ~ symbol a next Servo to it which means that it generates the PWM signal needed for the Servo. 6. Use your arrow key to arrow down, and then press Enter twice. We want the closed bracket to remain where it is but our cursor to move down the page. 7. Next we will code the action phase of our servo,. Again, once you have typed this in press enter and you will get a closed bracket at the bottom of the page.
8. The last part of the code is the motion. There are 4 lines of code that will make up the action of our servo. The first line, myservo.write(0); tells the big servo to spin fast to the left and tells the small servo to go to 0 degrees. Programming The next line, a delay(2000); Servo tells the Arduino to wait before reading the next line of code (so the big Servo would keep spinning to the left for 2 seconds and the small Servo would stay at 0 degrees for 2 seconds). Next, myservo.write(180); tells the big servo to spin fast to the right and tells the small servo to go to 180 degrees. The last line, delay(2000); again tells the Arduino to wait for 2 seconds before reading the next line of code (so the big Servo would keep spinning to the right for 2 seconds and the small Servo would stay at 180 degrees for 2 seconds. So the value we put in myservo.write( ); tells it the motion to do and the delay( ); essentially tells it how long to do that motion. 9. Once all of your code is written, upload the code to your Arduino. If you get any errors, see troubleshooting below.
1. Connect your wires to Programming your Servo. The red wire a Servo connects to the red wire, the black wire connects to the black wire, and the white wire connects to the white wire. (Remember, the color of the wire helps us to keep things straight. This is especially true with a Servo where the manufacturer has made red to be positive and black to be negative.) Next, connect these wires to the Arduino. Connect the red wire to Vin or 5V (positive), connect the black wire to GND (negative) and connect the white wire to pin 9 (the signal). Note: in my picture the white wire is actually yellow, but they behave the same. You may also have a brown wire instead of a black wire or an orange wire instead of a red wire, but again they behave the same. Your servo should now be moving. Congratulations! If you have multiple Servos, test them and see how the same code affects the different Servos. If you get an error when you upload, download the Troubleshooting document and try to find a solution. Most common problems: missed semicolons, missed brackets, misspelled words, and not connecting to the board. If your code uploads but the servo does not move, check that your wires are all in the right place. Next, check that your setup looks exactly like the code above. Often students change the number in myservo.attach( ); but if you change this value you will need to move your white wire to that number. You also must choose a number that can generate a PWM signal, these are the numbers with the ~ symbol next to them (3, 5, 6, 9, 10, and 11). 1. Try to make your big servo spin to the left for 3 seconds and to the right for 1 second. 2. Try to make your regular servo go to 0 for 1 second, 45 for 1 second, 90 for 1 second, 135 for 1 second, 180 for 1 second, and then back, stopping at each of the above mentioned degrees for 1 second each. For example, 0, 45, 90, 135, 180, 135, 90, 45, 0, etc. 3. Try to make your big Servo spin slow to the left forever.
4. Try a new function, random! In myservo.write( ); type in random(0, 180) and this will give a random value between 0 and 180, meaning that your regular Servo will go to a random degree and your full rotation Servo Programming will spin a random a Servo direction and speed. Keep the delay big enough to see the effect, either 1000 or 2000, or make the delay random too! Note: Only the loop is shown in the solutions below, as the setup and pre-setup are the same for all solutions. Challenge 1 Solution: myservo.write(0); delay(3000); myservo.write(180); Challenge 2 Solution: myservo.write(0); myservo.write(45); myservo.write(90); myservo.write(135); myservo.write(180); myservo.write(135); myservo.write(90); myservo.write(45); Challenge 3 Solution: myservo.write(80); Challenge 4 Solution: myservo.write(random(0,180)); delay(2000);