IRReceiver.decode() stops receiving values after DC Motor reaches certain speed

My son and I are trying to build a simple circuit involving an IR receiver and a DC motor. All components came in the Elegoo Super Starter Kit UNO R3 Project. This is our first Arduino project and first post looking for help.

We're able to use the remote control to start and stop the motor, but only if the motor doesn't exceed a certain speed. Once it exceeds that speed, the IR receiver stops receiving and reporting any commands. The red light no the receiver doesn't even flash.

As an example; pressing 1, then 2, then 3 on the remote control will result in the motor starting up at the slowest speed, then go a bit faster, then a bit faster after each button press, but after that the IR receiver stops working. If we start immediately on a fast speed, no other button presses are ever received.

We've seen some posts regarding this issue with suggestions that the issue is with the IR Receiver using Timer 2 and this can conflict with certain pins. We have tried following the guidance in those posts, but it hasn't helped.

When it is in the unresponsive state with the motor running, occasionally the following output is written to the Serial Monitor console with no input from the remote control. This is output by the call to IrReceiver.printIRResultShort, as shown in the code below.

Protocol=UNKNOWN Hash=0x0 1 bits received

Any help troubleshooting this would be much appreciated.

Here's the circuit board:

Here's the code.

#include <IRremote.hpp> 

//The PIN the IR Receiver is plugged into
int IR_RECEIVE_PIN = 7; 

//The PIN the DC Motor is plugged into
int motorPin = 5;

//The default and last speeds of the motor
int speed = -1;
int lastSpeed = 0;

//The last IR value received, so it can be skipped on repeats
long lastIRValue = -1;

void setup() {     
  Serial.begin(9600);     

  //Start the receiver listening via the defined pin
  IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);

  //Associate the DC motor with the defined pin
  pinMode(motorPin, OUTPUT);
}

void loop() {    
  if (IrReceiver.decode()) {
      //Prints the detailed dump of the received values from the IR receiver
      IrReceiver.printIRResultShort(&Serial);

      //Skip this value if it is the same as the last value
      long newIRValue = IrReceiver.decodedIRData.command;
      if (newIRValue != 0 && newIRValue != lastIRValue) {
        lastIRValue = newIRValue;

        //Determine what has been pressed on the remote
        switch(newIRValue) {
           case 0x46: 
            Serial.println("UP");
            break;
           case 0x15: 
            Serial.println("DOWN");
            break;
           case 0x44:
            Serial.println("LEFT");
            break;
           case 0x43:
            Serial.println("RIGHT");
            break;
           case 0x16: 
            Serial.println("Speed 0");
            speed = 0;
            break;
           case 0xC: 
            Serial.println("Speed 1");
            speed = 25;
            break;
           case 0x18: 
            Serial.println("Speed 2");
            speed = 50;
            break;
           case 0x5E: 
            Serial.println("Speed 3");
            speed = 75;
            break;
           case 0x8: 
            Serial.println("Speed 4");
            speed = 100;
            break;
           case 0x1C: 
            Serial.println("Speed 5");
            speed = 125;
            break;
           case 0x5A: 
            Serial.println("Speed 6");
            speed = 150;
            break;
           case 0x42: 
            Serial.println("Speed 7");
            speed = 175;
            break;
           case 0x52: 
            Serial.println("Speed 8");
            speed = 200;
            break;
           case 0x4A: 
            Serial.println("Speed 9");
            speed = 225;
            break;
           case 0x45:
            Serial.println("Starting motor");

            //If no speed has been set via the number pad, default to 100
            if (speed < 0) {
              speed = 100;
            }
            break;
           case 0x47:
            Serial.println("Stopping motor");
            speed = 0;
            break;
           case 0x0:  
            Serial.println("Too Fast, Cmd Repeat"); 
            break;
           default:   
            Serial.println("Ooops, wrong key!");
        }
      }

      //If the speed has changed since the last time it was set and is within the allowed range, set the motor speed
      if (lastSpeed != speed && speed >= 0 && speed <= 255) {
        lastSpeed = speed;
        Serial.print("Setting motor speed to ");
        Serial.println(speed, DEC);
        analogWrite(motorPin, speed);        
      }

      //Prepare to receive the next value
      IrReceiver.resume();
  }
} 

From your photo I can see that you are powering the DC motor from the Arduino power and this is NEVER a good idea. You are lucky if you have not burned something. I'm not entirely sure, but you probably have a problem with enough power and / or noise from the DC motor that confuses the sensor - these long cables are very nice antennas. You didn't say anything about the DC motor - operating voltage and maximum current, for example.

Thanks for the quick response, flashko.

The circuit for the motor is based on this tutorial, which seems to include a similar warning to that raised above regarding powering the motor, or at least damaging the board if not wired up correctly:

Warning − Do not drive the motor directly from Arduino board pins. This may damage the board. Use a driver Circuit or an IC.

Which, to my noob eyes at least, implies that by following the directions in the tutorial we would be avoiding any over powering issues. It would help if we could get some clarification on what exactly we have done wrong here, either because we have followed the tutorial incorrectly or because there is something wrong with the tutorial itself.

Regarding the size of the motor, the "Elegoo Super Starter Kit UNO R3 Project" just describes the DC motor as a "3-6V motor". There are no specs written on the motor itself, at least as far as I can find. I couldn't find any more detail than this anywhere else by googling either.

