Trouble changing motor speed

I’ve been trying to get a simple DC motor controlled by a remote control. (I used an old fan remote I had lying around, and found the IR codes pretty easily.) I can get the motor to reverse directions and to stop, but when I change speed, the motor just stops. Also, at that point the IR code from the remote seems to give the wrong code # in the Serial monitor. After that, there’s no response from the remote control until I re-upload the code to the Arduino.

Below is the code I’m using (on an Arduino UNO), with the motor connected through an H-bridge. I’ve attached a wiring diagram. I also attached a screenshot of the Serial Monitor showing how the IR codes work fine to change direction and to stop the motor, until I change the speed.

Thanks to anyone willing to help me troubleshoot this project.

#include <IRremote.h>
#define forwardKey 55590 // "Mode" key on GE fan remote
#define backKey 63750  // "Timer" key on GE fan remote
#define offKey 33150  // "Power" key on GE fan remote
#define speedUpKey 41310 // "Temp+" key on GE fan remote
#define speedDownKey 20910 // "Temp-" key on GE fan remote

int receiver_pin = 3;

int enablePin = 11;
int in1Pin = 10;
int in2Pin = 9;
int onSpeed = 255;
int motorEnable = 0;

IRrecv receiver(receiver_pin);
decode_results output;

void setup(){
 pinMode(in1Pin, OUTPUT);
 pinMode(in2Pin, OUTPUT);
 pinMode(enablePin, OUTPUT); 

void loop(){
   if (motorEnable == 1){
   analogWrite(enablePin, onSpeed); 
   else {
   analogWrite(enablePin, 0);       
   if (receiver.decode(&output)) {
   unsigned int value = output.value;
   switch (value){
     case forwardKey:
       digitalWrite(in1Pin, HIGH);
       digitalWrite(in2Pin, LOW);
       motorEnable = 1;
     case backKey:
       digitalWrite(in1Pin, LOW);
       digitalWrite(in2Pin, HIGH);
       motorEnable = 1;
     case offKey:
       motorEnable = 0;
     case speedUpKey:
       onSpeed = onSpeed + 5;
         if (onSpeed >=255){
           onSpeed = 255;
     case speedDownKey:
       onSpeed = onSpeed -5;
         if (onSpeed <= 0){
           onSpeed = 0;
   Serial.print("  ");
   delay (100);

void modeChange(){

To make it easy for people to help you please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

Your code is too long for me to study quickly without copying to a text editor.

Also use the AutoFormat tool to indent your code for easier reading.


What I notice is that the receive (pin3) and the pin used for output pwm (pin11) are both on Timer2.

I'm not familiar with how the timer is used in the IRreceive and what interactions there could be, but I would try move the pwm output pin to Timer 1 on D9 or D10.

Thank you - I think that did the trick. I switched pins 10 and 11 so that the output was now on D10 instead of D11.

I’m curious why this might have been a problem. I assume there’s something about the timing circuits in the Arduino that I don’t understand.

I'm curious why this might have been a problem. I assume there's something about the timing circuits in the Arduino that I don't understand.

Both IRreceive and analogWrite() are both trying to use hardware Timer2 in incompatible ways. You only saw this when using a pwm value less than 255, because analogWrite(11, 255) defaults to digitalWrite() and does not actually use the Timer2.

Timer conflicts between two different Arduino libraries are fairly common. In your case there was a timer conflict between analogWrite() and the lR library. A detailed reading of the source code for both will show the exact problem.

The IR library can be reconfigured to use a different timer, or as you have done, you can move the pwm output to a different timer.