Due to high demand and inclement weather, expect some shipping delays at this time, orders may not ship for 4-5 business days.
0

analog input from HUZZAH32-ESP32
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

analog input from HUZZAH32-ESP32

by earthres on Thu Nov 18, 2021 12:43 pm

Can someone explain to me why this code for a HUZZAH32-ESP32 doesn't work? With a TMP36 sensor it gives values like this (temperature around 8-9 C):

11:36:31.845 -> 720, 0.5802, 8.02, 46.44
11:36:33.824 -> 736, 0.5931, 9.31, 48.76
11:36:35.844 -> 736, 0.5931, 9.31, 48.76
11:36:37.816 -> 733, 0.5907, 9.07, 48.33
11:36:39.842 -> 736, 0.5931, 9.31, 48.76

indoors where the temperature is actually around 25 C. I know the TMP36 works OK because I've also tested it on an UNO.

Code: Select all | TOGGLE FULL SIZE
const int TMPpin=34; // GPIO 34 = A2
int V_digitized, delayTime=2000;
float V_analog,T,T_F; // temperature in deg C
void setup() {
  pinMode(TMPpin,INPUT);
  Serial.begin(9600);
}
void loop() {
  V_digitized=analogRead(TMPpin);
  V_analog=3.3*V_digitized/4095.;
  T=100.*(V_analog-0.5); // for TMP36 sensor
  T_F=T*9./5.+32;
  Serial.print(V_digitized); Serial.print(", ");
  Serial.print(V_analog,4);
  Serial.print(", ");
  Serial.print(T,2); Serial.print(", ");
  Serial.print(T_F,2);
  Serial.println();
  delay(delayTime);
}

earthres
 
Posts: 44
Joined: Fri May 28, 2021 10:48 am

Re: analog input from HUZZAH32-ESP32

by reschue on Thu Nov 18, 2021 2:04 pm

Your code looks OK. If you say the temperature should be 25C, the sensor should be producing an output voltage of 750mV and the analog read should be returning a value of around 930.

So either the temp sensor is producing the wrong output voltage, or the analog read is lying.

You might temporarily disconnect the sensor from the CPU and measure the output voltage to confirm the analog input pin is not in conflict with some other function that might be holding it low.

reschue
 
Posts: 157
Joined: Sun Jun 17, 2018 4:36 pm

Re: analog input from HUZZAH32-ESP32

by earthres on Thu Nov 18, 2021 3:20 pm

esp.jpg
esp.jpg (79.58 KiB) Viewed 352 times

Hmm... There is online information about using the ESP32 for analog inputs that says the conversion for default settings is supposed to be

V_analog=1.1*V_digitized/1023.;

