Raspberry pi to matlab

raspi

Connection to Raspberry Pi board

Description

This object represents a connection from the MATLAB ® software to the Raspberry Pi™ board. To interact with the Raspberry Pi board, use this object with the functions listed in Object Functions.

Creation

Syntax

Description

mypi = raspi creates a connection, mypi , from the MATLAB software to the Raspberry Pi board. Use this syntax to connect or reconnect to the same board.

You do not need to supply the user IP address, user name, and password to create a connection. The raspi object reuses these settings from the most recent successful connection to a Raspberry Pi board. These settings must be provided for the first connection created during the setup process.

In MATLAB Online™ , the raspi object reuses the settings from the most recent successful connection. If connecting for the first time, this syntax is used to create a connection to the first Raspberry Pi board listed by raspilist with status «Ready to connect» .

After connecting to the board, you can use mypi to interact with the Raspberry Pi board and peripheral devices.

To close the connection, use clear to remove mypi and any other connections that use mypi .

mypi = raspi( ipaddress , username , password ) overrides the IP address, user name, and password from the previous connection. Use this syntax to connect to a board whose settings are different from the previous successful connection. After changing the password on a board, use this syntax. Or, after connecting from the MATLAB software to a second Raspberry Pi board, use this syntax. You can use this syntax without username and address if a successful connection has been previously created with this syntax. This syntax is not supported in MATLAB Online .

Note

The firmware has a default user name, ‘pi’ , and password, ‘raspberry’ . The pi user name has sudo root powers at the command line. It is a good security practice to change the default password to a strong password.

mypi = raspi( hostname , username , password ) uses the host name instead of the IP address to make a connection to a Raspberry Pi. Use this syntax to connect a board whose settings are different from the previous successful connection or to connect to a second board. You can use this syntax without username and address if a successful connection has been previously created with this syntax. This syntax is not supported in MATLAB Online .

mypi = raspi( name ) creates a connection to a Raspberry Pi board in MATLAB Online using the name assigned to the board during the setup steps described in Connect to Raspberry Pi Hardware Board in MATLAB Online.

Note

This syntax is supported only in MATLAB Online .

mypi = raspi( serialnumber ) creates a connection to a Raspberry Pi board in MATLAB Online using its serial number. Identify the serial number using raspilist .

Note

This syntax is supported only in MATLAB Online .

mypi = raspi( ___ , «Timeout» ,time) creates a connection to a Raspberry Pi board in MATLAB Online , and waits for up to time seconds for the connection to complete. The value of time must be greater than the default connection timeout period of 12.5 seconds. You can use this syntax to specify a larger timeout value if you are unable to connect to your board.

Use this syntax with any of the input argument combinations in previous syntaxes.

Note

This syntax is supported only in MATLAB Online .

Источник

Raspberry pi to matlab

Add support for Raspberry Pi™ hardware to the MATLAB ® product by installing the MATLAB Support Package for Raspberry Pi Hardware.

This process installs the following items on your host computer:

Third-party software development tools

This process also installs a customized version of Raspbian Linux ® on the Raspberry Pi hardware.

When you complete this installation, you can use MATLAB commands to control and retrieve data from Raspberry Pi hardware and peripherals.

The Raspberry Pi hardware is also referred to as a board or as target hardware.

Raspberry Pi Operating System Requirements

You can set up your Raspberry Pi hardware using either the MathWorks ® Raspbian image or by customizing the Raspbian Linux operating system on your Raspberry Pi, namely Stretch or Buster. The MathWorks Raspbian image is composed of the latest Raspbian image with all the libraries and packages required to make the image compatible with MATLAB and Simulink ® .

If you customize the operating system running on your Raspberry Pi, the custom image should be composed of all the libraries and packages required to make the image compatible with MATLAB and Simulink. If you choose to customize your Raspberry Pi OS, the Review Required Packages and Libraries screen on the Raspberry Pi Hardware Setup window displays a complete list of libraries and packages that you need to install on your Raspberry Pi hardware.

Install, Update, or Uninstall Support Package

Install Support Package

On the MATLAB Home tab, in the Environment section, select Add-Ons > Get Hardware Support Packages.

In the Add-On Explorer window, click the support package and then click Install.

Update Support Package

On the MATLAB Home tab, in the Resources section, select Help > Check for Updates.

Uninstall Support Package

On the MATLAB Home tab, in the Environment section, click Add-Ons > Manage Add-Ons.

In the Add-On Manager window, find and click the support package, and then click Uninstall.

Hardware Setup

Hardware boards and devices supported by MathWorks require additional configuration and setup steps to connect to MATLAB and Simulink. Each support package provides a hardware setup process that guides you through registering, configuring, and connecting to your hardware board.

