[Link]
blog
Raspberry Pi
with MATLAB
Hans-Petter Halvorsen
Contents
• Raspberry Pi
• MATLAB
• MATLAB Support Package for Raspberry Pi
• GPIO
• Examples
– LED
– PWM
– Push Button
– Camera
[Link]
Raspberry Pi
Hans-Petter Halvorsen Table of Contents
Raspberry Pi
GPIO Pins
SD Card Ethernet
(the Back )
Camera
Connector USB A x 4
Power Supply (USB C) microHDMI x 2
Raspberry Pi
• The Raspberry Pi is a small computer that can do lots of things
• It has a small footprint (about 9x6cm) and it is cheap ($35+)
• You plug it into a monitor and attach a keyboard and mouse
• It has so-called GPIO pins (General Purpose Input/Output) for
connection sensors and other electronic components like LEDs, etc.
• Raspberry Pi is as well suited for prototyping, datalogging and
different electronics projects, a media center, etc.
• It can be used to learn programming, it and other technical skills, etc.
• RP has limited power (CPU, RAM, etc.) so it cannot normally replace a
desktop computer or laptop for ordinary use
[Link]
[Link]
MATLAB
Hans-Petter Halvorsen Table of Contents
MATLAB
• MATLAB is a tool for technical computing,
computation and visualization in an integrated
environment.
• MATLAB is an abbreviation for MATrix LABoratory
• It is well suited for matrix manipulation and problem
solving related to Linear Algebra, Modelling,
Simulation and Control applications, etc.
• MATLAB is popular in Universities, Teaching and
Research and Development (R&D)
• [Link]
MATLAB
[Link]
MATLAB Support Package
for Raspberry Pi
Hans-Petter Halvorsen Table of Contents
Raspberry Pi + MATLAB
MATLAB Support Package for Raspberry Pi
Network
PC with MATLAB
With MATLAB Support Package for Raspberry Pi, the Raspberry Pi is connected to a
computer running MATLAB. Processing is done on the computer with MATLAB.
[Link]
Raspberry Pi + MATLAB
Push Button
Sensors
We can read data from Sensors, Cameras,
control LEDs, etc. from our MATLAB
LED Camera
environment on the Desktop Computer
GPIO Pins
MATLAB
PC
Raspberry Pi
MATLAB Support Package for Raspberry Pi
Getting Started with MATLAB Support Package for Raspberry Pi: [Link]
MATLAB Support Package for Raspberry Pi
Getting Started with MATLAB Support Package for Raspberry Pi: [Link]
[Link]
Hardware Setup
Hans-Petter Halvorsen Table of Contents
Hardware Setup
We have 2 options/alternatives:
• Setup Hardware with
MathWorks Raspbian Image
• Customize the existing
Operating System running on
my Hardware
Hardware Setup Alternatives
• Setup Hardware with MathWorks Raspbian
Image
• Customize the existing Operating System
running on my Hardware
[Link]
Hardware Setup - Alternative 1 (Recommended)
Hardware Setup
Setup Hardware with MathWorks Raspbian Image
Hans-Petter Halvorsen Table of Contents
Hardware Setup
Setup Hardware with MathWorks Raspbian Image
Hardware Setup
Getting Started with MATLAB Support Package for Raspberry Pi: [Link]
Test Hardware
Documentation and Examples
Blinking LED Example
Observe that the built-in LED
on the Raspberry Pi is blinking
Blinking LED Example
clear
WE use the following Function: clc
writeLED(r, "LED0", 1); r = raspi;
for i = 1:10
disp(i);
writeLED(r, "LED0", 0);
pause(0.5);
writeLED(r, "LED0", 1)
pause(0.5);
end
[Link]
Hardware Setup - Alternative 2
Hardware Setup
Customize the existing Operating System running on my Hardware
Hans-Petter Halvorsen Table of Contents
Hardware Setup
Customize the existing Operating System running on my Hardware
Hardware Setup
Hardware Setup
[Link]
GPIO
Hans-Petter Halvorsen Table of Contents
GPIO
GPIO
GPIO Features
The GPIO pins are Digital Pins which are either True
(+3.3V) or False (0V). These can be used to turn on/off
LEDs, etc.
The Digital Pins can be either Output or Input.
In addition, some of the pins also offer some other
Features:
• PWM (Pulse Width Modulation)
Digital Buses (for reading data from Sensors, etc.):
• SPI
• I2C
[Link]
GPIO Code Examples
Hans-Petter Halvorsen Table of Contents
Necessary Equipment
• Raspberry Pi
• Breadboard
• LEDs
• Push Buttons
• Resistors
• Wires (Jumper Wires)
[Link]
LED Example
Hans-Petter Halvorsen Table of Contents
Necessary Equipment
• Raspberry Pi
• Breadboard
• LED
• Resistor (270Ω)
• Wires (Jumper Wires)
Breadboard Wiring
Make sure not to short-circuit
the components that you
wire on the breadboard
Raspberry Pi GPIO Pins
Wiring
LED
R=270Ω
GND (Pin 34)
GPIO16 (Pin 36)
Breadboard
Blinking LED GPIO Example
Turn LED ON Example
clear rpi
rpi = raspi();
gpiopin = 16;
ledvalue = 1;
writeDigitalPin(rpi, gpiopin, ledvalue);
Blinking LED GPIO Example
clear rpi
rpi = raspi();
gpiopin = 16;
for i = 1:10
ledvalue = 1;
writeDigitalPin(rpi, gpiopin, ledvalue);
pause(0.5);
ledvalue = 0;
writeDigitalPin(rpi, gpiopin, ledvalue);
pause(0.5);
end
Blinking LED GPIO Example
clear rpi
rpi = raspi();
gpiopin = 16;
for i = 1:10
ledvalue = 1;
writeDigitalPin(rpi, gpiopin, ledvalue);
pause(0.5);
ledvalue = 0;
writeDigitalPin(rpi, gpiopin, ledvalue);
pause(0.5);
end
[Link]
PWM
Pulse Width Modulation
Hans-Petter Halvorsen Table of Contents
Controlling LED Brightness using PWM
• We've seen how to turn an LED on and off, but how do
we control its brightness levels?
• An LED's brightness is determined by controlling the
amount of current flowing through it, but that requires a
lot more hardware components.
• A simple trick we can do is to flash the LED faster than
the eye can see!
• By controlling the amount of time the LED is on versus
off, we can change its perceived brightness.
• This is known as Pulse Width Modulation (PWM).
Controlling LED Brightness using PWM
Below we see how we can use PWM to control the brightness of a LED
[Link]
PWM as “Analog Out“
HIGH 3.3V
The Raspberry Pi has no 0.33V
real Analog Out pins, but LOW 0V
we can use a PWM pin.
PWM can be used to
control brightness of a HIGH 3.3V
LED, control the speed of 1.65V
a Fan, control a DC LOW 0V
Motor, etc.
0 – 3.3V HIGH 3.3V
LOW 0V
2.97V
Raspberry Pi GPIO Pins
Wiring
LED
R=270Ω
GND (Pin 34)
GPIO16 (Pin 36)
Breadboard
writePWMVoltage
clear mypi
mypi = raspi() volt between 0V (0%) and 3.3V (100%)
This means 1.65V = 50% brightness of the LED
gpiopin = 16;
configurePin(mypi, gpiopin, 'PWM');
writePWMFrequency(mypi, gpiopin, 2000);
volt = 1.65
writePWMVoltage(mypi, gpiopin, volt);
writePWMDutyCycle
clear mypi
mypi = raspi() dutycycle between 0 (0%) and 1 (100%)
This means 0.5 = 50% brightness of the LED
gpiopin = 16;
configurePin(mypi, gpiopin, 'PWM');
writePWMFrequency(mypi, gpiopin, 2000);
dutycycle = 0.5
writePWMDutyCycle(mypi, gpiopin, dutycycle);
PWM in MATLAB - Summary
clear mypi
mypi = raspi()
gpiopin = 16;
configurePin(mypi, gpiopin, 'PWM');
writePWMFrequency(mypi, gpiopin, 2000); dutycycle between 0 (0%) and 1 (100%)
This means 0.5 = 50% brightness of the LED
volt = 1.65
writePWMVoltage(mypi, gpiopin, volt);
clear mypi
volt between 0V (0%) and 3.3V (100%) mypi = raspi()
This means 1.65V = 50% brightness of gpiopin = 16;
the LED
configurePin(mypi, gpiopin, 'PWM');
writePWMFrequency(mypi, gpiopin, 2000);
dutycycle = 0.5
writePWMDutyCycle(mypi, gpiopin, dutycycle);
PWM with MATLAB
clear rpi
mypi = raspi()
gpiopin = 16;
configurePin(mypi, gpiopin, 'PWM');
writePWMFrequency(mypi, gpiopin, 2000);
for dutycycle = 0:0.1:1
writePWMDutyCycle(mypi, gpiopin, dutycycle);
pause(0.5);
end
dutycycle = 0;
writePWMDutyCycle(mypi, gpiopin, dutycycle);
[Link]
Push Button
Hans-Petter Halvorsen Table of Contents
Necessary Equipment
• Raspberry Pi
• Breadboard
• Push Button
• LED
• Resistors, 𝑅 = 270Ω, 𝑅 = 10𝑘Ω
• Wires (Jumper Wires)
Push Button/Switch
• Pushbuttons or switches connect two
points in a circuit when you press them.
• You can use it to turn on a Light when
holding down the button, etc.
Light
+ Switch (On/Off)
Battery
-
[Link]
Push Button
(Pull-up Resistor)
Hans-Petter Halvorsen Table of Contents
Push Button (Pull-up Resistor)
Button is NOT Pushed
=> True/High
Button is Pushed
=> False/Low
Button Setup (Pull-up Resistor)
+3.3V
Using external Pull-up Resistor
Raspberry Pi GPIO Pins
GND
𝑅 = 10𝑘Ω
Pin 36 (GPIO 16)
Pull-up Resistor
True/High False/Low
+3.3/5V +3.3/5V
Resistor Resistor
DI DI
Switch We Push the Button Switch
Open Closed
GND GND
Pull-down/Pull-up Resistor
Why do we need a pull-up or pull-down resistor in the
circuit?
• If you disconnect the digital I/O pin from everything, it
will behave in an irregular way.
• This is because the input is "floating" - that is, it will
randomly return either HIGH or LOW.
• That's why you need a pull-up or pull-down resistor in
the circuit.
clear r
clc Push Button Example
r = raspi();
gpiopin_read = 16;
for i = 1:10
status = readDigitalPin(r, gpiopin_read);
if (status == 0)
disp("Button Pushed")
else
disp("Please Push the Button")
end
pause(1);
end
[Link]
Push Button
(Pull-down Resistor)
Hans-Petter Halvorsen Table of Contents
Push Button (Pull-down Resistor)
Button is NOT Pushed => False/Low
Button is Pushed => True/High
Button Setup (Pull-down Resistor)
+3.3V
Using external Pull-down Resistor
Raspberry Pi GPIO Pins
GND
𝑅 = 10𝑘Ω
Pin 36 (GPIO 16)
Pull-down Resistor
We could also have wired according to a “Pull-down” Resistor
False/Low True/High
+3.3/5V +3.3/5V
Switch Switch
Open Closed
DI DI
We Push the Button
Resistor Resistor
GND GND
clear r
clc Push Button Example
r = raspi();
gpiopin_read = 16;
for i = 1:10
status = readDigitalPin(r, gpiopin_read);
if (status == 1)
disp("Button Pushed")
else
disp("Please Push the Button")
end
pause(1);
end
[Link]
Push Button + LED
Hans-Petter Halvorsen Table of Contents
Push Button + LED Example
( + 3.3V)
Button + LED
Pin 1
Using external Pull-down Resistor
Raspberry Pi
GPIO Pins
LED
𝑅 = 270Ω
Button
𝑅 = 10𝑘Ω
Pin 34 (GND)
Pin 37 Pin 36 (GPIO 16)
(GPIO 26)
Push Button + LED Example
clear r
clc
gpiopin_button = 16;
gpiopin_led = 26;
r = raspi();
disp("Raspberry Pi Ready")
for i = 1:10
status = readDigitalPin(r, gpiopin_button);
if (status == 1)
ledvalue = 1;
writeDigitalPin(r, gpiopin_led, ledvalue);
disp("LED On")
else
ledvalue = 0;
writeDigitalPin(r, gpiopin_led, ledvalue);
disp("LED Off")
end
pause(1);
end
clear r
disp("Program is Finished")
[Link]
Camera
Hans-Petter Halvorsen Table of Contents
Raspberry Pi + Camera
Raspberry Pi
We can either use a specialized
Raspberry Pi Camera or an
ordinary/standard USB Camera
USB Connector
Raspberry Pi Camera Module v2
Camera
Connector
CSI (Camera Serial Interface) Standard USB Web Camera
Web Camera Example
Connect an USB Web Camera to
the USB port on the Raspberry Pi
Web Camera Example
clear
clc
mypi = raspi
mycam = webcam(mypi)
img = snapshot(mycam)
imagesc(img)
drawnow
Hans-Petter Halvorsen
University of South-Eastern Norway
[Link]
E-mail: [Link]@[Link]
Web: [Link]