Blink LED when a button is pressed - GPIO/LED/Button Example
#!/bin/bash
# Define GPIO pin numbers
BUTTON_PIN=136
LED_PIN=137
# Set up GPIO pins
echo "$BUTTON_PIN" > /sys/class/gpio/export
echo "$LED_PIN" > /sys/class/gpio/export
echo "in" > "/sys/class/gpio/gpio$BUTTON_PIN/direction"
echo "out" > "/sys/class/gpio/gpio$LED_PIN/direction"
# Function to turn on the LED
turn_on_led() {
echo 1 > "/sys/class/gpio/gpio$LED_PIN/value"
}
# Function to turn off the LED
turn_off_led() {
echo 0 > "/sys/class/gpio/gpio$LED_PIN/value"
}
# Main loop
while true; do
# Check the state of the button
button_state=$(cat "/sys/class/gpio/gpio$BUTTON_PIN/value")
# If the button is pressed (value is 0), turn on the LED
if [ "$button_state" -eq 0 ]; then
turn_on_led
else
turn_off_led
fi
# Sleep for a short duration to avoid high CPU usage
sleep 0.1
doneLast updated