To build your Arduino car with IR remote and receiver, using the L298N motor driver to control the motors, here is the wiring guide based on the code you provided.
Components Required:
- Arduino Board (e.g., Arduino Uno)
- L298N Motor Driver
- IR Receiver Module
- DC Motors (2)
- IR Remote Control
- Jumper Wires
- Breadboard (optional)
Wiring Connections:
- IR Receiver Connections:
- VCC of the IR receiver to 5V on the Arduino.
- GND of the IR receiver to GND on the Arduino.
- OUT (Signal Pin) of the IR receiver to Digital Pin 2 on the Arduino (as defined in the code).
- L298N Motor Driver Connections:
- IN1 of L298N to Pin 9 on Arduino (motor 1 control).
- IN2 of L298N to Pin 8 on Arduino (motor 1 control).
- IN3 of L298N to Pin 7 on Arduino (motor 2 control).
- IN4 of L298N to Pin 6 on Arduino (motor 2 control).
- ENA of L298N to 5V (Enable motor driver).
- ENB of L298N to 5V (Enable motor driver).
- OUT1 of L298N to Motor 1 terminal 1.
- OUT2 of L298N to Motor 1 terminal 2.
- OUT3 of L298N to Motor 2 terminal 1.
- OUT4 of L298N to Motor 2 terminal 2.
- VCC of L298N to 12V (motor power supply, depending on motor voltage).
- GND of L298N to GND on the Arduino.
- Decode IR Remote Button:
- You can either visit this url “https://techcraftandhacks.in/find-ir-remote-buttons-code/” to decode remote button or use below code to first decode and then make necessary changes. And then again upload the code. If decoded value is “E718FF00”, then add 0x in beginning. It should look like this “0xE718FF00” after 0x.
#include <IRremote.h> // Include IRremote library
#define DECODE_NEC // Define the protocol (NEC)
const byte IR_RECEIVE_PIN = 2; // IR receiver pin
// Declare motor control pins for L298N Motor Driver
const int motor1Pin1 = 9; // IN1 on L298N
const int motor1Pin2 = 8; // IN2 on L298N
const int motor2Pin1 = 7; // IN3 on L298N
const int motor2Pin2 = 6; // IN4 on L298N
void setup()
{
Serial.begin(9600); // Start serial communication
IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK, USE_DEFAULT_FEEDBACK_LED_PIN); // Initialize IR receiver
// Set motor control pins as OUTPUT
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(motor2Pin1, OUTPUT);
pinMode(motor2Pin2, OUTPUT);
}
void loop()
{
if (IrReceiver.decode()) // Check if IR signal received
{
IrReceiver.resume(); // Prepare for next signal
}
if (IrReceiver.decodedIRData.command != 0) // If valid IR command received
{
checkIRcode(); // Process and print IR code
}
}
void checkIRcode()
{
Serial.print("Raw = ");
Serial.print(IrReceiver.decodedIRData.decodedRawData, HEX); // Print raw data in HEX
Serial.print(" Command = ");
Serial.println(IrReceiver.decodedIRData.command); // Print decoded command
// Check for button 1 (Forward motion)
if (IrReceiver.decodedIRData.decodedRawData == 0xE718FF00) // Compare the raw data (use HEX format)
{
moveForward(); // Move motors forward
}
// Check for button 2 (Backward motion)
if (IrReceiver.decodedIRData.decodedRawData == 0xAD52FF00) // Compare the raw data (use HEX format)
{
moveBackward(); // Move motors backward
}
// Check for button 3 (Stop motors)
if (IrReceiver.decodedIRData.decodedRawData == 0xE31CFF00) // Replace with actual code from your remote
{
stopMotors(); // Stop all motors
}
// Check for button 4 (Move left)
if (IrReceiver.decodedIRData.decodedRawData == 0xF708FF00) // Replace with actual code from your remote
{
moveLeft(); // Turn left
}
// Check for button 5 (Move right)
if (IrReceiver.decodedIRData.decodedRawData == 0xA55AFF00) // Replace with actual code from your remote
{
moveRight(); // Turn right
}
IrReceiver.decodedIRData.command = 0; // Reset command after processing
}
// Function to move motors forward
void moveForward() {
digitalWrite(motor1Pin1, HIGH); // Rotate motor 1 forward
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, HIGH); // Rotate motor 2 forward
digitalWrite(motor2Pin2, LOW);
}
// Function to move motors backward
void moveBackward() {
digitalWrite(motor1Pin1, LOW); // Rotate motor 1 backward
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor2Pin1, LOW); // Rotate motor 2 backward
digitalWrite(motor2Pin2, HIGH);
}
// Function to stop all motors
void stopMotors() {
digitalWrite(motor1Pin1, LOW); // Stop motor 1
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW); // Stop motor 2
digitalWrite(motor2Pin2, LOW);
}
// Function to move motors to the left (turn left)
void moveLeft() {
digitalWrite(motor1Pin1, LOW); // Rotate motor 1 backward
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor2Pin1, HIGH); // Rotate motor 2 forward
digitalWrite(motor2Pin2, LOW);
}
// Function to move motors to the right (turn right)
void moveRight() {
digitalWrite(motor1Pin1, HIGH); // Rotate motor 1 forward
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW); // Rotate motor 2 backward
digitalWrite(motor2Pin2, HIGH);
}
Power Considerations:
- Make sure to use an appropriate power supply for the motors.
Testing:
- Upload your provided code to the Arduino.
- Power up the Arduino and the motors.
- Test the movements by pressing the corresponding buttons on your IR remote. The car should move according to the buttons pressed (Forward, Backward, Left, Right, Stop).