Main Content

Raspberry Pi LED Control

This example shows you how to control Raspberry Pi™ LED from an Android™ device.

Introduction

Raspberry Pi is a popular, low-cost, credit card sized single-board computer. You can create compelling projects by using an Android device as a front end to control hardware and peripherals attached to a Raspberry Pi board wirelessly. In this example, you will configure and run two Simulink models onto Raspberry Pi hardware and Android device respectively. You will be able to control the LED on/off on Raspberry Pi board from Android device.

You will learn how to:

  • Set up network connection between Raspberry Pi hardware and Android device

  • Configure and run a Simulink model for Raspberry Pi hardware to receive UDP packets from Android device

  • Configure and run a Simulink model for Android device to send UDP packets to Raspberry Pi hardware

Prerequisites

Required Hardware

  • Raspberry Pi board

  • Android phone or tablet

Task 1 - Install Simulink Support Package for Raspberry Pi Hardware

You needSimulink Support Package for Raspberry Pi Hardwareto run Simulink model on Raspberry Pi board.

1.Download and Install Simulink Support Package for Raspberry Pi Hardware

2.(Recommended) Complete theGetting Started With RaspberryPi Hardwareexample inSimulink Support Package for Raspberry Pi Hardwareyou just installed.

Task 2 - Configure Network Connection

In this task, you will set up network connection between Raspberry Pi board and Android device. The communication protocol used in this example is UDP.

1.Connect Raspberry Pi board to the network with Ethernet cable through Ethernet port.

2.Connect Android device to the same network through Wi-Fi. CheckSettings->Wi-Fi->[Wi-Fi network connected]找到你的IP地址Android device.

3.Verify the connection between your Raspberry Pi board and Android device.

Execute the following command on the MATLAB command prompt:

r = raspberrypi

This command returns an object with IP address info for the Raspberry Pi board.

Run command系统(r, sudo萍(Android_IP_Address) 10 - c)with the Android device IP address found in step 2 to verify the connection. e.g.

system(r,'sudo ping 172.31.205.40 -c 10')

Task 3 - Run Simulink Models on Raspberry Pi Board and Android Device

1.Open preconfiguredRaspberry Pi Modeland configure it with the IP address of your Raspberry Pi board. %%%2.On theHardwaretab of the Simulink model, in theModesection, selectRun on boardand then clickBuild, Deploy & Startto build and run this model on Raspberry Pi board.

3.Open preconfiguredAndroid Model. Double-click on theUDP Sendblock. Open the block mask and enter the IP address of your Raspberry Pi board in theRemote IP addressedit box. ClickOKto save and close the block mask. %%%4.On theHardwaretab of the Simulink model, in theModesection, selectRun on boardand then clickBuild, Deploy & Startto build and run this model on your Android device. An Android app will run on your Android device.

5.Once the Android app runs, press the switch button in the Android app and observe the LED on/off.