Introduction: 4 Wheel Car With IR Control (Optional Ultrasonic Sensor)
In this project I will show you how to make a vehicle controlled by a IR remote control. for this we will use an infrared receiver. It will show you how to receive and decode the received button codes.
results: video of result
Step 1: List Element:
- iR Receiver and IR remote
- Arduino uno
- L298N
- 4x TT gear Motor
- 4x wheels
- 9V Battery
- Ultrasonic Sensor (optional)
Step 2: What Is L298N?
Usage:
H-Bridge's are typically used in controlling motors speed and direction, but can be used for other projects such as driving the brightness of certain lighting projects such as high powered LED arrays.
How it works:
An H-Bridge is a circuit that can drive a current in either polarity and be controlled by *Pulse Width Modulation (PWM).
* Pulse Width Modulation is a means in controlling the duration of an electronic pulse. In motors try to imagine the brush as a water wheel and electrons as a the flowing droplets of water. The voltage would be the water flowing over the wheel at a constant rate, the more water flowing the higher the voltage. Motors are rated at certain voltages and can be damaged if the voltage is applied to heavily or if it is dropped quickly to slow the motor down. Thus PWM. Take the water wheel analogy and think of the water hitting it in pulses but at a constant flow. The longer the pulses the faster the wheel will turn, the shorter the pulses, the slower the water wheel will turn. Motors will last much longer and be more reliable if controlled through PWM.
Pins:
- Out 1: Motor A lead out
- Out 2: Motor A lead out
- Out 3: Motor B lead out
- Out 4: Motor B lead out (Can actually be from 5v-35v, just marked as 12v)
- GND: Ground
- 5v: 5v input (unnecessary if your power source is 7v-35v, if the power source is 7v-35v then it can act as a 5v out)
- EnA: Enables PWM signal for Motor A (Please see the "Arduino Sketch Considerations" section)
- In1: Enable Motor A
- In2: Enable Motor A
- In3: Enable Motor B
- In4: Enable Motor B
- EnB: Enables PWM signal for Motor B (Please see the "Arduino Sketch Considerations" section)
Specifications:
- Double H bridge Drive Chip: L298N
- Logical voltage: 5V Drive voltage: 5V-35V
- Logical current: 0-36mA Drive current: 2A (MAX single bridge)
- Max power: 25W
- Dimensions: 43 x 43 x 26mm
- Weight: 26g
*Built-in 5v power supply, when the driving voltage is 7v-35v
Source: Source
Step 3: What Is Ultrasonic Sensor?
How it Works:
It emits an ultrasound at 40 000 Hz which travels through the air and if there is an object or obstacle on its path It will bounce back to the module. Considering the travel time and the speed of the sound you can calculate the distance.
Pins:
- GND:Ground
- VCC:5V
- TrigPin
- EchoPin
In order to generate the ultrasound you need to set the Trig on a High State for 10 µs. That will send out an 8 cycle sonic burst which will travel at the speed sound and it will be received in the Echo pin. The Echo pin will output the time in microseconds the sound wave traveled.
For example, if the object is 10 cm away from the sensor, and the speed of the sound is 340 m/s or 0.034 cm/µs the sound wave will need to travel about 294 u seconds. But what you will get from the Echo pin will be double that number because the sound wave needs to travel forward and bounce backward. So in order to get the distance in cm we need to multiply the received travel time value from the echo pin by 0.034 and divide it by 2.
Source: Source
Step 4: Schema:
1. L298N to Arduino L298N to Arduino
- D13 to IN1(ORANGE)
- D12 to IN2 (WHITE)
- D11 to IN3 (GREEN)
- D10 to IN4 (PURPLE)
- Vin to 5V (RED)
- GND to GND (BLUE)
* Two Jumper needed for ENA&ENB
2. Motor to L298N Circuit for Motor to L298N (Motor 1&3)Circuit for Motor to L298N (Motor 2&4)
- Motor 1 and 3 connect together to IN1, IN2
- Motor 2 and 4 connect together to IN3, IN4
3. IR sensor to Arduino Circuit for IR sensor to Arduino
- Data to D7 (BLUE)
- Vcc to 5V (RED)
- GND to GND (BLACK)
4. Ultrasonic sensor to Arduino (optional) Circuit for Ultrasonic sensor to Arduino
- Vcc to 5V (RED)
- Trig to D8 (BLUE)
- Echo to D9 (BROWN)
- GND to GND (BLACK)
Step 5: 3D Print Body:
Step 6: Testing Code:
1. Download the library (IR remote - 2.8.0.zip) : download library
- Open Arduino IDE
- Select : Sketch -> Include Library-> Add .ZIP Library-> select IR remote - 2.8.0.zip
2. Using Serial Monitor to check each remote button code: Source Code
#include <IRremote.h>#define IRPIN 7IRrecv irrecv(IRPIN);decode_results result;void setup(){Serial.begin(9600);irrecv.enableIRIn();Serial.println("Enabled IRin");}void loop(){if (irrecv.decode(&result)){Serial.println(result.value);irrecv.resume();}}
Step 7: Code for Remote Control:
#include <IRremote.h>
#define irPin 7 IRrecv irrecv(irPin); decode_results results; void setup() { pinMode(10,OUTPUT); //Sets the motors as an Output pinMode(11,OUTPUT); pinMode(12,OUTPUT); pinMode(13,OUTPUT); Serial.begin(9600); //Starts the serial communication irrecv.enableIRIn(); } void loop() { if (irrecv.decode(&results)) { Serial.println(results.value); switch (results.value) { case 16718055: // button 2 FORDWARD forward(); break; case 16716015: // button 4 LEFT left(); break; case 16726215: // button 5 STOP Stop(); break; case 16734885: // button 6 RIGHT right(); break; case 16730805: // button 8 BACK back(); break; } irrecv.resume(); } Ultrasonic(); } void forward() { digitalWrite(10,HIGH); digitalWrite(11,LOW); digitalWrite(12,LOW); digitalWrite(13,HIGH); } void back() { digitalWrite(10,LOW); digitalWrite(11,HIGH); digitalWrite(12,HIGH); digitalWrite(13, LOW); } void left() { digitalWrite(10,LOW); digitalWrite(11,LOW); digitalWrite(12,LOW); digitalWrite(13, HIGH); } void right() { digitalWrite(10,HIGH); digitalWrite(11,LOW); digitalWrite(12,LOW); digitalWrite(13, LOW); } void Stop() { digitalWrite(10,LOW); digitalWrite(11,LOW); digitalWrite(12,LOW); digitalWrite(13,LOW); }
Step 8: Additional Choice (Ultrasonic Sensor):
Code for Checking distance: Source Code
const int trigPin = 9; const int echoPin = 10; // defines variables long duration; int distance; void setup() { pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output pinMode(echoPin, INPUT); // Sets the echoPin as an Input Serial.begin(9600); // Starts the serial communication } void loop() { // Clears the trigPin digitalWrite(trigPin, LOW); delayMicroseconds(2); // Sets the trigPin on HIGH state for 10 micro seconds digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Reads the echoPin, returns the sound wave travel time in microseconds duration = pulseIn(echoPin, HIGH); // Calculating the distance distance= duration*0.034/2; // Prints the distance on the Serial Monitor Serial.print("Distance: "); Serial.println(distance); }<br>
Code for Ultrasonic Uses:
void Ultrasonic() { if (distance<8){ //when distance between Ultrasonic sensor and stuff<8, then stop the vehicle digitalWrite(10,LOW); digitalWrite(11,LOW); digitalWrite(12,LOW); digitalWrite(13,LOW); delay(3000); } }
Step 9: Overall:
download: remotecar.ino
#include <IRremote.h> //Include Arduino-IRremote library #define irPin 7 //define irpin to Digtal 7 for IR sensor const int trigPin = 8; //SET trigPin to Digital 8 for Ultrasonic sensor const int echoPin = 9; //SET echoPin to Digital 9 for Ultrasonic sensor IRrecv irrecv(irPin); //SET var - irrecv from IRrecv read irPin decode_results results; //SET decode_results from IRrecv to var-results long duration; //'long' type data duration int distance; //'integer' type date duration void setup() { pinMode(10,OUTPUT); //Sets the motors as an Output pinMode(11,OUTPUT); pinMode(12,OUTPUT); pinMode(13,OUTPUT); pinMode(trigPin, OUTPUT); //Sets the trigPin as an Output pinMode(echoPin, INPUT); // Sets the echoPin as an Input Serial.begin(9600); //Starts the serial communication irrecv.enableIRIn(); } void loop() { // Clears the trigPin digitalWrite(trigPin, LOW); delayMicroseconds(2); // Sets the trigPin on HIGH state for 10 micro seconds digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Reads the echoPin, returns the sound wave travel time in microseconds duration = pulseIn(echoPin, HIGH); // Calculating the distance distance= duration*0.034/2; // Prints the distance on the Serial Monitor Serial.print("Distance: "); Serial.println(distance); if (irrecv.decode(&results)) { Serial.println(results.value); switch (results.value) { case 16718055: // button 2 FORDWARD forward(); break; case 16716015: // button 4 LEFT left(); break; case 16726215: // button 5 STOP Stop(); break; case 16734885: // button 6 RIGHT right(); break; case 16730805: // button 8 BACK back(); break; } irrecv.resume(); } Ultrasonic(); } void forward() { digitalWrite(10,HIGH); digitalWrite(11,LOW); digitalWrite(12,LOW); digitalWrite(13,HIGH); } void back() { digitalWrite(10,LOW); digitalWrite(11,HIGH); digitalWrite(12,HIGH); digitalWrite(13, LOW); } void left() { digitalWrite(10,LOW); digitalWrite(11,LOW); digitalWrite(12,LOW); digitalWrite(13, HIGH); } void right() { digitalWrite(10,HIGH); digitalWrite(11,LOW); digitalWrite(12,LOW); digitalWrite(13, LOW); } void Stop() { digitalWrite(10,LOW); digitalWrite(11,LOW); digitalWrite(12,LOW); digitalWrite(13,LOW); } void Ultrasonic() { if (distance<8){ //if distance between Ultrasonic sensor and stuff<8 cm stop the vehicle digitalWrite(10,LOW); digitalWrite(11,LOW); digitalWrite(12,LOW); digitalWrite(13,LOW); delay(3000); } }<br>
Step 10: Faced Problems and Solutions:
- Multiple need for 5V in Arduino
Solution: Add a small breadboard
2. Pair of Motor not going in same direction
*for example: when motor 1 goes clockwise motor 3 goes the opposite
Solution: Reconnect motor's +/- wires, switch around
3. 9V battery is not enough for supply
Solution: Have a 9V Wall Adaptor
4. IR sensor is not sensitive enough
Solution: Add a stand towards remote, could receive the signal straight forward
Step 11: Future Project Extensions and Explorations:
1. When the Ultrasonic sensor detects (distance less than 8 cm), 2 LED will light up.
* if Ultrasonic == True { LED == HIGH }
2. When Ultrasonic sensor detects, the car will back up for few seconds and make a turn.
* if Ultrasonic == True { forward(); left(); }
3. Speed channable
* if distance> 100 { speed = speed+50}
2 Comments
Question 4 months ago on Step 10
I have wired the circuit and loaded the code however when I press the remote buttons, the motors just twitch but don't keep going. Where am I going wrong?
2 years ago
Thanks for sharing