You are on page 1of 5

Food Living Outside Play Technology Workshop

Arduino motor drives cheap toy car


by techbitar on September 28, 2011

Table of Contents

Arduino motor drives cheap toy car . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

Intro: Arduino motor drives cheap toy car . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

File Downloads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

Related Instructables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

http://www.instructables.com/id/Arduino-motor-drives-cheap-toy-car/
Intro: Arduino motor drives cheap toy car
NOTE: This Instructable replaces a previous one titled "Automate cheap toy car with Arduino." For some strange reason, I was unable to insert photo slides and
add explanations to the first one. I hope to fix this by re-creating the original Instructable here.

I have bought a cheap toy car for a few Euros then rigged it with Arduino Uno, Motor Shield, IR sensor. Not too shabby.

I divided this project into two parts. In Part I, I take the cheap toy car apart then place the Arduino Uno + Motor Shield on the platform that contains 2 motors, one in the
back for locomotion and the other in the front to turn car left and right by turning a pinion.

For Part II, I added a Sharp IR sensor then programmed the Arduino to avoid obstacles. Since electronic parts are expensive in Jordan and are subject to arbitrary tariffs
and in some cases confiscations, it was necessary to scavenge parts from locally available products.

In future videos, I will add more sensors and enhance the robot's intelligence. I am also considering rewiring the robot as a differential steering robot since the bulk of
entry level robots seem based on it. This should give me access to tons of sample code as well as make my code of use to more designers.

You can see video of instructions below.

This is my first robot ever. Actually it's my first Arduino project ever. Heck it's my first electronics project ever. Love Arduino :)

//--------------------- SKETCH TO DRIVE ARDUINO CAR -------------------------------------


/*
This sample robot control code was mixed on 19 September 2011 by Mr. Hazim Bitar. Original sketch
was published by Ladyada.net toturial: http://www.ladyada.net/make/mshield/use.html

This code tests the Arduino Uno and the Motor Shield's which drives a gutted 6 Euro toy car I got from Gardens.
The toy car has two DC motors, one to drive the car's back wheels and the front motor to turn fron wheels gear left or right.

The Arduino is powered from a 9v battery while the Motor Shield, motorDrive, and motorTurn are powered
from the toy car's 4.5v (3 X AA) batteries.
*/

#include <AFMotor.h>

AF_DCMotor motorDrive(1, MOTOR12_64KHZ); // create motor #1, 64KHz pwm, to drive rear wheels forward and reverse
AF_DCMotor motorTurn(2, MOTOR12_64KHZ); // create motor #2, 64KHz pwm, to turn front wheels L and R

int ForwardDelay = 100; //for how long to keep forward movement


int ReverseDelay = 4000; // for how long to keep reversing
int DriveSpeed = 200;
int TurnForce = 240;

/* Sonar settings */
const int SonarPin = 0;
long anVolt, inches, cm;
int sum=0; //Create sum variable so it can be averaged
int avgrange=60; //Quantity of values to average (sample size)
int DistanceToObject = 6; // distance to obstacle before reverse - in inches
int TurnPause = 500; // delay between changes in turn
int AvgDelay = 6;

