-
On a previous project, I used CircuitPython with a Pico W, and I was able to read the internal voltage without any special hardware setup. For my new project, I'm using MicroPython with an ESP32. Is there anyway to do something similar in MicroPython? For example, this is how I did it in CircuitPython: import analogio
from board import VOLTAGE_MONITOR
def get_vsys():
try:
global vsys_voltage
pin = analogio.AnalogIn(VOLTAGE_MONITOR)
adc_reading = pin.value
adc_voltage = (adc_reading * pin.reference_voltage) / 65535
vsys_voltage = adc_voltage * 3
pin.deinit()
except Exception as e:
print("get_vsys problem: ", e)
get_vsys() |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 5 replies
-
That's because the PICOs have the ›special hardware‹ already on board. |
Beta Was this translation helpful? Give feedback.
-
It depends on which ESP32 board you have. For instance, some of the XIAO boards as well as the Unexpected Maker boards provide a resistive voltage divider that can be used to measure the battery voltage (vs. the 3.3V supply voltage, which is generally less useful). |
Beta Was this translation helpful? Give feedback.
-
This won't help you read your board's internal voltage, but it can help you test the ESP32's ADC and DAC. Here's a simple script you can try: from machine import Pin, ADC, DAC
import time
# DAC output on GPIO25
dac1 = DAC(Pin(25)) # DAC channel 1 on GPIO25, Channel 2 on GPIO26
# ADC input on GPIO36 (32-39)
adc = ADC(Pin(34))
adc.atten(ADC.ATTN_11DB) # Full 0-3.3V range
w = 0 # 0-255:0-3.3v - use to set DAC
while True:
w += 1
if w>255: w = 0
dac1.write(w)
v = adc.read() # 0-4095
print(f"ADC: {v}, DAC: {w}, VOLT: {(v/4095)*3.3}")
time.sleep(0.4) Use a jumper wire to connect pins 25 and 34, then run the script. You will get something like this:
|
Beta Was this translation helpful? Give feedback.
-
The 6% is chip to chip variation. If you calibrate , ie changing # read remote battery voltage
while count < 8:
battery_ADC_reading = adc1_ch4.read()
result = result + battery_ADC_reading
time.sleep_ms(10)
count += 1
battery = (result * 3) / 1675 / 8 # 8 samples
battery = round(battery, 2)
count = 0
result = 0 |
Beta Was this translation helpful? Give feedback.
-
I've had a couple of goes at calibrating esp32s/rpi picos using auto generated lookup tables. One thing to be aware of is that the max 4095 count with 11 dB attn rarely occurs at the nominal 3v3 volts, the batch of boards I was working with it was closer to 3v1. |
Beta Was this translation helpful? Give feedback.
It depends on which ESP32 board you have. For instance, some of the XIAO boards as well as the Unexpected Maker boards provide a resistive voltage divider that can be used to measure the battery voltage (vs. the 3.3V supply voltage, which is generally less useful).