Arduino Robot Car with Obstacle Avoidance. I’ve always been excited about autonomous cars and radio controlled toy cars so it was time for me to build my own with an Arduino Nano. The finished car has two modes. First is manual mode which allows you to drive it wirelessly from an Arduino UNO and a joystick shield. Second is the autonomous mode which drives the car continuously forward and avoids obstacles by stopping and steering away immediately. Obstacles are detected by the ultrasonic sensor attached to the front of the car.
Let’s now see how I built this car.
I didn’t want to spend too much time building the chassis so I decided to buy a robot car kitfrom ebay. After assembling the kit I soldered the wires onto the motors and screwed the 4xAA battery holder then I was ready to add the Arduino electronics.
Motor drive circuit
Driving the motors from the Arduino is not possible as they draw too much current so you need a separate motor drive unit to help the Arduino. I purchased a pretty, red L298N module from ebay. Here’s how it looks like:
This module can drive two motors up to 2000mA each and maximum 50 volts. That’s perfectly fine as our motors will only be driven from the 4xAA batteries which is 6 volts in total. The two motors are connected to port 1 and port 2 (see numbers on the image). Wires can be interchanged as reversed polarity only makes the motors go in reverse.
Behind pin 3 and 4 there is a jumper. If this jumper is in place then you have to make sure your supply voltage is lower than 12V. It will also allow you to use pin 5 as regulated 5V output for your Arduino. On my car I connected the positive wire from the battery pack to pin 3 and the ground wire to number 4. Next I connected the 5V output pin from the motor drive module, which is pin number 5, to my Arduino Nano’s 5V pin and I also connected pin number 4 to my Arduino’s GND. Power is now connected.
The 6 pins marked with number 6 are there to take motor control commands from the Arduino, 3 for each motor. Here’s how it works:
– ENA and ENB are the enable pins for the two motors. If the jumper is in place the motors will be driven at full speed. If we take off the jumpers and connect a PWM signal from the Arduino to them then we can change the speed of the motors.
– IN1 and IN2 can change the direction of motor number 1. If we send HIGH to IN1 and LOW to IN2 then the motor will go forward, if IN1 is LOW and IN2 is HIGH then the motor will go backwards. The same applies to the second motor and the IN3 and IN4 pins.
– If I want to run the first motor at half speed forward then ENA is a PWM value of 128, IN1 is HIGH and IN2 is LOW
– If I want to run the second motor at full speed backwards then ENB is a PWM value of 255, IN3 is LOW and IN4 is HIGH
I added a 1000uF to pin 4 and 5 as the motors draw a lot of current when speeding up and the Arduino restarted quite often when there was a voltage drop. This fixed the problem and now the the motor drive circuit done.
The hardware build was so easy that my 4-year-old daughter managed to understand the circuit and what each of the components do.
Autonomous mode and the ultrasonic sensor
I used a cheap ultrasonic distance sensor with a 4 meter range. That’s perfectly enough as I set the obstacle detection limit to only 20 centimetres. The sensor has 4 pins: VCC is connected to the Arduino’s 5V pin, GND to GND, Trig to D6 and Echo to D5.
The example code to read the sensor is only a few lines:
After I had all the motors and the distance sensor connected up I put the motors into a continues forward motion for the autonomous mode. In the meantime the car was constantly checking the distance sensor and instructed the motors to steer away until there was no longer anything in front. Here's what I did in the loop() function:
Manual mode and the joystick shield
This was a little bit more complicated as we now have wireless communication. The controller was an Arduino UNO with a joystick shield. You can also see the nRF24l01 antenna on the left top corner.
This controller was powered from a 9V battery. The button and the joystick on the shield are simple sensors so you can read them with the digitalRead() and analogRead() functions. The below piece of code is the final code I have for the joystick. It first checks the state of the joystick then sends through the wireless messages through the nRF24l01 module.
To read this from the car you open a reading pipe then check the radio.available() function for incoming messages. Below is the final piece of code that is running on the car:
I also got the bluetooth communication working between my phone and the Arduino but only with an Arduino UNO. It’s probably because the Arduino Nano I was using in this project requires a 3.3V signal level on RX and TX and the UNO can take 5V. If you have done this before please email me and let me know!
Learn hardware prototyping and become a better developer.