Looking at the tangled web of antennas, I noted that the yellow wire from pin 5 into the motor circuit was wound up around and through the wires going into the IR Receiver. I unwound this a little and tried to keep it as far from those wires as possible and it did seem to make a difference.

I was able to get it to run the motor up to higher speeds via the remote control and still maintain control to slow it down and stop it again. Once I hit the 7 button, which sets the speed to 175, the IR Receiver stops responding again.

It seems like this is indeed the root cause. I have some better shielded wiring out in the workshop that I can do some experimenting with to see if that will help as well. That will have to wait until tomorrow.

Thanks for your help. If you have any further insights to share regarding

powering the DC motor from the Arduino power

it would be appreciated.

Thanks again.

I think they use a much smaller motor in the tutorial but even so I wouldn't power it that way. If you want to control the direction of rotation you will need an H-bridge but if you want to control only the speed is enough transistor and protection diode as you use. Instead of a BJT transistor, I would prefer a logic level N-Chanel MOSFET, and especially for powerful loads with high current, MOSFET is the best choice. A small ceramic capacitor is placed as explained to reduce interference from the DC motor.

I've even seen them put 3.

I don't have much experience with DC motors and I don't know which method is preferable. As I said, I think it is mandatory to power the DC motor from a separate and sufficiently powerful source - such as an adapter or a powerful battery. If you have a source of 4-6 volts and a current of at least 2-3 amps, you can connect the DC motor directly and measure how much current it consumes. This way you will know what the DC motor needs for normal operation (at idle, of course, when the DC motor is loaded, the current will be much higher). Then turn on the Arduino as you did but power the DC motor from a separate and appropriate power source, remembering that it must have a common ground with the Arduino.
By the way, what is this blue thing with which you glued the DC motor to the desk?

Thanks for the detailed response.

For context, we're making a pretty simple first build. Basically a 'doodle bot', but with a bit of extra complexity using the Arduino to give us some control over it via the infrared remote.

The objective is to learn how to use and combine these different components with a relatively simple build.

The motor just needs to spin a small weight to make the 'bot' wobble and bounce around. We just need to control the speed of the motor. No direction control required.

The original intent was to power the whole thing off a 9V battery. If we need to I guess we could add another battery that is wired directly into the motor's circuit, but I'm not sure how to do that off a single Arduino and still have a common ground with the Arduino. We only have the one.

That said, given the context outlined above, we're trying to keep this as simple as possible. We don't need a particularly robust solution for this. If it works long enough for us to complete the build and learn our lessons along the way, I would still count it as a success.

The blue thing on top of the motor that gets swung around is the same thing we're using to stick the motor to the side of the desk. It's just a wad of blue-tack.

Thanks again.

The reason for all those warnings is that when you run the Arduino from a 9V battery it uses a rather feeble internal regulator to get the 5V output. It's intended only to power LEDs and other low current things, not motors which take relatively high currents. Running a motor like that is likely to kill the regulator.

While you have it powered via USB that regulator is bypassed so you can often get away with slightly higher power devices connected.

If you want to run from batteries your best bet is 3 x AA batteries connected in series and then connected to the 5V pin. They can safely run the Arduino and a small motor.

Steve

Arduino Uno does not seem very suitable for the purpose. If you have an Arduino Nano, it will be more convenient for you to work. With these vibrations you may have problems with the breadboard and the connected cables and I think it would be best to solder all the cables. Maybe a power supply as @slipstick said will be good choise. The 9V battery is definitely not good choice. By the way, in addition to adjusting the motor speed, if you also use changing the direction of rotation, you could get a much more interesting result with this bot.

Thanks, Steve. There seem to be a lot of internal details of the Arduino that we need to take into account when building these kind of things. Learning by experience.

I'll look into how we can swap out the 9 volt battery for three double As.

We've already started work on swapping out the breadboard for a soldered equivalent circuit and have proven to ourselves that it will work. Finishing that off is on our list of things to do tomorrow.

Unfortunately we're limited to just the components we already have on hand. We're doing this as part of an online course. This particular build, which has minimal constraints regarding what we can come up with to create, pretty much just requires us to do so something with an Arduino, and we only have a total of four days from start of the build to submission, which is quite limiting. Particularly when it comes to acquiring new parts and troubleshooting issues in unfamiliar hardware and software.

Switching directions of the motor could be a good option. Something I hadn't considered previously. We originally had planned to include a servo in the mix as well that would adjust the balance, and hopefully the direction, by swinging/moving a separate small weight one direction or another based on which button is pressed on the remote, but we weren't able to get the IR Receiver, motor and servo all working off the same power supply. Presumably due to the power related issues raised in this thread. We can control it independently, but not when they're all connected together.

As mentioned above, we only have one day left before we submit our build, so we just dropped this idea.

As mentioned above, we'll try to swap out the 9V battery for 3 double As tomorrow, but all we really need to achieve success here is control something via code written and deployed to the Arduino using one or more sensors. We'll at least now be able to articulate the issues with the way we have done it based on the feedback received here.

When we move on beyond this short course and start building real projects that need to be more robust, or extending and fixing the ones we have built so far, we'll try to address all of these remaining issues that we won't have time to address properly now.

For what it's worth, the responsiveness and quality of the feedback that I've received on this forum on my first post has been fantastic.

Thanks again for all the help.