ESP32 Analog To Digital Conversion Accuracy

The ESP32 is a powerful microcontroller with many input/output ports. Specifically, it contains two 12-bit multiplexed analog to digital converters (ADCs) for a total of 18 channels. ADC1 is attached to eight GPIOs from 32 to 39. ADC2 is attached to 10 GPIOs (0, 2, 4, 12 to 15 and 25 to 27). There are some restrictions on the use of ADC2 when the WiFi transceiver is active. Since most applications are WiFi-enabled, it is best to use ADC1 when possible.

The 12-bit resolution of the ADC maps the input voltage from 0 to 3.3 Vdc to byte values of 0 to 4,095. Unfortunately, the ADC converters exhibit non-linear characteristics. There is a lower threshold voltage of about 0.13 volts before the ADC starts to register and the volts per bit ratio above about 2.5 volts deviates from the nearly linear ratio between these two regions.

Some careful measurements on a doit.com DevKit v1 obtained the results in Table 1. Figure 1 clearly shows the non-linearities at the lower and upper ends of the input voltage.

By inspection, the ADC has better than 3% accuracy on the input range of 0.14 to 2.6 volts corresponding to ADC output byte values of 100 to 3000. Between 0.21 and 2.53 V the accuracy is better than 1%. Fitting a linear equation to the 1% range produces this relationship:

Vin = 0.0008 * byte + 0.1372 (volts)

Figure 2 shows the entire characteristic of the ADC when compared with the fitted curve. It is clear that the ADC is not usable below 0.13 volts and inaccurate above 2.5 volts.

The next step is to fit another equation to ADC values above 3000:

Vin = 0.0005 * byte + 1.0874 (volts)

Figure 3 shows that these two “piecewise linear” equations produce better than 1% accuracy from 0.14 volts to 3.1 volts.

Code to implement this is easy:

const int PIN_VBAT = 34;
float volts = 0;
int adc = 0;
adc = analogRead( PIN_VBAT );
if ( adc > 3000 )
{
volts = 0.0005 * adc + 1.0874;
}
else
{
volts = 0.0008 * adc + 0.1372;
}

As a practical matter, it is wise to filter the ADC output to remove noise and jitter. There are some nice Arduino libraries to do this but it is quite straightforward to just sum a bunch of readings and take an average. Here is the complete code with the two fitted equations, filtering, and scaling described below:

Scaling

If you want to measure an input voltage above the natural 3 volt range of the ADC you must add a resistor voltage divider to the ESP32 ADC input. You can find formulas to do this on the Internet but there is an excellent online calculator at http://jansson.us/resistors.html. Here are steps to designing a divider for a 50-volt input:

  1. Navigate to http://jansson.us/resistors.html.
  2. Select the resistor series. Resistors in the E12 series are the cheapest and most available.
  3. Find the calculator for “Resistor Ratio” and select the Voltage Divider radio button.
  4. For the maximum input voltage enter 50 for VH.
  5. For the maximum output voltage enter 3.11 for VL. This seems to be the maximum input voltage for the ESP32 corresponding to ADC output of 4,095.
  6. Press the Calculate button. The result is a resistor ratio of 15.077170418006432 to 1.
  7. The calculator provides three possible solutions:
    a. Single: a 150 kΩ in series with 10 kΩ. This is a ratio of 15:1 which is within 0.51% of the desired ratio.
    b. Series: a 300 kΩ resistor in series with the series combination of 18 kΩ and 3.9 kΩ. This is within 0.06 % of the desired ratio.
    c. Parallel: a 120 kΩ resistor in series with the parallel combination of 10 kΩ and 39 kΩ. This is an exact solution.
  8. Add a line to the code to change the scale of the voltage (note that 50.0 is the VH value and 3.11 is the VL value as used in the Resistor Ratio calculator):
    volts = 50.0 * volts / 3.11;
Screenshot from jansson.us/resistors.html

Any of the three voltage divider circuits shown in Figure 4 can be used depending upon the resistor values that are available to you.

Figure 4 – Voltage Divider Circuits

Zambretti Forecaster

