Simvik

Read an active-low button

A button wired to ground reads 0 when pressed. Report it as pressed. An easy embedded C problem (GPIO), run and graded in the browser.

easy · GPIO · gpio · input · active-low

A button connects a pin to ground when it is pressed, and an internal pull-up holds the pin high otherwise, so the input is active low: the pin reads 0 while the button is pressed.

Write button_pressed(pins, pin), which returns true when the button on pin is pressed, given the input register value pins.

Starter code

bool button_pressed(uint8_t pins, uint8_t pin) {
  return (pins >> pin) & 1u;
}

Example tests

Pin low means pressed

show(button_pressed(0xFB, 2));

Expected output:

pressed

Pin high means released

show(button_pressed(0xFF, 2));

Expected output:

released

Learn it first

  • GPIO · Pin modes, push-pull versus open-drain, pulls, edge interrupts and debouncing.

More GPIO problems