IRemote + pwm led = random data!

Hi,
This is my first post here!

I'm using a ir receiver salvaged from a old DVD player. It works fine dumping the buttons but as soon as i add code to control the led brightness using pwm it starts getting random codes!

This works perfect:

#include <IRremote.h>
int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;

void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
  irrecv.blink13(true);
}

void loop() {
  if (irrecv.decode(&results)) {
    Serial.println(results.value); //Dumping all codes!
    irrecv.resume(); // Receive the next value
  }
}

Sample output:

4294967295
551502015
4294967295
551534655
551502015
551502015
4294967295
4294967295
4294967295
551534655
551534655

If i add code to control some led's it gets crazy!
As soon as i click the first first time in a button it just dumps random values!

#define VOL_UP 551502015
#define VOL_DOWN 551534655
#define REPEAT 4294967295

#include <IRremote.h>

int RECV_PIN = 11;
unsigned long last;
IRrecv irrecv(RECV_PIN);

int LED_PIN = 9;
int LED_VAL = 1;
decode_results results;

void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
  irrecv.blink13(true);
  pinMode(LED_PIN, OUTPUT);
}

void loop() {
  if (irrecv.decode(&results)) {  
    if (results.value ==  VOL_UP){
      Serial.println("Vol +");
      LED_VAL ++;
    }
    else if(results.value == VOL_DOWN){
       Serial.println("Vol -");
       LED_VAL --;
    }
    //Dumping all codes!
    Serial.println(results.value);
 
    analogWrite(LED_PIN,LED_VAL);
    irrecv.resume(); // Receive the next value
  }
}

Result:
First button press always work after that i only get random data!

Vol +
(not pressing any button! all this numbers are random!)
551502015
3388409905
1555091363
3245444095
164803964
1074333796
4156594252
3070687522
467297586
2603894849
1947468179
1168822731

The circuit its quite simple i think... (image in attachments)
The random numbers stop as soon as i disconnect the wire that controls the led! Or disconnect the transistor!
Also if i only do a analogwrite of 0 or 255 so, or on or off it works :astonished:
What i'm doing wrong???
(The irReceiver is the device in the middle the pins are DATA,GND,VCC i read that from the original pcb)
Thanks for the help!

Welcome..

You may be getting inteference from the red LEDs via the IR Receiver, particularly if driving them with PWM. Try shielding the IR receiver from the LEDs or testing with just on/off for the LEDs initially.

Also, not usually a good idea to connect your LEDs in parallel thru 1 resistor, better to have resistor per LED.

PS: Also, enable the pullup on pin 11 (IR receiver)

I used red in the schematic but i'm using white LED's
With on/off it works ok (0,255)
I connected them in parallel because i don't have many resistors at hand..

To enable the pull up i use INPUT_PULLUP?

danielb7390:
I used red in the schematic but i'm using white LED's

White is also made up of RED so same applies :slight_smile:

danielb7390:
To enable the pull up i use INPUT_PULLUP?

Yes, for pin 11.

Did you shield(cover) the IR receiver?

To enable the PULLUP i had to edit it in IRremote.cpp. Still same random data.
How would i shield the ir receiver?

Mine is exactly like this one:

danielb7390:
To enable the PULLUP i had to edit it in IRremote.cpp. Still same random data.
How would i shield the ir receiver?

pinMode(11, INPUT_PULLUP) ; in setup() ....alternatively you can put a 4.7k resistor between 5V and pin 11.

danielb7390:
How would i shield the ir receiver?

Start by covering the IR Receiver with your hand or somthing (opaque) that does not allow light to pass thru. Also make sure the LEDs are pointing away from the IR receiver.

So if the problem persists you can just add some code to ignore the random/invalid signals and proceed with your project.

I believe the led's aren't the problem... even if i disconnect all of them it still gives random data...
It only stops giving random data if i remove the wire from pin 6 or remove the transistor...

Also placed one hand around the led and the other around the receiver still random data..

pinMode(11, INPUT_PULLUP) still gives the same.

i already tried to ignore the random stuff problem is then it wont get the correct codes from the remote... is messes up all the codes...

EDIT:Interesting.. just tried with another ir receiver this one only gives random data if i connect the led if i disconnect it works... the other i have to disconnect the wire from pin6 to work correctly..

You are probably getting inteference via the power to the IR receiver.

Look at the data sheet, there is usually a decoupling capictor & resistor which can be added to the IR receiver, to help with this.

....like the example provided here http://www.vishay.com/docs/82459/tsop48.pdf

You should be able to copy this example.

Indeed... just got the pcb here and it has a electrolytic and a ceramic capacitor between vcc and gnd..
Will try and report back!

EDIT: Interesting... the original ir receiver i got from the pcb where i got the 2 capacitors now still doesnt work but the other one i had works perfect now with the 2 capacitors!!!

Also can you tell me why you said its bad to have the led's in parallel?

danielb7390:
Also can you tell me why you said its bad to have the led's in parallel?

In theory, all or most of the current could just flow thru one or at best the brightness would be uneven betweeen the LEDs. Best practice is to have a resistor in series for each one.

You could probably get away with having 2 (maybe 3) in series with just one resistor, depending on the Vf (voltage drop) of each LED.

You will find better explanations via google...

Thanks for the explanation and help!
I'm just using in parallel because i already said i don't have many resistors... i'm only starting in the arduino and electronics world with some lent stuff from my friend...