Newbie PWM led question, I thought 0 was 0% duty cycle but I experience other

So I am just learning a bit and I've read that 0 is 0% duty cycle an 255 is 100% however when I input a pwm variable into a digitalwrite of an led 0 is full bright and 255 is off. I cannot figure out what I'm missing but I'm sure you guys can tell me the err of my ways.

Thanks much,

Dave

How is the LED wired?

TT_Vert:
however when I input a pwm variable into a digitalwrite

If you want PWM you should do analgWrite() :wink:

But indeed, it depends om how you connect it. The Arduino can sink current as well, even with the LOW of a PWM signal. So when you connect a led between Vcc and a pi, the value 0 results in 0% duty aka LOW all the time so the led will turn on all the time.

Thanks guys. I did actually use analogWrite, my mistake. That's actually another topic I need to learn more about.
I've seen in many led fading and motor control instances that analogWrite is much smoother than digitalWrite so i need to find the best application for each.
I have it wired from a pwm pin to a 220Ohm resistor to the anode of led. Cathode to ground .

Even when I write 254 to the led for example the led is off, it's like the pwm is reversed from what is expect

Dave

TT_Vert:
so i need to find the best application for each.

Simple, digitalWrite() is just HOGH LOW aka on/off. analogWrite() is PWM aka dimming/speed control.

With anode to a pin and cathode to ground 255 should be max and 0 should be off indeed. Which Arduino do you use and more important, which pin on that Arduino? Not all pins support analogWrite() but all pins on a Uno support digitalWrite().

Post code, post observations, post schematic.

I tried both pin 3/4 on the uno R3 thinking maybe it was a pin issue but with the same result.

Dave

See reply #5

That's weird. Then indeed, post code and schematic (a hand drawing will do, please NO Fritzing breadboard crap). Because pin 3 and 4 should act different. pin 4 is NOT a PWM pin so will not be able to dim it at all. (It's just LOW if you analogWrite() it with a value of <150 and HIGH when >=150.) Pin 3 is a PWM pin so does work with analogWrite()

Are you sure you're not just waggling the pull up resistor?

Here is a basic sketch just to show you it's not the coding. This was just a test I made after i realized the for statements I was using were not making the LED act as I felt it should. With this script it is off. Set it to 0-10 it is full bright, probably even higher. 127 its half dim. I'm out of town but I'll try to draw it w/ characters. I'm sorry I'm not sure what waggling the pull up resistor means. I apparently can't upload photos here and my drawing doesn't seem to format properly when posted, here is a quick paint drawing of the basic circuit.

Link to circuit schematic (Don't laugh please, best I can do currently)

------------------------------[/tt]

int LED=4;   //Define PWM LED as pin 4


void setup() {
Serial.begin(9600);  //start serial port
//pinMode(LED, OUTPUT);  //pin 4 is an output

}

void loop() {

    analogWrite(LED, 209); 
  

}

Moderator edit: Code tags, and an attempt to make the diagram legible (?)

Pin 4 is not a pwm pin on most Arduinos, so which Arduino are you using?

Uno R3. I've tried pin 3,4, 7,8,9,10,11,12 to see if the problem followed any particular pin and it does not. The sketch above i just drew up really quickly as I'm out of town and didn't recall the PWM PINS. I did change the defined the pin and did an analogWrite when changing pins to test.

Dave

What you are telling us you are doing is right, what you tell us you observing is wrong. There is a disconnect here. I suspect you are not doing what you think you are doing, but without a proper schematic and photograph who can tell.

I agree which is why i asked. I'm100% that is how it's wired and also 100% on the basic sketch but I'll check when I get back next week. Are there ahh things that prior have done to have this happen so perhaps I can check those things? It's basically one line of code and a pwm pin feeding the hot side via a resistor though, even a newbie like me should be able to manage that.

Thanks for the input guys, great forum!!
Dave

I'm100% that is how it's wired and also 100% on the basic sketch

Yes and oddly enough when ever I make an error, which is perhaps more often than you might think after 50 years in this game, I am always 100% sure it was right the first time. The more puzzling the results of a mistake the more sill it was I have found.

Yours sounds like you have the Arduino pin sinking current not sourcing it, but without a photo I can't be definitive.

#define LED 3    //Define LED as pin 3 (UNO only supports PWM on pins 3,5,6,9,10,11)

void setup() {
  Serial.begin(9600);  //start serial port
  analogWrite(LED, 209);  // only call function when changing PWM duty cycle.
  Serial.println("LED PWM set to value 209");
}

void loop() {
  delay(500);  // 1/2 second delay
  analogWrite(LED, 109);  // Dim LED for another 1/2 second
  Serial.println("LED PWM set to value 109");
  delay(500);
  analogWrite(LED, 209);  // Return LED to normal brightness for another 1/2 second
  Serial.println("LED PWM set to value 209");
}

OP provided a link to a simplistic schematic in reply #10; just a bit difficult to see in between the other text :wink:

TT_Vert:
Uno R3. I've tried pin 3,4, 7,8,9,10,11,12 to see if the problem followed any particular pin and it does not. The sketch above i just drew up really quickly as I'm out of town and didn't recall the PWM PINS. I did change the defined the pin and did an analogWrite when changing pins to test.

Dave

Randomly picking pins does not help :wink: There are only 6 PWM pins on the Uno (3, 5, 6, 9, 10, 11); see analogWrite reference

But as Grumpy_Mike said, check, double check, triple check your circuit.

You can use below code to test the PWM functionality; it's not accurate but is should give an indication. Remove all stuff that you have connected and simply place a wire between pin 3 (PWM or any other PWM pin) and pin A0 (to read back the PWM pin; can basically be any pin but I only had an analog input pin available in my setup).

byte buffer[1300];

void setup()
{
  Serial.begin(115200);
  analogWrite(3, 20);
}

void loop()
{
  static int index = 0;
  buffer[index++] = digitalRead(A0);
  if (index == sizeof(buffer))
  {
    for (int cnt = 0; cnt < sizeof(buffer); cnt++)
    {
      Serial.print(buffer[cnt]);
    }
    for (;;);
  }
}

On the serial monitor, you should see a repeating sequence with lots of zeroes and a couple of ones.
Next change the value in analogWrite to e.g. 100 and to e.g. 220 and observe.

The code will do 1300 readings (digitalRead) and once done it will print them and hang forever.

If you use Serial.println() instead of Serial.print() in the for-loop, you can use tools -> serial plotter for a visual indication.

TT_Vert:
and a pwm pin feeding the hot side via a resistor though

But in the "schematic" the resistor is on the "cold" side. Not that it matters but it shows how conflicting the info is... What you tell and think you do might not be what you actually do.

septillion:
But in the "schematic" the resistor is on the "cold" side. Not that it matters but it shows how conflicting the info is... What you tell and think you do might not be what you actually do.

I apologize guys, I was rushing to draw that in paint fresh off a very early morning flight. The resistor is in fact on the hot side and is between the PWM pin the LED anode. I've fixed the quick drawing to reflect the actual circuit.

Dave