What We’ll Do in this Lesson
2
★ Wire a 3 pin Servo Motor★ Write a more advanced code to control a Servo Motors motion in a
sweeping pattern based on a feedback loop
Let’s Get Ready!
3
You’ll need these things to make your circuit
4
Black Wire
Red Wire
Yellow Wire
Servo Motor
Safety!
5
Keep yourself safe when working with Arduinos!
6
You should never connect power and ground wires directly while they are plugged into your Arduino board.
This results in a short circuit.
A short circuit can result in temperatures hot enough to burn the user and the Arduino.
Always think safety first. When in doubt, seek help!
Let’s Build your Circuit!
7
Step 1
8
Use your colored wires to match the wires leading out of the Servo motor. Then, plug the black ground wire into the GND pin. Plug the red power wire into the VIN pin. Plug the yellow signal wire into Pin 9.
What type of pin is pin 9?
Plug In!
9
Connect your Arduino to your computer using the USB cable. If done correctly, the green light on the Arduino board should power on!
Let’s Write your Code!
10
Port
11
Make sure your computer knows which port the Arduino is plugged into.
Step 1
12
Include the Servo Library , name your Servo, and create a variable for the servo position.
Step 3
13
Tell the Arduino where the Servo is plugged in
About Loop Codes
14
The next code you will write features a For Loop. This allows our code to adapt to the actions it performs. A For Loop checks the status of your code and completes actions until all conditions are met. Then, it can move onto another series of actions based on a new set of conditions.
We will write a code that will check the position of your servo and perform an action based on its position.
We will make the servo travel from 0 to 180 degrees in one For Loop.
We will make the servo travel from 180 to 0 degrees in another For Loop.
Step 4
15
Write your loop code to control the Servos motion based on a feedback loop
Which LED code does this resemble?
Verify
16
Verify your code by clicking the check in the upper left corner. Codes with errors will show an orange warning at the bottom.
Upload
17
Upload your code to your Arduino by clicking the arrow in the top left corner. Notice the green status bar in the lower right corner.
Did it work??
18
Troubleshooting
19
If your Arduino isn’t performing as expected...
20
★ Wiring issues○ Double check that your wires are plugged all the way in to the correct pins
and the correct locations on the breadboard○ Check the alignment of wires on your Servo to the pins on the board. The
black wire is always GND. The power wire may be red or brown. The signal wire may be white, orange, or yellow.
★ Coding issues○ Check your capitalization and punctuation against the example code.○ Make sure you uploaded your code. It should read “Done Uploading” in the
bottom right corner.
Challenges!
21
Can you take it a step further?
22
★ Try sweeping your Servo in a different pattern or at different intervals.
★ Can you edit this code to make an LED Fade?
★ Can you add an LED that fades with the Servo sweep?
For more lessons and project ideas, visit The Arduino Teacher
www.ArduinoTeacher.com