Skip to content

Instantly share code, notes, and snippets.

@microbit-carlos
Last active June 28, 2022 18:43
Show Gist options
  • Save microbit-carlos/baebc389bccc4366abf3701acc7fc61c to your computer and use it in GitHub Desktop.
Save microbit-carlos/baebc389bccc4366abf3701acc7fc61c to your computer and use it in GitHub Desktop.
micro:bit MicroPython change ADC reference voltage
from microbit import pin0
NRF_ADC_BASE = 0x40007000
NRF_ADC_CONFIG = NRF_ADC_BASE + 0x504
@micropython.asm_thumb
def reg_read(r0):
ldr(r0, [r0, 0])
@micropython.asm_thumb
def reg_bit_clear(r0, r1):
ldr(r2, [r0, 0])
bic(r2, r1)
str(r2, [r0, 0])
# Check ADC Config register default
print("ADC config should be 0x18: {:#010x}".format(reg_read(NRF_ADC_CONFIG)))
# mbed configured: 10bit, input 1/3, ref 1/3 vdd, enable ADC in AIN4-P0.03-pin0
pin0.read_analog()
print("ADC config should be 0x106A: {:#010x}".format(reg_read(NRF_ADC_CONFIG)))
# Set ref voltage to internal ref 1.2V (band gap), set bits 5-6 to 0x00
reg_bit_clear(NRF_ADC_CONFIG, 0x60)
print("ADC config should be 0x100A: {:#010x}".format(reg_read(NRF_ADC_CONFIG)))
@keble6
Copy link

keble6 commented Jun 28, 2022

I have the same problem as davek61 - i.e. "When I use it to monitor the battery voltage I always get 1023 no matter what the battery voltage is". I don't understand why.
I am using a V1 microbit and I have tested with other code e.g. https://meanderingpi.com/2017/08/05/microbit-analog-inputs/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment