Quantcast
Channel: Arduino Forum - Latest topics
Viewing all articles
Browse latest Browse all 15287

ESP32 - analogRead(A10) not working with WiFi

$
0
0

I am running into an issue getting analogRead(A10) to work with WiFi enabled on an ESP32 board. I've already had these boards produced at a FAB, so changing the analog pin at this point isn't possible.

I've just now discovered that, according to ESP32 documentation, ADC2 may not work with WiFi. In my code, when commenting out #include <WiFi.h> at the start and WiFi.begin(); in setup, analogRead works properly. With these included, it only outputs 0.

Is there anything I can do in the firmware to fix this? Is it possible to pause some function that WiFi is using to make the reading, then unpause it after? Anything else?

My code is below, used for detecting charge on a battery and outputting to charge indicator LEDs.

#include <WiFi.h>

#define LED_BUILTIN 2

#define LEVEL1 25           // Level1 : GPIO 25 of ESP32
#define LEVEL2 26           // Level2 : GPIO 26 of ESP32
#define LEVEL3 27           // Level3 : GPIO 27 of ESP32
#define LEVEL4 14           // Level4 : GPIO 14 of ESP32

#define VBUS_DETECT 12       //GPIO 13

#define BATTERY_READ A10      //GPIO 4(A10) : Analog READ PIN for the battery voltage

#define BATTERY_0   1950         // ADC value for Battery 0%  - 1950
#define BATTERY_25  2303         // ADC value for Battery 25% - 2200
#define BATTERY_50  2400         // ADC value for Battery 50% - 2275
#define BATTERY_75  2410         // ADC value for Battery 75% - 2310
#define BATTERY_100 2850         // ADC value for Battery 100%  2850

int LEVELS[4] = {25, 26, 27, 14};
int batSensorValue = 0;  // value read from the pot
int full_charged = 0;
hw_timer_t *Timer0_Cfg = NULL;
int prevBatIndex = 0;
int batTimerCount = 0;
int gSensorValue = 0;
int adjustedSensorValue = 0;
int functionalSensorValue = 0;
uint8_t sensorBuffer = 4;
bool chargeFlashFlag = 0;

int getLevelIndex(int value) {
  switch (value) {
    case 0 ... BATTERY_25 - 1:
      return 1;
    case BATTERY_25 ... BATTERY_50 - 1:
      return 2;
    case BATTERY_50 ... BATTERY_75 - 1:
      return 3;
    case BATTERY_75 ... BATTERY_100 - 1:
      return 4;  
    default:
      return 5;
  }
}


void IRAM_ATTR Timer0_ISR() {
  batTimerCount++;

  // read VBUS status in value
  int vbusStatus = digitalRead(VBUS_DETECT);  
  // read the analog in value:
  batSensorValue = analogRead(BATTERY_READ);
  gSensorValue += batSensorValue;

  if(batTimerCount < 10) return;    //continue after 10 loops, then average

  adjustedSensorValue = (vbusStatus == 1 ? ((gSensorValue/10)-58) : (gSensorValue/10));       //if plugged in, reduce sensor value

  if (  (BATTERY_0 - sensorBuffer) <= adjustedSensorValue <= (BATTERY_0 + sensorBuffer) ||
        (BATTERY_25 - sensorBuffer) <= adjustedSensorValue <= (BATTERY_25 + sensorBuffer) ||
        (BATTERY_50 - sensorBuffer) <= adjustedSensorValue <= (BATTERY_50 + sensorBuffer) ||
        (BATTERY_75 - sensorBuffer) <= adjustedSensorValue <= (BATTERY_75 + sensorBuffer) ||
        (BATTERY_100 - sensorBuffer) <= adjustedSensorValue <= (BATTERY_100 + sensorBuffer) )
  {
    functionalSensorValue = adjustedSensorValue + sensorBuffer + 1;
  }
  else functionalSensorValue = adjustedSensorValue; 

  int levelIndex = getLevelIndex(functionalSensorValue);    //define LED level - if plugged in, reduce sensor value

  if (levelIndex == 5) full_charged = 1; else full_charged = 0;

  Serial.print("levelIndex = ");
  Serial.println(levelIndex);

  Serial.print("sensor = ");
  Serial.println(adjustedSensorValue);

  batTimerCount = 0;
  gSensorValue = 0;
  
  
  if(prevBatIndex > vbusStatus) {
    prevBatIndex = 0;
    return;
  }

  for(int i = 0; i < 4; i++) {
    digitalWrite(LEVELS[i], i < levelIndex ? HIGH : LOW);                         //light charger LEDs
  }  

  if(full_charged == 0 && vbusStatus == 1 && chargeFlashFlag == 0)  {
    digitalWrite(LEVELS[levelIndex-1], !digitalRead(LEVELS[levelIndex-1]));       //flash charger LEDs if plugged in and not 100%
  }

  if (vbusStatus == 1) chargeFlashFlag = (chargeFlashFlag+1)%2;
  //Serial.println(chargeFlashFlag);

  prevBatIndex = vbusStatus;

}

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(115200);
  
  WiFi.begin();

  pinMode(LEVEL1, OUTPUT);
  pinMode(LEVEL2, OUTPUT);
  pinMode(LEVEL3, OUTPUT);
  pinMode(LEVEL4, OUTPUT);

  pinMode(VBUS_DETECT, INPUT);

  Timer0_Cfg = timerBegin(0, 80, true);
  timerAttachInterrupt(Timer0_Cfg, &Timer0_ISR, true);
  timerAlarmWrite(Timer0_Cfg, 100000, true);
  timerAlarmEnable(Timer0_Cfg);
}

void loop() {
  delay(1000);
}

1 post - 1 participant

Read full topic


Viewing all articles
Browse latest Browse all 15287

Trending Articles