Skip to content

Commit d682f11

Browse files
committed
NONWORKING test for ch2 adc on esp32
1 parent 4ac71fe commit d682f11

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

io_utils.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,34 @@ void scanPins(){
8585
Serial.println("");
8686
}
8787

88+
#ifdef ESP32
89+
// ADC2 restoring procedure
90+
// This is a Workaround to use ADC2 Pins on ESP32 when Wifi or Bluetooth is on.
91+
// (usually only ADC1 Pins are usable for analogRead() when Wifi or Bluetooth is on.)
92+
// -- We save the ADC2 control register before WifiBegin() or BluetoothBegin()
93+
// -- then restore its original value, then set a specific bit of the ADC2 control register
94+
//to avoid inverted readings: we do the latter two before every analogRead() cycle.
95+
// -- This achieves ADC2 usability even when Wifi/Bluetooth are turned on!
96+
97+
#include "esp32-hal-adc.h" // needed for adc pin reset
98+
#include "soc/sens_reg.h" // needed for manipulating ADC2 control register
99+
uint64_t reg_b; // Used to store ADC2 control register
100+
int value;
101+
102+
void storeADC() {
103+
// Save ADC2 control register value : Do this before begin Wifi/Bluetooth
104+
reg_b = READ_PERI_REG(SENS_SAR_READ_CTRL2_REG);
105+
// Wifi.Begin(); // or similar wifi init function or Bluetooth begin()
106+
}
107+
108+
void restoreADC() {
109+
// ADC2 control register restoring
110+
WRITE_PERI_REG(SENS_SAR_READ_CTRL2_REG, reg_b);
111+
//VERY IMPORTANT: DO THIS TO NOT HAVE INVERTED VALUES!
112+
SET_PERI_REG_MASK(SENS_SAR_READ_CTRL2_REG, SENS_SAR2_DATA_INV);
113+
Serial.println(analogRead(4));
114+
Serial.println(analogRead(33));
115+
}
116+
#endif
117+
88118
#endif

0 commit comments

Comments
 (0)