You are on page 1of 97

Copyright

BeagleBone Black Programming using Matlab


Agus Kurniawan
1st Edition, 2015
Copyright © 2015 Agus Kurniawan
Table of Contents
Copyright
Preface
1. Preparing Development Environment
1.1 BeagleBone Black
1.2 Getting Hardware
1.3 Development Tools
1.4 Electronics Devices
1.5 BeagleBone Black Unboxing
1.6 A Basic of BeagleBone Black Operating and
Programming
2. Setting Up BeagleBone Black Development for MATLAB
2.1 Getting Started
2.2 Connecting BeagleBone Black to Computer
2.3 Installing BeagleBone Black Driver
2.4 Setting up BeagleBone Black Development for
MATLAB
2.5 Hello BeagleBone Black: Blinking LED
3. Working with GPIO
3.1 Getting Started
3.2 Working with Blinking LED
3.3 Demo : LED and Pushbutton
3.3.1 Wiring
3.3.2 Writing a Program
3.3.3 Testing
4. Working with PWM and ADC
4.1 Getting Started
4.2 Demo PWM Duty Cycle : RGB LED
4.2.1 Wiring
4.2.2 Writing Program
4.2.3 Testing
4.3 Demo PWM Voltage: LED Brightness
4.3.1 Wiring
4.3.2 Writing a Program
4.3.3 Testing
4.4 Demo Analog Input (ADC): Working with
Potentiometer
4.4.1 Wiring
4.4.2 Writing Program
4.4.3 Testing
5. Working with I2C
5.1 Getting Started
5.2 Writing Program
5.3 Demo 1: Scanning I2C
5.4 Demo 2: Reading Data from Sensor Based I2C
6. Working with SPI
6.1 Getting Started
6.2 Demo : SPI Loopback
7. Working with Serial Port
7.1 Getting Started
7.2 Wiring
7.3 Writing a MATLAB Program
7.4 Testing
8. Working with Web Camera
8.1 Getting Started
8.2 Connecting a Camera into BeagleBone Black Board
8.3 Take a Picture from Camera
8.4 What’s the Next?
9. Working with BeagleBone Black Linux Command
9.1 Getting Started
9.2 SSH
9.3 Linux Shell
10. Measuring and Plotting Sensor Data in Real-Time
10.1 Getting Started
10.2 Wiring
10.3 Writing a Program
10.4 Testing
Source Code
Contact
Preface

This book was written to help anyone who wants to develop BeagleBone Black board using MATLAB. It describes
the basic elements of BeagleBone Black development using MATLAB.

Agus Kurniawan
Depok, October 2015
1. Preparing Development Environment
1.1 BeagleBone Black
BeagleBone Black is a low-cost, community-supported development platform for
developers and hobbyists. Boot Linux in under 10 seconds and get started on development
in less than 5 minutes with just a single USB cable. Further information about BeagleBone
Black, you can visit and read this board on http://beagleboard.org/black .
The following is a specification of BeagleBone Black

Processor: AM335x 1GHz ARM® Cortex-A8


512MB DDR3 RAM
4GB 8-bit eMMC on-board flash storage
3D graphics accelerator
NEON floating-point accelerator
2x PRU 32-bit microcontrollers
USB client for power & communications
USB host
Ethernet
HDMI
2x 46 pin headers

You can see BeagleBone Black device in the Figure below.


In this book, I use BeagleBone Black (Revision C).
1.2 Getting Hardware
How to get BeagleBone Black?
Officially you can buy it from the elemet14,
http://www.element14.com/community/community/designcenter/single-board-
computers/next-gen_beaglebone . You can also buy this board on the following online
store:

Sparkfun, https://www.sparkfun.com/products/12857
Amazon, http://www.amazon.com/Beagleboard-BBONE-BLACK-4G-BeagleBone-
Rev-C/dp/B00K7EEX2U/
Exp-tech, http://www.exp-tech.de/mainboards-prototyping/other/beaglebone-black-
rev-c-element14
Seeed studio, http://www.seeedstudio.com/depot/Embest-BeagleBone-Black-RevC-
Singleboard-Computer-p-1860.html
Adafruit, http://www.adafruit.com/products/1876

In addition, you can buy it on your local electronics store.