The Zambretti Forecaster is a weather forecasting tool in the form of a circular slide rule introduced in 1920.  It claims to accurately predict near-term weather based on barometric pressure, the trend in the pressure (rising, steady, or falling), and wind direction. It relies on the fundamental meteorology of pressure fronts. Watch any TV weather forecast and you will see high- and low-pressure areas prominently displayed on the map. The boundaries between pressure areas are strongly associated with wind and precipitation. Additionally, high-pressure is usually associated with fair weather while low-pressure is associated with cloudy, rainy, or snowy weather.

The are many interesting things about the Zambretti Forecaster. First, there was no Mr. or Ms. Zambretti, rather, it was a trade name used by Negretti and Zambra, a famous London instrument-making company from the mid-19th Century through 1999. Second, there was a modern reproduction on sale in the mid-aughts of the 21st Century though it does not appear to be currently available.

Kevin Scott, Ph. D. has reverse-engineered the modern reproduction in very fine work reported at http://drkfs.net/zambretti.htm. He derived an algorithm that is easily implemented in computer code.

Since the D1M-WX1 weather stations accurately monitor barometric pressure, it is easy to apply the Zambretti Forecaster technique to a ThingSpeak channel. My adaptation of Dr.  Scott’s algorithm is provided below as a snippet for a MatLab visualization. To see this in action, open https://thingspeak.com/channels/286120. The Zambretti Forecast is at the bottom of the Current Values display.

Next steps:

  1.  Consider including the rate of barometric change in the forecast.
  2. Track the Zambretti Forecast against weather service predictions.
  3. Publish a pdf with instructions for making your own Zambretti Forecaster.

MATLAB code snippet:

Sending APRS Analog Telemetry – The basics

The amateur radio Automatic Packet Reporting System (APRS) is typically used for position and weather reporting. It can also handle eight digital and five analog telemetry channels. The IoT Kits© solar-powered weather stations have four APRS analog telemetry channels for LiPo cell voltage, time awake, WiFi signal strength, and light intensity. Four digital channels are used for warning alarms: low cell voltage, low signal strength, and sensor failures.

Analog telemetry is transmitted as an 8-bit word, therefore, the analog signal must be mapped to a range of 0 to 255. A good example is the mapping of LiPo cell voltage. An obvious mapping strategy would be to map voltage from 0 to, say, to 5 Vdc. This would quantize the results in steps of about  20 mV (5 V / 255) but it is unnecessary to report voltages below about 3 V because the D1 Mini will stop working.  If we map the range of 2.5 V to 5 V we will cut the quantization step in half to 10 mV, twice the precision of the first approach.  The weather station measures the cell voltage, subtracts 2.5 and divides the result by 10 mV. Say the cell voltage is 4.2, the mapped value is (4.2 – 2.5) ÷ 0.010 = 170.

The telemetry receiver must reverse this process by multiplying the received byte value by 0.010 and adding 2.5 like this: (170 × 0.010) + 2.5 = 4.2 Volts.

How does the receiver know that it must perform this operation to interpret the voltage? Two approaches are possible: we can send the telemetry “blind” since WE know how to interpret it, or we can send instructions so that anyone can understand our telemetry. The IoT Kits weather stations use the second method by sending instructions every two hours so that APRS services such as aprs.fi can correctly plot our data.

It actually takes four messages to completely define the analog telemetry:

T – T is the APRS symbol for telemetry. It is followed by a three-digit sequence number. Earlier versions of the D1M-WX1 weather stations sent a sequence number from 000 to 999 derived from UNIX time obtained fron an NTP server. Current versions send a number from 000 to 255 stored in the ESP8266 RTC memory and incremented on each wake cycle.

PARM – Defines the name of the parameter. APRS limits the name to three to six characters depending on the particular channel. The weather station transmits Vcell, RSSI, Light, and time awake.

UNIT – Defines the units for each channel with the name limited to 3 to 6 characters:  Vdc, dBm, lux, sec.

EQNS – Each analog channel has a quadratic equation that tells listeners how to interpret the 8-bit value associated with each channel.

A quadratic equation is a polynomial of the second order. That means that it is a polynomial function in one or more variables in which the highest-degree term is of the second degree. APRS uses this quadratic equation to interpret the received telemetry data: a · x2 + b · x + c where x is the received byte value.

