Welcome to Hong Kong Bitfoic Electronics Co., Ltd
[email protected] Request a Quote Contact Us My order
en ru en ru en
Home > Components >How to resolve the WiFi and ADC2 Sharing Dilemma?

How to resolve the WiFi and ADC2 Sharing Dilemma?

4/19/2024

 

Why explore this problem?

 

Due to the needs of the laboratory, I use ESP32cam to collect pictures, videos, and other information and transmit it to the IoT platform. At the same time, I also collect sensor information such as temperature, humidity, and PH value, and transmit it to the IoT platform simultaneously.

Because there is a lot of content, they are tested separately:

Test whether esp32cam connects to wifi and works normally

Test whether the esp32cam picture is collected normally and whether it can be transmitted to the external server through wifi

Test whether esp32cam can collect sensing information such as temperature, humidity, and pH value and output it.

 

The problem arises at this time. When testing functions 1, 2, and 3 separately, all functions are normal. However, after merging the program, the values collected by the PH sensor are all 0. I always thought that there was a problem with the sensor or an error that occurred during code merging, and I was delayed in these places. It took a long time; later, after consulting a lot of information, I found that it was a problem with the ESP32 development board hardware.

 

 

Check relevant information

 

The PH value sensor is an analog acquisition sensor and needs to be connected to the ADC interface of the ESP32 for digital-to-analog conversion. However, the ESP32 series only has two ADC digital-to-analog converters. When the ESP32 development board is connected to WIFI, WIFI will occupy ADC2, and WIFI The priority of occupying ADC2 is relatively high, and other modules cannot use ADC2 to obtain values, which is why the values collected by the PH sensor are all 0. However, ADC1 is not affected. At this time, you only need to connect the analog sensor to ADC1 for normal use.

Official Github: wifi and ADC2 cannot be used at the same time.

The pin functions of the ADC on the ESP32 development board are defined above (

ADC pins

):

 

Figure1-ESP32 pinout

Figure1-ESP32 pinout

 

But for the ESP32cam development board, this is another difficulty. The ESP32cam development board does not lead to the IO pins of ADC1, and some of the ADC1 pins are occupied by the camera module, so the ADC1 digital-to-analog converter cannot be used normally on the ESP32cam development board. The pin functions of the ADC on the ESP32cam development board are defined as above (

ADC pins

 

Figure2-ESP32 pinout

 

Figure2-ESP32 pinout

 

 

 

A solution to sharing WIFI and ADC2 in ESP32cam

 

Through the above explanation and various other documents, tutorials, reference materials, etc., it is said that WIFI and ADC2 cannot be shared. But the essence is that WIFI occupies ADC2, causing other analog sensors to be unable to read data through ADC2.

Question: Is it possible to turn off the WIFI function before ESP32 reads the analog sensor data connected to ADC2, turn on the WIFI function after reading the data, and then send the data to the external network platform?

The answer is: Yes, that is, you can use WIFI and ADC2 alternately.

Through actual measurement in the ESP32cam development board, it was found that this can normally read the correct value of the analog signal sensor connected to ADC2, but it will take more time to reconnect to WIFI each time. The time to reconnect to WIFI is variable, about 1 second.

This method can solve the problem of using ADC2 to collect data from analog sensors that do not collect information with high real-time performance. For example, for a PH value sensor, PH value information is collected every 3 minutes or 5 minutes. At this time, the 1 second required to reconnect to WIFI can be ignored for minute-level data collection. However, for analog signal sensors using ADC2 that have high real-time requirements, the 1 second it takes to reconnect to WIFI becomes unacceptable, and this method is useless.

 

The Arduino code shared by WIFI and ADC2 in ESP32cam is as follows:

#include <Arduino.h>#include <WiFi.h>#include "esp_camera.h"#include <vector>#include <string.h>using namespace std;

// ph and tem Variable settings const int phPin = 2;float Value = 0;

 

// wifi Account and password const char *ssid = "TP-LINK_1760";const char *password = "987654321";

void wifi_init() {

  WiFi.mode(WIFI_STA);

  WiFi.setSleep(false);  //Turn off wifi sleep in STA mode to improve response speed WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {

    delay(500);

    Serial.print(".");

  }

  Serial.println("WiFi Connected!");

  Serial.print("IP Address:");

  Serial.println(WiFi.localIP());}

void setup() {

 

  Serial.begin(115200);

  //  wifi initialization wifi_init();

 

  pinMode(phPin, INPUT);}void loop() {

  // turn off wifi  WiFi.disconnect(true);

  WiFi.mode(WIFI_OFF);

  

  // adc2 Read data pinMode(phPin, INPUT);

  Value = analogRead(phPin);

  

  // turn on wifi  wifi_init();

  // Serial port print data  Serial.print(Value);

  delay(500);}

 

Figure3-code

Figure3-code

 

Related Articles
Popular parts number