Beaglebone Black Programming Using Matlab - Compress
Beaglebone Black Programming Using Matlab - Compress
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
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
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).
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.
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.
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)
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:
To get information about BeagleBone Black GPIO, you can write these scripts.
board = beaglebone;
showPins(board);
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
Save those scripts into a file, called blinking.m. Now you can run it.
>> blinking
3.3.1 Wiring
The following is hardware wiring:
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
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:
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.
Note:
Pin 1: Red
Pin 2: Common pin
Pin 3: Green
Pin 4: Blue
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
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
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
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
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
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
clear board;
On Command Window, you should see I2C address of sensor device. For instance, my
sensor was detected on 0x48.
end
function exitprogram(b)
clear b;
disp('program has exit');
end
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.
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
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.
end
function exitprogram(b)
clear b;
disp('program has exit');
end
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.
clear wcam;
clear board;
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;
After executed the program, you will get SSH dialog, PuTTY console.
clear board;
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);
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];
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