1.3 Development Tools
In this book, we use Matlab development tools to write programs with target on
BeagleBone Black. You also need Matlab account to install Matlab add-on for
BeagleBone Black board.
Currently BeagleBone Black development for Matlab is only available for Windows
platform, http://www.mathworks.com/hardware-support/beaglebone-black-matlab.html .
In this book, I use Windows 10 and Matlab 2015b for testing.
1.4 Electronics Devices
We need electronic components to build our testing, for instance, Resistor, LED, sensor
devices and etc. I recommend you can buy electronic component kit. You find them on
your local electronics shops

Several sensor or actuator devices will be explored to evaluate our BeagleBone Black
development.
1.5 BeagleBone Black Unboxing
After you bought BeagleBone Black, you got the following box. I obtained this board
from Exp-Tech, http://www.exp-tech.de .

After opened, you will get the following items, shown in Figure below.
Next chapter, we explore how to get started with BeagleBone Black.
1.6 A Basic of BeagleBone Black Operating and
Programming
In this book, I don’t explain topics about BeagleBone Black administration and
programming in general. We focus on BeagleBone Black programming using Matlab.
Fortunately, I have written a book, BeagleBone Black Programming by Example which
you may need to learn about a basic operation and programming on BeagleBone Black
board.
Please visit on my blog, http://blog.aguskurniawan.net/post/BeagleBone-Black-
Programming-by-Example.aspx, to obtain information about this book.
2. Setting Up BeagleBone Black Development for MATLAB

This chapter explains how to work on setting up BeagleBone Black (BBB) board on a
computer and then, access it from MATLAB.
2.1 Getting Started
In this chapter, we set up BeagleBone Black board development using MATLAB support
package for BeagleBone Black hardware. To set up this development, you must have
MATLAB 2014a or later and MATLAB account to verify while installing.
To understand BeagleBone Black board pinouts, please see the following Figure.
2.2 Connecting BeagleBone Black to Computer
Connect your BeagleBone Black board via USB Client to computer which runs Windows
OS. We don’t need a power adapter. After connected, Windows should recognize as USB
flash disk, shown in Figure below.

\\

Open the Flashdisk so you should see a list of file. Open Start.htm file using a browser.
You should see the form, shown in Figure below.
2.3 Installing BeagleBone Black Driver
The next step is to install BeagleBone Black driver for Windows. After opened
START.htm file, you can see a list of BeagleBone Black driver . Download it for Windows
based on your platform (64-bit or 32-bit).

Run the driver file.


Just follow installation instruction. On the middle of installation process, you should
confirm to install the driver.

If done, you can click Finish button on the installation dialog.


In addition to, you should install Virtual COM Port driver. You can download and install it
on http://www.ftdichip.com/Drivers/VCP.htm .

Download and install. Follow installation instruction.


After done, try to open a browser and navigate to http://192.168.7.2/ . You should see the
following form.

After finished, you can set up BeagleBone Black development for MATLAB.
2.4 Setting up BeagleBone Black Development for MATLAB
In this section, we try to set up BeagleBone Black development for MATLAB. You can
configure MATLAB Support Package for BeagleBone Black hardware using MATLAB
2014a or later. We also need internet connection to download this package.
Let’s start.
Run MATLAB application. Click Get Hardware Support Packages on Add-Ons icon
on toolbox.

Then, you get a dialog. Select Install from Internet.


If done, click Next> button.
Select BeagleBone Black on Support for. You should see two BeagleBone Black support
packages. You can select Interact with BeagleBone Black from MATLAB.
If done, click Next> button.

You will be asked to logon with your MATLAB account. You should have MATLAB
license. Click Log In button.

You should the authentication dialog. Fill your account. After that, click Log In button.
If success, you should get a software license agreement. Checked I accept and then click
Next> button.

You will get confirmation. Click Next> button.


Click Install button to start installation.

Click Finish button to close installation.


2.5 Hello BeagleBone Black: Blinking LED
In this section, we build a blinking LED program using MATLAB. BeagleBone Black
board provides onboard LEDs.
Let’s start to write our Blink program.
Make sure your BeagleBone Black board already connected to your computer via USB
Client.
On MATLAB Command Windows, write this command.
>> bbb = beaglebone

This code will try to connect BeagleBone Black board. If connected, it shows BeagleBone
Black information, shown in Figure below.

To see a list of onboard-LEDs, you can use showLEDs() by passing BeagleBone Black
object.
>> bbb = beaglebone
>> showLEDs(bbb)

showLEDs() function will show a picture of onboard LEDs.


For instance, we want to use onboard LED USR1. Use writeLED() to turn on/off the LED.
If we pass value 1 into writeLED(), we will turn on the LED. Otherwise, pass 0 into
writeLED() function to turn off the LED.
>> writeLED(bob,’usr1’,1)
>> writeLED(bob,’usr1’,0)
>> clear bbb

