LED + Servo 2 devices, 1 Arduino Learn to connect and write code to control both a Servo and an LED at the same time. Many students who come through the lab ask if they can use both an LED and a Servo on their projects. While this is not something we typically allow in a short building time, it is something that is possible with a bit of coding and a few more wires. Arduino Breadboard (large or small, I used large) 1 LED 2 red wires 1 resistor (100 300 ohms) 2 black wires 1 Servo (I used a full rotation Servo, but you could also use a regular Servo) 1 white wire
1. We ll start by connecting our wires and we ll focus on connecting the Servo first. First, connect a red wire from 5V on the Arduino to the red connection of the Servo. 2. Next, connect a black wire from GND to the black connection of the Servo.
3. Next, connect a white wire from Pin 9 to the white connection of the Servo (mine is yellow instead of white). 4. Now that the Servo is connected, we can focus on the LED. Connect a red wire from Pin 13 to the positive rail on the breadboard (or the red row).
5. Next connect a black wire from GND to the negative rail (or the black row). 6. Next, we need a resistor to make sure we don t burn out our LED. Connect the resistor from the positive rail (or the red row) to another row (I used row 17 or you could use the blue row).
7. Lastly, connect the LED. The long leg (positive) will go in the same row as the resistor (17 for me, maybe blue for you) and the short leg (negative) will go in the negative rail (or black row). 1. Start by opening Arduino and deleting the text to start with a blank document. Don t forget to select the right port! It may be tempting to just copy and paste the Servo code and LED Blink code into one document, and that is essentially what this looks like, but there are some details that I think it is better if we go over slowly.
2. Whenever we use a Servo, we need to include the Servo library. To do this, go to Sketch, Include Library, Servo. This will bring up #include <Servo.h>. 3. Next, we need to name our Servo, just like in the Servo lesson, so again I will call it myservo. Remember, you can name your Servo whatever you want, just be sure to use that name throughout your entire code. 4. We are also going to name our LED. This may be a new term for you. We ll say that the word led refers to the integer number 13. Because 13 is an integer, we call this an int.
5. Now that we have defined our variables, we re ready to do our setup. Because we are using 2 devices, a Servo and an LED, we need to setup both. These two lines are very similar to the setups of the Servo and LED Blink code but all in one setup since you can only have 1 setup per sketch. 6. Next we can write our loop. As I said before, it may be tempting to copy and paste the 2 codes together. If you tried that you would end up with the following: void loop() { delay(1000); //wait 1 second delay(1000); //wait 1 second myservo.write(0); //spin the big servo to the left delay(2000); myservo.write(180); //spin the big servo to the right delay(2000); } Although this will work, it will not have the desired effect on your LED and Servo. The LED will turn on, stay on for a second, then turn off, but because it runs through all the lines of code before starting over from the top, the light will stay off for 5 seconds (1000 + 2000 + 2000). Same thing with the Servo. It will start out not moving, then will spin to the left for 2 seconds and then to the right for 4 seconds (2000 + 1000 + 1000). If you want the light to blink on and off every second and the Servo to change directions every second, you will need to do both actions (for the LED and the Servo) before each delay. It should look like the code below.
Congratulations! You should now have an LED Blinking and a Servo spinning back and forth in unison. Feel free to change the delay or make the Servo spin slower by changing the value in myservo.write( ); (see Programming a Servo for more information). If you re having issues with your code, review all the lines of code in my example code to make sure you didn t miss something or refer to the Troubleshooting document for more help. If your LED is not working, be sure that all of your connections have been made properly and then try a different LED. If your Servo is not working, make sure that all of your wires are in the correct pins and that the red black and white wires are in the right order on the servo attachment. If that looks correct, you may want to try another Sevro.
I m going to give you some other combinations of blinking and spinning that I d like you to try to figure out on your own as challenge problems. The solutions are posted below, but try doing them yourself first. 1. Try to make your LED continue to blink at the same rate (on for 1 second, then off for 1 second) and have your Servo spin only one way (instead of both ways)? This would allow you to change the rate of the blinking without changing the motion of the Servo. 2. Try to make your LED Blink fast (say a delay of 100) and your Servo spin both ways for 1 second each direction? 3. Try to incorporate the LED Fade code with a Servo? You may want to start by having the Servo only spinning one way and then try to have it change directions with the rate of the fade. Note: For most of these challenges I only posted the loop since the variables and setup are the same. I also posted comments that you do not need to write into your code. Challenge 1 Solution: void loop() { myservo.write(0); //spin the big servo to the left delay(1000); //wait 1 second delay(1000); //wait 1 second } You can always change the delay to be longer or shorter. This will change the rate at which your light blinks, but because you only told your Servo to spin to the left and never gave any other directions it will continue to spin to the left no matter the value of the delay. Challenge 2 Solution: We need to have the delay for the servo be longer than the delay for the LED, but we can t treat them like separate things because the delay makes the entire program wait. So the loop should look something like this: void loop() { myservo.write(0); //spin the big servo to the left delay(100); //servo delay = 100 delay(100); //servo delay = 200 delay(100); //servo delay = 300 delay(100); //servo delay = 400 delay(100); //servo delay = 500
} delay(100); //servo delay = 600 delay(100); //servo delay = 700 delay(100); //servo delay = 800 delay(100); //servo delay = 900 delay(100); //servo delay = 1000 myservo.write(180); //spin the big servo to the right delay(100); //servo delay = 100 delay(100); //servo delay = 200 delay(100); //servo delay = 300 delay(100); //servo delay = 400 delay(100); //servo delay = 500 delay(100); //servo delay = 600 delay(100); //servo delay = 700 delay(100); //servo delay = 800 delay(100); //servo delay = 900 delay(100); //servo delay = 1000 If you wanted the servo to spin longer, then you would need to add more of the on/off lines in between telling it different positions. Remember that using copy (Ctrl + C) and paste (Ctrl + V) is very helpful if you are repeating lines of code over and over again. Challenge 3 Solution: I started with the LED Fade example and then added in the Servo code to make the Servo spin only one way. Making the Servo change directions is a harder task, but may be easier once you complete Servo Sweep Challenge #2 #include <Servo.h> Servo myservo; int led = 11; int brightness = 0; int fadeamount = 5; void setup() { myservo.attach(9); pinmode(led, OUTPUT); }
void loop() { myservo.write(0); analogwrite(led, brightness); brightness = brightness + fadeamount; if (brightness == 0 brightness == 255) { fadeamount = -fadeamount ; } delay(30); }