When I analogWrite a voltage of anything less than 5 to both LEDs, I get the following:
How Many Times Do You Want the Red LED to Blink?
How Many Times Do You Want the Yellow LED to Blink?
The Red LED is Blinking
- You are on Blink #: 1*
How Many Times Do You Want the Red LED to Blink?
analogWrite works for both LEDs if one of the values is 255. 254 on both gives the same issue. The code also works when a digitalWrite is applied to one of the pins:
digitalWrite applied to both: working.
analogWrite applied to Red LED, digitalWrite applied to Yellow: working.
analogWrite applied to Yellow LED, digitalWrite applied to Red: working.
analogWrite_Problem.ino (2.06 KB)
Please post your code.
In code tags.
When I analogWrite a voltage of anything less than 5 to both LED
When you analogWrite, you only ever write 5V or 0V.
No, analogWrite is just the same, only faster.
TheMemberFormerlyKnownAsAWOL:
No, analogWrite is just the same, only faster.
What's the purpose of the range of value you may provide to digitalWrite then? digitalWrite is only LOW or HIGH while for analogWrite it is a whole range (defined by analogWriteResolution).
TheMemberFormerlyKnownAsAWOL:
When you analogWrite, you only ever write 5V or 0V.
jslovi:
Isn't that digitalWrite?
@TheMemberFormerlyKnownAsAWOL
Are they equivalent/doing the same job?
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
and
pinMode(13, OUTPUT);
analogWrite(13, 255); //255 ~= 5V
analogWrite starts a PWM signal on the desired pin, a PWM signal is either LOW or HIGH and the value "written" is the interval/time it is HIGH. You cannot "write" 3V to an analog pin, it will switch between LOW and HIGH to simulate 3V - it can be smoothed to 3V with a capacitor, though..
GolamMostafa:
@TheMemberFormerlyKnownAsAWOL
Are they equivalent/doing the same job?
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
**and**
pinMode(13, OUTPUT);
analogWrite(13, 255); //255 ~= 5V
Why don't you look at the source?
GolamMostafa:
pinMode(13, OUTPUT);
analogWrite(13, 255); //255 ~= 5V
Should we ignore the fact that pin 13 is a purely digital pin ?
Danois90:
analogWrite starts a PWM signal on the desired pin, a PWM signal is either LOW or HIGH and the value "written" is the interval/time it is HIGH. You cannot "write" 3V to an analog pin, it will switch between LOW and HIGH to simulate 3V - it can be smoothed to 3V with a capacitor, though..
1. These are the designated DPins (Fig-1) on UNO, which respond to ananlogWrte(DPin, 8bitArgument).

Figure-1:
2. analogWrite(13, 255); of Post#5 is not a valid code.
3. analogWrite(3, 145); will set the ON period of the 490 Hz signal of Fig-1 at 1160 us. //(2040/255)*145 = 1160
4. To make 3.5 volt output (after capacitor filtering) at DPin-3 of Fig-1, we have to execute the following code:
analogWrite(3, 179, [s]1428[/s]); //[s](1020/2.5)*3.5 = 1428[/s](255/5)*3.5 = 179Edit:

GolamMostafa:
2. analogWrite(13, 255); of Post#5 is not a valid code.
It absolutely is.
It will even do exactly what you expect, unlike this analogWrite(3, 1428);
I tested the code and it seems to work. What is the question?
I would expect that if "analogWrite(3, 255);" gives a voltage of 5V, then "analogWrite(3, 1428);" will generate a voltage of around 28V.
Obviously, it doesn't.
AnalogWrite(pin, 255) "should" produce (5.0 * 255/256)V, based on the simple concept that the output is n/256*5V where n is the control parameter.
If it doesn't, it's weird, because that is what any 8 bit DAC will do.
But the reference page for analogWrite states
value: the duty cycle: between 0 (always off) and 255 (always on).
, so 5 *256/256
TheMemberFormerlyKnownAsAWOL:
But the reference page for analogWrite states, so 5 *256/256
It is correct. However in order to enforce that simplification, one of the output steps will be non-linear. Depending on the PWM implementation, it must either jump from 0-2 or from 254-256. Do you see what I mean? 0-256 is 257 steps.
I think the designers were not thinking clearly about this. It's one of the things that were done to eliminate noob confusion, but created more instead.
I see what you mean, but does the reference state or imply that the relationship will be linear?
Hi everyone,
Thank you for your responses, however it seems that the discussion has strayed into the minutiae of analogWrite and digitalWrite. I will try to better outline my goal and issue.
My goal is to analogWrite a low voltage to two LEDs; Red and Yellow. This code does not work for me unless one of the pins specified (Red for example) is receiving 5v, either through "digitalWrite(redLEDPin, HIGH)" or "analogWrite(redLEDPin, 255)". If this is not in place in the code, after specifying the number of times to blink, the serial monitor will display the following;
"The Red LED is Blinking!
You are on blink #: 1
Please specify the number of blinks for the Red LED."
As opposed to the expected result of:
"The Red LED is Blinking!
You are on blink #: 1
You are on blink #: 2
You are on blink #: 3
You are on blink #: 4
You are on blink #: 5
The Yellow LED is Blinking!
You are on blink #: 1
You are on blink #: 2
You are on blink #: 3
You are on blink #: 4
You are on blink #: 5"
etc.
aarg:
I tested the code and it seems to work. What is the question?
If you can use analogWrite a voltage less than 5 to both pins in the code, then I can ostensibly rule out the code as the issue.
I tested the unmodified code from the first post on an UNO. It produced the expected result as above. Therefore, I suggest that you look at your hardware. What board are you using, or did you already say? Please post a drawing of photo of your wiring. Try disconnecting the LEDs and running the same sketch.
TheMemberFormerlyKnownAsAWOL:
I see what you mean, but does the reference state or imply that the relationship will be linear?
As usual, it's what it doesn't say that is important. 