Two LED: Analog Output = One lights up

Hey, friends. I’m new the to the forum!

So, I’m following this

https://www.youtube.com/watch?v=9FBMVt-iFrM&list=PLGs0VKk2DiYx6CMdOQR_hmJ2NbB4mZQn-&index=8

(Great tutorial!)

So I have two LEDs (Red/Yellow) making them blink, it works perfect with digitalWrite
When I change to analogWrite only my Red LED blinks. The yellow doesn’t blink at all.

In my serial monitor I still get feedback “Yellow is blinking”.

This is what I tried:

  • Changed the pins in the sketch = Red Blinks / No Yellow
  • Copied the red loop pasted over the yellow (changed the names to yellow variables etc) = Red Blinks / No Yellow
  • I even copied the code straight off from TechTopBoy’s webapge (the mister in the tutorial)
    I then changed the digitalWrite to analogWrite = Red Blinks/No Yellow

My “on” values are 51 and off are 0.

I am probably missing something small. Nothing wrong with the physical connection, since if I go back to digitalWrite everything works out normally. I insert my code bellow, some parts are in swedish but it’s just side notes for learning. Hope you can help me out <3

My Code:

int rled = 10;        //red pin
int yled = 7;         //yellow pin

int rnum;             //red #blinks        org: int rnum = 3; // itn rnum; betyder fortarande declared
int ron = 300;        //red ON time
int roff = 300;       //red OFF time
String rmsg ="Red #"; //msg for yellow blinks in sermo

int ynum;         //yellow #blinks      org: int ynum = 3;
int yon = 300;        //yellow ON time
int yoff = 300;       //yellow OFF time

String ymsg="Yellow #"; //msg for red blinks in sermo

void setup() {
  Serial.begin(9600);
  pinMode(rled, OUTPUT);
  pinMode(yled, OUTPUT);
 
  Serial.println("How many RED blinks? (Number -10)");    //1)PROMTA VIKTIGT: glöm ej {} efter ()==0
  while (Serial.available()==0) {}           //2)WAIT inom {} loopar så länge som while () innehållet är TRUE. Vi testar om true 0/1  (Serial.print()==0) är en testfråga
  rnum = Serial.parseInt();                 //3) READ

  Serial.println("How many YELLOW blinks? (Number 1-10)");
  while (Serial.available()==0) {} 
  ynum = Serial.parseInt();
  Serial.println(" ");    
}



void loop() {
  

  int j=1;                          // declare innan, ej som for loop intern declare. local variable gäller innanför VoidLoop
  while (j<=rnum) {
      Serial.print(rmsg);                 
      Serial.println(j);
                                  // method-WRITE the PINX to VALUE. 0-255(0-5V) 51=1V
      analogWrite(rled, 51);     // analogWrite RED 1V ON
      delay(ron);            
      analogWrite(rled, 0);      // analogWrite RED 0V OFF
      delay(roff);
      j=j+1;
   }
                                       // Semikolon för seperera, alltså ej i slutet av Loop
   for (int j=1; j<=ynum; j=j+1) {    //(j=1 variabel    j<=3 loopa så länge j less than 3 lr (=)   j=j+1 efter varje loop GÖR j+1
      Serial.print(ymsg);                 
      Serial.println(j);
                                  // method-WRITE the PINX to VALUE. 0-255(0-5V) 51=1V
      analogWrite(yled, 51);     // analogWrite RED 1V ON
      delay(yon);            
      analogWrite(yled, 0);      // analogWrite RED 0V OFF
      delay(yoff);
      j=j+1;                             //Serial.print = samma rad. .println bryter rad   Serial.println(j);  

    }
  
Serial.println(" ");
}

Yellow LEDs need a little more voltage than red, try:

analogWrite(yled, 100);

Which Arduino do you have? On the Uno and other 328 based Arduinos, pin 7 is not a PWM pin.

@outsider "Yellow LEDs need a little more voltage than red, try:" analogWrite(yled, 100);

How does this give more voltage?

.

You can only use analogWrite() on a PWM pin. If you have an UNO pin 7 is not PWM, see the documentation for more info.

LarryD: @outsider "Yellow LEDs need a little more voltage than red, try:" analogWrite(yled, 100);

How does this give more voltage?

.

@outsider YES!! Thank you! It was the voltage! I was scared of putting too much voltage so the LED would break. I did 102 (51*2V) didn't work, this time I did 153 and it worked!

@LarryD digitalWrite is only HIGH or LOW (On/Off)

analogWrite is a value between 0-255 where (0-5 Volt) and I put in too little :)

Tanks again outsider!

"digitalWrite is only HIGH or LOW (On/Off) analogWrite is a value between 0-255 where (0-5 Volt) and I put in too little :)"

Yes digitalWrite is on/off, 0/5v So is analogWrite to a PWM pin!

Both are 0 to 5 volts, your problem is you need a PWM pin and a 'pulse width wide enough' so your eye can detect the light. There is no more voltage ;)

.

LarryD: Yes digitalWrite is on/off, 0/5v So is analogWrite to a PWM pin!

Both are 0 to 5 volts, your problem is you need a PWM pin and a 'pulse width wide enough' so your eye can detect the light. There is no more voltage ;)

.

That felt like slap in the face. Hmm, I didn't fully understand this.

