Close

Analog grumblings

A project log for ESP32 Greenhouse Monitor

Collect temp/humidity, soil moisture, sunlight data to be stored on local web server

jeff-taylorJeff Taylor 01/24/2018 at 15:570 Comments

I spent most of my evening trying to get an analog input working on the ESP32.  The code is quite different from my previous experience with an ESP8266 in that the analogRead() command is no longer present.  On the new processor you set up the port bit width and attenuation first, then read the port with adc1_get_raw(pin).  Seems simple enough once you figure it out, but I had two things working against.  The first was that in searching through Google results, EVERY single page I found where someone was presenting their example of how to use an analog port on the ESP32 (and they all even showed pictures of their ESP32 on a breadboard!) had code that still used the analogRead command.  Yeah thanks guys, you obviously didn't actually test what you are presenting!  Which brings me to the second problem...

After watching the temperature values yesterday I realized that even pulling data once every 20 seconds was still causing some heating in the BME280.  I know, I know, the manufacturer recommends once every 60 seconds, but it's hard to remain patient when you're testing code.  However since the temp functions and display are working well, I went ahead and shifted the data read to once every 60 seconds, which brought the measured temps down to almost exactly what my in-house digital thermometer was reading (not that I have much faith in THAT device's accuracy).  And then I forgot I had made this change...  While playing with the photoresistor I naturally dropped the code for taking measurements into the same function where I was reading the BME280 from, and then wondered why I wasn't getting rapid readings on the display.  It finally hit me this morning what the problem was, so I made a quick change in the code, and then was able to swap out resistors until I found something that seems to work well with the photoresistor I have.

Another big difference in the analog ports on the ESP32 is that instead of accepting Vcc as the 100% level, these only require 1.1V to max out.  That required quite a shift in the resistor divider, so instead of using the common 10k resistor I ended up with a 220ohm.  Sitting here under room lights I am reading about 42%, but if I shine a light on the photoresistor it maxes out at 100%.  And I believe it is still sensitive enough to detect bright moonlight.

I also ordered a 4-gang relay board yesterday.  They all claim to be 5v devices, however each port couples with an opto-isolator which means it really only requires a digital signal strong enough to trip the isolator (although the board itself still requires a 5V source to latch the relays).  I'm betting I will be able to trigger the relays reliably with a 3.3v signal, but if not I can just add some cheap transistors to switch the 5v source.

Discussions