Stories on maker education and innovation 

Home creativity Moving Your DIY Robot using Arduino and Servos
formats

Moving Your DIY Robot using Arduino and Servos

Sumobot Bot JR

Interested in building your own Arduino robot? For me, it has been a fun project to do with my kids. We built our first the robot using the PiBot robotics kit. Additional iterations utilized Legos, Sumobot Jr, cardboard, and other materials around the house. It’s fun to invent your own chassis and designs. Using some of the ideas from this blog post,
you’ll be able to build your own DIY robot. Hope you enjoy the journey.

In future blog posts, we’ll show ways to control your robot using a Wifi connection, Raspberry Pi, Droidscript and Android. This blog post will focus on controlling the wheels of the robot using continuous rotation servos and
a simple communication protocol.

Here’s a components that you’ll need to get started:

This post will focus on programming the servo’s with Arduino’s programming environment. For sample instructions on building a Sumo Bot Jr, check out the following video. In this post, we’ll assume that you have put together your robot chassis and your servos, breadboard, battery pack and Arduino have been connected to your chassis.

Servo Robot

  1. Connect the GND pin on the Arduino to the ground line of the bread board. The ground line is marked with a blue stripe.
  2. Connect the black wires of the servos to the ground line.
  3. Connect the red wires of the servos to the voltage line of the bread board. The voltage line is marked with a red stripe.
  4. Connect the white wire of the left servo to pin 9 of the Arduino. This wire will act as a signal wire between the servo and the Arduino.
  5. Connect the white wire of the right servo to pin 10 of the Arduino.
  6. Connect the black wire of the battery pack to the ground line.
  7. Connect the red wire of the battery pack to the voltage line.
  8. Install 4 AA batteries into the battery pack.

At this point, we’re ready to install some Arduino code into the Arduino. Copy the following Arduino sketch and upload into your Arduino. To learn more about uploading sketches using the Arduino IDE, check out the following video:

Here’s another tutorial on setting up your Arduino and uploading sketches:
https://www.arduino.cc/en/Main/Howto

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <Servo.h>

Servo leftServo;  
Servo rightServo;  

int LEFT_FORWARD_VALUE = 180;
int LEFT_BACK_VALUE = 0;
int STOP_VALUE = 90;
int RIGHT_FORWARD_VALUE = 0;
int RIGHT_BACK_VALUE = 180;
int LEFT_SERVO_PIN = 9;
int RIGHT_SERVO_PIN = 10;
 
void setup()
{
  Serial.begin(9600);
  leftServo.attach(LEFT_SERVO_PIN);
  rightServo.attach(RIGHT_SERVO_PIN);
  stop();
}
//================================================================================
void forward()
{
    leftServo.write(LEFT_FORWARD_VALUE);
    rightServo.write(RIGHT_FORWARD_VALUE);
}
//================================================================================
void stop()
{
    leftServo.write(STOP_VALUE);
    rightServo.write(STOP_VALUE);
}
//================================================================================
void back()
{
    leftServo.write(LEFT_BACK_VALUE);
    rightServo.write(RIGHT_BACK_VALUE);
}
//================================================================================
void left()
{
    leftServo.write(LEFT_BACK_VALUE);
    rightServo.write(RIGHT_FORWARD_VALUE);
}
//================================================================================
void right()
{
    leftServo.write(LEFT_FORWARD_VALUE);
    rightServo.write(RIGHT_BACK_VALUE);
}
//================================================================================
void loop()
{
  if (Serial.available() > 0) {
    int inByte = Serial.read();

    switch (inByte) {
    case 'w':    forward(); break;
    case 's':    back(); break;
    case 'a':    left(); break;
    case 'd':    right(); break;
    case ' ':    stop(); break;
    }  
  }  
}

How does this code work?

We start by importing the “Servo” header and declaring the left and right
servos.

1
2
3
4
#include <Servo.h>

Servo leftServo;  
Servo rightServo;

The continuous rotation servo has a simple protocol for controlling rotational
motion using the frequency of voltage pulses. In the Arduino framework, the following
code stops the rotation of the servo.

leftServo.write(90);

To make the servo spin forward, use the following code:

leftServo.write(180);

To make the servo spin backward, use the following code:

leftServo.write(0);

With these ideas in mind, we define the following constants for the left and right servos.

1
2
3
4
5
int LEFT_FORWARD_VALUE = 180;
int LEFT_BACK_VALUE = 0;
int STOP_VALUE = 90;
int RIGHT_FORWARD_VALUE = 0;
int RIGHT_BACK_VALUE = 180;

We also define the constants for the Arduino digital pins.

int LEFT_SERVO_PIN = 9;
int RIGHT_SERVO_PIN = 10;

In the following code, we setup the serial port, attach the left and right servos,
and send the stop command.

1
2
3
4
5
6
7
void setup()
{
  Serial.begin(9600);
  leftServo.attach(LEFT_SERVO_PIN);
  rightServo.attach(RIGHT_SERVO_PIN);
  stop();
}

The following functions are used to move the robot forward, backward, left, and right.
There’s also a function to stop movement.

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
//================================================================================
void forward()
{
    leftServo.write(LEFT_FORWARD_VALUE);
    rightServo.write(RIGHT_FORWARD_VALUE);
}
//================================================================================
void stop()
{
    leftServo.write(STOP_VALUE);
    rightServo.write(STOP_VALUE);
}
//================================================================================
void back()
{
    leftServo.write(LEFT_BACK_VALUE);
    rightServo.write(RIGHT_BACK_VALUE);
}
//================================================================================
void left()
{
    leftServo.write(LEFT_BACK_VALUE);
    rightServo.write(RIGHT_FORWARD_VALUE);
}
//================================================================================
void right()
{
    leftServo.write(LEFT_FORWARD_VALUE);
    rightServo.write(RIGHT_BACK_VALUE);
}

//================================================================================

In the loop function which is called repeatedly, the Arduino waits for a character
from the serial port. If the Arduino receives a “w”, the program sends a forward command.
If the Arduino receives a “s”, the program sends a backward command.
If the Arduino receives a space, the program sends a stop command.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void loop()
{
  if (Serial.available() > 0) {
    int inByte = Serial.read();

    switch (inByte) {
    case 'w':    forward(); break;
    case 's':    back(); break;
    case 'a':    left(); break;
    case 'd':    right(); break;
    case ' ':    stop(); break;
    }  
  }  
}

In the Arduino IDE, press CTRL+SHIFT+M to open the serial port monitor. This window is used to send bytes to the Arduino. Try typing “w” and press enter. The robot should move forward. Try typing space and press enter. The robot should stop.

Congrads! You’ve built your first Arduino robot!

Top Stories on InspiredToEducate.NET

Learning To Code

 

 
 Share on Facebook Share on Twitter Share on Reddit Share on LinkedIn
No Comments  comments 
© Inspired To Educate
credit