So I'm not increasing the volts, I'm increasing the "pulse width"? Getting confused, what makes it dim and brighter?

What is this more exactly, any tip on good read or where I can dig deeper? Thanks!

[quote author=Blue Eyes link=msg=2994307 date=1478727767] You can only use analogWrite() on a PWM pin. If you have an UNO pin 7 is not PWM, see the documentation for more info.

[/quote]

Didn't see your post, thanks! Will look this up!

What's to look up? The Uno pins that do PWM are marked with ~ or similar - 3,5,6,9,10,11

analogWrite

hellophant:
@outsider YES!! Thank you! It was the voltage! I was scared of putting too much voltage so the LED would break. I did 102 (51*2V) didn’t work, this time I did 153 and it worked!

Wrong wrong wrong.

Notice how writing any value above your 153 up to 255 does not change the brightness at all?

You need to familiarize yourself with how PWM and analogWrite works.

The only reason 153 works and 102 didn’t, is because 102 is closer to 0(LOW), 153 is closer to 1(HIGH), given that the range is 0-255.

I you want to write 51 to yellow, you need to change it from pin 7 to 3, 5, 6, 9, or 11. Change the hardware as well as the code.

https://www.arduino.cc/en/Reference/AnalogWrite

@INTP "The only reason 153 works and 102 didn't, is because 102 is closer to 0(LOW), 153 is closer to 1(HIGH), given that the range is 0-255.

I you want to write 51 to yellow, you need to change it from pin 7 to 3, 5, 6, 9, or 11. Change the hardware as well as the code."

-This makes perfect sense when you say it like that.

~ = PWM Pin, I didn't even notice that sign, thanks!!

SO, even at the PWM I'm not actually increasing the Volts to make it brighter. "Generate a steady square wave of the specified duty cycle" (The analogWrite-page)

Duty cycle what is this exactly? Don't understand this part, should I just accept and go with it? :P

Thanks guys, this made lots more sense than before!

Duty cycle what is this exactly?

It is the portion of time that the pin is HIGH, out of the total window, on a scale of 0 to 255. The higher the duty cycle, the higher the apparent voltage of the pin (apparent to motors and LEDs, that is).

PaulS: It is the portion of time that the pin is HIGH, out of the total window, on a scale of 0 to 255. The higher the duty cycle, the higher the apparent voltage of the pin (apparent to motors and LEDs, that is).

So it basically turns the pin HIGH and LOW, on/off so fast that we can't see it?

The effect is called 'persistence of vision'. A light that turns on and then off will appear on for longer than it actually was because the receptors in your eyes take time to refresh (reuptake of neurotransmitters). Human eyes weren't evolved to stare at unnatural points of bright light, obviously. When a light is turned on and off at a rate during the eye's refresh, the light appears on constantly. But altering the on and off time is perceived as different levels of brightness as the light comes on and off at different levels of the eye being refreshed.

So, duty cycle is a percentage of total time. A 100% duty cycle is a light that is at a constant HIGH, it really isn't blinking. A 50% duty cycle is a light that is on half of the time and off half of the time, and will not be perceived as bright. The particular timing of half on half off is a frequency which can vary. A light on for 5 seconds and off for 5 seconds and repeats is technically a 50% duty cycle, but for persistence of vision effect of course is too slow. Frequency is separate from duty cycle, many people conflate them. Just remember that duty cycle is absolute percentage of time being on given some window of sampling.

INTP: The effect is called 'persistence of vision'. A light that turns on and then off will appear on for longer than it actually was because the receptors in your eyes take time to refresh (reuptake of neurotransmitters). Human eyes weren't evolved to stare at unnatural points of bright light, obviously. When a light is turned on and off at a rate during the eye's refresh, the light appears on constantly. But altering the on and off time is perceived as different levels of brightness as the light comes on and off at different levels of the eye being refreshed.

So, duty cycle is a percentage of total time. A 100% duty cycle is a light that is at a constant HIGH, it really isn't blinking. A 50% duty cycle is a light that is on half of the time and off half of the time, and will not be perceived as bright. The particular timing of half on half off is a frequency which can vary. A light on for 5 seconds and off for 5 seconds and repeats is technically a 50% duty cycle, but for persistence of vision effect of course is too slow. Frequency is separate from duty cycle, many people conflate them. Just remember that duty cycle is absolute percentage of time being on given some window of sampling.

Since you're amazing. Thanks, INTP! General question this does not stress the component compared to constant 100% HIGH, in this case the LED?

The LED is not stressed as long as it's running under its recommended current. For discrete 3mm, 5mm, SMD LEDs, that's often 20mA.

hellophant: Since you're amazing. Thanks, INTP! General question this does not stress the component compared to constant 100% HIGH, in this case the LED?

Constant 100% duty cycle is how basically all LEDs will be operating. The PWM/analogWrite stuff is the rarer scenario. The indicator LEDs on you computer, devices, microwave, car, these are all 100% duty cycle. What kills LEDs is the current passing through them. It is recommended to keep current for most 'regular' LEDs below 20mA, but they don't suddenly die at 21mA, of course. Depending on the LED, and reading the data sheet, they will sometimes say something like 'max current @ __% duty cycle (or pulse duration in ms) is __mA' some amount much higher than the 20mA or constant current rating.