Don’t forget to close BeagleBone Black object by calling clear function.


A sample of program output can be seen in Figure below.
You should see a lighting LED (USR1) on BeagleBone Black board.
3. Working with GPIO

In this chapter I’m going to explain how to work with digital I/O on BeagleBone Black
and write several programs for demo.
3.1 Getting Started
MATLAB support for BeagleBone Black board provides three functions which we can use
on digital I/O processing. You can read GPIO on BeagleBone Black board
on http://www.mathworks.com/help/supportpkg/beagleboneio/gpio-pins.html .
The following is the functions:

configureDigitalPin() is used to define pin mode either as input or output.


Reference: http://www.mathworks.com/help/supportpkg/beagleboneio/ref/configuredigitalpi
writeDigitalPin() is used to write digital data into a specific digital pin.
Reference: http://www.mathworks.com/help/supportpkg/beagleboneio/ref/writedigitalpin.htm
readDigitalPin() is used to read digital input on specific digital pin.
Reference: http://www.mathworks.com/help/supportpkg/beagleboneio/ref/readdigitalpin.htm

To get information about BeagleBone Black GPIO, you can write these scripts.
board = beaglebone;
showPins(board);

A sample of program output.

After called showPins(), you can get GPIO pins dialog.


A detail of BeagleBone Black board pinout can be read on this
site, http://www.mathworks.com/help/supportpkg/beagleboneio/ug/beaglebone-black-pin-
map.html .

To illustrate how to work with digital I/O, we build two simple programs by utilizing LED
and pushbutton.
3.2 Working with Blinking LED
The first demo is to build a blinking LED. You need a LED which is connected to P8_8
pin of BeagleBone Black board. The following is my hardware wiring.

Now you can create a new MATLAB script file by clicking New Script icon to create a
new script file.

After that, you can get a script editor, shown in Figure below.
Then, you can write these scripts for blinking LED demo.
board = beaglebone();
led = 'P8_8';
configureDigitalPin(board,led,'output')
for k=1:10
disp('turn on LED');
writeDigitalPin(board,led,1);
pause(1);
disp('turn off LED');
writeDigitalPin(board,led,0);
pause(1);
end

disp('close BeagleBone Black board');


clear board;

Save those scripts into a file, called blinking.m. Now you can run it.
>> blinking

The following is a sample output of blinking program.


You should see blinking LED on BeagleBone Black board.
3.3 Demo : LED and Pushbutton
we build a program using LED and pushbutton. When we press a pushbutton, LED will
lighting. It’s a simple;).

3.3.1 Wiring
The following is hardware wiring:

LED is connected to BeagleBone Blackpin P8_8


Pushbutton is connected to BeagleBone Black pin P8_7

A sample of hardware implementation is shown in Figure below.

3.3.2 Writing a Program


Now you can write these scripts.
function [] = led_pushbutton()
pushbutton = 'P8_7';
led = 'P8_8';
board = beaglebone();
finishup = onCleanup(@() exitprogram(board));

configureDigitalPin(board,pushbutton,'input');
configureDigitalPin(board,led,'output')
disp('press Ctr-C to exit');
while 1
state = readDigitalPin(board,pushbutton);
writeDigitalPin(board,led,state);
disp(state);
pause(0.5);
end

end

function exitprogram(b)
clear b;
disp('program has exit');
end

We use onCleanup(), http://www.mathworks.com/help/matlab/ref/oncleanup.html , to


catch CTRL+C for quiting the program.
Save this program into file, called led_pushbutton.m.

3.3.3 Testing
Run this program by typing this command on Command Window on Matlab.
>> led_pushbutton

Press pushbutton. Then, you should see lighting LED. Press CTRL+C to exit program.
Program output:
LED is lighting if a pushbutton is pressed.
4. Working with PWM and ADC

This chapter explains how to work with BeagleBone Black Analog I/O using MATLAB.
4.1 Getting Started
MATLAB support for BeagleBone Black board provides five functions which we can use
on analog I/O processing. You can read them on
pwm, http://www.mathworks.com/help/supportpkg/beagleboneio/pwm.html . The
following is the functions:

enablePWM() is used to enable pin as PWM pin.


