Main Content

Flash BeagleBone Black LED in Response to Input

This example shows how to flash the onboard LED when you press a button that is connected to a GPIO pin.

Caution

Excessive voltage and current can damage the BeagleBone® Black hardware. Observe the manufacturer precautions for handling the BeagleBone Black hardware and connecting it to other devices. For more information, see the local copy of the BeagleBone drivers and documentation in the BeagleBone Black Getting Started folder on your host computer, or Getting Started with BeagleBone Black.

  1. Using a breadboard, set up the following circuit:

    • Connect one of the +3.3V pins on the GPIO header to a button.

    • Connect pin 23 on the GPIO header to a 220 or 330 Ω resistor.

    • Connect the unconnected ends of the button and resistor to each other.

  2. Use the showLEDs function to show an illustration that identifies each pin.

    The figure shows the output of a call to showLEDs.

  3. Run the code and press the button. The button closes the circuit between the +3.3V pin and pin 23. When readDigitalPin detects the raised voltage, buttonPressed becomes true, and writeLED toggles the LED on and off ten times.

    for ii = 1:100
        buttonPressed = readDigitalPin(bbb,'P9_12')
        if buttonPressed
            for jj = 1:10
                writeLED(bbb,'usr0',1)
                pause(0.05)
                writeLED(bbb,'usr0',0)
                pause(0.05)
            end
        end
        pause(0.1)
    end

To restore LED 'usr0' to its default, which is to indicate Linux® kernel activity, restart the BeagleBone Black hardware.