Robot On Wheels

Introduction

There are a lot of techniques to build a moving robot, but of course, you’ll need motors to produce motion.

With Arduino, you can control all normal electric motors: solenoids, servos, stepper motors, vibrating motors, and DC motors. The simplest motor is DC (Direct Current) -motor. In this exercise, you can use geared DC-motors or rotating servo-motors.

First, we’ll build a vehicle running on wheels, that moves around by itself.

Parts

In addition to Mehackit-board (or Arduino), breadboard, wires, and USB cable, you’ll need:

PartPictureDescription
2 x Geared DC-motor or 2 x Rotating servomotorIn your Maker kit, there's either two black geared DC-motors or two blue rotating servomotors. You can use either option for this exercise!
Motor driver L293D, i.e., H-bridgeYou'll need this part if you use black geared dc-motors. This driver is a microchip that consists of several components, which can amplify the current coming from Arduino, prevent charges produced by the motors, and thus protect Arduino's pins. Most importantly, you can use the microchip to control the rotational speed and direction of the motor.
9V-battery and connector9V battery powers Arduino.
4 X AA battery (6 V) and connectorThis is used as a power source for the motors. If you don't have these batteris or the connector, you can power both the Arduino and the motors with the 9V-battery for a short period of time!
Moving Vehicle DIY-kitMaker kit includes a DIY-kit that consists of plastic boards, bolts, nuts and spacers. If you don't have it, you can improvise and build a frame for your robot with e.g. cardboard or legos!

Building the frame

Maker kit includes a DIY-kit that consists of plastic boards, bolts, nuts and spacers. In the video below, there’s instructions for building the vehicle with this kit!

In the video, two blue servomotors are attached to the vehicle. If you have black geared motors, attach both of them to the acrylic boards according to the image below. Otherwise, you can follow the instructions in the video.

Mehackit Maker kit - The Robot Vehicle

The Circuit - DC-motors

If you have black geared dc-motors, build the circuit with the instructions below. If you have blue servomotors, jump to the next part!

For this connection, you’ll need a lot of wires and some patience. We’ll take one step at a time - you don’t have to understand everything just yet!

1. Connecting the Microcircuit to the Voltage and to Ground

  • Make sure that the L293D is the right way (the hole pointing up)
  • Connect the 5V voltage coming from Arduino to the top right pin of the microcircuit
  • Using a wire, divide the grounding to the right side of the breadboard
  • Ground the microcircuit from four places

2. Connecting the Motors

Note!
In the picture, there are two regular electric motors, but in this exercise, we’ll use geared motors.

Connecting the Motors:

  • Connect the motors on each side like in the picture

3. The Main Switches of the Motors

  • Connect the top left corner and the bottom right corner of the microcircuit to Arduino’s 5V voltage.
  • When voltage is applied to these pins, the motors are operational. The power source for the motors is connected next.

4. External power and the Control Pins of the Motors

  • Connect the 6V power souce to the lower left corner of the microhchip and to a ground shared with the Arduino. This power source provides enough current for the motors. You can find dditional information about powering the motors in the end of this project.
  • On both sides of the microcircuit, connect the second highest and the second lowest pin to Arduino like in the picture. These pins control the rotational direction and speed of the motors.

The Circuit - Servomotors

If you use blue servomotors, build the circuit below.

  • Connect servomotors to a digital pin, to ground and to 6V external power source. This power source provides enough current for the motors. You can find dditional information about powering the motors in the end of this project.
  • Remember to connect the external power source to a ground shared with the Arduino.

Programming - DC-motors

In this part you can find instructions for programming the black geared DC-motors. If you use servomotors, jump to the next part!

The Aim of the Program

The robot repeats randomly the following actions: turn right or left (for 1,5 seconds), reverse (for 4 seconds) and go forward (for 4 seconds). If both motors rotate forward or backward, the robot goes straight. In turns, only one of the motors rotates.

Familiar commands

Task
Follow the instructions and build the program little by little!

1. Definitions

You’ve now connected four pins of the microcircuit to four pins of Arduino (3, 9, 10 and 11). Through them, we’ll transmit the microcircuit’s instructions on changing the rotational speed and direction of the motors.

First, define which Arduino’s pins are connected to the microcircuit’s pin. Do this by using four integer variables in the top of the program.

// control pins of the motor and corresponding pins of Arduino:
int rightMot1 = 3;   
int rightMot2 = 9;   
int leftMot1 = 11; 
int leftMot2 = 10;

Next, define a couple of variables, which you use to control the motors - speed and action.

int speed=100;   // rotational speed of the motors
int action=1;   // variable receives values between 1-4: the robot has four different actions.

In setup, define the pins of Arduino as outputs.

