Robot Car – p2: Building a prototype

Before thinking to the car body I decided to first build a “shoe box” prototype. This decision was partly because I’ve not built my 3D printer at that time but before soldering the components on a PCB board it is a good strategy to make a prototype using a breadboard.


The breadboard it very good for prototyping even if it is not a good solution for the long terme because it is difficult to keep all the connections in the right place.

Arduino card:

For prototyping the Arduino UNO is a a very good option. You have enough port easily pluggable  a USB port to upload your code. There are two reason why you’ll choose another card: if you need more port or if you want something smaller to integrate. We will discuss this later bu for now we go with the UNO.


You want to make sure that you don’t create connection hasard because to many wires get loose. The recommended solution is to fix the Arduino UNO and the breadboard on s small wood board so they will stick together.

Power Supply:

The Arduino can run with a voltage from 5v up to 12v. I like to use AA refillable batteries because they are cheap and easy to replace. The only issues is that is normal AA batteries gives 1.5V the refillable deliver only 1.2V. So I choose a pack of 6 refillable batteries placed in a 6 batteries box.

DC Motors:

7.2 volts is enough to feed the 2 DC motors but the Arduino output can not generate enough current to run such motors. For controlling the motor speed you need to use a bridge. You can use a motor shield that you can plug on the top of the UNO but this is not the solution I choose since I was not sure to keep the UNO in the final project. I preferred to go with a cheaper solution using a L298N H Bridge that can control the speed two DC motors in both direction (front,back).

To control the 2 motors directions and speed you need 4 digital and 2 PWM outputs. To understand better how the L298N works and how to control it from the Arduino you can read this excellent page from Instructables: Arduino Modules – L298N Dual H-Bridge Motor Controller

Controlling the direction:


With a 2 wheel car the direction can be changed by changing the relative speed of the two wheels. You can even have the vehicle rotate on itself by having the 2 wheels rotating in opposite directions.

But the speed of DC motor is not regular enough to expect that if you apply the same voltage on both wheels the car will go straight. To make sure that you keep the expected direction you need a control mechanism. I choose a HMC5883L a 3 axis magnetometer compass. This is a cheap module that measure the X/Y/Z angle with the earth magnetic field and gives you an absolute direction.

This module uses the I2C protocol to communicate the 3 coordinates. The Wire library implements this protocol and makes the reading of the coordinates is easy.

The main difficulty of this way the direction is not very precise and you can measure the new direction only once the car has done a turn. You then adapt the relative speed of the two wheel to correct the car direction.

Avoiding obstacles:


To detect distant obstacles I have chosen a very common solution: a ultrasonic detector: HC-SR04. This module is made of a ultrasonic emitter an a receptor. The module can emit a sound when an output pin is switched to HIGH and raise an input pin to HIGH when the echo sound is received. You can easily drive this module using your own code. The speed of sound in air is around 343 m/s which means that for a 1 m distant object it takes around 6ms to come back (3ms each way). So sending a signal an measuring the distance by counting the number of microseconds spend during the top and the echo is easy. You can use the pulseIn command to get the exact time when the pin raises. For more details you can check this post: SR04-Example.

After multiple tests I found much easier to use a library call NewPing. This library works very well and solve some issues I’ve got with a “Chinese”  make of the module.

The module can detect objects up to 2 m but some small width objects like chair legs are not always detected. We will discuss this point in the paragraph “Lessons learned from the prototype”.

Testing the prototype:

You can see the prototype running in the following video:

Lessons learned from the prototype:

It is great to see your first prototype running and validate your options but as you can see in the video there are some behavior that need to be fixed:


Controlling the direction with a compass is not responsive enough to make the car go straight. You can see on the video that because of late detection you can only correct the angle after few seconds and the car has already done a small turn. The trajectory looks more to a zig-zag curve than a nice straight line.

To avoid this behavior we  need to put a more precise system to measure the wheel rotation. The motors axis can be terminated by a coding wheel.


The coding wheel is a simple wheel with radial slots that you couple with a optocoupler device: LTH 307-01. The optocoupler is composed of a LED and a photo transistor. When something passes between the two the voltage on the phototransistor goes down. If you find the LTH 307-01 cabling too complex you can also use a end-stop like the one used for 3D printers.

To calculate the wheel speed you must count the slots  passing in front of the led. The easiest solution is to tie the output on the optocoupler to an interrupt of the Arduino. The Arduino UNO provide 2 interrupts vectors so you will use one for each wheel.

Obstacles not detected:

You can see a the end of the video that the car hit a chair leg and get stuck. We will need to detect such case can can still happen because our obstacle detection is not perfect. In fact the coding wheel describe before can also help us detect that the wheel a no longer spinning or spinning too slow because the car is stuck somewhere.

In that case we will program a specific behavior to undock the car.

What next:

We have now a good idea of what can be the first version of the robot car. In the following post I will describe it in details.

In the future posts I’ll also share Arduino code with the necessary explanations.

Be the first to comment

Leave a Reply

Your email address will not be published.