
My oldest son got an RC car for Christmas a few years ago, but now he’s lost the remote and hasn’t used it in ages. So I asked him if it was ok for daddy to mess with it and see if we couldn’t build something cool out of it instead of it just sitting there gathering dust.
Now, him being a cool kid and all, he let me start the work. First thing we did was have a look at what was already inside to we could determine what we needed to build to let this car drive on its own.
Inside there were 2 DC-motors, the one driving forward bigger then the one driving the steering. The battery-pack located underneath was of course dimensioned to drive these motors, so we’ll be using that to power the car, while we have a separate power supply for the Arduino which we are using to steer the vehicle and analyze input from sensors to avoid hitting stuff.
For controlling the distance to the nearest obstacle I decided to use ultra-sonic sensors. These send out a sound the ear can’t hear, and calculates the distance based on how long it takes for the sound to bounce back. I’m planning on using one in the front and one in the back of the vehicle. First I had to test them on a breadboard to make sure I understood how these worked and was able to get a reading from them. The sensors are a pair of HC-SR04 from DealExtreme (US$4.00).
These worked great together with the NewPing library for Arduino.
I used this code to test them, and got readings in my Serial Output window:
1 2 3 |
// --------------------------------------------------------------------------- // Example NewPing library sketch that does a ping about 20 times per second. // --------------------------------------------------------------------------- |
1 |
#include <NewPing.h> |
1 2 3 4 5 |
#define TRIGGER_PIN 7 // Arduino pin tied to trigger pin on the ultrasonic sensor. #define ECHO_PIN 6 // Arduino pin tied to echo pin on the ultrasonic sensor. #define TRIGGER_PIN2 5 // Arduino pin tied to trigger pin on the ultrasonic sensor. #define ECHO_PIN2 4 // Arduino pin tied to echo pin on the ultrasonic sensor. #define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm. |
1 2 |
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance. NewPing sonar2(TRIGGER_PIN2, ECHO_PIN2, MAX_DISTANCE); |
1 2 3 |
void setup() { Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results. } |
1 2 3 4 5 6 7 8 9 10 11 12 |
void loop() { delay(500); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings. unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS). Serial.print("Ping: "); Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range) Serial.println("cm"); delay(500); unsigned int uS2 = sonar2.ping(); // Send ping, get ping time in microseconds (uS). Serial.print("Ping2: "); Serial.print(uS2 / US_ROUNDTRIP_CM); Serial.println("cm"); } |
To drive the motors I ordered a L9110 DC / Stepper Motor Driver, also from DealExtreme (US$4.30). This shield can be used to control two DC motors with external power supply, so fits perfectly for this project.
After hooking up the wiring like this:
- GND – GND on the external power supply
- VCC – positive on the external power supply
- B-IB – pin 9
- B-IA – pin 6
- A-IB – pin 5
- A-IA – pin 3
To try this out I uploaded the following sketch to the Arduino:
1 2 3 4 |
int iaa = 3; int iba = 5; int iab = 6; int ibb = 9; |
1 2 3 4 5 6 7 8 |
void setup() { // initialize the digital pin as an output. pinMode(iaa, OUTPUT); pinMode(iba, OUTPUT); pinMode(iab, OUTPUT); pinMode(ibb, OUTPUT); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
// the loop routine runs over and over again forever: void loop() { analogWrite(iaa, 125); analogWrite(iab, 125); analogWrite(iba, 0); analogWrite(ibb, 0); delay(10000); analogWrite(iaa, 0); analogWrite(iab, 0); analogWrite(iba, 255); analogWrite(ibb, 255); delay(10000); } |
This proved to work and it drove one motor at half-ish speed for 10 seconds, then the other at full speed for 10 seconds and repeating.
Now the time had come to hook it all up to the chassis and combine the code to go for a test drive.
Here you can see everything hooked up and ready to go for a test-drive.
I uploaded the following code, really just a combination of the earlier, separate ones for the motors and the ultra-sonic sensors.
1 |
#include <NewPing.h> |
1 2 3 4 5 |
#define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor. #define ECHO_PIN 10 // Arduino pin tied to echo pin on the ultrasonic sensor. #define TRIGGER_PIN2 13 // Arduino pin tied to trigger pin on the ultrasonic sensor. #define ECHO_PIN2 11 // Arduino pin tied to echo pin on the ultrasonic sensor. #define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm. |
1 2 |
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance. NewPing sonar2(TRIGGER_PIN2, ECHO_PIN2, MAX_DISTANCE); |
1 2 3 4 |
int aia = 3; int aib = 5; int bia = 6; int bib = 9; |
1 2 3 4 5 6 |
void setup() { pinMode(aia, OUTPUT); pinMode(aib, OUTPUT); pinMode(bia, OUTPUT); pinMode(bib, OUTPUT); } |
1 2 |
void loop() { unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS). |
1 2 3 4 5 |
if ((uS / US_ROUNDTRIP_CM) > 25|| (uS / US_ROUNDTRIP_CM)==0) { // (uS / US_ROUNDTRIP_CM)==0) because if too far away it will return 0, must look at this later some time. analogWrite(aia, 0); analogWrite(aib, 175); analogWrite(bia, 0); analogWrite(bib, 255); |
1 |
else { |
1 2 3 4 5 6 7 8 9 |
unsigned int uS2 = sonar2.ping(); // Send ping, get ping time in microseconds (uS). analogWrite(aia, 75); analogWrite(aib, 0); analogWrite(bia, 255); analogWrite(bib, 0); delay(1500); } delay(50); } |
This resulted in this:
At least it went forward and backwards like it should, but I need to set up the sensors in a better way. I’m thinking maybe on the front roof. After this was shot the vehicle got stuck and one of the motor controllers burned so it needs to get replaced (luckily I’ve got another one lying around). Before hooking the other one up I need to figure out how to stop the motors if it gets stuck.
Updates will come as the project progress.
Hi Arduino r/c car great but if a tv remote is used with a infared sensor what would the same schematic look like?
The point here is that it will drive around by itself, not me controlling it..