void setup() {
pinMode(SonarPin, INPUT); // set Sonar pin for input

Serial.begin(9600); // set up Serial library at 9600 bps


Serial.println("Gabba3at-X Test!");

motorDrive.setSpeed(DriveSpeed); // set the speed to 200/255


motorTurn.setSpeed(TurnForce); // set the speed to 200/255. This motor just turns front wheel gear L or R.

// reset direction and signal start

delay(TurnPause); delay(TurnPause); delay(TurnPause);


motorTurn.run(FORWARD);
Serial.println("turn forward");
delay(TurnPause);
delay(TurnPause);
http://www.instructables.com/id/Arduino-motor-drives-cheap-toy-car/
delay(TurnPause);
motorTurn.run(BACKWARD);
Serial.println("turn back");
delay(TurnPause);
delay(TurnPause);
delay(TurnPause);
motorTurn.run(RELEASE);
delay(TurnPause) ;

void loop() {

Serial.println("Forward/Straight move...");

sum = 0; // reset distance sum

for(int i = 0; i < avgrange ; i++)


{
/* Adafruit: The MaxSonar EZ1 outputs analog voltage with a scaling factor of
(Vcc/512) per inch. A supply of 5V yields ~9.8mV per inch. On the other hand,
the Arduino’s analog-to-digital converter (ADC) has a range of 1024, which means
each bit is ~4. 9mV. For that reason, to convert the number returned by the ADC to
inches, we have to divide by 2.
*/

anVolt = analogRead(SonarPin);
sum += anVolt;
delay(AvgDelay);
}

inches = sum/avgrange/2;
cm = inches * 2.54;
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();

if (inches <= DistanceToObject) { // object ahead

Serial.println("There's an obstacle. Go back");

// stop back motor


motorDrive.run(RELEASE); // FREE
delay(TurnPause);

// reset front motor


motorTurn.run(BACKWARD); // Turn
delay(TurnPause);
motorTurn.run(FORWARD); // Turn
delay(TurnPause);
motorTurn.run(FORWARD); // Turn
delay(TurnPause);

// start back motor to go in reverse


motorDrive.run(BACKWARD); // Reverse
delay(ReverseDelay);

// reset back motor


motorDrive.run(RELEASE); // Reverse
delay(TurnPause);

// reset front motor


motorTurn.run(RELEASE); // Turn ahead
delay(TurnPause);

motorDrive.run(FORWARD); // turn motor on going Forward which will spin rear wheel in Reverse
}

else {
motorDrive.run(FORWARD);
}

http://www.instructables.com/id/Arduino-motor-drives-cheap-toy-car/
Image Notes Image Notes
1. I bough this El Cheapo car from for less than 10 Euros. It's actually an RC toy 1. El Cheap car < 10 Euros
car. I figured since most robot fans in Jordan are too poor to buy high end robot 2. This motor drives the back wheels.
parts, it's better for me to work on scavenged parts this way my workshop 3. This motor moves front wheels left and right but does not move car forward.
students will be able to sustain their hobby on their own. 4. This is where 3 AA batteries are held.
5. Arduino Uno
6. Arduino Motor Shield Kit. I soldered this kit together and I did a terrible job
because I did not read the instructions carefully. my El cheapo 3 Euros solder
stick was also a factor. But i the end, it worked.
7. PIR sensor. I did not use it in this project but I have plans for it in future
robots.
8. Ultrasonic sensor. This wan also was not used in this project.
9. This is the Sharp IR sonar.
10. 9V to drive Arduino.
11. 9V battery holder to plug into Arduino
12. AA batteries.
13. Imagine in this spot 10uF by-pass capacitor I placed between VCC and
GND on the IR sensor. Without it, the readings on the sensor were so erratic to
the point of uselessness.

Image Notes Image Notes


1. IR sensor 1. This is the terminal on the Arduino Motor shield. I used M1 to drive back
2. 10uf by-pass capacitor for IR sensor. wheels, which in turn provide locomotion for car. I connected M2 to front motor
to turn wheels left and right.
2. Again, the 10uf by-pass capacitor between GND and VCC of Sharp IR
sensor to stabilize readings.

http://www.instructables.com/id/Arduino-motor-drives-cheap-toy-car/
Image Notes
1. IR sensor
2. This motor turns front wheels left and right. it was connected to M2 on motor
shield.
3. I believe this is a 0.1uF capacitor between terminals of each motor to protect
circuit from motor noise.
4. This is one ugly MF robot.

File Downloads

SonarCar.pde (3 KB)
[NOTE: When saving, if you see .tmp as the file ext, rename it to 'SonarCar.pde']

Related Instructables

Arduino-based Autonomous My Autonomous Make a scary Ard-e: The robot


robot with IR vehicles by HomeMade Robot 3: scarab robot by with an Arduino
radar by Nemweb Wall-E Robot Autonomous djsures as a brain by
techbitar (Photos) by Sensor Platform imadami
djsures 'Jimbo' by
jamesthequack

http://www.instructables.com/id/Arduino-motor-drives-cheap-toy-car/

You might also like