Reference: http://www.mathworks.com/help/supportpkg/beagleboneio/ref/enablepwm.html
writePWMVoltage() is used to write PWM voltage on digital pin.
Reference: http://www.mathworks.com/help/supportpkg/beagleboneio/ref/writepwmvoltage.
writePWMDutyCycle() is used to set PWM duty cycle on digital pin.
Reference: http://www.mathworks.com/help/supportpkg/beagleboneio/ref/writepwmdutycyc
readVoltage() to read analog input on Analog pin.
Reference: http://www.mathworks.com/help/supportpkg/beagleboneio/ref/readvoltage.html

A detail of BeagleBone Black board pinout can be read on this


site, http://www.mathworks.com/help/supportpkg/beagleboneio/ug/beaglebone-black-pin-
map.html .
In this chapter, we try to access BeagleBone Black Analog I/O using MATLAB. There are
three scenarios for our cases:

Controlling RGB LED


Controlling LED brightness
Reading Analog input using Potentiometer

Let’s start.
4.2 Demo PWM Duty Cycle : RGB LED
In this scenario we build a program to control RGB LED color using BeagleBone Black
Analog output (PWM). You can see PWM pins as PWMxA and PWMxB, x is number.
RGB LED has 4 pins that you can see it on Figure below.

To understand these pins, you can see the following Figure.

Note:

Pin 1: Red
Pin 2: Common pin
Pin 3: Green
Pin 4: Blue

Now we can start to build a program and hardware implementation.

4.2.1 Wiring
Firstly we implement RGB LED hardware. For our testing, we configure the following
PWM pins.

RGB LED pin 1 (red) is connected to BeagleBone Black PWM pin P9_14
RGB LED pin 2 is connected to BeagleBone Black VCC +3.3V
RGB LED pin 3 (green) is connected to BeagleBone Black PWM PWM pin P9_16
RGB LED pin 4 (blue) is connected to BeagleBone Black PWM pin P9_22

Here is a sample implementation with BeagleBone Black board.

4.2.2 Writing Program


To display a certain color, we must combine colors from red, green, blue. MATLAB
provides API for PWM such as writePWMDutyCycle() with analog value from 0 to 1.
Let”s start to build a program. Firstly, open MATLAB. Then, write these scripts.
function [] = led_rgb()
board = beaglebone();
finishup = onCleanup(@() exitprogram(board));
PWM1A = 'P9_14'; % red
PWM1B = 'P9_16'; % green
PWM0A = 'P9_22'; % blue
enablePWM(board,PWM1A);
enablePWM(board,PWM1B);
enablePWM(board,PWM0A);
disp('press Ctr-C to exit');
while 1
disp('red');
write_rgb(board,PWM1A,PWM1B,PWM0A,0,1,1); % red
pause(1);

disp('green');
write_rgb(board,PWM1A,PWM1B,PWM0A,1,0,1); % green
pause(1);

disp('blue');
write_rgb(board,PWM1A,PWM1B,PWM0A,1,1,0); % blue
pause(1);

disp('yellow');
write_rgb(board,PWM1A,PWM1B,PWM0A,0,0,1); % yellow
pause(1);

disp('purple');
write_rgb(board,PWM1A,PWM1B,PWM0A,0.3,1,0.3); % purple
pause(1);

disp('aqua');
write_rgb(board,PWM1A,PWM1B,PWM0A,1,0,0); % aqua
pause(1);
end

end

function write_rgb(board,p1,p2,p3,r,g,b)
writePWMDutyCycle(board,p1,r);
writePWMDutyCycle(board,p2,g);
writePWMDutyCycle(board,p3,b);
end

function exitprogram(b)
clear b;
disp('program has exit');
end
Save this program as led_rgb.m.
This program will generate six colors: red, green, blue, yellow, purple, and aqua.

4.2.3 Testing
Upload and run the program. You should see several color on RGB LED.
>> led_rgb

The following is a sample demo on Command Window on MATLAB.

The following is a sample demo on hardware.


4.3 Demo PWM Voltage: LED Brightness
In this demo, we try to control LED brightness by controlling voltage on LED. MATLAB
for BeagleBone Black support provides writePWMVoltage() function to set voltage on
PWM pins. We can set a voltage value from 0 to 3.3 for BeagleBone Black. Further
information about , please read it
on http://www.mathworks.com/help/supportpkg/beagleboneio/ref/writepwmvoltage.html .
Let’s build.