The EQNS message transmits a set of three coefficients for each analog channel: a, b, and c

For the Vcell channel, the EQNS message looks like this: 0, 0.01, 2.5 meaning a = 0, b = 0.01, and c = 2.5. The receiver understands this to mean Vcell = 0 · x2 + 0.01 · x + 2.5

Let’s try it with a byte value of 163 as shown in the received telemetry data following:

Vcell = 0 ·1632 + 0.01 · 122 + 2.5

Vcell = 0 + 1.63 + 2.5 = 4.13 Volts

This is exactly what we had planned! This is how engineering and applied mathematics work in the real world.

Here is a sample of telemetry definitions from a D1M-WX1 weather station:

2018-10-09 15:53:32 EDT: W4KRL-15>APRS,TCPIP*,qAC,T2DENMARK:T#144,163,078,013,080,,1111,Solar Power WX Station
2018-10-09 15:53:32 EDT: W4KRL-15>APRS,TCPIP*,qAC,T2DENMARK::W4KRL-15 :PARM.Vcell,RSSI,Light,Awake,,BME28,BH17,loV,loS
2018-10-09 15:53:32 EDT: W4KRL-15>APRS,TCPIP*,qAC,T2DENMARK::W4KRL-15 :UNIT.Vdc,dBm,lux,secs,,OK,OK,OK,OK
2018-10-09 15:53:32 EDT: W4KRL-15>APRS,TCPIP*,qAC,T2DENMARK::W4KRL-15 :EQNS.0,0.01,2.5,0,-1,0,1,0,0,0,0.1,0
2018-10-09 15:53:32 EDT: W4KRL-15>APRS,TCPIP*,qAC,T2DENMARK::W4KRL-15 :BITS.Solar Power WX Station

Temporary Station using DIY Solar Shield

It has rained since we arrived at Bethany Beach, Delaware for vacation so it has not been able to test how well the DIY solar radiation shield performs in sunlight. It is hung on a small twig of a tree in the backyard.

ThingSpeak feed: https://thingspeak.com/channels/430753

APRS Feed: https://aprs-fi/W4KRL-13

DIY Instrument Shield

An instrument shelter provides a flow of air around weather sensors while protecting them from direct sunlight and rain. Without a shield, the sun can heat the temperature sensor giving a false high-temperature reading.

Professional weather stations use a large louvered box called a Stevenson Screen that is built to specific dimensions for standardized weather measurements. Smaller plastic shields can cost $100 or more. My station uses an AcuRite Radiation Shield that sometimes sells for as little as $10. Making your own instrument shelter can be an easy and fun project that may cost less than $6.

These instructions will give you a general idea how to build a shield. You can easily adapt the design to the materials you can find. The basic item is a shallow dish with nearly vertical straight sides. Dishes made as plant saucers are an excellent choice. The dishes are stacked on top of each other separated by beads and held together by wire ties. It makes a surprisingly sturdy design.

Here are suggested items. Any reasonable substitute will work if you can’t find these exact items:
• Dotchi 4-in Clear Plastic Plant Saucer – Lowe’s #606062 – $0.34. Buy six and maybe a few spares.
• Valspar White Spray Paint – Lowe’s #282255 – $0.99.
• Nylon Wire Ties – 6 to 11-inches long – Dollar Tree $1.00 or Harbor Freight $2.00
• Plastic or wood craft beads of the “large hole” variety. The hole must be large enough to allow the wire tie to pass through – Dollar Tree or Michael’s

You need to have at least six small saucers with flat bottoms and steep shallow sides. Plant saucers of 4 to 6-inch inch (100 mm – 150 mm) diameter work well. They are very thin so larger saucers are not strong enough. Paint them white with spray paint inside and out.

After the paint is dry, cut a hole in the flat bottom of five of the saucers leaving a ring at least 1/4-inch (6 mm) wide. The sixth uncut saucer will be the top of the shield. The holes should be a little larger than the width of the weather station printed circuit board, say 1.75-inches (45 mm). Cut three small slots wide enough to pass the tail of a wire tie in the flat bottom ring of each saucer. The slots must all line up. One way to do this is to make three equally spaced guide marks on the top saucer with a permanent marker. Place each of the other saucers over the top one and put marks in the same places. You can see the guide marks by holding them up to a light. Cut short slots at all the marks with a craft knife.

