Instantly share code, notes, and snippets.

Embed
What would you like to do?
Raspberry Pi - detect button press
// button.c
// gcc -o button button.c -l bcm2835
// run as root
// sudo ./button
#include <bcm2835.h>
#include <stdio.h>
// Input on RPi pin GPIO 15
#define PIN RPI_GPIO_P1_15
int main(int argc, char **argv)
{
if (!bcm2835_init())
return 1;
// Set PIN to be an input
bcm2835_gpio_fsel(PIN, BCM2835_GPIO_FSEL_INPT);
// add a pullup
bcm2835_gpio_set_pud(PIN, BCM2835_GPIO_PUD_UP);
// and enble low
bcm2835_gpio_len(PIN);
while (1)
{
if (bcm2835_gpio_eds(PIN))
{
// Now clear the eds flag by setting it to 1
bcm2835_gpio_set_eds(PIN);
printf("Button Pressed!\n");
}
delay(500);
}
bcm2835_close();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment