Go back to the examples list

Reading a button

Reading the state of a button is as simple as turning an LED on, but in the other direction : use GPIO::enableInput() instead of GPIO::enableOutput() and GPIO::get() instead of GPIO::set().

When enabling a pin in input mode, an optional pulling option can be selected, as explained in the GPIO module reference. It is common for a button to be connected in such a way that it will connect the pin to GND when pressed, and keep the line open otherwise. It it therefore necessary to enable a pullup (pulling resistor to Vcc) in order to raise the line to a high level when the button is not pressed. For this reason, GPIO::get(PIN_BUTTON) will return true by default, and false when the button is pressed : this is called negative logic.

The example below is really simple : it turns the red LED on or off every time the button is pressed. Two versions of the same program are presented : a version using the helper module specifically for the Carbide board, and a generic version using only the main library functions which can be used on any board.

Carbide

button.cpp [download]
wget https://libtungsten.io/static/code/examples/button_carbide/button.cpp

#include <carbide.h>

int main() {
    // Init the board, including defining LED and button pins
    Carbide::init(true);

    // Keep a trace of the current LED state
    bool ledState = false;

    while (true) {
        // Check if the button has been pressed
        if (Carbide::buttonRisingEdge()) {
            // Invert and set the LED state
            ledState = !ledState;
            Carbide::setLedR(ledState);
        }
    }
}

Makefile [download]
wget https://libtungsten.io/static/code/examples/button_carbide/Makefile

NAME=button

BOOTLOADER=true
CARBIDE=true

# Available modules : adc dac eic gloc i2c spi tc trng usart
# Some modules such as gpio and flash are already compiled by default
# and must not be added here.
MODULES=

# The toolchain's bin/ path, don't forget to customize it
# If this directory is already in your PATH, comment this line.
TOOLCHAIN_PATH=/opt/arm-none-eabi/bin/

# Include the main lib makefile
include libtungsten/Makefile

Generic

button.cpp [download]
wget https://libtungsten.io/static/code/examples/button/button.cpp

#include <core.h>
#include <gpio.h>

int main() {
    // Init the microcontroller
    Core::init();

    // Define the pin on which the button is connected
    const GPIO::Pin PIN_BUTTON = GPIO::PA04;

    // Enable this pin in input
    GPIO::enableInput(PIN_BUTTON, GPIO::Pulling::PULLUP);

    // Define the pin on which the LED is connected
    // On Carbide, this is the red LED
    const GPIO::Pin PIN_LED = GPIO::PA00;

    // Enable this pin in output to a HIGH state
    GPIO::enableOutput(PIN_LED, GPIO::HIGH);

    // Keep a trace of the current LED state
    bool ledState = false;

    while (true) {
        // Check if the button has been pressed (negative logic, hence fallingEdge())
        if (GPIO::fallingEdge(PIN_BUTTON)) {
            // Invert and set the LED state
            ledState = !ledState;
            GPIO::set(PIN_LED, !ledState); // Negative logic
        }
    }
}

Makefile [download]
wget https://libtungsten.io/static/code/examples/button/Makefile

NAME=button

BOOTLOADER=true

# Available modules : adc dac eic gloc i2c spi tc trng usart
# Some modules such as gpio and flash are already compiled by default
# and must not be added here.
MODULES=

# The toolchain's bin/ path, don't forget to customize it
# If this directory is already in your PATH, comment this line.
TOOLCHAIN_PATH=/opt/arm-none-eabi/bin/

# Include the main lib makefile
include libtungsten/Makefile