If the support package is already installed, you can start the hardware setup by opening the Add-On Manager.

In the Add-On Manager, start the hardware setup process by clicking the Setup button, .

After starting, the Hardware Setup window provides instructions for configuring the support package to work with your hardware.

Follow the instructions on each page of the Hardware Setup window. When the hardware setup process completes, you can open the examples to get familiar with the product and its features.

MATLAB Command

You clicked a link that corresponds to this MATLAB command:

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Источник

MATLAB Support Package for Raspberry Pi Hardware

MATLAB ® Support Package for Raspberry Pi™ Hardware enables you to communicate with a Raspberry Pi remotely from a computer running MATLAB or through a web browser with MATLAB Online™ . You can acquire data from sensors and imaging devices connected to the Raspberry Pi and process them in MATLAB. You can also communicate with other hardware through the GPIO, serial, I2C, and SPI pins.

The support package functionality is extended if you have MATLAB Coder™ . With MATLAB Coder , you can take the same MATLAB code used to interactively control the Raspberry Pi from your computer and deploy it directly to the Raspberry Pi to run as a standalone executable.

Installation and Setup

Install support for the hardware, update the firmware, and connect to the hardware

Connection to Raspberry Pi Hardware

Create a connection to Raspberry Pi hardware

Run on Target Hardware

Deploy a MATLAB function as a standalone executable on the hardware

Use the Raspberry Pi’s LED

GPIO Pins

Use the Raspberry Pi’s GPIO pins

Serial Port

Use the Raspberry Pi’s serial port

I2C Interface

Use the Raspberry Pi’s I2C interface

SPI Interface

Use the Raspberry Pi’s SPI interface

CAN Interface

Read and write CAN messages to CAN shields on Raspberry Pi hardware

Camera Board

Use the Raspberry Pi’s add-on Camera Board

Sense HAT

Use the Raspberry Pi’s add-on Sense HAT Board

Web Camera

Use the Raspberry Pi web camera

Pulse Width Modulation

Use the Raspberry Pi to create pulse-width-modulation (PWM) signals on GPIO pins

Servo

Write to servo motors connected to the Raspberry Pi hardware

Linux

Use the Linux ® command shell and manage files on Raspberry Pi hardware

Display

Use the Raspberry Pi’s display

Audio

Use the Raspberry Pi audio player and capture

Источник

Working with Raspberry Pi Hardware

This example shows you basics of working with Raspberry Pi™ Hardware.

Introduction

Raspberry Pi is a single board, credit-card size computer that can run Linux®. Raspberry Pi hardware has low-level interfaces intended to connect directly with external devices such as A/D converters, sensors, motor drivers, etc. You can take advantage of these low-level interfaces to develop meaningful real-world applications. The Raspberry Pi support package includes MATLAB® command-line interfaces to communicate with external devices connected to Raspberry Pi hardware. You can, for example, turn a LED connected to one of the GPIO pins on or off or sense the position of a push button from the MATLAB command prompt.

Most of the low-level interfaces of Raspberry Pi hardware are not plug-and-play. To use these low-level interfaces, you must have a sound understanding of basic electrical concepts. If you mis-wire a GPIO pin, for example, you risk losing a GPIO pin, and, in some cases, your Raspberry Pi hardware.

This example is intended to familiarize you with low level interfaces of the Raspberry Pi hardware, establish sound practices for wiring and connections when working with external electrical components and use MATLAB command-line interface for Raspberry Pi hardware to control simple devices like LEDs, push buttons, and relays.

Prerequisites

Required Hardware

To run this example, you need the following hardware:

Raspberry Pi hardware

A power supply with at least 1A output

Breadboard and jumper cables

330 Ohm, 1 kOhm and 10 kOhm resistors

A push button or switch

Overview of Raspberry Pi Hardware

In addition to USB, Ethernet, HDMI, and Audio Out ports, the Raspberry Pi hardware has expansion headers that offer general purpose digital I/O, SPI, I2C, and serial UART. Depending on the revision of your Raspberry Pi hardware, the available digital I/O pins, pin-outs for I2C, SPI, and serial ports change.

All versions of the Raspberry Pi hardware have one or more expansion headers. You can use some of the pins on the expansion headers for digital I/O. To see a diagram of the pins of your Raspberry Pi hardware, use showPins() method of raspi object.

The showPins() method displays a MATLAB figure showing a diagram of pins available. The raspi object determines the version number of your Raspberry Pi hardware and displays the correct pin diagram for your version of the board. Raspberry Pi Model B+ board has more I/O pins exposed than Raspberry Pi Model B board.