4.3.1 Wiring
We connect a LED on BeagleBone Black PWM pin on P9_14. The following is my
hardware wiring.
4.3.2 Writing a Program
Now you can open MATLAB and write these scripts.
function [] = led_brightness()
board = beaglebone();
PWM1A = 'P9_14';
finishup = onCleanup(@() exitprogram(board));
enablePWM(board,PWM1A);
disp('press Ctr-C to exit');
while 1
for k = 0:0.3:3.3
writePWMVoltage(board,PWM1A,k);
pause(1);
end
for k = 3.3:-0.3:0
writePWMVoltage(board,PWM1A,k);
pause(1);
end
end

end

function exitprogram(b)
clear b;
disp('program has exit');
end

Save these scripts into a file, called led_brightness.m.


This program will set voltage value on PWM pin on P9_14 3 from 0 to 3.3 and then set a
value from 3.3 to 0 too.

4.3.3 Testing
Make sure BeagleBone Black board already connected to your computer. You can run the
program by typing this command.
>> led_brightness

On hardware side, you should see LED brightness changing gradually.


4.4 Demo Analog Input (ADC): Working with Potentiometer
In this section, we learn how to read analog input on BeagleBone Black board. For
illustration, I use Potentiometer as analog input source. Our scenario is to read analog
value from Potentiometer. Then, display it on console. To read analog input, we can use
readVoltage() function. Further information, please read this function
on http://www.mathworks.com/help/supportpkg/beagleboneio/ref/readvoltage.html .
Let’s start!.

4.4.1 Wiring
To understand Potentiometer, you see its scheme in Figure below.
You can connect VCC to BeagleBone Black board on VCC +3.3V pin. Vout
to BeagleBone Black board Analog input AIN0 which is located on P9_39. In addition,
GND to BeagleBone Black board GND. The following is hardware implementation. I use
slide potentiometer.
4.4.2 Writing Program
Firstly, create a program via MATLAB. To read analog input, we can use readVoltage()
function. Ok, Let’s write these scripts.
function [] = potentiometer()
board = beaglebone();
finishup = onCleanup(@() exitprogram(board));
adc = 'AIN0';
disp('press Ctr-C to exit');
while 1
analog = readVoltage(board, adc);
disp(['analog= ',num2str(analog)]);
pause(1);
end

end

function exitprogram(b)
clear b;
disp('program has exit');
end

Save this code as potentiometer.m

4.4.3 Testing
To run the program, you can type this command.
>> potentiometer

You should see analog value on Command Window. You can change analog input via
potentiometer.
A sample of program output can be seen in Figure below.
5. Working with I2C

In this chapter we learn how to work with I2C on BeagleBone Black board using
MATLAB.
5.1 Getting Started
The I2C (Inter-Integrated Circuit) bus was designed by Philips in the early ’80s to allow
easy communication between components which reside on the same circuit board. TWI
stands for Two Wire Interface and for most marts this bus is identical to I²C. The name
TWI was introduced by Atmel and other companies to avoid conflicts with trademark
issues related to I²C.
I2C bus consists of two wires, SDA (Serial Data Line) and SCL (Serial Clock Line). You
can see I2C pins on BeagleBone Black board in Figure below.
MATLAB for BeagleBone Black support provides several functions to access I2C
protocol. You can read it
on http://www.mathworks.com/help/supportpkg/beagleboneio/i2c-interface.html .
For testing, I used PCF8591 AD/DA Converter module with sensor and actuator devices.
You can find it on the following online store:

Amazon, http://www.amazon.com/PCF8591-Converter-Module-Digital-
Conversion/dp/B00BXX4UWC/
eBay, http://www.ebay.com
Dealextreme, http://www.dx.com/p/pcf8591-ad-da-analog-to-digital-digital-to-
analog-converter-module-w-dupont-cable-deep-blue-336384
Aliexpress, http://www.aliexpress.com/

In addition, you can find this device on your local electronics store/online store.
This module has mini form model too, for instance, you can find it on Amazon,
http://www.amazon.com/WaveShare-PCF8591T-Converter-Evaluation-
Development/dp/B00KM6X2OI/ .

This module use PCF8591 IC and you can read the datasheet on the following URLs.

http://www.electrodragon.com/w/images/e/ed/PCF8591.pdf
http://www.nxp.com/documents/data_sheet/PCF8591.pdf
In this chapter, we build a program to access sensor via I2C using MATLAB
on BeagleBone Black board.
To obtain a list of supported I2C on BeagleBone Black board, you can call
AvailableI2CBuses. Type these commands on MATLAB Command Window.
>> board = beaglebone
>> board.AvailableI2CBuses

Note:
Make sure BeagleBone Black board already connected to a computer.
You should see a list of supported I2C on BeagleBone Black.
5.2 Writing Program
We use PCF8591 AD/DA Converter as I2C source. You can connect PCF8591 AD/DA
Converter to I2C1 BeagleBone Black board directly.
The following is our wiring lab