Look at how wire ties work: one side of the tail is smooth, the other side has ridges that engage with a tongue in the head of the tie.

Place the tail of a wire tie through each of the slots in the top saucer with the ridged side facing the center of the saucer hole. Put at least two beads over the tails of the wire ties coming out the other side of the saucer and thread them through the holes in the next saucer. Do this for each saucer.

You should now have a loose stack of saucers with three wire ties sticking out the bottom. Place a bead on each of the wire ties, then carefully press the head of another wire tie over each tail. You can tighten these up by hand to hold the stack firmly together. The tails of the new wire ties will cross more-or-less in the center of the bottom saucer. Cut three small slots in the side of the bottom saucer opposite from each wire tie. You can gently pry up the tails and insert the LiPo cell and weather station PCB into the hole with the solar cell outside. Pass the tails through the slots to secure the weather station in the shield.

Mounting the station is up to you. One possibility is to pass wire tie tails through the heads in the top of the shield and fasten them to some convenient bracket. For a few dollars more, buy a set of magnetic hooks like the ones Harbor Freight sells as item # 98502. Before you put your station inside the shield, drop a 1-1/2-inch (40 mm) steel (not stainless) washer into the shield. Place the magnetic hook on the other side so that it attracts the washer.

Solar Radiation Shield under test

The D1M-WX1 and D1S-WX1 solar-powered weather stations heat up in direct sunlight giving an incorrectly high-temperature reading. Early results from testing an Acurite solar radiation shield are very good.

The shield is available from several online vendors. Check around for a good price as the same item sells for $10 to $60 from different vendors.

https://www.acurite.com/solar-radiation-shield.html

I inserted a D1S shield-type station into the Acurite product and hot-melt glued the solar panel to the side of the solar shield. The shield comes apart by carefully prying small plastic tabs between each layer. This makes it easy to run wires into the shield.

On sunny days, the unshielded sensor heats up by more than 20ºC (36ºF) compared to the sensor in the AcuRite shield.

Since product availability is problematic, I am going to look into the possibility of building a DIY solar shield with dollar store components.

Power supply measurements

Most electronic projects operate on low voltage direct current. This is often provided by a USB connection or a battery. There are several possibilities to power a project from the 120 Vac power line. A common way is to use a wall adapter (wall wart) and feed low voltage direct current to your project through a power connector. A good feature of this approach is that the project enclosure is smaller because it does not need to house a power supply.

If you have a project that controls line power, like a Wi-Fi controlled receptacle, it is more convenient to incorporate the power supply in the project enclosure so that you do not need to connect to both the power line and a wall adapter. The solution is to include the guts of a wall adapter into the project enclosure.

Rather than taking apart a wall adapter, you can buy a phone charger kit from eBay vendors. Try searching for “diy training phone charger kit”.

According to the eBay listings, the specification is 5 Vdc at 350 mA to 500 mA with an input of 100-220 V 50//60Hz 0.3 A. This would be adequate for many Arduino and ESP8266 projects, sadly, the supply can barely meet 170 mA.

Next step is to modify the circuit to see if the output can be raised. Short out R7 and play around with R2, R3, and C2.

Di Mini (ESP8266) Wi-Fi Status Codes

The ESP8266 using the Arduino Core reports Wi-Fi status with the WiFi.status() function. The D1M-WX1 Weather Station attempts to connect to Wi-Fi every 250 milliseconds until the “Connect” status is found. If this does not happen after 15 attempts, it prints the status to the Serial Monitor.

Normally, the LED on the ESP8266 flashes five or six times until the connection is made. The LED will be dark during the six or seven seconds it takes to post the weather data on the Internet.  It will flash three times before the station goes to sleep for 10 minutes. The code is in the logonToRouter() function of the weather station sketches.

If a Wi-Fi connection can not be made, the LED will flash 16 times and the exit code will appear on the Serial Monitor. The status codes are:

ESP8266 WiFi Result Codes

