Main Content


Turn LED on or off

Add-On Required: This feature requires the MATLAB Support Package for Raspberry Pi Hardware add-on.



writeLED(mypi,led,value) overrides the default behavior of the LED and turns it on or off.


Control the On-Board LED

You can locate and control the on-board LED, turning it on and off.

Create a connection from the MATLAB® software to the Raspberry Pi® board.

mypi = raspi
mypi = 

  Raspi with Properties:

           DeviceAddress: 'raspberrypi-hysdu8X38o'
                    Port: 18725
               BoardName: 'Raspberry Pi Model B Rev 2'
           AvailableLEDs: {'led0'}
    AvailableDigitalPins: [4 14 15 17 18 22 23 24 25 27 30 31]
    AvailableSPIChannels: {}
      AvailableI2CBuses: {'i2c-0'  'i2c-1'}
             I2CBusSpeed: 100000

  Supported peripherals

The AvailableLEDs property shows the name of the user-controllable LED.

Show the location of the user-controllable LED on the board.


Turn on the specified LED by setting its value to 1 or true.


Turn off the LED by setting its value to 0 or false.


Restarting the Raspberry Pi hardware returns the LED to its previous function as an activity indicator.

Flash the LED in Response to an Input

You can flash the LED in response to an input signal on one of the GPIO pins.

For example, you can use a button and a resistor in series to connect one of the +3.3 V outputs to GPIO 23. When you press the button, readDigitalPin reads the positive voltage, if buttonPressed becomes true, and the program flashes the LED 10 times.

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

Input Arguments

collapse all

Connection to the Raspberry Pi hardware board, specified as a raspi object.

LED name, specified as a string.

To get the name and location of user-controllable LEDs, use showLEDs.

Example: 'led0'

Data Types: char

LED off or on, specified as a logical value.

Example: 0

Data Types: logical

Extended Capabilities