Using ADC with the ESP8266

The ESP8266 family of boards still amaze me with their size and cost-effectiveness, but I will confess; the dearth of official information for some of the more esoteric features can be annoying.

For my remote garage-door sensor, I wanted to determine the angle of the sensor using a 3-axis accelerometer. The docs don’t really tell you much, but they do mention that the ADC pin can be used to measure the battery voltage or an external voltage, but you have to choose which one. I didn’t see this at first, having googled elsewhere and found an example, but from googling, I did learn that the external voltage you’re measuring has to be between 0 and 1 volts. I think you may be safe going up to 3.3, but it will read a value of 1024 for everything over 1V.

OK, no problem – I added a couple resistors as a voltage divider (see post on garage door sensor for schematic) and I started getting reasonable values instead of nothing but 1024.

Now comes the weird part.

After  putting everything together more permanently for “field testing”, I was suddenly getting nothing but 65535! So, more googling and I come across posts like this one describing how you have to define the correct app/include/user_config.h before building your firmware (guess I should have read the docs :-)). Well, I enjoy hacking with these suckers, but my enjoyment ends with building the firmware using build chains. Fortunately, there’s an online tool for building custom firmwares. This is what I had done, since I needed MQTT support. Otherwise, I just stick with downloading the latest pre-built firmware. The posts mention modifying the byte at position 107 in the esp_init_data.bin that is generated from the build as another option – presumably this is generated separately by the build tool.

So, apparently, the firmware I’m using is set to report the battery voltage. I changed my read(0)’s to readvdd33() and started getting values that varied slightly. This is weird, because I SWEAR it worked – using the same firmware – when I had it bread-boarded, but it WAS a different chip, so I guess somehow that value was set differently, despite using the same ROM image. I had generated the ROM using the online tool, so I’m guessing the generated ROM does not extend into, or include, this chunk of init-data space. I got the pre-built esp_init_data_default.bin from the source repository and used a hex editor to change the byte at position 107 to (hex) 0x33 (I had to search around to figure out the value, too – leave at zero for battery sensing). You can grab my pre-edited file from here.

Lastly, there was some ambiguity around where to flash this to. If you have a 512KB Flash space, you flash it to 0x7C00. I’m using an ESP-12 and this didn’t work. Turns out ESP-12’s have a 4MB flash. For those, it needs to be flashed to 0x3FC000. I just used the ESP8266Flasher.exe tool to flash the file to the correct location:


After flashing the file to that location, my 0-to-1024 external values were back :-).

This entry was posted in Technical. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s