CodeMeaningDescription
1No SSID AvailableUnit is too far from the Wi-Fi access point, the SSID and/or password is incorrect, or the SSID is for a 5GHz-band access point.
2Scan CompletedScanning for available networks is completed.
3ConnectedSuccess
4Connection FailedThe opposite of success.
5Connection Lost
6Disconnected
255No ShieldUsed for compatibility with the Arduino WiFi Shield - not relevant to the ESP8266.

Reference: https://www.arduino.cc/en/Reference/WiFiStatus

Sleep Mode for the Wemos D1 Mini

The key to using solar power in an Internet of Things project is to put the microcontroller unit into sleep modeafter the sensors are read and the data posted to your IoT server. The NodeMCU and the Wemos D1 WiFi, a larger profile that mimics the Arduino UNO form, reliably woke from deep sleep. So far, the Adafruit HUZZAH fails to wake after 20 or 30 sleep cycles. I’m still working on the HUZZAH but the letter carrier just brought a package with three Wemos D1 Mini ESP8266 devkits and an OLED display shield.

Would the D1 Mini also work well with deep sleep? Answering that question had to wait until I checked out the OLED display.

The D1 Mini devkits are supplied with both male and female headers. You must install the female headers if you want to use the OLED shield as a plug-in unit. Make sure you install the headers on the correct side so that the shields will stack with correct connections.

Both Adafruit and Sparkfun have Arduino libraries for this display. The Sparkfun library loaded easily and the examples brought up bright, clear images.

The next step was to make a DIY shield to mount BMP180 and HT21D sensors. The BMP180 measures atmospheric pressure and temperature. The HTU21D measures temperature and humidity. [I will post some photos in a week.] It makes a nice compact little cube with the sensor shield stacked between the D1 Mini and the OLED.

Having proven the OLED display, I removed it and set about to put the D1 Mini and sensor shield into deep sleep. There were some problems doing this. When the ESP8266 wakes up, it needs to be reset to restart execution of the firmware. The ESP8266 provides a signal on pin D0 to do this so D0 must be connected to the Reset pin. However, this connection sometimes interferes with putting the ESP8288 into programming mode and must be opened to reprogram the chip.

I also ran into some problems when power was removed while the chip was in deep sleep.

After getting the chip to sleep and wake up I ran some current tests. The D1 Mini drew an average of 80mA when awake and 2.4mA when asleep. The D1 Mini has a small red LED that lights when the board is powered. I pulled the LED off the board with a pair of pliers. That dropped the sleep current down to 70µA.  This makes the D1 Mini look like a very good choice for solar powered IoT projects.

External sensors for weather station

Recently the local Institute of Electrical and Electronic Engineers (IEEE) held two “hands-on” sessions to build an Internet of Things (IoT) weather station. We built it on a breadboard so that the attendees would not have to solder wires. Several of the participants have kept their stations on-line and two have placed the unit outdoors.

Finding a suitable outdoor enclosure for the sensors is difficult. There are several available for relatively large sensors with costs from $30 to hundreds. Fortunately, a scrounging mission to Walmart turned up a nice little box for 88¢!

The box is 88 x 60 x 50mm (3.25 x 2.38 x 2.00-in). The box is transparent and has a lid the fits over the sides of the box. There is some play between the lid and the box so it allows some air movement. This can be “enhanced” by drilling four 6mm (1/4-inch) holes in the bottom of the box.

The box neatly holds a BME280 barometric pressure/temperature/humidity sensor and a BH1750 light intensity sensor. A piece of stripboard mounts the sensor breakout boards. Since the sensors are I2C devices they need four wires (two I2C and 2 power) to connect to the “control unit”. I cut the ends off a flat USB cable from the dollar store. It requires a lot of careful patience to solder the very small wires to the stripboard. I am looking for a better method.

My box is mounted to a second-floor window frame with clear packing tape. The window is above a small shingled roof. This is not an ideal location because solar heating of the roof probably affects the temperature reading. In practice, the temperature accurately tracks the local WeatherBug station reading.

The unit has been outside since 10 November 2016. It has been through several rain storms with no problems.

Outdoor Unit in rain

Load more