This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Flash the Raspberry Pi LED in Response to an Input

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


Excessive voltage and current can damage the Raspberry Pi™ hardware. Observe the manufacturer’s precautions for handling the Raspberry Pi hardware and connecting it to other devices. For more information, see

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 Ohm resistor.

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


Use showLEDs to show an illustration that identifies each pin.

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, if buttonPressed becomes true, and writeLED toggles the LED on and off ten times.

for ii = 1:100
    buttonPressed = readDigitalPin(mypi,23)
    if buttonPressed
        for jj = 1:10