# analogWrite issues

**URL:** <https://forum.arduino.cc/t/analogwrite-issues/656070>\
**Category:** Programming\
**Created:** [May 20, 2020, 4:23pm UTC](https://forum.arduino.cc/t/analogwrite-issues/656070 "2020-05-20T16:23:25Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![rory401](https://avatars.discourse-cdn.com/v4/letter/r/f17d59/32.png) [@rory401](https://forum.arduino.cc/u/rory401)\
**Post date:** [May 20, 2020, 4:23pm UTC](https://forum.arduino.cc/t/analogwrite-issues/656070/1 "2020-05-20T16:23:25Z")

</div>

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](https://forum.arduino.cc/uploads/short-url/d95MBTkycCALWAEuSUwIKO1PXdt.ino) (2.06 KB)

---

<div class="post-metadata">

**Author:** ![anon73444976](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon73444976](https://forum.arduino.cc/u/anon73444976)\
**Post date:** [May 20, 2020, 5:16pm UTC](https://forum.arduino.cc/t/analogwrite-issues/656070/2 "2020-05-20T17:16:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jslovi](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jslovi/32/526225_2.png) [@jslovi](https://forum.arduino.cc/u/jslovi)\
**Post date:** [May 20, 2020, 6:13pm UTC](https://forum.arduino.cc/t/analogwrite-issues/656070/3 "2020-05-20T18:13:03Z")

</div>

> TheMemberFormerlyKnownAsAWOL:  
> When you analogWrite, you only ever write 5V or 0V.

Isn't that digitalWrite?

---

<div class="post-metadata">

**Author:** ![anon73444976](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon73444976](https://forum.arduino.cc/u/anon73444976)\
**Post date:** [May 20, 2020, 6:15pm UTC](https://forum.arduino.cc/t/analogwrite-issues/656070/4 "2020-05-20T18:15:09Z")

</div>

No, analogWrite is just the same, only faster.

---

<div class="post-metadata">

**Author:** ![jslovi](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jslovi/32/526225_2.png) [@jslovi](https://forum.arduino.cc/u/jslovi)\
**Post date:** [May 20, 2020, 6:26pm UTC](https://forum.arduino.cc/t/analogwrite-issues/656070/5 "2020-05-20T18:26:27Z")

</div>

> 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).

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [May 20, 2020, 6:31pm UTC](https://forum.arduino.cc/t/analogwrite-issues/656070/6 "2020-05-20T18:31:05Z")

</div>

> TheMemberFormerlyKnownAsAWOL:  
> When you analogWrite, you only ever write 5V or 0V.

> jslovi:  
> Isn't that digitalWrite?

@TheMemberFormerlyKnownAsAWOL

Are they equivalent/doing the same job?

```arduino
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);

```

**and**

```arduino
pinMode(13, OUTPUT);
analogWrite(13, 255);    //255 ~= 5V

```

---

<div class="post-metadata">

**Author:** ![Danois90](https://avatars.discourse-cdn.com/v4/letter/d/50afbb/32.png) [@Danois90](https://forum.arduino.cc/u/Danois90)\
**Post date:** [May 20, 2020, 6:32pm UTC](https://forum.arduino.cc/t/analogwrite-issues/656070/7 "2020-05-20T18:32:56Z")

</div>

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..

---

<div class="post-metadata">

**Author:** ![anon73444976](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon73444976](https://forum.arduino.cc/u/anon73444976)\
**Post date:** [May 20, 2020, 6:37pm UTC](https://forum.arduino.cc/t/analogwrite-issues/656070/8 "2020-05-20T18:37:02Z")

</div>

> GolamMostafa:  
> @TheMemberFormerlyKnownAsAWOL
> 
> Are they equivalent/doing the same job?
> 
> ```arduino
> pinMode(13, OUTPUT);
> 
> ```

digitalWrite(13, HIGH);

> ```arduino
> 
> **and**
> 
> ```
> 
> pinMode(13, OUTPUT);  
> analogWrite(13, 255); //255 ~= 5V
> 
> ```arduino
> 
> ```

Why don't you look at the source?

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [May 20, 2020, 6:37pm UTC](https://forum.arduino.cc/t/analogwrite-issues/656070/9 "2020-05-20T18:37:37Z")

</div>

> GolamMostafa:
> 
> ```arduino
> pinMode(13, OUTPUT);
> 
> ```

analogWrite(13, 255); &nbsp; &nbsp;//255 ~= 5V

> ```arduino
> 
> ```

Should we ignore the fact that pin 13 is a purely digital pin ?

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [May 20, 2020, 6:49pm UTC](https://forum.arduino.cc/t/analogwrite-issues/656070/10 "2020-05-20T18:49:50Z")

</div>

> 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).  
 ![pwm328.png](https://europe1.discourse-cdn.com/arduino/original/4X/d/9/6/d9608139ac06c497ba2bd09924661bae96171b2b.png)  
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 = 179`Edit:

![pwm328.png](https://europe1.discourse-cdn.com/arduino/original/4X/d/9/6/d9608139ac06c497ba2bd09924661bae96171b2b.png)

---

<div class="post-metadata">

**Author:** ![anon73444976](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon73444976](https://forum.arduino.cc/u/anon73444976)\
**Post date:** [May 20, 2020, 7:11pm UTC](https://forum.arduino.cc/t/analogwrite-issues/656070/11 "2020-05-20T19:11:50Z")

</div>

> 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); `

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [May 20, 2020, 7:34pm UTC](https://forum.arduino.cc/t/analogwrite-issues/656070/12 "2020-05-20T19:34:30Z")

</div>

I tested the code and it seems to work. What is the question?

---

<div class="post-metadata">

**Author:** ![anon73444976](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon73444976](https://forum.arduino.cc/u/anon73444976)\
**Post date:** [May 20, 2020, 7:44pm UTC](https://forum.arduino.cc/t/analogwrite-issues/656070/13 "2020-05-20T19:44:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [May 20, 2020, 7:53pm UTC](https://forum.arduino.cc/t/analogwrite-issues/656070/14 "2020-05-20T19:53:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![anon73444976](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon73444976](https://forum.arduino.cc/u/anon73444976)\
**Post date:** [May 20, 2020, 8:01pm UTC](https://forum.arduino.cc/t/analogwrite-issues/656070/15 "2020-05-20T20:01:44Z")

</div>

But the reference page for analogWrite states

> value: the duty cycle: between 0 (always off) and 255 (always on).

, so 5 \*256/256

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [May 20, 2020, 8:11pm UTC](https://forum.arduino.cc/t/analogwrite-issues/656070/16 "2020-05-20T20:11:19Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![anon73444976](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon73444976](https://forum.arduino.cc/u/anon73444976)\
**Post date:** [May 20, 2020, 8:15pm UTC](https://forum.arduino.cc/t/analogwrite-issues/656070/17 "2020-05-20T20:15:11Z")

</div>

I see what you mean, but does the reference state or imply that the relationship will be linear?

---

<div class="post-metadata">

**Author:** ![rory401](https://avatars.discourse-cdn.com/v4/letter/r/f17d59/32.png) [@rory401](https://forum.arduino.cc/u/rory401)\
**Post date:** [May 21, 2020, 12:01am UTC](https://forum.arduino.cc/t/analogwrite-issues/656070/18 "2020-05-21T00:01:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [May 21, 2020, 12:10am UTC](https://forum.arduino.cc/t/analogwrite-issues/656070/19 "2020-05-21T00:10:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [May 21, 2020, 12:31am UTC](https://forum.arduino.cc/t/analogwrite-issues/656070/20 "2020-05-21T00:31:05Z")

</div>

> 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. 🙂

[Next page](https://forum.arduino.cc/t/analogwrite-issues/656070.md?page=2)
