Hello! I'm trying to control a DC motor using an IR remote. The program works perfectly when the motor is turned off, however the IR receiver doesn't seem to respond whenever the motor is turned on. I understand this to be related to noise being created by the motor. I know there's a way to reduce noise using capacitors but I'm not sure how to insert them into the circuit, please help!!!
here is my code:
#include <IRremote.h> //Make sure to install the library
int IRpin=3;
IRrecv IR(IRpin);
decode_results cmd;
String myCom;
//MOTOR
int motorPin = 5;
void setup(){
IR.enableIRIn();
pinMode(motorPin, OUTPUT);
}
void loop() {
while (IR.decode(&cmd)==0){
}
delay(200);
IR.resume();
analogWrite(motorPin, 255);
switch (cmd.value){
//up arrow: motor runs
case 0xCC3359A6:
myCom = "up";
digitalWrite(motorPin, HIGH);
break;
//down arrow: motor stops
case 0xCC33D926:
myCom = "down";
digitalWrite(motorPin, LOW);
break;
}
Serial.println(myCom);
}
Here is the circuit diagram:
Note: to control the motor I'm using a transistor and another battery for more power.
Thank you! Can't believe it actually worked. Don't want to take up too much of your time but could you please explain how this works if it isn't too complicated, I'm just starting out and learning the basics so I would really appreciate it Federico.
Capacitors conduct AC but block DC. The brushes in the motor create AC noise that can get onto the power supply. The capacitors bypass higher frequency noise keeping the noise off the power supply.