Servo
The servo comes in a bag that also has a smaller bag of components in it. You'll need both for this project. Take the servo out as well as the small servo arm and a screw for it. One of the longer screws is better. Take the servo arm and servo and connect them. The arm should be pointing up with none of it hanging off the servo body. After unwrapping the servo cable, plug it into Digital socket D6.





















How To Upload These Examples
This is the procedure for each Upload section within each tutorial. First, open a new Arduino sketch in the Arduino IDE. Next, select all of the text and delete it so the sketch is now completely blank. Switch back over to this Learning Module and press Copy to Clipboard. Go back to the Arduino IDE and paste the code you just copied into the sketch. Before uploading, check in the Tools menu on top that 1) Board is selected as Arduino/Genuino UNO and 2) your port is correct for your computer, then press upload. A popup will ask you to save. Change the name to whatever project you're working on and press save. The code will now upload to your Arduino Board.










Upload
Upload the code shown below. This tutorial uses Digital socket 6. If you are using a different socket, update the code after copying it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <Servo.h>
//Change here if you're using a different socket
#define servoSocket 6 //<- digital socket number
Servo robotServo;
int pos = 0;
int servoSpeed = 15;
void setup() {
robotServo.attach(servoSocket);
}
void loop() {
for (pos = 0; pos <= 180; pos += 1) {
// in steps of 1 degree
robotServo.write(pos);
delay(servoSpeed);
}
for (pos = 180; pos >= 0; pos -= 1) {
robotServo.write(pos);
delay(servoSpeed);
}
}
Observe






Look at how the servo swings back and forth. There are two loops in the code. One for swinging clockwise and the other for counter-clockwise.
Modify
Increase or decrease the
servoSpeed
variable and watch what changes. It might not do what you'd think.
How to Modify Code
All code modifications will be done via variables that are at the top of the sketch. In the copied over Arduino sketch you can change the value of variables, upload the code, and observe what effect your change had.






Experiment
Play with that variable to make the servo go faster or slower.
Light Sensor
Take a cable and attach one end to the light sensor and the other to an Analog socket, A0.








Upload
Upload the code below. This tutorial uses Analog socket A0. If you are using a different socket, update the code after copying it.
1
2
3
4
5
6
7
8
9
10
11
12
13
//Change here if you're using a different socket
#define sensorSocket A0//<- analog socket number
int val;
void setup() {
Serial.begin(9600);
}
void loop() {
val = analogRead(sensorSocket);
Serial.println(val);
}
Observe
You'll be asked to use the Serial Plotter on some projects. This reads any information coming from the Arduino and displays it on a graph. It can only be accessed after you've uploaded your code. It is in the Tools menu. Once opened, make sure the
Baud rate
is set to 9600.




Open up the Serial Plotter and move your hand over the light sensor. Check out what values you can get. Look at the light value you get when the sensor is uncovered and covered. Remember those because we'll need them for your Robot Friend.




Robot Buddy
Let's finish up the code for the first project blueprint.
Upload
Upload the code shown below. The variable trigger value should be a number close to the covered sensor light value. You'll want it a bit (ie: 10-30) higher than what you measured. For example, if you measured 80, 100 would be a good value to use.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <Servo.h>
#define servoSocket 6
#define lightSensorSocket A0
Servo robotArm;
int pos;
int val;
int trigger = 100; //<- Change to YOUR measured value
int waveSpeed = 5;
int leftWave = 90;
int rightWave = 0;
void setup() {
robotArm.attach(servoSocket);
}
void loop() {
val = analogRead(lightSensorSocket);
if (val < trigger) {
for (pos = rightWave; pos <= leftWave; pos += 1) {
robotArm.write(pos);
delay(waveSpeed);
}
for (pos = leftWave; pos >= rightWave; pos -= 1) {
robotArm.write(pos);
delay(waveSpeed);
}
for (pos = rightWave; pos <= leftWave; pos += 1) {
robotArm.write(pos);
delay(waveSpeed);
}
}
}
Observe
Put your hand over the light sensor and watch the servo move. The change in light triggers the servo to move back and forth, or wave to you.




Modify
Increasing the
trigger
will make it more sensitive to the change in light.
Experiment
Changing the
leftWave
and
rightWave
variables will change how far the Robot waves.
waveSpeed
changes how fast the Robot waves.
Robot Template
Recommended - PDF Template
Print out the template and assemble your new buddy! Find some cardboard and cut out the template. Disconnect the servo from it's socket. Place it through the rectangle hole in the Robot's missing arm. Seat the servo and use some tape to keep it in place. Place another piece of tape behind the servo arm then attach the Robot's arm. Use the screw from the beginning and screw it into the hole in the arm and into the servo. With the construction complete, plug the servo back into the same socket. Wave at your Robot and watch it wave back!






























Taking it further
Feel free to use this code to explore what else you can do with the servo. Maybe your robot is afraid of light and covers his eyes, rather than waves? Maybe you want your robot to wave at you when triggered by sound? Maybe you want to completely change the look of your robot and not use our template? Maybe you don't want it to be a robot at all?
As you progress through the project blueprints, revisit this project. Try connecting different sensors as you learn how to use them in the other projects. Don't be afraid to experiment and share any cool modifications with the community. We love seeing and sharing what you make!