Introduction: Arduino Robot Moving Forward and Backwards Using HC-SR04 and L293D
This is a project that i have made to show the students how they can program a simple robot car to react depending of the input that it gets from an ultrasonic sensor. The robot demonstrated in the hour of code in the 1st Junior High School of Papagos with the aid of my fellow colleagues Mrs. Maravelia S. and Mr. Vamvanaris M.
Step 1: Parts List
First of all the parts that needed are the following
1. The chassis and motor kit that is easy to buy from ebay for about 10€ link here
2. An arduino board. I used an arduino uno R3 bought from ebay for 4€. For the original link here. For a cheap compatible one link here
3. A mini breaboard from ebay costs about 0.80€ from ebay link here (i have used a medium one as i didn't had a spare mini)
4. An L293D motor driver H-bridge IC costs about 0.8€ from ebay link here
5. An ultrasonic sensor HC-SR04 costs about 0.8€ from ebay link here
6. Battery pack (you can use four alkaline or NiMH AA that give 6V or two 18650 that give 7.2V like i did)
7. An 7805 voltage regulator IC (optional depending of the battery pack) link here
Step 2: Connections
It is essential to understand the use of L293D dual H-bridge Motor driver.
As you can see in the image there are connection for two motors (pins 3,6 (motor A1) and 11,14 (motor A2) accordingly). So the motors connect to these pins and depending of the arduino sketch rotate clockwise or anticlockwise. But in order to control their speed you have to use pin 1 enable for motor A1 and pin 9 enable for motor A2 and supply them with voltages from arduino analog output pins. This is also a way to calibrate the movement of the robot because the motors are not of the best quality and one is usually faster than the other.
For the power supply if you are using a battery pack up of more than 6V you have to use a 7805 voltage regulator that will give you a constant 5V voltage to supply the arduino, the L293D and the HC-SR04. As you can see from the schematic pin1 is input pin2 in Ground and pin3 is 5V output.
If you are not sure or afraid that you might burn something it is better to supply the arduino with its regular 9V battery and give 5V from the arduino to L293D and HC-SR04 and just give different power supply to the motors.
Please check that L293D connects to two power supplies, pin 16 (Vs+) which needs 5v for the chip to work and pin 8 (Vss+) which sends power to the motors and can control more power (up to 24V as you can see in the datasheet here). L293D is quite resilient and i haven't managed to destroy one yet, as with other electronics but it is better to study the connections prior to start connecting it with real voltages.
So before connecting anything study the schematics of L293D, HC-SR04, L7805, the fritzing diagram and the arduino ide code.
Step 3: Arduino Code
In the included arduino code file there is documentation for every pin connection.
You can see that depending of the distance the robot moves fast forward, forward, stop, backwards and fast backward. I am using the NewPing library for the HC-SR04 that is more accurate than my manual handling of HC-SR04.
You can test the distance that HC-SR04 measures using the serial port at 115200 baud rate. First remove the Vss (pin8 at L293D) cable so the motor are not working (and you don't have to hold the robot into the air)
Also there is documentation of calibrating the motors if one is faster than the other.