Skip to content

Commit d2b0aaa

Browse files
committed
More accurate ESP32 analog read
1 parent 3379a60 commit d2b0aaa

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

src/lib/pushButton/PushButton.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ Button::Button(int pin, int initState, int32_t trigger) {
4646
void Button::poll() {
4747
int lastState = state;
4848
if (isAnalog) {
49-
int analogValue = analogRead(pin);
49+
#ifdef ESP32
50+
int analogValue = round((analogReadMilliVolts(pin)/3300.0F)*(float)ANALOG_READ_RANGE);
51+
#else
52+
int analogValue = analogRead(pin);
53+
#endif
5054

5155
if (DOWN == HIGH) {
5256
if (analogValue >= threshold + hysteresis) state = HIGH; else state = LOW;

src/lib/sense/Sense.cpp

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,12 @@ SenseInput::SenseInput(int pin, int initState, int32_t trigger) {
5050
int SenseInput::isOn() {
5151
int value = lastValue;
5252
if (isAnalog) {
53-
int sample = analogRead(pin);
53+
#ifdef ESP32
54+
int sample = round((analogReadMilliVolts(pin)/3300.0F)*(float)ANALOG_READ_RANGE);
55+
#else
56+
int sample = analogRead(pin);
57+
#endif
58+
5459
if (sample >= threshold + hysteresis) value = HIGH;
5560
if (sample <= threshold - hysteresis) value = LOW;
5661
} else {
@@ -67,7 +72,12 @@ int SenseInput::isOn() {
6772
int SenseInput::changed() {
6873
int value = lastChangedValue;
6974
if (isAnalog) {
70-
int sample = analogRead(pin);
75+
#ifdef ESP32
76+
int sample = round((analogReadMilliVolts(pin)/3300.0F)*(float)ANALOG_READ_RANGE);
77+
#else
78+
int sample = analogRead(pin);
79+
#endif
80+
7181
if (sample >= threshold + hysteresis) value = HIGH;
7282
if (sample < threshold - hysteresis) value = LOW;
7383
} else {
@@ -95,7 +105,17 @@ void SenseInput::poll() {
95105
}
96106

97107
void SenseInput::reset() {
98-
if (isAnalog) { if ((int)analogRead(pin) > threshold) lastValue = HIGH; else lastValue = LOW; } else lastValue = digitalReadEx(pin);
108+
if (isAnalog) {
109+
#ifdef ESP32
110+
int sample = round((analogReadMilliVolts(pin)/3300.0F)*(float)ANALOG_READ_RANGE);
111+
#else
112+
int sample = analogRead(pin);
113+
#endif
114+
if (sample > threshold) lastValue = HIGH; else lastValue = LOW;
115+
} else {
116+
lastValue = digitalReadEx(pin);
117+
}
118+
99119
stableSample = lastValue;
100120
}
101121

src/libApp/temperature/Thermistor.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,12 @@ void Thermistor::poll() {
4444
if (device[index] == THERMISTOR2) thermistorType = 1;
4545

4646
if (thermistorType >= 0 && devicePin[index] != OFF) {
47-
// get the total resistance
48-
int counts = analogRead(devicePin[index]);
47+
48+
#ifdef ESP32
49+
int counts = round((analogReadMilliVolts(devicePin[index])/3300.0F)*(float)ANALOG_READ_RANGE);
50+
#else
51+
int counts = analogRead(devicePin[index]);
52+
#endif
4953

5054
// calculate the device resistance
5155
float resistance;

0 commit comments

Comments
 (0)