AnalogRead and Write breaks WLED

2020-05-20_00-01-27

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?

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.

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

This link may help: 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.

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.

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.


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.

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.

Ok, quick online search on github issues similar to your issue came up with this issue: 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.

1 Like

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

@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 :frowning:
I will check the code of the audio reactive fork, they are using the analog input for microphone readings every millisecond without issues seemingly.

1 Like

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

A blocking delay?

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