I am using a Leonardo board with IR remote and an LED. I have used remote library and everything seems to be fine. LED is connected to PIN9 and IR receiver is connected to PIN11.
Now the issue is when I connect a motor, it seems to not work at all. I have connected an LED to check if remote is working. When I press the power button, LED turns on and the motor starts running. Serial monitor shows the received hex code. Once the motor starts working, it fails to detect any further codes from the remote and serial monitor shows junk values as received values even though I do not press any button on the remote controller.
Here is the code. I do not have a schematic, but can give a picture of how it is connected.
VCC ===== MOTOR + ==== MOTOR - ===== TRANSISTOR Collector (NPN)
Pin9 ===== 1K resistor === TRANSISTOR Base
Transistor Emitter === Ground
Flyback diode across motor
TSOP1738 ===== Ground to Ground, Vcc to +5V and Output pin to Pin11.
Edit: I managed to draw the schematic
Code is here:
/*
This sketch uses Ken Shirriff's IR library
*/
#include "IRstepI.h"
// Hex values of each button on remote control
#define POWER 0xF61E2A57//power//0x1FE48B7
#define MODE 0x1AD1ED61//menu//0x1FE58A7
#define MUTE 0x1FE7887
#define PLAY 0x1FE807F
#define REWIND 0x81930A09//down//0x1FE40BF
#define FORWARD 0xC20370A1//up//0x1FEC03F
#define EQUIL 0x1FE20DF
#define VOLUMEM 0x983AB4C1//left//0x1FEA05F
#define VOLUMEP 0x21035431//right//0x1FE609F
#define ZER0 0x1FEE01F
#define REPT 0x1FE10EF
#define USD 0x1FE906F
#define ONE 0x1FE50AF
#define TWO 0x1FED827
#define THREE 0x1FEF807
#define FOUR 0x1FE30CF
#define FIVE 0x1FEB04F
#define SIX 0x1FE708F
#define SEVEN 0x1FE00FF
#define EIGHT 0x1FEF00F
#define NINE 0x1FE9867
int receiver = 11;
int powerLed = 13;
int powerStatus = 0;
int motorPin = 9;
int ledOn = 500;
int ledOff = 500;
int remoteValue = 50;
IRrecv irrecv(receiver);
decode_results results;
void setup()
{
pinMode(powerLed, OUTPUT);
pinMode(motorPin, OUTPUT);
// Set up serial communication
// Serial.begin(9600);
// while (!Serial) {
// ; // wait for serial port to connect
// }
irrecv.enableIRIn(); // Start the receiver
}
void loop()
{
if (irrecv.decode(&results))
{
// Serial.print("Hex Value = ");
// Serial.print(results.value, HEX); // print raw values
translateIR();
irrecv.resume(); // wait for the next value
}
controlComponents();
}
void translateIR()
{
// Serial.print(" : Button = ");
switch(results.value)
{
case POWER:
// Serial.print(" POWER ");
powerBoard(); // turn on circuit
break;
case MODE:
// Serial.print(" MODE ");
break;
case MUTE:
// Serial.print(" MUTE ");
break;
case PLAY:
// Serial.print(" PLAY ");
break;
case REWIND:
// Serial.print(" REWIND ");
remoteValue = remoteValue - 10;
break;
case FORWARD:
// Serial.print(" FORWARD ");
remoteValue = remoteValue + 10;
break;
case EQUIL:
// Serial.print(" EQUILISER ");
break;
case VOLUMEM:
// Serial.print(" VOLUME MINUS ");
break;
case VOLUMEP:
// Serial.print(" VOLUME PLUS ");
break;
case ZER0:
// Serial.print(" ZERO ");
break;
case REPT:
// Serial.print(" REPEAT ");
break;
case USD:
// Serial.print(" US/D ");
break;
case ONE:
// Serial.print(" ONE ");
break;
case TWO:
// Serial.print(" TWO ");
break;
case THREE:
// Serial.print(" THREE ");
break;
case FOUR:
// Serial.print(" FOUR ");
break;
case FIVE:
// Serial.print(" FIVE ");
break;
case SIX:
// Serial.print(" SIX ");
break;
case SEVEN:
// Serial.print(" SEVEN ");
break;
case EIGHT:
// Serial.print(" EIGHT ");
break;
case NINE:
// Serial.print(" NINE ");
break;
// default:
// Serial.print(" #NOT MAPPED ");
}
// Serial.println();
delay(250); // avoid multiple presses
}
// This turns on power to the circuit
void powerBoard(){
if (powerStatus==0) {
digitalWrite(powerLed, HIGH); // Turn on the LED
powerStatus = 1;
}
else {
digitalWrite(powerLed, LOW); // Turn off the LED
powerStatus = 0;
}
}
// This is the main logic function. Any additional components
// can be attached here and extended.
void controlComponents(){
if(powerStatus == 1) // checks if power button is on
{
controlMotor();
}
else{
digitalWrite(motorPin, LOW); // sets motor off
}
}
void controlMotor() {
if(powerStatus == 1)
if (remoteValue > 255)
remoteValue = 255;
if (remoteValue < 0)
remoteValue = 0;
analogWrite(motorPin, remoteValue); // analogRead values go from 0
}
Hi, measure your 5V when you run the motor, see if it is regulating enough.
It is usual practice to avoid running motors and relays on the same 5V as the arduino, due to spikes and motor noise.
Place a 0.1uF capacitor across the motor terminals in parallel with the diode.
What is the spec of your motor?
Voltage across motor is around 4.8V and it is rated at 3V to 6V (which should be ok to run at this voltage). Tried using 0.1u ceramic cap across the motor and also across IR receiver, but no luck.
As a last attempt, I will try using a different power supply for motor and check.
Oh.. ok. I missed it in the schematic, but have it in the circuit.
However, I guess this should not make any difference (though it can kill the transistor, but does no harm to the IR receiver or the motor)