PCF8591 AD/DA Converter SDA —> BeagleBone Black I2C1_SDA (P9_18)


PCF8591 AD/DA Converter SCL —> BeagleBone Black I2C1_SCL (P9_17)
PCF8591 AD/DA Converter VCC —> BeagleBone Black VCC +3.3V
PCF8591 AD/DA Converter GND —> BeagleBone Black GND

Hardware implementation can be shown in Figure below.


5.3 Demo 1: Scanning I2C
After attached our sensor to BeagleBone Black, we can scan our I2C address using
scanI2CBus() function. Firstly, we enable I2C1 on BeagleBone Black. Then, scan I2C
address.
Write these scripts.
board = beaglebone();
enableI2C(board,1); % 'i2c-2'
address = scanI2CBus(board,'i2c-2');
disp(['i2c address: ', address]);

clear board;

Save the program into a file, called i2c_scan.m.


Run the program.
>> i2c_scan

On Command Window, you should see I2C address of sensor device. For instance, my
sensor was detected on 0x48.

I2C address will be used on the next demo.


5.4 Demo 2: Reading Data from Sensor Based I2C
We use I2C on BeagleBone Black board using i2c object, see
http://www.mathworks.com/help/supportpkg/beagleboneio/i2c-interface.html . PCF8591
AD/DA Converter module has three sensor devices: Thermistor, Photo-voltaic cell and
Potentiometer. This module runs on I2C bus with address 0x48. In this case, we read all
sensor data.
Write these scripts.
function [] = i2c_sensor()
board = beaglebone();
enableI2C(board,1); % 'i2c-2'
i2c_address = 'i2c-2';
finishup = onCleanup(@() exitprogram(board));
disp('press Ctr-C to exit');
PCF8591 = '0x48';
PCF8591_ADC_CH0 = '40'; % thermistor
PCF8591_ADC_CH1 = '41'; % photo-voltaic
PCF8591_ADC_CH3 = '43'; % potentiometer
i2c = i2cdev(board, i2c_address, PCF8591);
disp(['thermistor ', 'photo ', 'potentiometer']);
while 1
thermistor = read_adc(i2c,hex2dec(PCF8591_ADC_CH0));
pause(0.5);
photo = read_adc(i2c,hex2dec(PCF8591_ADC_CH1));
pause(0.5);
potentiometer = read_adc(i2c,hex2dec(PCF8591_ADC_CH3));
pause(0.5);
disp([thermistor, photo, potentiometer]);
end

end

function adc = read_adc(dev,config)


write(dev,config);
read(dev, 1);
out = read(dev, 1);
adc = out;
end

function exitprogram(b)
clear b;
disp('program has exit');
end

Save these scripts into a file, called i2c_sensor.m.


Run the program.
>> i2c_sensor
If success, you should see values for Thermistor, Photo-voltaic, and potentiometer.
6. Working with SPI

In this chapter I’m going to explain how to work with SPI on BeagleBone Black board
using MATLAB.
6.1 Getting Started
The Serial Peripheral Interface (SPI) is a communication bus that is used to interface one
or more slave peripheral integrated circuits (ICs) to a single master SPI device; usually a
microcontroller or microprocessor of some sort. In general, SPI has MOSI, MISO, SCK
and SS pins.
There are two SPI in BeagleBone Black board which can be seen in Figure below.

In general, you learn SPI using MATLAB on this


document, http://www.mathworks.com/help/supportpkg/beagleboneio/spi-interface.html.
To access SPI on BeagleBone Black board from MATLAB, we can do the following steps:

Open BeagleBone Black communication by creating BeagleBone object


Enable SPI by calling
enableSPI, http://www.mathworks.com/help/supportpkg/beagleboneio/ref/enablespi.html
Open SPI connection by calling spidev with passing SS
pin, http://www.mathworks.com/help/supportpkg/beagleboneio/ref/beaglebone.spidev.html
To write and read SPI data, we can use writeRead with passing spidev
object, http://www.mathworks.com/help/supportpkg/beagleboneio/ref/writeread.html

For testing, type these commands on MATLAB Command Window.


>> board = beaglebone
>> board.AvailableSPIChannels
>> enableSPI(board,0)
>> board.AvailableSPIChannels

You see SPI0 has address on ‘spidev1.0’.


