Robot Friend

Start 

Let's build the first project in your 12-in-1 project kit. A Robot friend that reacts to your environment. He'll end up looking something like this.

Robot Friend BlueprintRobot Friend Blueprint

Modules 

Gather the following parts to complete this project.

Parts

All Parts
All Parts
Servo
Light
Cable
PartQuantity
Servo 1
Light 1
Cable 1

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.

All the parts you'll need
All the parts you'll need
Open the plastic bag
Take out the bag of parts inside...
... and the servo
Open the smaller bag...
Be careful! There are many little parts
Take out the small 'Servo Arm'...
... and ...
... one longer screw
Now we can get started
Take the servo...
... and the 'Servo Arm'
Line up the circles
Place them together with the 'Servo Arm' pointing up on the servo body
The 'Servo Arm' shouldn't be hanging off the servo body
Take the wire around the servo...
... and unwrap it
Take the cable ...
... plug 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.

Open a new sketch
Open a new sketch
Highlight all code and delete it
A blank sketch
Copy code from our website
Paste that into the blank sketch
Double check
Press Upload
Save your sketch

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.

Download file Copy to clipboard
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

Watch the servo move back and forth
Watch the servo move back and forth

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.

The prewritten sketch
The prewritten sketch
Variable to change
Changes variable
Upload and observe

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.

All the parts for the next step
All the parts for the next step
Take a cable
Unwrap it
Connect one side to the sensor socket
The other to Analog socket A0
Ready to upload code

Upload

Upload the code below. This tutorial uses Analog socket A0. If you are using a different socket, update the code after copying it.

Download file Copy to clipboard
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.

Download file Copy to clipboard
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

SVG Template

AI 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!

Some cardboard and the printed template
Some cardboard and the printed template
Line up your materials
Cut the solid lines
All cut out
Take the servo
Disconnect it from the socket
Grabbing the socket end...
... stick it through the Robot's missing arm
All the way through so the servo is snug in the rectangle hole
You can add some tape to keep it in place
Now we'll attach the arm
Get a piece of tape
Attach it behind the 'Servo Arm'
Get the Robot's arm...
... place it on top of the 'Servo Arm'
Find that screw from earlier
It will go through the Robot's arm and into the servo
Be careful! It's small and pointy
Through the mounting hole and into the servo
You're all done with the construction
High five!
Take the servo cable...
... place back into the same socket
Test out his waving feature!
Moving the sensor might make the waving more natural
You're first blueprint is finished!

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!