Reading VCC without external components

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
User avatar
devnull
Posts: 473
Joined: Sat Jan 07, 2017 1:52 am
Location: Singapore / Cornwall
Contact:

Reading VCC without external components

Post by devnull » Thu Feb 23, 2017 5:51 am

The Board is ESP-12S.

Is the method shown here the recommended method of setting the registers to allow reading the A/D value of the VCC pin, or has this now been integrated into the firmware ??

https://github.com/micropython/micropython/issues/2352

Code: Select all

import esp
from flashbdev import bdev
import machine

ADC_MODE_VCC = 255
ADC_MODE_ADC = 0

def set_adc_mode(mode):
    sector_size = bdev.SEC_SIZE
    flash_size = esp.flash_size() # device dependent
    init_sector = int(flash_size / sector_size - 4)
    data = bytearray(esp.flash_read(init_sector * sector_size, sector_size))
    if data[107] == mode:
        return  # flash is already correct; nothing to do
    else:
        data[107] = mode  # re-write flash
        esp.flash_erase(init_sector)
        esp.flash_write(init_sector * sector_size, data)
        print("ADC mode changed in flash; restart to use it!")
        return
But the value I am getting is not between 0 and 1023:

Code: Select all

>>> import machine
>>> vcc = machine.ADC(1)
>>> vcc.read()
2973
>>> 
Is this value actuall 2.973 volts or ???
Last edited by devnull on Thu Feb 23, 2017 9:57 am, edited 5 times in total.

shaoziyang
Posts: 363
Joined: Sun Apr 17, 2016 1:55 pm

Re: Reading VCC without external components

Post by shaoziyang » Thu Feb 23, 2017 8:49 am

ESP8266's ADC is 10-bit, so value is 0-1023.

you may read it as ADC(0).read()

User avatar
devnull
Posts: 473
Joined: Sat Jan 07, 2017 1:52 am
Location: Singapore / Cornwall
Contact:

Re: Reading VCC without external components

Post by devnull » Thu Feb 23, 2017 9:16 am

Thanks, but that just results in 65535

>>> from machine import ADC
>>> ADC(0).read()
65535
>>>

This link address specifically mentions reading channel #1 of the ADC ?

https://github.com/micropython/micropython/issues/2352

shaoziyang
Posts: 363
Joined: Sun Apr 17, 2016 1:55 pm

Re: Reading VCC without external components

Post by shaoziyang » Thu Feb 23, 2017 9:33 am

This may cause by your board.

In my board, ADC(0).read() is work, and ADC(1).read() return 65535

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: Reading VCC without external components

Post by deshipu » Thu Feb 23, 2017 12:07 pm

I think that ADC(0) is the external A0 pin, and its range is 0-1024 (0-1V), and ADC(1) is the internal VCC measurement. Since you can only use one or the other of them, depending on what ADC mode is set, the other one will return 65535.

User avatar
devnull
Posts: 473
Joined: Sat Jan 07, 2017 1:52 am
Location: Singapore / Cornwall
Contact:

Re: Reading VCC without external components

Post by devnull » Thu Feb 23, 2017 12:38 pm

Thanks, but the reading I am getting looks like a millivolt value and not a 0 - 1023 value !

Code: Select all

>>> import machine
>>> vcc = machine.ADC(1)
>>> vcc.read()
2973
Update

I have verified that if I change the supply voltage, this reading also changes, however comparing the VCC pin with a voltmeter provides a value that is a few hundred millivolts different than the one provided by vcc.read()

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: Reading VCC without external components

Post by deshipu » Thu Feb 23, 2017 12:51 pm

The ADC(1) reading is in 1/1024s of volt, see the docs you linked to in the first post: https://github.com/esp8266/Arduino/issu ... -113099775

User avatar
devnull
Posts: 473
Joined: Sat Jan 07, 2017 1:52 am
Location: Singapore / Cornwall
Contact:

Re: Reading VCC without external components

Post by devnull » Thu Feb 23, 2017 12:54 pm

Ahh, sorrie, I missed that at the beginning of the post.

So 2973 = 3277.4 mv

i.e. ADC(0).read() * 1.024

Post Reply