In this chapter, we build a SPI Loopback app. Let’s start!.
6.2 Demo : SPI Loopback
In this demo, we use SPI0 on BeagleBone Black. To develop SPI loopback, we can
connect MOSI pin to MISO pin. This means you connect pin P9_18 to pin P9_21 using
cable.
The following is a sample of wiring.

On MATLAB, you can write thses scripts.


function [] = spi_loopback()
board = beaglebone();
enableSPI(board,0)
spi_address = 'spidev1.0';
finishup = onCleanup(@() exitprogram(board));
spi = spidev(board, spi_address, 0);
k = 3;
m = 10;
n = 30;
disp('press Ctr-C to exit');
while 1
disp('datain: ');
dataIn = [k m n];
disp(dataIn);
dataOut = writeRead(spi,dataIn);
disp('dataout: ');
disp(dataOut);
pause(1.5);
k = k + 1;
m = m + 1;
n = n + 1;
end

end

function exitprogram(b)
clear b;
disp('program has exit');
end

Save these scripts into a file, callsed spi_loopback.m. Then, run this program on
Command Windows of MATLAB.
>> spi_loopback

A sample output program can be seen in Figure below.


7. Working with Serial Port

In this chapter I’m going to explain how to work with serial port on BeagleBone Black
board using MATLAB.
7.1 Getting Started
Serial port has RX and TX. BeagleBone Black board has 5 serial ports. You can see them
on the following Figure. Serial port pins are defined as UARTx_RX and UARTx_TX.

You can read serial port API on MATLAB on this


website, http://www.mathworks.com/help/supportpkg/beagleboneio/serial-port.html .
For testing, type theses commands on MATLAB Command Window.
>> board = beaglebone
>> board.AvailableSerialPorts
>> enableSerialPort(board,1)
>> board.AvailableSerialPorts

You should see serial port address, for instance, ‘/dev/tty01’.


The next step we are going to build a MATLAB program with Serial Port loopback.
7.2 Wiring
To build We connect UART1_TX (P9_24) and UART1_RX (P9_26) using cable.
The following is a sample of hardware implementation.
7.3 Writing a MATLAB Program
We can use read() and write() to read and write data on Serial Port. In this demo, we send
and read data on Serial Port.
Write these scripts.
function [] = serialport_loopback()
board = beaglebone();
enableSerialPort(board,1)
uart_address = '/dev/ttyO1';
finishup = onCleanup(@() exitprogram(board));
serial = serialdev(board,uart_address,9600);
k = 3;
m = 10;
n = 30;
disp('press Ctr-C to exit');
while 1
disp('datain: ');
dataIn = [k m n];
disp(dataIn);
write(serial, dataIn,'uint8');
dataOut = read(serial,3,'uint8');
disp('dataout: ');
disp(dataOut);
pause(1.5);
k = k + 1;
m = m + 1;
n = n + 1;
end

end

function exitprogram(b)
clear b;
disp('program has exit');
end

Save these scripts into a file, serialport_loopback.m.


7.4 Testing
Now you can run the program on Command Window from MATLAB.
>> serialport_loopback

A sample output program can be seen in Figure below.


8. Working with Web Camera

In this chapter I’m going to explain how to work with a camera on BeagleBone Black
board using MATLAB.
8.1 Getting Started
In this chapter, we work with a camera. You can use any web camera. To program a
camera from MATLAB on BeagleBone Black, you can use webcam object. Please read
this document, http://www.mathworks.com/help/supportpkg/beagleboneio/camera-
board.html .
To understand BeagleBone Black board pinouts, please see the following Figure.
8.2 Connecting a Camera into BeagleBone Black Board
For testing, I use Microsoft Lifecam VX-700 which is connected to BeagleBone Black
Board.

Connect a camera device to BeagleBone Black via USB Host.


The following is my hardware wiring.
8.3 Take a Picture from Camera
In this scenario, we will take a picture from a camera and show it on Figure dialog.
Write these scripts.
board = beaglebone();
wcam = webcam(board);
disp(wcam);
disp('snapshot');
for k = 1:100
img = snapshot(wcam);
imagesc(img);
drawnow;
end

clear wcam;
clear board;

Save these scripts into a file, called demo_camera.m.


Now you can run this program.
>> demo_camera

The program output can be seen in Figure below.

You should see the Figure dialog too.


8.4 What’s the Next?
The last program is to take a picture from a camera which attached on BeagleBone Black
board. The next step is to extend our image using image processing or applying machine
learning to detect objects.
You can use several Matlab toolboxs to work with your scenario. I recommend you to
learn about image processing and machine learning which refers to image processing.
9. Working with BeagleBone Black Linux Command