This image shows some data from a TMP36 sensor, on the left with this conversion and on the right, right after that, the same TMP36 sensor in the same indoor conditions connected to an UNO. The UNO values are good, and the ~0.5C variations are consistent with the output of this device on a 0-5 V 10-bit board. The HUZZAH32-ESP32 values are too high, which could be fixed by lowering the 1.1 value. but they are also VERY noisy, which is a serious problem. (Connecting a 0.1 uF cap across output to ground didn't help.)

These results suggest that using the HUZZAH32-ESP32 to process analog inputs is not a very good idea! Fortunately, many sensors are I2C, and this works OK.

earthres
 
Posts: 44
Joined: Fri May 28, 2021 10:48 am

Re: analog input from HUZZAH32-ESP32

by reschue on Thu Nov 18, 2021 7:54 pm

Sorry, I'm not familiar with that CPU. Looks like it's a 10-bit A/D with a 1.1 Volt reference.

With regard to the noise, do you have the supply pin on the TMP36 bypassed to ground close to the device? And, what is "ground" on your set-up? is it just a wire between the sensor and the CPU, or is it sensor attached directly to the ground plane?

reschue
 
Posts: 157
Joined: Sun Jun 17, 2018 4:36 pm

Re: analog input from HUZZAH32-ESP32

by earthres on Fri Nov 19, 2021 10:18 am

This is an Adafruit board, so what I've hoped for is a response from someone at Adafruit who can definitively explain how to treat analog inputs on the HUZZAH32-ESP32 -- a topic about which Adafruit is very uncharacteristically deficient. As for your specific questions about connections, for the image I showed in my post the TMP36 is in both cases just connected through breadboards with #22 wires about 2-3" long and clearly there's no noise problem with the UNO connection.

It's also clear that the analog pin voltage reference isn't exactly 1.1 V (the nominal(?) value specified in some online sources) because the digital count to analog conversion gives temperature values that are too high by a few degrees C. It's also true, BTW, that according to several online sources, ESP32 board A-to-D conversions aren't even linear over their entire range.

From what I've seen so far, there's no point to using analog sensors with this board. It would be much better to use an I2C device like an ADS1115 16-bit four-channel programmable gain analog input breakout board, or even the slightly cheaper ADS1015 12-bit version (both of which are available from Adafruit).

If there's something I'm missing here, it shouldn't be difficult for someone at Adafruit simply to hook up a TMP36 sensor to a HUZZAH32-ESP, get it working properly, and tell me how they did it. :-)

earthres
 
Posts: 44
Joined: Fri May 28, 2021 10:48 am

Re: analog input from HUZZAH32-ESP32

by reschue on Fri Nov 19, 2021 11:05 am

1 volt references on 10 bit A/D's mean that your LSB is 1 millivolt. Pretty hard to keep noise out of the measurements. I'll defer to the Adafruit experts to answer your question on why it works on one board but not another.

In the mean time, you can always do some brute force filtering on the A/D output. I've always like the lossy integrator approach. Three lines of code, no multiplies or divides:
Code: Select all | TOGGLE FULL SIZE
// filter the input with a lossy integrator
// take 15/16th of the current value
// add in the new reading
// divide out the factor of 16
// the integrator stabilizes at 16x the value of the input
// for a longer time constant, shift by 5 (factor of 32), etc.

V_integrator -= V_integrator >> 4;
V_integrator += analogRead(TMPpin);
V_digitized = V_integrator >> 4;

reschue
 
Posts: 157
Joined: Sun Jun 17, 2018 4:36 pm

Re: analog input from HUZZAH32-ESP32

by earthres on Fri Nov 19, 2021 12:17 pm

Thanks for your input. But I'm still hoping that someone from Adafruit will provide some input on this matter!

earthres
 
Posts: 44
Joined: Fri May 28, 2021 10:48 am

Re: analog input from HUZZAH32-ESP32

by earthres on Sat Nov 27, 2021 11:09 am

I don't understand why Adafruit's moderator for this forum hasn't weighed in on what seems like a simple question: How does one properly interpret voltage inputs applied to a HUZZAH32-ESP32 analog input pin? I have tried using a TMP3x temperature sensor just because it's a very simple analog sensor that works perfectly well, within its stated accuracy limitations, with Arduino boards like UNOs. But as far as I've been able to determine, as indicated in my previous posts, you just can't get good A-to-D-to-A data from a HUZZAH32-ESP32. I would be pleased to be wrong!

earthres
 
Posts: 44
Joined: Fri May 28, 2021 10:48 am

Re: analog input from HUZZAH32-ESP32

by reschue on Sat Nov 27, 2021 1:13 pm

You might want to create a new post. An old post with many replies may not rise above the radar horizon of the moderators.

reschue
 
Posts: 157
Joined: Sun Jun 17, 2018 4:36 pm

Re: analog input from HUZZAH32-ESP32

by franklin97355 on Sat Nov 27, 2021 1:50 pm

Sorry for missing your post. I'll forward it to someone more cognizant with the ESP32-S2.

franklin97355
 
Posts: 22424
Joined: Mon Apr 21, 2008 2:33 pm
Location: Lacomb, OR.

Re: analog input from HUZZAH32-ESP32

by adafruit2 on Sat Nov 27, 2021 2:29 pm

hiya, yah basically the ESP series does not have good analog inputs compared to the ATmega or SAMD series (or even nRF52) - there's non-linearity and they're a bit noisy.
https://docs.espressif.com/projects/esp ... zing-noise

you could calibrate the ADC to get close to what you want but for a very sensitive chip like the TMP36, it'll be a struggle. tbh, one is best off up updating to an I2C sensor or DS18b20!

adafruit2
Site Admin
 
Posts: 20490
Joined: Fri Mar 11, 2005 7:36 pm

Re: analog input from HUZZAH32-ESP32

by earthres on Sat Nov 27, 2021 3:18 pm

OK, thanks. This is the response I was looking for. I'm not sure why you say that the TMP36 (or 35) is a "very sensitive chip." It performs no better or worse than any other analog sensor in applications with other Arduino boards (I've used lots of different analog sensors). And, I would say that the analog input pins are "very," rather a "bit" noisy, but maybe that's just me! I referred to a TMP36 in these posts because its output is easy to understand and I have several of them lying around.
In the link below, explaining the pin outs on the HUZZAH32-ESP32, the detailed description about how to use A0-A6, including which of those pins can be used with WiFi sketches, gives no details about how to use those pins for A-to-D-to-A applications, or even any hint that there might be a problem with using those pins with analog output sensors.
There is the additional problem that even neglecting the noise and nonlinearity problems cited in the link you provided, the internal reference voltage used for the D-to-A conversion varies by a LOT among different ESP32s compared to, say, an UNO. (And I believe it's also true (?) that ESP32 boards don't support use of an external reference voltage on analog pins like UNOs, etc., do.)
I think it would be appropriate to update Adafruit's product description to let people know that the HUZZAH32-ESP32 analog input pins are essentially useless for interpreting data from analog sensors. They MIGHT occasionally be useful if all you need is just a digital "count" value rather than a D-to-A conversion, too...
Having said all this, except for the analog input problems, the HUZZAH32-ESP32 is a great little board :-)

https://cdn-learn.adafruit.com/download ... 1597681529

earthres
 
Posts: 44
Joined: Fri May 28, 2021 10:48 am

Re: analog input from HUZZAH32-ESP32

by adafruit2 on Sat Nov 27, 2021 3:24 pm

the reason we say the TMP36 is sensitive is that it is NOT ratiometric (like, say the ADXL335 https://www.adafruit.com/product/163) - that means that if the ARef varies a little, that error is reflected in the measurement. with ratiometric sensors, the ARef error does not affect measurement, it cancels out! since the ESP does not have external ARef, best bet is to just go with a digital sensor.

adafruit2
Site Admin
 
Posts: 20490
Joined: Fri Mar 11, 2005 7:36 pm

Re: analog input from HUZZAH32-ESP32

by earthres on Sat Nov 27, 2021 4:21 pm

Agreed. You are right that it's best to use digital devices on this board. Being able to supply a solid external reference voltage, like the 3.3 V output on an UNO, does fix problems with sensors like the TMP3x. The best solution if you MUST use analog devices is to attach them to an ADS1115 or ADS1015, with its I2C interface.

earthres
 
Posts: 44
Joined: Fri May 28, 2021 10:48 am

Please be positive and constructive with your questions and comments.