File tree 1 file changed +30
-0
lines changed
1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change @@ -85,4 +85,34 @@ void scanPins(){
85
85
Serial .println ("" );
86
86
}
87
87
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
+
88
118
#endif
You can’t perform that action at this time.
0 commit comments