void setup(){     
  pinMode(rightMot1, OUTPUT); 
  pinMode(rightMot2, OUTPUT);
  pinMode(leftMot1, OUTPUT);
  pinMode(leftMot2, OUTPUT);
}

Next, we’ll program how the motors work.

2. Motors Running

In the loop-function, we’ll randomly select, what the robot does next. The options are:

  • Action 1: go forward
  • Action 2: gobackward
  • Action 3: turn left
  • Action 4: turn right
void loop() {
  action = (random(1,5)); // randomly select a value between 1-4 
  // Note: random function doesn't acknowledge the highest value, i.e., 5!
  delay(1000);
  // Next, we'll write the different actions of the robot here
}

One value out of four is randomly selected for the variable action, and each value means a different action. We’ll write the actions in loop function.

// Go forward
if (action == 1) { 
    analogWrite(rightMot1, speed);// rotates forward with the speed "speed"
    analogWrite(rightMot2, 0);  // speed of the right motor backwards is 0
    analogWrite(leftMot1, speed);// rotates forward with the speed "speed"
    analogWrite(leftMot2, 0);  // speed of the left motor backwards is 0
    delay(4000);   // robot moves forward for 4 seconds
  }

// Go backward:
if (action == 2) { 
    analogWrite(rightMot1, 0);     // speed of the right motor forward is 0
    analogWrite(rightMot2, speed);// rotates backwards with the speed "speed"
    analogWrite(leftMot1, 0);   // speed of the left motor forward is 0
    analogWrite(leftMot2, speed);// rotates backward with the speed "speed"
    delay(4000);      // robot moves backwards for 4 seconds
  }

// Turn left:
if (action == 3) { 
    analogWrite(rightMot1, 0);     // speed of the right motor forward is 0
    analogWrite(rightMot2, 0);     // speed of the right motor backward is 0
    analogWrite(leftMot1, speed);// the left motor rotates forward
    analogWrite(leftMot2, 0);     // speed of the left motor backwards is 0
    delay(1500);       // the turn lasts for 1,5 seconds
  }


// Turn right:
if (action == 4) { 
    analogWrite(rightMot1, speed); // the right motor rotates forward 
    analogWrite(rightMot2, 0);      // speed of the right motor backwards is 0
    analogWrite(leftMot1, 0);      // speed of the left motor forward is 0
    analogWrite(leftMot2, 0);      // speed of the left motor backwards is 0
    delay(1500);      // the turn lasts for 1,5 seconds
}
Task
Upload the program to Arduino and test the robot! If you want, alter the duration of the actions.
Task
Instead of selecting the action randomly, program the robot to follow a determined path. Try to make it go around your table and come back to the starting point!

You can very well use this robot as a base for the final project: you can camouflage it, add all sorts of things from a mop to a paintbrush or a catapult to it, control it with different sensors, program it to go different routes… In the next exercise, we’ll add a sensor to the robot!

If you have challenges, check the part Challenges with the robot? in the end of this project.

Programming - Servomotors

The Aim of the Program

The robot repeats randomly the following actions: turn right or left (for 1,5 seconds), reverse (for 4 seconds) and go forward (for 4 seconds). If both motors rotate forward or backward, the robot goes straight. In turns, only one of the motors rotates.

If you have programmed servomotors before, all these commands are probably familiar to you. If not, you’ll learn more soon!

Familiar commands:

New commands:

Task
Follow the instructions and build the program little by little!

1. Definitions

  • Include the Servo-library
  • Create Servo-objects servo1 and servo2
  • In the setup, define the pins of the servos.
#include <Servo.h>

Servo servo1;
Servo servo2;

void setup() {
  servo1.attach(3);
  servo2.attach(11);
}

You can control the servos with servo.write-command. The value given to servos must be 0-180, e.g. servo1.write(150). The logic is the following:

  • Value 0: Full speed
  • Value 90: Servo stays still
  • Value 180: Full speed to other direction.

Next, values 150 and 30 are used for controlling the servos. These values should result in the same speed, but to different directions.

What makes things a little bit more complicated is that the servos are inevitably connected to the vehicle so that e.g. the command servo.write(150) makes ‘the other servo rotate forward and the other backwards! But don’t worry, you’ll find proper values by experimenting!

int speed1 = 150;  // speed
int speed2 = 30;   // speed to other direction
int action = 1;   // variable receives values between 1-4: the robot has four different actions.

2. Motors Running

In the loop-function, we’ll randomly select, what the robot does next. The options are:

  • Action 1: go forward
  • Action 2: gobackward
  • Action 3: turn left
  • Action 4: turn right
