analogWrite() working in some cases, not working in other ones

Hello, I’m trying to lit on a LED when the IR receiver receives the 1 key. All is working fine when I turn it on/off with

digitalWrite(ledpin, HIGH)

But the LED is designed for 4’5V, and I don’t want to mess with resistors. I tried to change the digitalWrite for:

analogWrite(ledpin, 200)

so that it does not burn, but it does not turn on :drooling_face:

The complete sketch is

#include <IRremote.h>

const int RECV_PIN = 6;

IRrecv irrecv(RECV_PIN);

decode_results results;

int ledpin = 9;

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

void loop() {
  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    irrecv.resume(); // Receive the next value
  }
  switch(results.value) 
  {
    case 0xEE1128D7: analogWrite(ledpin, 200); break; //Led turns on when 1 is pressed
    case 0xEE11A857: analogWrite(ledpin, 0); break; // Lod turns off when 2 is pressed
  }
}

Also, it works perfeclty when I simply write a code like:

int led = 9;        


void setup()  { 

  pinMode(led, OUTPUT);
} 


void loop()  { 
  // set the brightness of pin 9:
  analogWrite(led, 200);    
                           
}

Thank you.

akpla: But the LED is designed for 4'5V, and I don't want to mess with resistors. I tried to change the digitalWrite for:

You don't want to mess with resistors huh? Do you mind messing with your output pins? As in, causing them to fail?

Wow, thanks for the advise, how can it break the pins? I thought the worst that could happen was that the led burnt. :blush: Doesn't PWM solve the problem?

No it doesn't. If you exceed the output limitation of your output driver (on the chip) it doesn't matter if you are doing it for a short time.

http://www.thebox.myzen.co.uk/Tutorial/LEDs.html

a pin has a max output current, an current greater than this max will damage the processor!. You will not be able to use that pin again. If your using the UNO that's only the chip, any of the other cards and you will need to replace the whole thing. The resistor acts to limit the current drawn from the pin - keeping the processor safe!

If you need to ask more on this subject then go a lookup ohms law, current, voltage, pd, watts and the other basics of electricity!

Until you do understand the above THEN FOLLOW THE INSTRUCTIONS - DO NOT GO YOUR OWN WAY - THIS IS A FIELD IN WHICH THINGS ARE DONE FOR GOOD REASON - IT'S ABOUT OPINIONS OR CONSENSUS OR AND OF THE 'OLOGY TYPE STUPIDITY.

Mark

Another possible problem with your code is that when you receive an IR signal, you are then continuously doing the analogue write on pin 9 in every loop, which may not let the PWM kick in (not 100% sure about this).

You should also update your code to ensure the analogue write is only issued once for each key press received.

But where is it exceeding the max current of the arduino? The led is less powerful than the arduino, so the only possible thing to burn is the led, what am I missing there?

The o/p of an Arduino UNO digital pin has a maximum rating of 40mA. If you were to put a load on that o/p and tried to take more than 40mA you will damage the chip. That is why it is important to limit the current o/p with a resistor or driver circuitry.

But connecting a 4.5V@20mA(?) led I’m not overloading the digital pin, I suppose.

akpla: But connecting a 4.5V@20mA(?) led I'm not overloading the digital pin, I suppose.

Connecting a what?

LEDs drop a forward voltage and turn into a short circuit. So if it's forward voltage is 4.5V (no idea what color that is) and its maximum current is 20mA, it is your responsibility to make sure the current is limited to 20mA.

akpla: But connecting a 4.5V@20mA(?) led I'm not overloading the digital pin, I suppose.

It wants 20 mA, it might be getting 100 mA, damaging itself and the processor.

How will it get 100mA if the max. output value is 40mA? I still don't get it, excuse me :blush:

akpla: How will it get 100mA if the max. output value is 40mA? I still don't get it, excuse me :blush:

The maximum output is the maximum it can handle, not the maximum it can "give". It "gives" as much as whatever is connected to it "asks" for. If what is connected to it "asks" for more than 40mA current, it will "give" it up until the point where it burns its pins.

Disregarding the current limiting resistor that OP seems to lack. Shouldn't analogWrite(ledpin, 200) put out around 4V?

cypherrage: Disregarding the current limiting resistor that OP seems to lack. Shouldn't analogWrite(ledpin, 200) put out around 4V?

analogWrite puts a pulse train out on a pin not a voltage.

LarryD:

cypherrage: Disregarding the current limiting resistor that OP seems to lack. Shouldn't analogWrite(ledpin, 200) put out around 4V?

analogWrite puts a pulse train out on a pin not a voltage.

Right, a pulse, a square wave, that goes from 0V?5V?0V?5V and depending on the duty cycle, the analog voltage is set (50% duty cycle = 2.5V), is this not correct?

If you've got a low-pass filter on the output, that's roughly true. Have you got a low-pass filter?

akpla:
How will it get 100mA if the max. output value is 40mA? I still don’t get it, excuse me :blush:

Think of your car. Its maximum (safe) RPM might be 4000. But if you put it in neutral and jam the accelerator to the floor it might get up to 10000 RPM before it burns itself out and explodes, with bits of metal flying everywhere.

It’s the same with output pins. You are not supposed to take more than 40 mA absolute maximum. It will give you more than that while it heats up and releases the blue smoke.

How will it get 100mA if the max. output value is 40mA?

As I said look up ohm's law. As for the rest you are miss reading the spec, 40mA is the maximum safe output, your settup will pull more current than this and thus cause damage.

Try it this way - I'am a hardware designer of 30 years - I don't say I don't need that resistor (or what ever) when i look at a circuit, I ask why did the circuit designer put it in. And your a complete nubie in the subject so just do what your told until you learn enough to ask questions. If you ever get to the stage that you don't need to ask questions then ....

Mark

Well, I finally understood it, thank you very much!

I apologize for my ignorance, but holmes4, I would never understand the problem if I simply searched in google or read books, this thread was very helpful for me.