L298P Motor Shield IR Control DC Motor

Hey I am using L298P Motor shield to control 2 DC motors via Arduino Mega 2560 with IR.
I managed already a lot of things and I have still one error. I test both motors before defining the IR receiver. They run in both directions like expected. But if I want to move both in both directions, button 1 and 3 in my example, one of them only runs in one direction, while the other one changes the direction like expected.

I already changes the timer since motor and IR had both timer 2 set. How can I solve this?

#include <IRremote.h> 
#define receiverIR 5                //Eingang IR Empfänger
//definiere Motor Outputs
int MOTOR1_DIRECTION_PIN = 13;
int MOTOR2_DIRECTION_PIN = 12;
int MOTOR1_SPEED_PIN = 11;
int MOTOR2_SPEED_PIN = 10;

int motorValue = 0;

void setup() {
  //definiere pins als output
  pinMode(MOTOR1_DIRECTION_PIN, OUTPUT);
  pinMode(MOTOR2_DIRECTION_PIN, OUTPUT);
  pinMode(MOTOR1_SPEED_PIN, OUTPUT);
  pinMode(MOTOR2_SPEED_PIN, OUTPUT);
  
  Serial.begin(115200);
  IrReceiver.begin(receiverIR, ENABLE_LED_FEEDBACK);
 }

void loop(){

  //decodes the infrared input
  if (IrReceiver.decode()){
    int command = IrReceiver.decodedIRData.command;
    Serial.println(command);
    //switch case to use the selected remote control button
    switch (command){
      case 0x16: //when you press the 1 button
      Serial.println("Button 1 gedrückt");
        motorValue = 100;
        digitalWrite(MOTOR1_DIRECTION_PIN,HIGH);
        Serial.println("Richtung 1 gesetzt");
        analogWrite(MOTOR1_SPEED_PIN,motorValue);
        Serial.println("1 GO");
        digitalWrite(MOTOR2_DIRECTION_PIN,LOW);
        Serial.println("Richtung 2 gesetzt");
        analogWrite(MOTOR2_SPEED_PIN,motorValue);
        Serial.println("2 GO");
        break;   
      case 0x19: //when you press the 2 button
        motorValue = 0;
        analogWrite(MOTOR1_SPEED_PIN,motorValue);
        analogWrite(MOTOR2_SPEED_PIN,motorValue);
        break; 
      case 0xD: //when you press the 3 button
        motorValue = 100;
        digitalWrite(MOTOR1_DIRECTION_PIN,LOW);
        digitalWrite(MOTOR2_DIRECTION_PIN,HIGH);
        analogWrite(MOTOR1_SPEED_PIN,motorValue);
        analogWrite(MOTOR2_SPEED_PIN,motorValue);
        break; 
    }
    Serial.println();
    IrReceiver.resume(); 
  }
  delay(10);
}

Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advice on) your project :wink: See About the Installation & Troubleshooting category.

IRRemote usually uses Timer1 so you can't use Pin 9 or Pin 10 for PWM. If you switch it to using Timer 2, you won't be able to use Pin 3 or Pin 11 for PWM.

1 Like

thank you for your reply. I know, i checked this in the IRremote library and changed the timer to timer3. Before that change only one motor worked in IR mode. Now both motors run in IR mode, but MotorB only in one direction and ignoring my High or Low signal for direction. Second motor on MotorA is fine. Ideally I want to control both motors independently.

Still using the same code as in the opening post? If not, please post your updated code in a new reply.

I reduced the code to the main issue, but the functionality did not change. I just made a change in the IRremote library to change the used timer.

If you swap the drivers does the problem move with the driver? That is a way to figure out if the problem is a bad driver or bad signals from the Arduino.

what do you mean with swap drivers?

I swaped motors, but it stayeds at MotorB( from L298p)

but I saw another thing today, the led indication for each Motor (green and yellow led) normally swaps depending on the direction. But for motor B always the green is on, which leeds me to a wrong signal from the board. But without IR it works fine.

The motor shield has
Motor-A Direction=12, Speed = 10 (Your MOTOR2)
Motor-B Direction=13, Speed = 11 (Your MOTOR1)

The LED Feedback feature of the IRRemote library uses Pin 13 (LED_BUILTIN). You should turn off that feature to use Pin 13 for Motor-B Direction. Change ENABLE_LED_FEEDBACK to DISABLE_LED_FEEDBACK.

1 Like

OMG you are wright. Now it is working. Thank you very much. Made my day. Love you guy. Thanks for your help. I was already trying another build with L293D, but now I can skip this. Thanks :slight_smile:

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