The GPIO pins use 3.3 volt logic levels and are not 5 volt tolerant. There is no over-voltage protection on the CPU chip. A CSI (camera serial interface) is available so that you can connect an available Camera Board. You can use some of the pins available on the Raspberry Pi hardware for multiple purposes. For example, if you do not need to use SPI interface, you can use SPI pins as general purpose digital I/O. The pins marked as SDA0 and SCL0 are preset to be used as an I2C interface. There are 1.8 kOhm pull-up resistors on the board for these pins.

You can power external components using 3.3 volt and 5 volt power pins on the expansion headers. The maximum permitted current draw from the 3.3 volt pins is 50 mA.

Best Practices and Restrictions

Do not connect electrical components to Raspberry Pi expansion headers while Raspberry Pi hardware is running. If you want to connect an electrical component, turn off your board first. Instructions for turning off you board are provided below.

Raspberry Pi pins use 3.3 volt logic levels. Do not connect devices using 5 volt logic levels directly to the Raspberry Pi pins.

Use logic level converters when interfacing devices using 5 volt logic levels to Raspberry Pi hardware.

Do not short a GPIO pin configured as digital output.

Do not short 5 volt power pins to any other pin on the expansion header. Be extremely careful when working in the vicinity of 5 volt power pins.

Do not touch bare expansion header pins while Raspberry Pi hardware is on. You can accidentally short some of the pins.

Turning Your Raspberry Pi Hardware On and Off

Raspberry Pi runs a Linux operating system. Turning off the power can result in corrupting the operating system image on the SD card. To turn off your board, first shut down the Linux operating system by executing the following:

You can also execute the preceding command above on an interactive command shell as described in the Getting Started with MATLAB Support Package for Raspberry Pi Hardware example. To turn off your board in MATLAB Online, you must execute the following Linux command from the command shell on the Raspberry Pi. Access the command shell remotely via SSH with PuTTY, as described in Remote Access.

After executing the command, wait until the operating system shuts down all LEDs on the board except for PWR LED. Then, unplug the power cable from the board. To restart your board, plug the power cable back into the board. It takes approximately a minute for the board to fully reboot.

Working with LEDs

A light-emitting diode (LED) is a semiconductor light source. An LED has two legs. One is called anode and the other is called cathode. The two legs of LED have different names because LED works only in one direction. The anode leg is longer than the cathode leg, and must be attached to the positive voltage terminal while the cathode is attached to the negative voltage terminal. The current going through an LED goes from anode to cathode. If you wire an LED backwards, it does not light up.

LEDs come in different colors and sizes. Common sizes are 3mm, 5mm and 10mm and refer to the diameter of the LED. A red LED, when it is on will have a forward voltage anywhere from 1.8 volts to 2.5 volts. When a LED is on, it behaves like a diode and passes a large amount of current that may produce enough heat to cause the LED to burn out. Therefore, you should always use a current-limiting resistor when working with an LED.

In this example, you connect a red LED across a GPIO pin and turn the LED on and off using the MATLAB command-line interface. You need a red LED and a 330 Ohm resistor. You connect the LED and the resistor as shown in the circuit diagram.

Here is another view showing the actual connections on a breadboard.

The anode (positive or long leg) is connected to the GPIO24 pin and the cathode is connected to the ground through the resistor. Once you make the connections, execute the following command at the MATLAB command prompt to turn the LED on.

The writeDigitalPin() method configures GPIO pin 24 as output and sets the logical value of the digital pin to one (logic high) causing 3.3 volts to be output at the pin. Writing a zero to a digital pin results in an output of logic low, which is digital ground in this case.

Make the LED «blink» for 10 seconds.

Working with Pushbuttons

A push button is a simple switch mechanism. When pressed or in closed position, the legs of a push button are shorted allowing electrical current to pass. When in open position, the switch does not conduct electricity. You can use a digital input pin to sense if the push button is in an open or closed position.

For this task, you need a breadboard-friendly pushbutton and 1 kOhm and 10 kOhm resistors. Connect the push button to GPIO pin 23, as shown in the following circuit diagram.

In this circuit, the GPIO pin sees ground when the push button is not pressed. When the pushbutton is pressed, the GPIO pin sees 3.3 volts. You can read the position of the push-button using the readDigitalPin() method.

Push button Controlled LED

Blink the LED rapidly for 1 second whenever the push button is pressed.

Summary

This example introduced the workflow for working with GPIO pins. You learned how to connect LEDs and push buttons to GPIO pins.

Open Example

You have a modified version of this example. Do you want to open this example with your edits?

Источник

Adblock
detector