rp2040tinygo

How to get buttons working on Badger2040 in Tinygo


I've been trying to access the Badger2040 buttons through Tinygo and not having any luck (I have succeeded in CircuitPython before).

When I try to change the led state based on Button A, the led is switched on and never switches off:

package main

import (
    "machine"
    "time"
)

func main() {
    led := machine.LED
    led.Configure(machine.PinConfig{Mode: machine.PinOutput})

    button_a := machine.BUTTON_A
    button_a.Configure(machine.PinConfig{Mode: machine.PinInputPullup})

    for {
        led.Set(button_a.Get())
        time.Sleep(time.Second / 4)
    }
}

If I change the led.Set to pass in !button_a.Get() then the led is always off.

It appears that button_a.Get() is always returning true.

Does anyone have any ideas please?


Solution

  • I based this on the included TinyGo examples (not for the Badger2040) - which used PinInputPullup - but it turns out the Badger2040 needs to use PinInputPulldown, i.e. configure the button like this:

        button_a.Configure(machine.PinConfig{Mode: machine.PinInputPulldown})
    

    Note that this works for the up/down/a/b/c buttons. The user button also toggles, but is inverted, i.e. returns true when not pressed.