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. Option 1 : Build an Arduino car based on an old RC chassis
1.1 Step 1 Remove the Existing Rc Circuit
1.2 Step 2 Install all components. Schematics as the above picture
1.3 Step 3 Code, Code, More Code…
2. Option 2 : Build an Arduino car based on 4 motors (two left and two right)
2.1 Step 1 Install all components. Schematics as the above picture
2.2 Step 2 Code Code and some more Code!!!
3. More functions (ADVANCED CAR)
Special function: Collision avoidance System
Special Function. Bluetooth dead or alive!!
Code for the advanced car version
Option 1 : Build an Arduino car based on an old RC chassis
Before starting, make sure that you have:
- Arduino uno board
- L298n motor driver
- HC-05 or HC-05 Bluetooth module
- RC Car that that you don’t need any more
- Battery Holder
- 2x 18650 batteries
- Lots of dupont wires
Optional
- 4x leds for lights
- 1x speaker/buzzer for horn sound
- 1x Ultrasonic Distance Sensor – HC-SR04
Step 1 Remove the Existing Rc Circuit
The fun part. Remove everything from the car but the motors and the gears used to move and steer the car. Leave also the cables from the motors in order to be connected to the L298n motor driver
Step 2 Install all components. Schematics as the above picture
Step 3 Code, Code, More Code…
Please unplug the RX and TX pin of the Bluetooth module. Otherwise, you will not be able to upload the code to the Arduino.
#define in1 7 //Right #define in2 6 //Left #define en1 3 //turn speed #define in3 10 //Back #define in4 11 //Forward #define en2 5 //Forward speed #define LED 4 // Front lights pin #define LED2 A1 // Back lights pin const int buzzer = A0; //Buzzer to arduino pin A0 (Horn) int command; //Int to store app command state. int pwm_speedA = 255; //forward speed int pwm_speedB = 255; //turn speed void setup() { pinMode(in1, OUTPUT); pinMode(in2, OUTPUT); pinMode(en1, OUTPUT); pinMode(in3, OUTPUT); pinMode(in4, OUTPUT); pinMode(en2, OUTPUT); pinMode(LED, OUTPUT); //Set the Front Lights pin. pinMode(LED2, OUTPUT); //Set the Back lights. pinMode(buzzer, OUTPUT); //buzzer to arduino pin A0 (Horn) Serial.begin(9600); //Set the baud rate to your Bluetooth module. } void loop() { /////////////////////////////////////////////////////////////////////////////////////////// if (Serial.available() > 0) { command = Serial.read(); Stop(); //Initialize with motors stoped. switch (command) { ///////////////////////////////////// Turn on/off Front lights case 'W': digitalWrite(LED, HIGH); break; case 'w': digitalWrite(LED, LOW); break; ////////////////////////////////////// Turn on/off Back lights case 'U': digitalWrite(LED2, HIGH); break; case 'u': digitalWrite(LED2, LOW); break; /////////////////////////////////////// Toggle on/off Horn case 'V': tone(buzzer, 2500); // Send 1KHz sound signal... break; case 'v': noTone(buzzer);// Stop sound... break; //////////////////////////////////////// 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); } void back() { digitalWrite(in3, HIGH); analogWrite(en2,pwm_speedA); } void left() { digitalWrite(in2, HIGH); analogWrite(en1,pwm_speedB); } void right() { digitalWrite(in1, HIGH); analogWrite(en1,pwm_speedB); } void forwardleft() { digitalWrite(in4, HIGH); analogWrite(en2,pwm_speedA); digitalWrite(in2, HIGH); analogWrite(en1,pwm_speedB); } void forwardright() { digitalWrite(in4, HIGH); analogWrite(en2,pwm_speedA); digitalWrite(in1, HIGH); analogWrite(en1,pwm_speedB); } void backright() { digitalWrite(in3, HIGH); analogWrite(en2,pwm_speedA); digitalWrite(in1, HIGH); analogWrite(en1,pwm_speedB); } void backleft() { digitalWrite(in3, HIGH); analogWrite(en2,pwm_speedA); digitalWrite(in2, HIGH); analogWrite(en1,pwm_speedB); } void Stop() { analogWrite(in1, 0); analogWrite(in2, 0); analogWrite(in3, 0); analogWrite(in4, 0); }
Option 2 : Build an Arduino car based on 4 motors (two left and two right)
Before starting, make sure that you have:
- Arduino uno board
- L298n motor driver
- HC-05 or HC-05 Bluetooth module
- Car with 4 motors as the above picture
- Battery Holder
- 2x 18650 batteries
- Lots of dupont wires
Optional
- 4x leds for lights
- 1x speaker/buzzer for horn sound
- 1x Ultrasonic Distance Sensor – HC-SR04
Step 2 Install all components. Schematics as the above picture
Step 3 Code Code and some more Code!!!
#define in1 7 //Right #define in2 6 //Right #define en1 3 //right speed #define in3 10 //Left #define in4 11 //Left #define en2 5 //Left speed #define LED 4 // Front lights pin #define LED2 A1 // Back lights pin const int buzzer = A0; //Buzzer to arduino pin A0 (Horn) int command; //Int to store app command state. int pwm_speedA = 255; //forward speed int pwm_speedB = 48; //turn speed void setup() { pinMode(in1, OUTPUT); pinMode(in2, OUTPUT); pinMode(en1, OUTPUT); pinMode(in3, OUTPUT); pinMode(in4, OUTPUT); pinMode(en2, OUTPUT); pinMode(LED, OUTPUT); //Set the Front Lights pin. pinMode(LED2, OUTPUT); //Set the Back lights. pinMode(buzzer, OUTPUT); //buzzer to arduino pin A0 (Horn) Serial.begin(9600); //Set the baud rate to your Bluetooth module. } void loop() { if (Serial.available() > 0) { command = Serial.read(); Stop(); //Initialize with motors stoped. switch (command) { ///////////////////////////////////// Turn on/off Front lights case 'W': digitalWrite(LED, HIGH); break; case 'w': digitalWrite(LED, LOW); break; ////////////////////////////////////// Turn on/off Back lights case 'U': digitalWrite(LED2, HIGH); break; case 'u': digitalWrite(LED2, LOW); break; /////////////////////////////////////// Toggle on/off Horn case 'V': tone(buzzer, 2500); // Send 1KHz sound signal... break; case 'v': noTone(buzzer);// Stop sound... break; //////////////////////////////////////// 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(in3, HIGH); analogWrite(en2,pwm_speedA); digitalWrite(in2, HIGH); analogWrite(en1,pwm_speedA); } void back() { digitalWrite(in1, HIGH); analogWrite(en1,pwm_speedA); digitalWrite(in4, 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(in3, HIGH); analogWrite(en2,pwm_speedA); digitalWrite(in2, HIGH); analogWrite(en1,pwm_speedB); } void forwardright() { digitalWrite(in3, HIGH); analogWrite(en2,pwm_speedB); digitalWrite(in2, HIGH); analogWrite(en1,pwm_speedA); } void backright() { digitalWrite(in1, HIGH); analogWrite(en1,pwm_speedA); digitalWrite(in4, HIGH); analogWrite(en2,pwm_speedB); } void backleft() { digitalWrite(in1, HIGH); analogWrite(en1,pwm_speedB); digitalWrite(in4, HIGH); analogWrite(en2,pwm_speedA); } void Stop() { analogWrite(in1, 0); analogWrite(in2, 0); analogWrite(in3, 0); analogWrite(in4, 0); }
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
2 motor RC car
4 motor tank
Special Function. Bluetooth dead or alive!!
Imagine you move forward your car and suddenly Bluetooth is disconnected. Your car will continue to move forward.
The solution. The state pin! Connect the state pin of your Bluetooth module to the Arduino pin 12.
Load the full version code below. That’s it . The code reads pin 12. If the Bluetooth is connected pin 12 has voltage if not the Arduino stops all motors
So to enable this function delete // comment from the above code within the code provided
if (digitalRead(BTpin)==LOW){
Stop();
}
If your is not responding or just moves for a tenth of second please make sure that your Bluetooth module supports this function. I own some Bluetooth modules that it didn’t work
Code for the advanced car
Please unplug the RX and TX pin of the Bluetooth module. Otherwise, you will not be able to upload the code to the Arduino.
#define trigPin 8 #define echoPin 9 float duration, distance; // HC-SR04 ultrasonic sensor variable int varultrasound; //variable to check if collision button is pressed #define in1 7 //Right #define in2 6 //Left #define en1 3 //turn speed #define in3 10 //Back #define in4 11 //Forward #define en2 5 //Forward speed #define LED 4 // Front lights pin #define LED2 A1 // Back lights pin const int buzzer = A0; //Buzzer to arduino pin A0 (Horn) const int buzzer2 = A2; //Buzzer for HC-SR04 ultrasonic sensor pin int command; //Int to store app command state. int pwm_speedA = 255; //forward speed int pwm_speedB = 255; //turn speed const byte BTpin = 12; //connect the STATE pin to Arduino pin D12 void setup() { pinMode(BTpin, INPUT); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(in1, OUTPUT); pinMode(in2, OUTPUT); pinMode(en1, OUTPUT); pinMode(in3, OUTPUT); pinMode(in4, OUTPUT); pinMode(en2, OUTPUT); pinMode(LED, OUTPUT); //Set the Front Lights pin. pinMode(LED2, OUTPUT); //Set the Back lights. pinMode(buzzer, OUTPUT); //buzzer to arduino pin A0 (Horn) pinMode(buzzer2, OUTPUT); //buzzer for HC-SR04 ultrasonic sensor pin varultrasound=0 ; // initialize variable Serial.begin(9600); //Set the baud rate to your Bluetooth module. } void loop() { digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Measure the response from the HC-SR04 Echo Pin duration = pulseIn(echoPin, HIGH); // Determine distance from duration // Use 343 metres per second as speed of sound distance = (duration / 2) * 0.0343; /////////////////////////////////////////////////////////////////////////////////////////// // check distance from obstacle and if the button "Collision" from the application is pressed if ((varultrasound > 0)&& (distance<=10)){ digitalWrite(in3, HIGH); analogWrite(en2,255); tone(buzzer2, 2500); // Send 1KHz sound signal... delay(500); analogWrite(in3, 0); analogWrite(en2,0); noTone(buzzer2);// Stop sound... } ///////////////////////////////////////////////////////////////////Read BTpin and check if Bluetooth Disconnected //if (digitalRead(BTpin)==LOW){ // Stop(); //} /////////////////////////////////////////////////////////////////////////////////////////// if (Serial.available() > 0) { command = Serial.read(); Stop(); //Initialize with motors stoped. switch (command) { ///////////////////////////////////// Turn on/off Front lights case 'W': digitalWrite(LED, HIGH); break; case 'w': digitalWrite(LED, LOW); break; ///////////////////////////////////// Change variable varultrasound if button "Collision is pressed case 'M': varultrasound=varultrasound+1 ; break; case 'm': varultrasound=0 ; break; ////////////////////////////////////// Turn on/off Back lights case 'U': digitalWrite(LED2, HIGH); break; case 'u': digitalWrite(LED2, LOW); break; /////////////////////////////////////// Toggle on/off Horn case 'V': tone(buzzer, 2500); // Send 1KHz sound signal... break; case 'v': noTone(buzzer);// Stop sound... break; //////////////////////////////////////// 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); } void back() { digitalWrite(in3, HIGH); analogWrite(en2,pwm_speedA); } void left() { digitalWrite(in2, HIGH); analogWrite(en1,pwm_speedB); } void right() { digitalWrite(in1, HIGH); analogWrite(en1,pwm_speedB); } void forwardleft() { digitalWrite(in4, HIGH); analogWrite(en2,pwm_speedA); digitalWrite(in2, HIGH); analogWrite(en1,pwm_speedB); } void forwardright() { digitalWrite(in4, HIGH); analogWrite(en2,pwm_speedA); digitalWrite(in1, HIGH); analogWrite(en1,pwm_speedB); } void backright() { digitalWrite(in3, HIGH); analogWrite(en2,pwm_speedA); digitalWrite(in1, HIGH); analogWrite(en1,pwm_speedB); } void backleft() { digitalWrite(in3, HIGH); analogWrite(en2,pwm_speedA); digitalWrite(in2, HIGH); analogWrite(en1,pwm_speedB); } void Stop() { analogWrite(in1, 0); analogWrite(in2, 0); analogWrite(in3, 0); analogWrite(in4, 0); }
#define trigPin 8 #define echoPin 9 float duration, distance; // HC-SR04 ultrasonic sensor variable int varultrasound; //variable to check if collision button is pressed #define in1 7 //Right #define in2 6 //Right #define en1 3 //right speed #define in3 10 //Left #define in4 11 //Left #define en2 5 //Left speed #define LED 4 // Front lights pin #define LED2 A1 // Back lights pin const int buzzer = A0; //Buzzer to arduino pin A0 (Horn) const int buzzer2 = A2; //Buzzer for HC-SR04 ultrasonic sensor pin int command; //Int to store app command state. #define R_S A6 //ir sensor Right #define L_S A7 //ir sensor Left int lineTracker=0; int pwm_speedA = 255; //forward speed int pwm_speedB = 48; //turn speed int pwm_speedC = 110; //line tracker speed const byte BTpin = 12; //connect the STATE pin to Arduino pin D12 void setup() { pinMode(BTpin, INPUT); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(in1, OUTPUT); pinMode(in2, OUTPUT); pinMode(en1, OUTPUT); pinMode(in3, OUTPUT); pinMode(in4, OUTPUT); pinMode(en2, OUTPUT); pinMode(LED, OUTPUT); //Set the Front Lights pin. pinMode(LED2, OUTPUT); //Set the Back lights. pinMode(buzzer, OUTPUT); //buzzer to arduino pin A0 (Horn) pinMode(buzzer2, OUTPUT); //buzzer for HC-SR04 ultrasonic sensor pin varultrasound=0 ; // initialize variable pinMode(R_S, INPUT); // declare if sensor as input pinMode(L_S, INPUT); // declare ir sensor as input Serial.begin(9600); //Set the baud rate to your Bluetooth module. } void loop() { digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Measure the response from the HC-SR04 Echo Pin duration = pulseIn(echoPin, HIGH); // Determine distance from duration // Use 343 metres per second as speed of sound distance = (duration / 2) * 0.0343; /////////////////check if line tracker button is pressed/////////////////////// if (lineTracker==1) { if((digitalRead(R_S) == 0)&&(digitalRead(L_S) == 0)){ pwm_speedA = pwm_speedC; forward(); } //if Right Sensor and Left Sensor are at White color then it will call forword function if((digitalRead(R_S) == 0)&&(digitalRead(L_S) == 1)){ pwm_speedA = pwm_speedC; right(); delay(10); right(); } //if Right Sensor is Black and Left Sensor is White then it will call turn Right function if((digitalRead(R_S) == 1)&&(digitalRead(L_S) == 0)){ pwm_speedA = pwm_speedC; left(); delay(10); left(); } //if Right Sensor is White and Left Sensor is Black then it will call turn Left function if((digitalRead(R_S) == 1)&&(digitalRead(L_S) == 1)){Stop();} //if Right Sensor and Left Sensor are at Black color then it will call Stop function } /////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////// // check distance from obstacle and if the button "Collision" from the application is pressed if ((varultrasound > 0)&& (distance<=10)){ digitalWrite(in4, HIGH); analogWrite(en2,255); digitalWrite(in1, HIGH); analogWrite(en1,255); tone(buzzer2, 2500); // Send 1KHz sound signal... delay(500); analogWrite(in4, 0); analogWrite(en2,0); analogWrite(in1, 0); analogWrite(en1,0); noTone(buzzer2);// Stop sound... } ///////////////////////////////////////////////////////////////////Read BTpin and check if Bluetooth Disconnected //if (digitalRead(BTpin)==LOW){ // Stop(); //} /////////////////////////////////////////////////////////////////////////////////////////// if (Serial.available() > 0) { command = Serial.read(); Stop(); //Initialize with motors stoped. switch (command) { ///////////////////////////////////// Turn on/off Front lights case 'W': digitalWrite(LED, HIGH); break; case 'w': digitalWrite(LED, LOW); break; ///////////////////////////////////// Change variable varultrasound if button "Collision is pressed case 'M': varultrasound=varultrasound+1 ; break; case 'm': varultrasound=0 ; break; ////////////////////////////////////// Turn on/off Back lights case 'U': digitalWrite(LED2, HIGH); break; case 'u': digitalWrite(LED2, LOW); break; /////////////////////////////////////// Toggle on/off Horn case 'V': tone(buzzer, 2500); // Send 1KHz sound signal... break; case 'v': noTone(buzzer);// Stop sound... break; //////////////////////////////////////// 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; case 'K': lineTracker= lineTracker+1; break; case 'k': lineTracker=0; Stop(); break; } } } void forward() { digitalWrite(in3, HIGH); digitalWrite(in4, LOW); analogWrite(en2,pwm_speedA); digitalWrite(in2, HIGH); digitalWrite(in1, LOW); analogWrite(en1,pwm_speedA); } void back() { digitalWrite(in1, HIGH); digitalWrite(in2, LOW); analogWrite(en1,pwm_speedA); digitalWrite(in4, HIGH); digitalWrite(in3, LOW); analogWrite(en2,pwm_speedA); } void left() { digitalWrite(in3, HIGH); digitalWrite(in4, LOW); analogWrite(en2,pwm_speedA); digitalWrite(in1, HIGH); digitalWrite(in2, LOW); analogWrite(en1,pwm_speedA); } void right() { digitalWrite(in2, HIGH); digitalWrite(in1, LOW); analogWrite(en1,pwm_speedA); digitalWrite(in4, HIGH); digitalWrite(in3, LOW); analogWrite(en2,pwm_speedA); } void forwardleft() { digitalWrite(in3, HIGH); digitalWrite(in4, LOW); analogWrite(en2,pwm_speedA); digitalWrite(in2, HIGH); digitalWrite(in1, LOW); analogWrite(en1,pwm_speedB); } void forwardright() { digitalWrite(in3, HIGH); digitalWrite(in4, LOW); analogWrite(en2,pwm_speedB); digitalWrite(in2, HIGH); digitalWrite(in1, LOW); analogWrite(en1,pwm_speedA); } void backright() { digitalWrite(in1, HIGH); digitalWrite(in2, LOW); analogWrite(en1,pwm_speedA); digitalWrite(in4, HIGH); digitalWrite(in3, LOW); analogWrite(en2,pwm_speedB); } void backleft() { digitalWrite(in1, HIGH); digitalWrite(in2, LOW); analogWrite(en1,pwm_speedB); digitalWrite(in4, HIGH); digitalWrite(in3, LOW); analogWrite(en2,pwm_speedA); } void Stop() { analogWrite(in1, LOW); analogWrite(in2, LOW); analogWrite(in3, LOW); analogWrite(in4, LOW); }
- Code for 2 motor RC car
-
#define trigPin 8 #define echoPin 9 float duration, distance; // HC-SR04 ultrasonic sensor variable int varultrasound; //variable to check if collision button is pressed #define in1 7 //Right #define in2 6 //Left #define en1 3 //turn speed #define in3 10 //Back #define in4 11 //Forward #define en2 5 //Forward speed #define LED 4 // Front lights pin #define LED2 A1 // Back lights pin const int buzzer = A0; //Buzzer to arduino pin A0 (Horn) const int buzzer2 = A2; //Buzzer for HC-SR04 ultrasonic sensor pin int command; //Int to store app command state. int pwm_speedA = 255; //forward speed int pwm_speedB = 255; //turn speed const byte BTpin = 12; //connect the STATE pin to Arduino pin D12 void setup() { pinMode(BTpin, INPUT); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(in1, OUTPUT); pinMode(in2, OUTPUT); pinMode(en1, OUTPUT); pinMode(in3, OUTPUT); pinMode(in4, OUTPUT); pinMode(en2, OUTPUT); pinMode(LED, OUTPUT); //Set the Front Lights pin. pinMode(LED2, OUTPUT); //Set the Back lights. pinMode(buzzer, OUTPUT); //buzzer to arduino pin A0 (Horn) pinMode(buzzer2, OUTPUT); //buzzer for HC-SR04 ultrasonic sensor pin varultrasound=0 ; // initialize variable Serial.begin(9600); //Set the baud rate to your Bluetooth module. } void loop() { digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Measure the response from the HC-SR04 Echo Pin duration = pulseIn(echoPin, HIGH); // Determine distance from duration // Use 343 metres per second as speed of sound distance = (duration / 2) * 0.0343; /////////////////////////////////////////////////////////////////////////////////////////// // check distance from obstacle and if the button "Collision" from the application is pressed if ((varultrasound > 0)&& (distance<=10)){ digitalWrite(in3, HIGH); analogWrite(en2,255); tone(buzzer2, 2500); // Send 1KHz sound signal... delay(500); analogWrite(in3, 0); analogWrite(en2,0); noTone(buzzer2);// Stop sound... } ///////////////////////////////////////////////////////////////////Read BTpin and check if Bluetooth Disconnected //if (digitalRead(BTpin)==LOW){ // Stop(); //} /////////////////////////////////////////////////////////////////////////////////////////// if (Serial.available() > 0) { command = Serial.read(); Stop(); //Initialize with motors stoped. switch (command) { ///////////////////////////////////// Turn on/off Front lights case 'W': digitalWrite(LED, HIGH); break; case 'w': digitalWrite(LED, LOW); break; ///////////////////////////////////// Change variable varultrasound if button "Collision is pressed case 'M': varultrasound=varultrasound+1 ; break; case 'm': varultrasound=0 ; break; ////////////////////////////////////// Turn on/off Back lights case 'U': digitalWrite(LED2, HIGH); break; case 'u': digitalWrite(LED2, LOW); break; /////////////////////////////////////// Toggle on/off Horn case 'V': tone(buzzer, 2500); // Send 1KHz sound signal... break; case 'v': noTone(buzzer);// Stop sound... break; //////////////////////////////////////// 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); } void back() { digitalWrite(in3, HIGH); analogWrite(en2,pwm_speedA); } void left() { digitalWrite(in2, HIGH); analogWrite(en1,pwm_speedB); } void right() { digitalWrite(in1, HIGH); analogWrite(en1,pwm_speedB); } void forwardleft() { digitalWrite(in4, HIGH); analogWrite(en2,pwm_speedA); digitalWrite(in2, HIGH); analogWrite(en1,pwm_speedB); } void forwardright() { digitalWrite(in4, HIGH); analogWrite(en2,pwm_speedA); digitalWrite(in1, HIGH); analogWrite(en1,pwm_speedB); } void backright() { digitalWrite(in3, HIGH); analogWrite(en2,pwm_speedA); digitalWrite(in1, HIGH); analogWrite(en1,pwm_speedB); } void backleft() { digitalWrite(in3, HIGH); analogWrite(en2,pwm_speedA); digitalWrite(in2, HIGH); analogWrite(en1,pwm_speedB); } void Stop() { analogWrite(in1, 0); analogWrite(in2, 0); analogWrite(in3, 0); analogWrite(in4, 0); }
- Code for 4 motor tank
-
#define trigPin 8 #define echoPin 9 float duration, distance; // HC-SR04 ultrasonic sensor variable int varultrasound; //variable to check if collision button is pressed #define in1 7 //Right #define in2 6 //Right #define en1 3 //right speed #define in3 10 //Left #define in4 11 //Left #define en2 5 //Left speed #define LED 4 // Front lights pin #define LED2 A1 // Back lights pin const int buzzer = A0; //Buzzer to arduino pin A0 (Horn) const int buzzer2 = A2; //Buzzer for HC-SR04 ultrasonic sensor pin int command; //Int to store app command state. #define R_S A6 //ir sensor Right #define L_S A7 //ir sensor Left int lineTracker=0; int pwm_speedA = 255; //forward speed int pwm_speedB = 48; //turn speed int pwm_speedC = 110; //line tracker speed const byte BTpin = 12; //connect the STATE pin to Arduino pin D12 void setup() { pinMode(BTpin, INPUT); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(in1, OUTPUT); pinMode(in2, OUTPUT); pinMode(en1, OUTPUT); pinMode(in3, OUTPUT); pinMode(in4, OUTPUT); pinMode(en2, OUTPUT); pinMode(LED, OUTPUT); //Set the Front Lights pin. pinMode(LED2, OUTPUT); //Set the Back lights. pinMode(buzzer, OUTPUT); //buzzer to arduino pin A0 (Horn) pinMode(buzzer2, OUTPUT); //buzzer for HC-SR04 ultrasonic sensor pin varultrasound=0 ; // initialize variable pinMode(R_S, INPUT); // declare if sensor as input pinMode(L_S, INPUT); // declare ir sensor as input Serial.begin(9600); //Set the baud rate to your Bluetooth module. } void loop() { digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Measure the response from the HC-SR04 Echo Pin duration = pulseIn(echoPin, HIGH); // Determine distance from duration // Use 343 metres per second as speed of sound distance = (duration / 2) * 0.0343; /////////////////check if line tracker button is pressed/////////////////////// if (lineTracker==1) { if((digitalRead(R_S) == 0)&&(digitalRead(L_S) == 0)){ pwm_speedA = pwm_speedC; forward(); } //if Right Sensor and Left Sensor are at White color then it will call forword function if((digitalRead(R_S) == 0)&&(digitalRead(L_S) == 1)){ pwm_speedA = pwm_speedC; right(); delay(10); right(); } //if Right Sensor is Black and Left Sensor is White then it will call turn Right function if((digitalRead(R_S) == 1)&&(digitalRead(L_S) == 0)){ pwm_speedA = pwm_speedC; left(); delay(10); left(); } //if Right Sensor is White and Left Sensor is Black then it will call turn Left function if((digitalRead(R_S) == 1)&&(digitalRead(L_S) == 1)){Stop();} //if Right Sensor and Left Sensor are at Black color then it will call Stop function } /////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////// // check distance from obstacle and if the button "Collision" from the application is pressed if ((varultrasound > 0)&& (distance<=10)){ digitalWrite(in4, HIGH); analogWrite(en2,255); digitalWrite(in1, HIGH); analogWrite(en1,255); tone(buzzer2, 2500); // Send 1KHz sound signal... delay(500); analogWrite(in4, 0); analogWrite(en2,0); analogWrite(in1, 0); analogWrite(en1,0); noTone(buzzer2);// Stop sound... } ///////////////////////////////////////////////////////////////////Read BTpin and check if Bluetooth Disconnected //if (digitalRead(BTpin)==LOW){ // Stop(); //} /////////////////////////////////////////////////////////////////////////////////////////// if (Serial.available() > 0) { command = Serial.read(); Stop(); //Initialize with motors stoped. switch (command) { ///////////////////////////////////// Turn on/off Front lights case 'W': digitalWrite(LED, HIGH); break; case 'w': digitalWrite(LED, LOW); break; ///////////////////////////////////// Change variable varultrasound if button "Collision is pressed case 'M': varultrasound=varultrasound+1 ; break; case 'm': varultrasound=0 ; break; ////////////////////////////////////// Turn on/off Back lights case 'U': digitalWrite(LED2, HIGH); break; case 'u': digitalWrite(LED2, LOW); break; /////////////////////////////////////// Toggle on/off Horn case 'V': tone(buzzer, 2500); // Send 1KHz sound signal... break; case 'v': noTone(buzzer);// Stop sound... break; //////////////////////////////////////// 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; case 'K': lineTracker= lineTracker+1; break; case 'k': lineTracker=0; Stop(); break; } } } void forward() { digitalWrite(in3, HIGH); digitalWrite(in4, LOW); analogWrite(en2,pwm_speedA); digitalWrite(in2, HIGH); digitalWrite(in1, LOW); analogWrite(en1,pwm_speedA); } void back() { digitalWrite(in1, HIGH); digitalWrite(in2, LOW); analogWrite(en1,pwm_speedA); digitalWrite(in4, HIGH); digitalWrite(in3, LOW); analogWrite(en2,pwm_speedA); } void left() { digitalWrite(in3, HIGH); digitalWrite(in4, LOW); analogWrite(en2,pwm_speedA); digitalWrite(in1, HIGH); digitalWrite(in2, LOW); analogWrite(en1,pwm_speedA); } void right() { digitalWrite(in2, HIGH); digitalWrite(in1, LOW); analogWrite(en1,pwm_speedA); digitalWrite(in4, HIGH); digitalWrite(in3, LOW); analogWrite(en2,pwm_speedA); } void forwardleft() { digitalWrite(in3, HIGH); digitalWrite(in4, LOW); analogWrite(en2,pwm_speedA); digitalWrite(in2, HIGH); digitalWrite(in1, LOW); analogWrite(en1,pwm_speedB); } void forwardright() { digitalWrite(in3, HIGH); digitalWrite(in4, LOW); analogWrite(en2,pwm_speedB); digitalWrite(in2, HIGH); digitalWrite(in1, LOW); analogWrite(en1,pwm_speedA); } void backright() { digitalWrite(in1, HIGH); digitalWrite(in2, LOW); analogWrite(en1,pwm_speedA); digitalWrite(in4, HIGH); digitalWrite(in3, LOW); analogWrite(en2,pwm_speedB); } void backleft() { digitalWrite(in1, HIGH); digitalWrite(in2, LOW); analogWrite(en1,pwm_speedB); digitalWrite(in4, HIGH); digitalWrite(in3, LOW); analogWrite(en2,pwm_speedA); } void Stop() { analogWrite(in1, LOW); analogWrite(in2, LOW); analogWrite(in3, LOW); analogWrite(in4, LOW); }
Android Application
The application is available on Google Play (link)
This application was created by me using appinventor. Maybe, in the future I will post a tutorial on how the app was made.
How the app works.
Simple. It just sends when a button is pressed a character over the serial port.
Also there are some special features
- Remote control Interface
- Vibration
- Sounds when buttons are pressed
- Front lights and back lights buttons
- Three Function buttons for custom use
- Panel showing the command send to the Bluetooth
- Link to web page with detailed instructions
- Arduino code provided
- Speed control
The characters send to the Bluetooth are
- Forward (F)
- Back (B)
- Right (R)
- Left (L)
- Forward Left (G)
- Forward Right (I)
- Backward Left (H)
- Backward Right (J)
- Front lights on (W) off (w)
- Back Lights on (U) off (u)
- Horn on (V) off (v)
- Special button F1 on (K) off (k)
- Special button F2 on (P) off (p)
- Special button F3 on (N) off (n)
- Collision Avoidance system Enable (M) Disable (m)
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