In this chapter I’m going to explain how to work with Linux shell on BeagleBone Black
board using MATLAB.
9.1 Getting Started
Linux shell is a user program or it’s environment provided for user interaction. Basically,
we can interact with BeagleBone Black Linux directly via SSH. In this chapter, we try to
access it via Matlab scripts.
Several APIs are related to BeagleBone Black Linux can be read
on http://www.mathworks.com/help/supportpkg/beagleboneio/linux.html .
9.2 SSH
Secure Shell (SSH), sometimes known as Secure Socket Shell, is a UNIX-based command
interface and protocol for securely getting access to a remote computer.
In this chapter, we try to access SSH on BeagleBone Black from MATLAB scripts. For
testing, we can open SSH connection using openShell() function. Write these scripts.
board = beaglebone();
openShell(board);

clear board;

Save these scripts into a file, called demo_ssh.m.


Now you can run this program.
>> demo_ssh

After executed the program, you will get SSH dialog, PuTTY console.

By defaule, username is root and password is <blank>/<empty>.


Try to run ls command on SSH console.
9.3 Linux Shell
We can call Linux shell inside MATLAB code using system(). For instance, ls command
to display a list of file and folder.
To understande about Linux shell, please read this document, http://linuxcommand.org .
Write these scripts.
board = beaglebone();
resp = system(board,'ls');
disp(resp);

clear board;

Save these scripts into a file, called demo_shell.m.


Now you can run this program.
>> demo_shell

You should see a list of file and folder on Home of Linux root from BeagleBone Black
Linux.
A sample of program output.
10. Measuring and Plotting Sensor Data in Real-Time

In this chapter I’m going to explain how to read data from sensor devices and plot it on
graph in real-time.
10.1 Getting Started
This section has an objective to show how to work with a real-time on measurement. We
read data from sensor devices and display it on graph.
Let’s start!.
10.2 Wiring
We use the same wiring from section 5.4.
10.3 Writing a Program
Now you run MATLAB and write these scripts.
function [] = sensing()
board = beaglebone();
enableI2C(board,1); % 'i2c-2'
i2c_address = 'i2c-2';
disp('press Ctr-C to exit');
h = figure(1);
finishup = onCleanup(@() exitprogram(board,h));

PCF8591 = '0x48';
PCF8591_ADC_CH0 = '40'; % thermistor
PCF8591_ADC_CH1 = '41'; % photo-voltaic
PCF8591_ADC_CH3 = '43'; % potentiometer
i2c = i2cdev(board, i2c_address, PCF8591);

hLine1 = line(nan, nan, 'Color','red');


hLine2 = line(nan, nan, 'Color', 'blue');
hLine3 = line(nan, nan, 'Color', 'black');
i = 0;
while 1
thermistor = read_adc(i2c,hex2dec(PCF8591_ADC_CH0));
pause(0.5);
photo = read_adc(i2c,hex2dec(PCF8591_ADC_CH1));
pause(0.5);
potentiometer = read_adc(i2c,hex2dec(PCF8591_ADC_CH3));
pause(0.5);

x1 = get(hLine1, 'XData');
y1 = get(hLine1, 'YData');
x2 = get(hLine2, 'XData');
y2 = get(hLine2, 'YData');
x3 = get(hLine3, 'XData');
y3 = get(hLine3, 'YData');

x1 = [x1 i];
y1 = [y1 thermistor];
x2 = [x2 i];
y2 = [y2 photo];
x3 = [x3 i];
y3 = [y3 potentiometer];

set(hLine1, 'XData', x1, 'YData', y1);


set(hLine2, 'XData', x2, 'YData', y2);
set(hLine3, 'XData', x3, 'YData', y3);
i = i + 1;
pause(1);
end

end
function adc = read_adc(dev,config)
write(dev,config);
read(dev, 1);
out = read(dev, 1);
adc = out;
end

function exitprogram(b,h)
clear b;
close(h);
disp('program has exit');
end

Save the program into a file, called sensing.m.


Basically, the program is similar to program from section 5.4. After read sensor data, we
display it using line object from MATLAB.
10.4 Testing
You can run the program by typing this command.
>> sensing

You should see sensor data on Figure.


Source Code

You can download source code on http://www.aguskurniawan.net/book/bbb_mat_982.zip .


Contact

If you have question related to this book, please contact me at aguskur@hotmail.com . My


blog: http://blog.aguskurniawan.net

You might also like