Saturday, June 15, 2019

Using a Sensor with an ESP8266

After playing with my newly acquired Sonoff switch I got the bug to explore additional wireless uses.  This time I wanted to hook up an environmental sensor to a WiFi capable device and then send the acquired data to a Blynk app for display.  

For the controller I chose an inexpensive ESP-WROOM-02 module (based upon the ESP8266 WiFi microchip).  When the ESP module first arrived I quickly realized I would need to either purchase or make my own break-out board in order to access the pins since the unit was an SMD device.  I knew it would be quicker if I bought a pre-made adapter but what fun would that be, so I used my PC board layout software to create a custom design, then etched and drilled a copper board.  The pictures below show a finished blank board alongside a fully populated one:

Break-out board
Fully populated board
If you look closely, you can see the embedded WiFi antenna running along the upper blue region of the board.

For testing purposes I decided to use a DHT22, which is one of the many inexpensive, single wire temperature and humidity sensors commonly available on the market.

After writing and compiling my program, the next step was to apply power, then flash the code into the chip and check for functionality.  It worked (no smoke!) but I soon discovered that the WiFi module was resetting itself periodically.  After much agonizing over potential coding issues I found that many other people were having the same problem.  It seems that when the module is in transmit mode the extra current demand causes a sudden drop in voltage from the power supply that randomly forces a hard reset.  The fix was to add two, 220uF capacitors in parallel across the chip's power and ground pins to smooth out the voltage spikes.  For extra insurance I also added a .1uF capacitor to reduce any high frequency noise.  Both additions seemed to do the trick.  Pretty exciting to finally see this working!  

Update (Late Fall 2019)
My module is now officially off the grid!  I decided to add some circuitry to eliminate the need to power my module from line voltage.  Taking a 6V solar panel I had purchased years ago, I added a 3.7V Lipo battery pack and a Lipo battery charger (from DFRobot) to create a stable 3.7-4.2V reference that would work 24/7.  Next, I fed this into a 3.3V LDO voltage regulator to provide the correct voltage for the ESP module and I was all set.

Initial testing was disappointing to say the least.  The 1700mAH battery was only lasting a few hours before it needed recharging.  In idle (receive) mode the circuitry consumes ~75mA while during data transmission it jumps to over 250mA.  The poor battery never had a chance.  After researching ideas on the web I found I could perform a software activated deep sleep operation on the chip, effectively shutting down most of the ESP module circuitry for a fixed period of time.  The only part of the chip still active would be a real time clock.  In this state the WiFi module consumes less than 1mA.  Then, when the time period expires (defined in software), a specific I/O pin on the module sends a signal (pulse) needed by the reset pin to wake up the chip.  All I had to do was add a couple of lines of code and hard wire the two pins together.  I decided that sleeping for 15 minutes, then waking up and processing data for 15-20 seconds before resuming another sleep cycle would be a good compromise for my project.

The nearly completed unit, mounted to the base of a plastic enclosure is shown below.  The Wifi module is actually on the underside of the copper board so it is not visible.