Hi. My name is Emmanouel and today I am going to share with you, instructions on how to build an Arduino based car using either an available toy car with two main motors (one for steering) and one for the forward/backward motion.
The other classical project is the RC tank. In this case the tank uses both left or right motors for moving and steering. If both right motors move forward and both left motors stay still or move with lower speed then the tank turns right.
In this tutorial we are going to build both a car and a tank.
List of contents
1.Build a Simple Arduino Tank/Car
1.1 Components and Parts
1.2 Schematics
1.3 Code
1.4 Android App
2. Step by Step Build Up
2.1 Step 1 Lights Lights and More Lights
2.2 Step 2
3. More functions (ADVANCED CAR)
Special function: Collision avoidance System
Special Function. Bluetooth dead or alive!!
Code for the advanced car version
First Things First. Build a simple version first.
Ready to build your own mini car? This tutorial shows you how to assemble a simple car using a chassis, a motor controller, and Bluetooth module.
Let’s build! Use the diagram below as your blueprint for assembling the car.
//////////////////////setup Dual Motor Driver Module L298N pins ///////////////////// #define in1 7 //Front Right #define in2 6 //Back Right #define en1 3 //right speed #define in3 10 //Front Left #define in4 11 //Back Left #define en2 5 //Left speed int BTcommand; //Int to store app Bluetooth command state. int pwm_speedA = 255; //forward speed int pwm_speedB = 48; //stiring speed void setup() { pinMode(in1, OUTPUT); pinMode(in2, OUTPUT); pinMode(en1, OUTPUT); pinMode(in3, OUTPUT); pinMode(in4, OUTPUT); pinMode(en2, OUTPUT); Serial.begin(9600); //Set the baud rate to your Bluetooth module. } void loop() { if (Serial.available() > 0) { BTcommand = Serial.read(); Stop(); //Initialize with motors stoped. switch (BTcommand) { case 'F': forward(); break; case 'B': back(); break; case 'L': left(); break; case 'R': right(); break; case 'G': forwardleft(); break; case 'I': forwardright(); break; case 'H': backleft(); break; case 'J': backright(); break; case '0': pwm_speedA = 100; break; case '1': pwm_speedA = 140; break; case '2': pwm_speedA = 153; break; case '3': pwm_speedA = 165; break; case '4': pwm_speedA = 178; break; case '5': pwm_speedA = 191; break; case '6': pwm_speedA = 204; break; case '7': pwm_speedA=216; break; case '8': pwm_speedA = 229; break; case '9': pwm_speedA = 242; break; case 'q': pwm_speedA = 255; break; } } } void forward() { digitalWrite(in4, HIGH); analogWrite(en2,pwm_speedA); digitalWrite(in1, HIGH); analogWrite(en1,pwm_speedA); } void back() { digitalWrite(in2, HIGH); analogWrite(en1,pwm_speedA); digitalWrite(in3, HIGH); analogWrite(en2,pwm_speedA); } void left() { digitalWrite(in3, HIGH); analogWrite(en2,pwm_speedA); digitalWrite(in1, HIGH); analogWrite(en1,pwm_speedA); } void right() { digitalWrite(in2, HIGH); analogWrite(en1,pwm_speedA); digitalWrite(in4, HIGH); analogWrite(en2,pwm_speedA); } void forwardleft() { digitalWrite(in4, HIGH); analogWrite(en2,pwm_speedB); digitalWrite(in1, HIGH); analogWrite(en1,pwm_speedA); } void forwardright() { digitalWrite(in4, HIGH); analogWrite(en2,pwm_speedA); digitalWrite(in1, HIGH); analogWrite(en1,pwm_speedB); } void backright() { digitalWrite(in2, HIGH); analogWrite(en1,pwm_speedB); digitalWrite(in3, HIGH); analogWrite(en2,pwm_speedA); } void backleft() { digitalWrite(in2, HIGH); analogWrite(en1,pwm_speedA); digitalWrite(in3, HIGH); analogWrite(en2,pwm_speedB); } void Stop() { analogWrite(in1, 0); analogWrite(in2, 0); analogWrite(in3, 0); analogWrite(in4, 0); }
- Lighting Control:
-
- Front, back, and brake lights can be manually controlled.
- The stop lights should be red LEDs that activate when the car is stationary.
- Bluetooth Control: Use Bluetooth to control the lights. Send the character “W” to turn the front lights on and “w” to turn them off. Similarly, use “U” and “u” for the rear lights.
-
- Collision Avoidance:
-
- An SR-04 sensor detects obstacles in front and behind the car.
- If an obstacle is within 20cm, the car stops, an additional horn is activated and then the car reverses for 20 milliseconds.
- Speed Control:
- Adjust speed manually using “HIGH,” “MEDIUM,” or “LOW” settings.
- Fine-tune speed by adjusting a speed bar (0-9).
- The corresponding number is sent via Bluetooth.
- Horn Control:
- The horn is activated by pressing a button and deactivated by releasing it.
- “V” is sent to activate the horn, and “v” is sent to deactivate it.
- Gyro Control:
- Two gyro control modes are available:
- Single-Axis Gyro: Turn the phone left/right to steer the car. Use app buttons for forward/backward movement.
- Dual-Axis Gyro: Tilt the phone to control both steering and forward/backward movement.
- Gyro control deactivates autonomous and line-following modes.
- Two gyro control modes are available:
- Laser Pointer:
- A laser pointer indicates the car’s direction, especially useful for camera-based driving.
- Automatic Lights:
- A light sensor automatically turns lights on/off based on ambient light conditions.
- Manual light control is disabled when auto-lights are active.
- Autonomous Mode:
- The car drives autonomously, avoiding obstacles.
- It stops, scans left and right, and turns towards the path with the most clearance.
- Gyro, collision avoidance, and line-following modes are deactivated in autonomous mode.
- Line-Following Mode:
- The car follows a black line using three sensors.
- The middle sensor detects the line, while the side sensors correct the car’s direction.
- Gyro and autonomous modes are deactivated in line-following mode.
- PS2 Controller Mode:
- The car can be controlled using a wireless PS2 controller.
- Switch the car to PS2 mode before turning it on.
- To switch back to Bluetooth mode, turn the car off, switch to BT mode, and turn it back on.
- Custom Buttons
- Two more buttons named F1 and F2 are customizable, allowing users to assign them to specific functions. To rename a button, simply long-press it.
- Bluetooth Reliability.
-
- Imagine driving your car and suddenly, the Bluetooth connection drops. This could lead to unintended consequences, especially if the car is in autonomous mode. To prevent such issues, connect the state pin of your Bluetooth module to Arduino pin 12. This pin indicates the module’s connection status.
- The Arduino continuously reads the voltage level on pin 12. If the voltage drops, it means the Bluetooth connection is lost. Upon detecting a connection loss, the Arduino immediately stops all motors, bringing the car to a safe halt. By implementing this simple yet effective solution, you can significantly enhance the safety and reliability of your Bluetooth-controlled car.
Adding Front Lights, Back Lights and Stop Lights
Arduino nano
KY-008 3Pin 650nm Red Laser Transmitter
As much as the stairs
3d printed LCD case
Link to 3d files https://www.thingiverse.com/thing:4693741
More functions
Special function: Collision avoidance System
You can install in front of the car an HC-SR04 ultrasonic sensor. In this way the sensor measures obstacles that are in the way. When the distance measured, is below 10 cm (you can adjust it) and the special key D is sent from the app (Menu -> Collision button is enabled) the car is going backwards for half a second and then it stops
SR04 pins
Echo Pin to Arduino Pin 9
Trig Pin to Arduino Pin 8The Schematics and the code for the Arduino in this case is
Troubleshooting
1. If the car is not going on the right direction.
If this happens and your car is not going in the right direction then you don’t have to modify any code. You have to just change the motor driver wires in pairs.
For instance swap in1 with in2 or in3 with in4 pins
2. Cannot upload the code to the Arduino
You must know that when you are uploading the code to the Arduino. You must unplug the RX and TX pin of the Bluetooth module. Otherwise, you will not be able to upload the code to the Arduino.
3. The car in not responding the right way or often Bluetooth disconnections.
There is not enough power supply. Please make sure that both 18650 batteries are full charged