This tutorial will show you how to use an Arduino to build a 2WD robotic car. You can extend this project by using sensors to measure wheel speed (for improved accuracy when steering or maintaining a straight line) or sensors to avoid objects or follow a line.
Parts required
Here is what you will need:
- Arduino Uno board
- 2WD robotic car kit (2 wheels, 2 motors, chassis)
- 4 x AA batteries
- Motor shield with 6 wires
- Arduino IDE software (free at http://www.arduino.cc/)
- USB cable
- 2 x wheel speed encoders and infrared sensors (optional)
- Assemble all chassis parts and attach the motors to the wheels (if not already assembled).
- Connect the red wire from the battery enclosure to VIN on the Motor Shield.
- Connect the black wire from the battery enclosure to GND on the Motor Shield.
- Connect the red wire from Motor A to A+ on the Motor Shield, and the black wire from Motor A to A- on the Motor Shield.
- Connect the red wire from Motor B to B+ on the Motor Shield, and the black wire from Motor B to B- on the Motor Shield.
- Attach the Motor Shield to the top of the Arduino Uno board – be careful not to bend or damage any of the pins!
- Connect the Arduino Uno board to the computer via the USB cable and upload the code using the Arduino IDE software.
- Insert four AA batteries in the battery enclosure.
- Test your robot! If your robot goes in the wrong direction or doesn’t move, check that you have the Motor pins in the correct places and make sure no pins are loose.
Extra challenges:
- Control your robotic car using an Android app on a smartphone via Bluetooth (refer to the Controlling an LED by Bluetooth tutorial for instructions on how to make an app using App Inventor and how to attach a Bluetooth module).
- Add code that will control the speed of the motors using the encoder discs and infrared sensors.
The code to test your robot
Use this code to test that your wheels spin and that your robot is wired up correctly.
void setup() { //Setup Channel A pinMode(12, OUTPUT); //Initiates Motor Channel A pin pinMode(9, OUTPUT); //Initiates Brake Channel A pin //Setup Channel B pinMode(13, OUTPUT); //Initiates Motor Channel B pin pinMode(8, OUTPUT); //Initiates Brake Channel B pin } void loop(){ //Motor A forward @ full speed digitalWrite(12, HIGH); //Establishes forward direction of Channel A digitalWrite(9, LOW); //Disengage the Brake for Channel A analogWrite(3, 255); //Spins the motor on Channel A at full speed //Motor B backward @ half speed digitalWrite(13, LOW); //Establishes backward direction of Channel B digitalWrite(8, LOW); //Disengage the Brake for Channel B analogWrite(11, 123); //Spins the motor on Channel B at half speed delay(3000); digitalWrite(9, HIGH); //Engage the Brake for Channel A digitalWrite(8, HIGH); //Engage the Brake for Channel B delay(1000); //Motor A forward @ full speed digitalWrite(12, LOW); //Establishes backward direction of Channel A digitalWrite(9, LOW); //Disengage the Brake for Channel A analogWrite(3, 123); //Spins the motor on Channel A at half speed //Motor B forward @ full speed digitalWrite(13, HIGH); //Establishes forward direction of Channel B digitalWrite(8, LOW); //Disengage the Brake for Channel B analogWrite(11, 255); //Spins the motor on Channel B at full speed delay(3000); digitalWrite(9, HIGH); //Engage the Brake for Channel A digitalWrite(8, HIGH); //Engage the Brake for Channel B delay(1000); } |
Click here if you would like a PDF copy of these instructions.