void loop() {
  action = (random(1,5)); // randomly select a value between 1-4 
  // Note: random function doesn't acknowledge the highest value, i.e., 5!
  delay(1000);
  // Next, we'll write the different actions of the robot here
}

One value out of four is randomly selected for the variable action, and each value means a different action. We’ll write the actions in loop function.

// Go forward
if (action == 1) { 
    servo1.write(speed1);
    servo2.write(speed2);
    delay(4000);   // robot moves forward for 4 seconds
  }

// Go backward:
if (action == 2) { 
    servo1.write(speed2);
    servo2.write(speed1);
    delay(4000);      // robot moves backwards for 4 seconds
  }

// Turn left:
if (action == 3) { 
    servo1.write(speed1);
    servo2.write(90);
    delay(1500);       // the turn lasts for 1,5 seconds
  }


// Turn right:
if (action == 4) { 
    servo1.write(90);  
    servo2.write(speed2);
    delay(1500);       // the turn lasts for 1,5 seconds
}
Task
Upload the program to Arduino and test the robot! If you want, alter the duration of the actions.
Task
Instead of selecting the action randomly, program the robot to follow a determined path. Try to make it go around your table and come back to the starting point!

You can very well use this robot as a base for the final project: you can camouflage it, add all sorts of things from a mop to a paintbrush or a catapult to it, control it with different sensors, program it to go different routes… In the next exercise, we’ll add a sensor to the robot!

Challenges with the Robot?

The wheels of the robot slip

Add friction by placing duct tape or Blu-Tack around the wheels.

The robot goes forward for 1,5 seconds and spins for 4 seconds

If this happens, the robot works the opposite way it should. Switch two wires of one of the motors to opposite holes in the breadboard. You’ll have to test, which motor needs its wires switched.

Motor or motors don’t rotate in any circumstances

If you use servomotors, go through the circuit one more time. If you use geared DC-motors, try the following things.

  • Make sure that the h-bridge is connected the right way (the semi-circle shaped hole the same way as in the schematics)
  • Take a look at the schematics, and make sure all the wires are connected correctly
  • Disconnect the h-bridge and reconnect it
  • Disconnect the wires from the side of the h-bridge that has the malfunctioning robot and reconnect them
  • Final alternative: disconnect all the components from the breadboard, move the h-bridge to a different location on the board and reconnect the wires

Powering the Motors

All electronic components need a right amount of Voltage and current to work reliably. With a servomotor and a DC-motor, the stats are the following.

Voltage: 4.8V-6V
Current: 100-800mA

Arduino’s pins can’t provide more than 500mA for a longer period of time, as is explained in this Adafruit tutorial. Together, motors may need more than 1000 mA and that’s why they work unreliably when powered with Arduino’s 5V-pin.

A good alternative is to use four AA-batteries as an external power source (6V in total).

More information:

https://learn.adafruit.com/ladyadas-learn-arduino-lesson-number-0/power-jack-and-supply

http://rcarduino.blogspot.com/2012/04/servo-problems-with-arduino-part-1.html

https://electronics.stackexchange.com/questions/67201/maximum-current-draw-on-vin-pin

Mehackit: Learn More: Motors & Power

Transcript

Servo motors are nice but there are other motors you can use with Arduino, too. You used the servo that moves back and forth but there are also servos that are continuous. They move 360 degrees. Servo motor consists of gears, a microchip and another kind of a motor called DC motor. Servo is easy to control by programming, thanks to all these extra parts, but with the right components you can control a DC motor, too. Fun fact: DC stands for direct current which is the type of current you get from a battery or a USB cable. If you add an uneven weight to the shaft you can make a vibration motor. There are DC motors with gears, too. They move a bit slower which is sometimes useful. You can even use DC motors to pump water. Another motor worth mentioning is the stepper motor. Stepper motors are great when you need more control over the rotation. Steppers can move, well, one step at the time so you can position them accurately. Sometimes motors require more current than an Arduino can give. In this case you will need an external power supply to keep the motor going. Just one servo in your Arduino circuit might already cause trouble with current and that's when the servo does something weird. The solution could be using an external power supply for the servo, for example a battery pack like. Servo is the only kind of motor you can connect directly to an Arduino. With the other types you need parts like transistors or motor driver chips to manage the extra current they need. But no need to worry, you'll learn more about managing all those motors in your future projects.

Additional information

H-bridge

H-Bridge L293D is an integrated circuit (IC, microcircuit) that consists of several different components. The H-bridge amplifies the current coming from Arduino and prevents charges produced by the motors and protects Arduino’s pins. Most importantly, the microcircuit can control the rotational direction and speed of the motor.

Functions

If you want, you can write the actions of the robot as functions. This makes the code cleaner: in loop, it’s easier to determine the logic of the program, and functions reveal the different movements of the robot.