In this project, you will learn how to build an Arduino-controlled car using an IR remote and receiver. This step-by-step guide covers everything from wiring the components, such as the L298N motor driver and DC motors, to writing the Arduino code that allows you to control the car’s movements.
Components Required:
- Arduino
- L298N Motor Driver
- IR Receiver Module
- IR Remote Control
- DC Motors (2)
- Wheels (2)
- Chassis (Car Body)
- Battery pack (e.g., 9V or 12V, depending on your motor specifications)
- Jumper wires
- Breadboard (optional)
Step 1: Wiring the L298N Motor Driver to Arduino
- Motor Driver Wiring:
- IN1 → Pin 3 of Arduino Nano (for Motor 1 direction control)
- IN2 → Pin 4 of Arduino Nano (for Motor 1 direction control)
- IN3 → Pin 5 of Arduino Nano (for Motor 2 direction control)
- IN4 → Pin 6 of Arduino Nano (for Motor 2 direction control)
- ENA → 5V (Enable pin for Motor 1)
- ENB → 5V (Enable pin for Motor 2)
- VCC → Battery supply (e.g., 9V or 12V depending on your motor)
- GND → GND (common ground with Arduino)
- Motor connections:
- Connect OUT1 and OUT2 to Motor 1 terminals.
- Connect OUT3 and OUT4 to Motor 2 terminals.
Step 2: Wiring the IR Receiver Module
- IR Receiver connections:
- VCC → 5V (on Arduino)
- GND → GND (on Arduino)
- OUT → Pin 2 of Arduino (for receiving IR signals)
Step 3: Arduino Code Setup
You will need to use the IRremote library to decode the signals from the remote control. Install the IRremote library via the Arduino Library Manager.
- Install the IRremote Library:
- Open Arduino IDE
- Go to Sketch > Include Library > Manage Libraries.
- In the Library Manager, search for IRremote and click Install.
- Write the Code: Here is a simple code to control the car’s movement using the IR remote. But before uploading below code get code of your IR Remote Button the link is: https://techcraftandhacks.in/find-ir-remote-buttons-code/
#include <IRremote.h>
// Pin definitions
const int recv_pin = 2; // IR Receiver Pin
IRrecv irrecv(recv_pin);
decode_results results;
// Motor control pins
const int motor1Pin1 = 3;
const int motor1Pin2 = 4;
const int motor2Pin1 = 5;
const int motor2Pin2 = 6;
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
// Motor pins setup
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(motor2Pin1, OUTPUT);
pinMode(motor2Pin2, OUTPUT);
}
void loop() {
if (irrecv.decode(&results)) {
long int decCode = results.value;
Serial.println(decCode); // Print the decoded IR signal
// Stop all motors
stopMotors();
// Forward
if (decCode == 0xFF30CF) { // Replace with the button code for forward
moveForward();
}
// Backward
if (decCode == 0xFF18E7) { // Replace with the button code for backward
moveBackward();
}
// Left
if (decCode == 0xFF7A85) { // Replace with the button code for left
turnLeft();
}
// Right
if (decCode == 0xFF10EF) { // Replace with the button code for right
turnRight();
}
// Stop
if (decCode == 0xFF38C7) { // Replace with the button code for stop
stopMotors();
}
irrecv.resume(); // Receive the next value
}
}
void moveForward() {
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
}
void moveBackward() {
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, HIGH);
}
void turnLeft() {
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
}
void turnRight() {
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, HIGH);
}
void stopMotors() {
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
}
Step 4: Uploading the Code
- Upload the code to your Arduino.
- Open the Serial Monitor to observe the IR codes sent by your remote when pressing different buttons.
Step 5: Test and Adjust
- Press buttons on your IR remote and check if the motors are responding as expected.
- The decCode in the
if
conditions will be the unique value associated with each button on your remote. You can use the Serial Monitor to identify the button codes. - Modify the IR codes in the code according to the ones your remote sends when each button is pressed.
Step 6: Powering the Car
- Connect the battery to the VCC and GND of the L298N motor driver. Make sure the battery is appropriate for your motors.
Troubleshooting:
- Motors not turning: Check the wiring, motor connections, and that the motor driver is receiving enough power.
- IR remote not working: Ensure the IR receiver is properly connected and the correct button codes are used.
- Arduino not responding to the remote: Ensure that the IR receiver is properly wired, and the library is correctly included.