Remember Pluto, our new two-wheeled robot prototype? That’s the one who has a camera eye and streams video to a browser. Well, today’s the time to start a series of articles that can show you through the process of its creation! I am Sergey Koba, PHP/Ruby Team Leader and Pluto’s creator, and I will be your guide.
Let’s go back to the start. The basic scope of work to build such a robot is as follows:
- it moves on two wheels (the third one is loose)
- it is controlled via a browser with internet access (a local Wi-Fi network)
- it streams video from the camera to the browser in real time
- the person who controls the robot—”the pilot”—can rotate the camera (left/right/up/down) to widen the field of vision.
As I was building Pluto, I faced a lot of unexpected problems. These problems did not have evident solutions that would be clarified in tutorials. That’s why I decided to create a set of articles about this robot. This one is the first, and I wanted to make it clear even for beginners in the sphere of robotics technology.
The very first part of the robot I wanted to implement was the chassis. I wanted to make a thing that moves. For that purpose, I had the following requisite equipment:
- two DC motors with wheels
- L298N driver module for motor control
- Arduino Uno R3 and expansion board Sensor Shield V5, which facilitates connection of working load
- a plastic platform to fasten the abovementioned equipment to
- legs and M2/M3 fasteners—screws and washers—can always come in handy
- wires that will connect contacts on Sensor Shield and peripherals (I found them in an pile of old computer spare parts)
The natural question that comes now is where to get the power source. The first pitfalls are awaiting here. It would be possible to power it via USB through Arduino, but the motors won’t spin—thus it’s not an option. What we have is a battery holder for 4 AA batteries, rechargeable or not.
If you want to get a longer ride, you’ll have to get better batteries. For example, I got such 3-cell batteries, 11.8 volts each.
Why two batteries? One for the motors, one for the equipment. Power surges can negatively affect the electronic brain of the robot, while the wheels are spinning. As an alternative, one battery may suffice at the beginning (for the motors), while the equipment can be powered via USB through a computer. Please consider using a DC converter for batteries with over 5 volts, which are required to run Arduino.
Let’s get down to basic assembling activities! First, connect Arduino Uno R3 with Sensor Shield V5.
Now let’s connect the motors to the driver…
It’s not a problem if you get those wires crossed—the motors will simply spin the other way round. This can be fixed by connecting the wires correctly—I had to solder wires to motors. Now we can connect the driver module with the Sensor Shield. Doing that, I chose the following pins (contacts on the board):
Color |
Assignment |
Arduino Pin (contact) |
Black | Speed of the left motor (ENA) | 5 |
White | Left motor / spin forward (IN1) | 11 |
Grey | Left motor / spin backward (IN2) | 9 |
Purple | Right motor / spin forward (IN3) | 8 |
Blue | Right motor / spin backward (IN4) | 7 |
Green | Speed of the right motor (ENB) | 6 |
ENA and ENB pins must be connected with Arduino pins, which are marked with “~”. These pins support pulse-width modulation (PWM). In this case, it’s pins 5 and 6. Please do not connect motors to pins 2 and 3, because they support interrupts—this issue will be covered in forthcoming articles.
I used the standard Arduino IDE to develop the programs and firmware of Arduino. Download, set up, connect Arduino to the USB outlet.
Now let’s launch Arduino IDE. On Linux, it should be launched with administrator permissions; they are needed for work with USB port (sudo ./arduino). For a good start, we are going to write a very simple program that controls the forward/backward spin of the wheels, as well as allows to change their speed.
#define MotorLeftSpeedPin 5 // Left (А) motor SPEED — ENA #define MotorLeftForwardPin 11 // Left (А) motor FORWARD — IN1 #define MotorLeftBackPin 9 // Left (А) motor BACK — IN2 #define MotorRightForwardPin 8 // Right (В) motor FORWARD — IN3 #define MotorRightBackPin 7 // Right (В) motor BACK — IN4 #define MotorRightSpeedPin 6 // Right (В) motor SPEED — ENB void setup() { /* setting up the behavior of the pins */ pinMode(MotorLeftForwardPin, OUTPUT); pinMode(MotorLeftBackPin, OUTPUT); pinMode(MotorLeftSpeedPin, OUTPUT); pinMode(MotorRightForwardPin, OUTPUT); pinMode(MotorRightBackPin, OUTPUT); pinMode(MotorRightSpeedPin, OUTPUT); } void loop() { /* setting up the speed of the motors */ analogWrite(MotorRightSpeedPin, 80); analogWrite(MotorLeftSpeedPin, 80); /* moving forward for 2 seconds */ digitalWrite(MotorRightForwardPin, HIGH); digitalWrite(MotorLeftForwardPin, HIGH); digitalWrite(MotorRightBackPin, LOW); digitalWrite(MotorLeftBackPin, LOW); delay(2000); /* setting up the speed of the motors */ analogWrite(MotorRightSpeedPin, 120); analogWrite(MotorLeftSpeedPin, 120); /* moving back for 2 seconds */ digitalWrite(MotorRightForwardPin, LOW); digitalWrite(MotorLeftForwardPin, LOW); digitalWrite(MotorRightBackPin, HIGH); digitalWrite(MotorLeftBackPin, HIGH); delay(2000); }
Every change in the code should be uploaded to Arduino anew (Upload arrow in the top right corner). Do not forget to select the correct USB port in menu Tools -> Port.
The code contains 2 functions: setup is executed once and contains initialization and settings of pins; loop is recurring, as long as Arduino is powered. PWM pins (ENA and ENB) are responsible for speed, and data is transmitted to them via analogWrite – a function that assumes a value from 0 to 255. Other pins are responsible for direction of motor rotation. They have 2 positions (HIGH and LOW), which are changed with the digitalWrite function. During transmission of a value to a pin, we send its number as the first parameter, and its value as the second one.
To get the motors started, they must be powered. For that purpose, we connect Arduino with the computer via USB, as well as connect the driver module with an external power source. In my case, the external power source is a battery with a thick white cord, wherein the blue wire is a +, and the brown one is earthing.
One of the nuances, widely discussed on forums, is that the driver module and Arduino should have common earthing. To do that, we use an auxiliary wire to connect the driver earthing (the one with the brown wire) with the earthing of Arduino Shield (see the photo above).
If everything is done correctly, and your power source is not discharged, after uploading the program to Arduino and powering the motors, they should be able to start rotating forwards and backwards.
That’s it for today! In the following article, we will teach Pluto to receive main commands via a serial port, move forwards/backwards and turn to the left and to the right.
Have fun building your own robot, and contact us with any questions!
The full working code can be found here.
Here is the full series of articles about Pluto: