3 Simple Ways to
Share What You Make

With Instructables you can share what you make with the world — and tap into an ever-growing community of creative experts.

PhotosPhotos

Share one or more photos of a project, recipe, or whatever you've made, quickly and easily.

Step by StepStep-By-Step

Share your step-by-step photos with text instructions of what you made so others can do it too!

VideoVideo

Share your how-to video. You'll need your embed code from a video site such as YouTube.

Arduino motor drives cheap toy car

Arduino motor drives cheap toy car
«
  • Gabba3at-X robot (4).JPG
  • Gabba3at-X robot (8).JPG
  • Gabba3at-X P2 (4).jpg
  • Gabba3at-X P2 (1).jpg
  • Gabba3at-X P2 (3).jpg
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);
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);
}

}
 
2 comments
Apr 29, 2012. 12:54 PMataiwo says:
can i use any type of sensor

Pro

Get More Out of Instructables

Already have an Account?

close

All Steps Viewing
View all steps of an Instructable on the same page when you're a Pro Member.

Upgrade to Pro today!
32
Followers
18
Author:techbitar
Did I unplug the solder iron?