Skip to content

Instantly share code, notes, and snippets.

@microbit-carlos
Created August 8, 2022 17:17
Show Gist options
  • Save microbit-carlos/1f256de4af49eaec058f033ef8d2669b to your computer and use it in GitHub Desktop.
Save microbit-carlos/1f256de4af49eaec058f033ef8d2669b to your computer and use it in GitHub Desktop.
micro:bit V2 MicroPython configure buttons as outputs
from microbit import *
from micropython import const
from machine import mem32
REG_GPIO_P0 = const(0x50000000) # Base address for the GPIO 0 peripheal
REG_GPIO_OUTSET = const(REG_GPIO_P0 + 0x508) # When a 32bit value is written to this address, each bit set to 1 sets the output pin high
REG_GPIO_OUTCLR = const(REG_GPIO_P0 + 0x50C) # When a 32bit value is written to this address, each bit set to 1 sets the output pin low
REG_GPIO_DIRSET = const(REG_GPIO_P0 + 0x518) # When a 32bit value is written to this address, each bit set to 1 sets the pin to output
REG_GPIO_DIRCLR = const(REG_GPIO_P0 + 0x51C) # When a 32bit value is written to this address, each bit set to 1 sets the pin to input
BUTON_A_PIN = const(14) # Button A is pin 5 in the edge connector -> P0_14 in the nRF52833
BUTON_B_PIN = const(23) # Button B is pin 5 in the edge connector -> P0_23 in the nRF52833
# First we set Button A to be an output
mem32[REG_GPIO_DIRSET] = 1 << BUTON_A_PIN
def set_button_a_low():
mem32[REG_GPIO_OUTCLR] = 1 << BUTON_A_PIN
def set_button_a_high():
mem32[REG_GPIO_OUTSET] = 1 << BUTON_A_PIN
def restore_button_a_as_input():
# If we want to use button_a.is_pressed() again we need to set it as input
mem32[REG_GPIO_DIRCLR] = 1 << BUTON_A_PIN
while True:
display.show(Image.ARROW_S)
set_button_a_low()
sleep(5000)
display.show(Image.ARROW_N)
set_button_a_high()
sleep(5000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment