• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

Problem analogRead while WiFi

Q&A Forum › Category: ESP32 › Problem analogRead while WiFi
0 Vote Up Vote Down
Xabi Alaez asked 5 years ago

Hello everybody,
I am working on a project where I need to read NTC thermistors via analogRead() function and then send them to another ESP32 via ESPNow. 
The problem is that analogRead() doesn’t read anything if I initialize WiFi via WiFi.mode(WIFI_STA) or WiFi.mode(WIFI_AP). I can’t understand why, and I have tried everything. Is there any known ESP32 problem with analogRead() and WiFi?
If I measure the thermistor before initializing WiFi, it works fine. But once I set WiFi to any mode, it stops working and reads 3.14V everytime, in any Pin. 
I have already tried (and didn’t work):

  • Disconnecting WiFi 3 seconds before taking the measures via WiFi.disconnect(true); WiFi.mode(WIFI_OFF);
  • Restarting the ESP32 after every measure via ESP.restart()
  • Using DeepSleep between measures
  • I know it’s not a ESPNow library problem because it doesn’t work even not using any ESPNow function.

The strange part is that, if I restart the ESP32 via the reboot button, the first measure (just before initializing WiFi) works fine.
Can anybody please help me?
 
Thanks in advance 🙂
 
Xabi

14 Answers
0 Vote Up Vote Down
Rui Santos Staff answered 5 years ago

Hello Xabi,
Which GPIO are you using to read the analog value from?
Thanks!

0 Vote Up Vote Down
Xabi Alaez answered 5 years ago

I’m reading from 10 of them:

#define THERMISTORPIN1 25
#define THERMISTORPIN2 26   
#define THERMISTORPIN3 27
#define THERMISTORPIN4 13
#define THERMISTORPIN5 2
#define THERMISTORPIN6 39
#define THERMISTORPIN7 34
#define THERMISTORPIN8 15
#define THERMISTORPIN9 12
#define THERMISTORPIN10 4

I tried connecting the pins one by one, but, after WiFi, none of them worked…

0 Vote Up Vote Down
Rui Santos Staff answered 5 years ago

Can you just try reading one GPIO first with analogRead and Wi-Fi?
For example just use GPIO 27 with your Wi-Fi sketch.

0 Vote Up Vote Down
Xabi Alaez answered 5 years ago

Hi Rui,
I tried what you said, and I got some results:
it works with PINs 36, 39, 34 and 35. I tried all the pins one by one and the rest of them doesn’t work once I inicializate WiFi. Do you know what can be causing that? 
I need to read at least 10 Thermistors. 
Thanks in advance,
Xabi

0 Vote Up Vote Down
Rui Santos Staff answered 5 years ago

I’m not sure why to be honest. I had the same problem with GPIO 4 before… I think it’s something about the WiFi library that doesn’t work with all pins. I don’t know for sure which pins work with analogRead() + Wi-Fi, so only testing will give you the accurate results.

0 Vote Up Vote Down
Apisit Mitpitak answered 4 years ago

functions.

I met hear from ESP web. He have note for ADC2 can not work between esp_wifi_start() and esp_wifi_stop()..
Note:
Since the ADC2 is shared with the WIFI module, which has higher priority, reading operation of adc2_get_raw() will fail between esp_wifi_start() and esp_wifi_stop(). Use the return code to see whether the reading is successful

0 Vote Up Vote Down
Rui Santos Staff answered 4 years ago

Thanks for the explanation Apisit! I wasn’t aware of that.

0 Vote Up Vote Down
David Hughes answered 4 years ago

So is there any solution to this apart from disconnecting from wifi to read analogue values and then reconnecting to send data? 
I have been chasing around this problem for a couple of days and have just come to the same conclusion with a simple test program. I have tried pins 4, 13 and 14 and they all exhibit the same behaviour. Are there any pins that work?

0 Vote Up Vote Down
Rui Santos Staff answered 4 years ago

Hello David, this problem happens with ADC2 GPIOS, so you should be able to do analog reading and Wi-Fi on Arduino IDE using these pins:

  • GPIO 36
  • GPIO 39
  • GPIO 34
  • GPIO 35
0 Vote Up Vote Down
David Hughes answered 4 years ago

Hi Rui, thanks for coming back on this so quickly. Yes I have tested GPIO 35 and it is fine. I also found that 32 and 33 are good too, the other pins on ADC1. So this solves the problem. Thanks so much!

0 Vote Up Vote Down
Brian Webb answered 5 months ago

Can we use any adcs2 pins for digital opertions whilst using wifi.begin or should we not use them at all?

0 Vote Up Vote Down
Sara Santos Staff answered 5 months ago

Hi.
Yes. You can use them for digital operations.
Regards,
Sara

0 Vote Up Vote Down
Arlen Raasch answered 5 months ago

Xabi,
You probably have moved on to other things by now, but to measure the analog voltage from several analog sources you might consider using an analog switch to select which analog source you want to read.
This would allow you to use a single ADC1 pin on the ESP32 to read the selected source, then select a new source with the analog switch.
Four GPIO lines configured as output would go to the S0, S1, S2, S3 pins of the analog switch and would select which of 16 analog sources would be routed to the ESP32’s analog input pin.
This is one analog switch:
https://www.digikey.com/en/products/detail/texas-instruments/CD74HC4067SM96/390310
The CD74HC4067 takes 5V to operate, would use four digital output GPIO pins to select the source to read, and could support reading up to 16 analog sources.
Hope this helps.
Arlen

0 Vote Up Vote Down
Sara Santos Staff answered 4 months ago

Hi Arlen.
Thanks for sharing that suggestion.
Regards,
Sara

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [eBook Updated] SMART HOME with RPi, ESP32, and ESP8266 (version 1.2) March 8, 2023
  • [eBook Updated] ESP32-CAM Projects (version 1.3) January 7, 2023

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2023 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.