Using IR Sensor to Control TT DC Gear Motors Issue

Hello, I'm doing a car project where I'm using 4 TT DC Gear Motors, a L298N Dual H-Bridge to control their direction and speed, and also an IR Sensor (KY-022) with a remote control to control the direction of the car (forward, backward, left, right or stop). The project works perfectly when the speed of the motors is 255, but if I try to reduce the speed even a little bit, let's say 200, it doesn't work as intended. For example, if I press the forward button on the remote only two motors start moving, and the IR sensor stops working, basically even if I press the stop button the motors don't stop, the LED IR Sensor lights up, but the motors don't follow the instructions. Again this only happens when I reduce the speed because if the speed is 255 everything works fine. This is the code I did:

#include <IRremote.hpp> // IR remote library

#define IR_RECEIVE_PIN 2 // Pin for IR receiver

// Motor control pins
const int in1 = 4; // Motor A control pins
const int in2 = 5; 
const int in3 = 6; // Motor B control pins
const int in4 = 7; 
const int enA = 3; // PWM pin for Motor A
const int enB = 9; // PWM pin for Motor B

// Motor speed
const int motorSpeed = 150; // Slightly increased speed for stability

void setup() {
    Serial.begin(115200); // Establish serial communication
    IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK); // Start the receiver

    // Set motor control pins as outputs
    pinMode(in1, OUTPUT);
    pinMode(in2, OUTPUT);
    pinMode(in3, OUTPUT);
    pinMode(in4, OUTPUT);
    pinMode(enA, OUTPUT);
    pinMode(enB, OUTPUT);

    // Initialize motors to be off
    Stop();
}

void loop() {
    if (IrReceiver.decode()) {
        // Control motors based on IR codes
        switch (IrReceiver.decodedIRData.decodedRawData) {
            case 0xE718FF00: // Press UP Button
                Forward();
                break;
            case 0xAD52FF00: // Press Down Button
                Backward();
                break;
            case 0xA55AFF00: // Press Right Button
                Right();
                break;
            case 0xF708FF00: // Press Left Button
                Left();
                break;
            case 0xE31CFF00: // Stop Button
                Stop();
                break;
        }
        IrReceiver.resume(); // Enable receiving of the next value
    }
}

void Forward() {
    digitalWrite(in1, LOW); // Motor A forward
    digitalWrite(in2, HIGH);
    analogWrite(enA, motorSpeed); // Set speed for Motor A
    digitalWrite(in3, LOW); // Motor B forward
    digitalWrite(in4, HIGH);
    analogWrite(enB, motorSpeed); // Set speed for Motor B
}

void Backward() {
    digitalWrite(in1, HIGH); // Motor A backward
    digitalWrite(in2, LOW);
    analogWrite(enA, motorSpeed); // Set speed for Motor A
    digitalWrite(in3, HIGH); // Motor B backward
    digitalWrite(in4, LOW);
    analogWrite(enB, motorSpeed); // Set speed for Motor B
}

void Right() {
    digitalWrite(in1, LOW); // Motor A stopped
    digitalWrite(in2, LOW);
    analogWrite(enA, 0); // Disable Motor A
    digitalWrite(in3, LOW); // Motor B forward
    digitalWrite(in4, HIGH);
    analogWrite(enB, motorSpeed); // Set speed for Motor B
}

void Left() {
    digitalWrite(in1, LOW); // Motor A forward
    digitalWrite(in2, HIGH);
    analogWrite(enA, motorSpeed); // Set speed for Motor A
    digitalWrite(in3, LOW); // Motor B stopped
    digitalWrite(in4, LOW);
    analogWrite(enB, 0); // Disable Motor B
}

void Stop() {
    digitalWrite(in1, LOW); // Stop all motors
    digitalWrite(in2, LOW);
    digitalWrite(in3, LOW);
    digitalWrite(in4, LOW);
    analogWrite(enA, 0); // Disable Motor A
    analogWrite(enB, 0); // Disable Motor B
}

At first I thought maybe the motors were not receiving enough power so I created a quick test code, where I reduce the speed of the four motors to 127, and surprisingly all motors worked even at 50% less speed. I tested them at 255 it worked, and I tested them at more different speeds and they worked. This is the test code:

const int in1 = 4; // Motor A control pins
const int in2 = 5; 
const int in3 = 6; // Motor B control pins
const int in4 = 7; 
const int enA = 3; // PWM pin for Motor A
const int enB = 9; // PWM pin for Motor B

void setup() {
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(enA, OUTPUT);  
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  pinMode(enB, OUTPUT);

}

void loop() {
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  analogWrite(enA, 127);
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
  analogWrite(enB, 127);
}

So, I'm guessing there is some kind of interference between the IR sensor and the motors when the speed is reduced. Also, it is worth mentioning that I'm using a wall adapter which is connected to a buck converter to step down the voltage from 12V to 6V since the motors support a max of 6V. Then, the power and ground of the output of the buck converter are going to the 12V and GND pins of the L298N, the 5V pin of the L298N also goes to the 5V pin in the arduino, and another GND is going from the L298N to the Arduino. The IR Sensor is connected to the 3.3V pin since its datasheet says it can work with 3.3V and to a GND in the arduino. I also tested it connecting the IR sensor to the 5V in the L298N, but same issue. I don't have a schematic, but I'll try to creaate one in case you need it. It would be good if someone has any idea on how to fix this. I found a video which is very similar to what I'm doing, the only difference is that I'm trying to reduce the speed which is where I run into issues.

Thank you.

Reference video: https://www.youtube.com/watch?v=Cw3SF7lulDo

IRremote occupies (in case of classic Arduino) timer2 and pins 3 and 11 can't be used for for analogWrite. Try some other pin for enA.
Altrnatively you can use:
IrReceiver.stopTimer();
and
IrReceiver.restartTimer();
to alternate motor and IR use in your code.

1 Like

The l298 wastes some voltage. Run the motors at full PWM, 255, and measure the voltage across the motor. Then adjust the buck until there's 6 volt across the motor.
As @kmin says, change pin 3 to another PWM pin.

1 Like

Thank you @kmin , the main issue was the fact that I had a connection in pin 3, but at the same time checking the voltage across the motors was useful as well. Thank you once again now everything works perfectly.

Thank you @Railroader , the main issue was related to pin 3 but after checking the voltage across the motors I also realized the motors were only receiving 4V even at full speed, so I adjusted the buck converter until it reached the corresponding 6V. Thank you.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.