# AnalogRead and Write breaks WLED

**URL:** <https://wled.discourse.group/t/analogread-and-write-breaks-wled/465>\
**Category:** Issues\
**Created:** [May 20, 2020, 3:35pm UTC](https://wled.discourse.group/t/analogread-and-write-breaks-wled/465 "2020-05-20T15:35:50Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anthony](https://yyz2.discourse-cdn.com/free1/user_avatar/wled.discourse.group/anthony/32/308_2.png) [@Anthony](https://wled.discourse.group/u/Anthony)\
**Post date:** [May 20, 2020, 3:35pm UTC](https://wled.discourse.group/t/analogread-and-write-breaks-wled/465/1 "2020-05-20T15:35:50Z")

</div>

![2020-05-20_00-01-27](https://global.discourse-cdn.com/free1/uploads/wled/original/1X/e3aa1257c3f6a5b576be1db6f2e696ced01fd679.png)

This code is so I can use the ESP8266 as a fan controller (with MOSFET) alongside controlling the lights.  
When adding just this code to usermod.cpp, the functionality of WLED stops. No WiFi network, LED strip starts changing colour randomly. The functionality of the potentiometer and fan speed works just fine though.  
What’s the problem here?

---

<div class="post-metadata">

**Author:** ![huggy-d1](https://yyz2.discourse-cdn.com/free1/user_avatar/wled.discourse.group/huggy-d1/32/51_2.png) [@huggy-d1](https://wled.discourse.group/u/huggy-d1)\
**Post date:** [May 20, 2020, 3:52pm UTC](https://wled.discourse.group/t/analogread-and-write-breaks-wled/465/2 "2020-05-20T15:52:08Z")

</div>

Try temporarily disabling printing. Look for the debug printing WLED uses, and then configure it so it can print out serial using a pin not used by default to control LEDs.

---

<div class="post-metadata">

**Author:** ![Anthony](https://yyz2.discourse-cdn.com/free1/user_avatar/wled.discourse.group/anthony/32/308_2.png) [@Anthony](https://wled.discourse.group/u/Anthony)\
**Post date:** [May 20, 2020, 4:10pm UTC](https://wled.discourse.group/t/analogread-and-write-breaks-wled/465/3 "2020-05-20T16:10:28Z")

</div>

I’m not using any pins that control LEDs  
The problem still occurs when I comment out serial printing too 😢

---

<div class="post-metadata">

**Author:** ![huggy-d1](https://yyz2.discourse-cdn.com/free1/user_avatar/wled.discourse.group/huggy-d1/32/51_2.png) [@huggy-d1](https://wled.discourse.group/u/huggy-d1)\
**Post date:** [May 20, 2020, 4:15pm UTC](https://wled.discourse.group/t/analogread-and-write-breaks-wled/465/4 "2020-05-20T16:15:40Z")

</div>

This link may help: [https://forum.arduino.cc/index.php?topic=457216.0](https://forum.arduino.cc/index.php?topic=457216.0)  
I see reading from A0 into variable pot, then writing out “0”, not D0 or A0.

I do not think that is what you want to do. I think GPIO0 is already spoken for.

---

<div class="post-metadata">

**Author:** ![huggy-d1](https://yyz2.discourse-cdn.com/free1/user_avatar/wled.discourse.group/huggy-d1/32/51_2.png) [@huggy-d1](https://wled.discourse.group/u/huggy-d1)\
**Post date:** [May 20, 2020, 4:18pm UTC](https://wled.discourse.group/t/analogread-and-write-breaks-wled/465/5 "2020-05-20T16:18:10Z")

</div>

I am confused why you say you are not controlling any LEDs.

1. Why use WLED if you are not controlling LEDs?
2. You said in the original post, “LED strip starts changing colour randomly”, so yes, you are controlling LEDs.  
I’m sure it’s just a slight difference in semantics when you say you are not controlling LEDs. Maybe you mean within your usermod.cpp you are not controlling LEDs?

The rest of WLED is handling that for you, and usermods should not affect that by using pins without verifying they are not already in use.

---

<div class="post-metadata">

**Author:** ![Anthony](https://yyz2.discourse-cdn.com/free1/user_avatar/wled.discourse.group/anthony/32/308_2.png) [@Anthony](https://wled.discourse.group/u/Anthony)\
**Post date:** [May 20, 2020, 5:30pm UTC](https://wled.discourse.group/t/analogread-and-write-breaks-wled/465/6 "2020-05-20T17:30:04Z")

</div>

Pin D4 is controlling LEDs like default, so I’m using D3 to control a fan with analogWrite. WLED works fine until I start using analogRead or Write.

---

<div class="post-metadata">

**Author:** ![huggy-d1](https://yyz2.discourse-cdn.com/free1/user_avatar/wled.discourse.group/huggy-d1/32/51_2.png) [@huggy-d1](https://wled.discourse.group/u/huggy-d1)\
**Post date:** [May 20, 2020, 9:09pm UTC](https://wled.discourse.group/t/analogread-and-write-breaks-wled/465/7 "2020-05-20T21:09:25Z")

</div>

> **[ESP8266 Pinout Reference: Which GPIO pins should you use? | Random Nerd...](https://randomnerdtutorials.com/esp8266-pinout-reference-gpios/)**
>
> The ESP8266 comes with 33 GPIOs with multiple functions. This article is a simple and easy to follow reference guide for the ESP8266 GPIOs.

  
Analog input is only available on esp8266 on ADC0 / A0  
Analog output is available as PWM on all GPIO’s, but that is not an analog write.  
GPIO0/D3 is used to flash the MCU, so don’t lose power if that is set low from your analog signal.

---

<div class="post-metadata">

**Author:** ![Anthony](https://yyz2.discourse-cdn.com/free1/user_avatar/wled.discourse.group/anthony/32/308_2.png) [@Anthony](https://wled.discourse.group/u/Anthony)\
**Post date:** [May 20, 2020, 10:50pm UTC](https://wled.discourse.group/t/analogread-and-write-breaks-wled/465/8 "2020-05-20T22:50:30Z")

</div>

It’s not a problem with the hardware setup. By just adding the analogRead in the code, which I’m reading from A0, the operation of WLED stops, like I described in my original question. The analogWrite doesn’t seem to be a problem, neither is the serial debug.

---

<div class="post-metadata">

**Author:** ![huggy-d1](https://yyz2.discourse-cdn.com/free1/user_avatar/wled.discourse.group/huggy-d1/32/51_2.png) [@huggy-d1](https://wled.discourse.group/u/huggy-d1)\
**Post date:** [May 21, 2020, 2:20pm UTC](https://wled.discourse.group/t/analogread-and-write-breaks-wled/465/9 "2020-05-21T14:20:46Z")

</div>

Ok, quick online search on github issues similar to your issue came up with this issue: [https://github.com/esp8266/Arduino/issues/1634](https://github.com/esp8266/Arduino/issues/1634)  
Calling the analogRead _every_ pass through the loop is causing the UI to become unresponsive.  
That link shows how to poll the analog input less often, perhaps every 50ms like in the example issue linked above.  
The analog write should also only be updated when a new value is read.

---

<div class="post-metadata">

**Author:** ![Aircoookie](https://yyz2.discourse-cdn.com/free1/user_avatar/wled.discourse.group/aircoookie/32/2_2.png) [@Aircoookie](https://wled.discourse.group/u/Aircoookie)\
**Post date:** [May 22, 2020, 9:05am UTC](https://wled.discourse.group/t/analogread-and-write-breaks-wled/465/10 "2020-05-22T09:05:18Z")

</div>

Hi, I believe that you are running into the same issue as in this thread:

> [@Analog Input?](https://wled.discourse.group/t/analog-input/380):
>
> Not sure if anyone else would be interested, so I’ll throw it out for comment… Support for an analog input to control the master brightness.

@tonyno also described that WiFi becomes unavailable when using `analogRead()`, I also suggested only reading about 10 times per second, but it did not seem to solve the issue ☹  
I will check the code of the audio reactive fork, they are using the analog input for microphone readings every millisecond without issues seemingly.

---

<div class="post-metadata">

**Author:** ![Anthony](https://yyz2.discourse-cdn.com/free1/user_avatar/wled.discourse.group/anthony/32/308_2.png) [@Anthony](https://wled.discourse.group/u/Anthony)\
**Post date:** [May 24, 2020, 11:21pm UTC](https://wled.discourse.group/t/analogread-and-write-breaks-wled/465/11 "2020-05-24T23:21:39Z")

</div>

With some testing, seems to work with a 200ms delay on every loop. Connecting is fine, and control works too. However, the lights seem to flicker whenever sending an input to change the colour. Doesn’t seem to be a problem without the 200ms analogRead. Really odd behaviour

---

<div class="post-metadata">

**Author:** ![tonyno](https://yyz2.discourse-cdn.com/free1/user_avatar/wled.discourse.group/tonyno/32/242_2.png) [@tonyno](https://wled.discourse.group/u/tonyno)\
**Post date:** [May 25, 2020, 1:54pm UTC](https://wled.discourse.group/t/analogread-and-write-breaks-wled/465/12 "2020-05-25T13:54:27Z")

</div>

A blocking delay?

---

<div class="post-metadata">

**Author:** ![Anthony](https://yyz2.discourse-cdn.com/free1/user_avatar/wled.discourse.group/anthony/32/308_2.png) [@Anthony](https://wled.discourse.group/u/Anthony)\
**Post date:** [May 28, 2020, 5:31pm UTC](https://wled.discourse.group/t/analogread-and-write-breaks-wled/465/13 "2020-05-28T17:31:57Z")

</div>

No, not delay()  
The millis() approach as suggested on the GitHub page
