IR Remote to control motor, won't turn motor off

Hello!

I am looking for help with this code/circuit. in TinkerCad, this code/setup works fine. In real life, I can change the direction of the motor with the remote but cannot get the motor to stop on command.

The IRL wiring is exact to the TinkerCad, save the 9V actually being a 4-AA battery pack, so I know my voltage for powering the motor is in the 4V range. I figured Tinkercad is easier to see my circuit on.

I will also paste my code. Note that IRL the commands are changed to match what my specific IR remote receives with each button.

While I am here, it is worth noting that a while ago, I also had tried controlling two smaller motors and a servo with the IR remote, but the IR receiver would start blinking rapidly and lose all functionality once the motors were turned on. I fear this is still happening with this newer motor, although I can still control direction, just not powering off. Eventually I need to power 4 motors and have remote control over them.

I know it is best to power the motors with external power, which I am doing. Otherwise, I have heard of MOSFETs and external control chips. Right now all I have is an L293D H-bridge Motor Driver. I know DC motors can induce noise that messes with IR receivers.

Any tips or tricks to get clean IR commands while using DC motors?

Thanks,
Blake

//Implement libraries and define pins
#include <IRremote.h>
#include <IRremote.hpp> 
#define RECEIVE 6
#define Mod 3
#define M2 2
#define M4 4

//int that stores the last command pressed on the IR remote
int command = 0; 

void setup(){
 	 
  //Begin scanning for remote inputs and start Serial Monitor
  	Serial.begin(115200);
	Serial.println("Enabling IR");
	IrReceiver.begin(RECEIVE, ENABLE_LED_FEEDBACK);
  Serial.print("Ready to receieve.");
  
  //Set pins for motor
  pinMode(Mod, OUTPUT);
  pinMode(M2, OUTPUT);
  pinMode(M4, OUTPUT);
  
}

void loop(){
  
   if (IrReceiver.decode()) {
        //* Print a short summary of received data
     
        IrReceiver.printIRResultShort(&Serial);
        IrReceiver.printIRSendUsage(&Serial);
        if (IrReceiver.decodedIRData.protocol == UNKNOWN) {
            Serial.println(F("Received noise or an unknown (or not yet enabled) protocol"));
            // We have an unknown protocol here, print more info
            IrReceiver.printIRResultRawFormatted(&Serial, true);
        }
		
     	//Storing last command in "command" variable
        command = IrReceiver.decodedIRData.command;
     	//Print the current command for debug and assignment
        Serial.println();

        //Start receiving next value after last
        IrReceiver.resume(); // Enable receiving of the next value

      
   }
  
  //Simple if statements. 6th button makes motor go forward
  if(command == 6){
    digitalWrite(M2, HIGH);
    digitalWrite(M4, LOW);
    analogWrite(Mod, 255);
  }
  //4th button makes motor reverse
   else if(command == 4){
    digitalWrite(M2, LOW);
    digitalWrite(M4, HIGH);
    analogWrite(Mod, 255);
  }
  //power button makes motor stop.
  else if(command == 0){
    digitalWrite(M2, LOW);
    digitalWrite(M4, LOW);
    analogWrite(Mod, 0);
  }
  
}

What do you see from this in your serial monitor? oops...
Would you add this to your Serial.println() and show the results when you press 0:

Serial.println(command);

Why 4V? A 4-AA battery pack gives 6V total, usually enough for small DC motors. Do you have a link to the exact DC motor you're using? I suspect that when you run it, the power becomes low for Arduino and loses IR signals. Try the same but with a best power 9V source (like an AC adaptor) and see if everything works.

You have read this? GitHub - Arduino-IRremote/Arduino-IRremote: Infrared remote library for Arduino: send and receive infrared signals with multiple protocols

Not all AA batteries supply 1.5V.

Zinc-Carbon and Alkaline do.

NiCd and NiMH supply 1.2V

Uh, yes, I know rechargeable batteries have lower voltage output. But it hasn't ever mentioned before :wink:

@docdoc, These batteries are fairly well used so I was just implying 4V, but you are right they are likely still putting out 6V!

I can try that, but the specifics of the problem are what's weird. I don't believe I lose signal because the motors will still change direction on command, just not turn off. I don't have super specifics, college provides cheapo parts, but it should be the 3-6v hobby motor. Dual shaft.

@ArminJo, I have not read that specific section, I already see the potential conflicts with analogWrite and the PWM pins! I'll give it a read.

@xfpd I did, and I can't believe I didn't do that first lol. It is certainly weird that the assigned command can become different from the command shown on the standard printout of the received IR data.

Do not fret everyone I'm sure I'll comment again with another issue! Lol!

Go figure already back! I think I am going to read up on the IR libraries issue with analogWrite and PWM pins, but right now I'm using both sides of the H-bridge and the IR Receiver starts not receiving after a few seconds, comes back randomly. You can imagine my code is very similar, just extra commands for the second motor to turn the opposite way.

Any ideas while I read up on this?

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