Welcome to Hong Kong Bitfoic Electronics Co., Ltd
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
  • ESP32-CAM Pinout Explanation and How to Use?
    ESP32-CAM Pinout Explanation and How to Use?
    4/18/2024 82

    ESP32-CAM is a development board with an ESP32-S chip, an OV2640 camera, a microSD card slot, and several GPIOs for connecting peripherals. ESP32-CAM is a small-sized camera module. The module can work independently as the smallest system, with a size of only 27*40.5*4.5mm.

    Read More >
  • Stand-Alone Linear Li-Ion / Li-Polymer Charge Management Controller MCP73833
    Stand-Alone Linear Li-Ion / Li-Polymer Charge Management Controller MCP73833
    4/9/2024 63

    The MCP73833/4 is a highly advanced linear charge management controller for use in space-limited, cost sensitive applications. Both a 10-lead, MSOP and a 10-lead, DFN packaging measuring 3 mm by 3 mm are offered for the MCP73833/4. In addition to its tiny size, the MCP73833/4 is perfect for portable applications because it requires a few additional components.

    Read More >
  • Optocoupler IC 4N35: Pinout, Datasheet, Features and Applications
    Optocoupler IC 4N35: Pinout, Datasheet, Features and Applications
    3/26/2024 108

    In the realm of electronics, where connectivity and isolation are paramount, the 4N35 optocoupler IC stands as a beacon of reliability and versatility. This small yet mighty device plays a crucial role in ensuring signal integrity and safety across a wide range of applications. In this article, we delve into the intricacies of the 4N35 optocoupler IC, exploring its datasheet, pinout, circuit diagram, and diverse uses.

    Read More >
  • UA741CP datasheet ,Specification, Features and Application
    UA741CP datasheet ,Specification, Features and Application
    3/21/2024 113

    The UA741CP is a general-purpose operational amplifier in an 8-pin DIP package. The high common-mode input voltage range and lack of latch-up make the amplifier ideal for voltage follower applications.

    Read More >
  • CR2450 vs CR2032: Which model can replace CR2450?
    CR2450 vs CR2032: Which model can replace CR2450?
    3/14/2024 188

    Among the various types of batteries available, coin cell batteries are commonly used in small devices like calculators, watches, and key fobs. Two popular types of coin cell batteries are the CR2450 and CR2032. While they may seem similar due to their naming conventions, they have distinct differences that affect their interchangeability and applications.

    Read More >
Popular parts number