PLEASE NOTE: This site is no longer being actively maintained. For frequently updated BeagleBone resources please visit

Thursday, July 3, 2014

On 2:19 PM by Richard St-Pierre


Make publication quality data plots, the easy way!  Automate your data collection and presentations. The BeagleBone, with its generous GPIO (General Purpose Input/Output) connections and measurement capability is a perfect computer for collecting data.  Combine it with a powerful plotting program and you have found a new friend to help you get work done faster.

Gnuplot (pronounced "new plot" - unlike typical GNU-based programs) is a portable graphing utility for many computing platforms.  Created for scientists and students, it allows you to quickly visualize data.  As a plotting engine, it can take a data file directly and produce a plot file, skipping the traditional manual steps of office tool suites.

This tutorial will take you step-by-step in an introduction to gnuplot on the BeagleBone Black running Debian Linux. You will learn how to collect measurement data, create a data file, and create a customizable graph of the results automatically (good puppy!).

What you will need


This project demonstrates how to get started with gnuplot on the BeagleBone Black running the Debian Linux distribution.  A graphical interface is desirable (but not mandatory). You can run the BeagleBone with a display and keyboard or connect via a VNC (Virtual Network Connection).

NOTE: With a non-graphical interface (using putty etc), you can still use gnuplot and send your 'terminal' output  to image files.

Step 1

Open a terminal window to enter the installation commands.
Update and upgrade the Debian distribution (if you have not done this in a while).
Then install the gnuplot plotting program:

sudo apt-get update && upgrade
sudo apt-get install gnuplot
sudo apt-get install gnuplot-x11

Step 2

Skip this step if you are directly connected with keyboard and display (go to STEP 3).

Verify that VNC is correctly installed. This will install x11vnc on Debian (if you need it).

sudo apt-get install x11vnc

On the BeagleBone side start VNC. {hint: copy-paste}

x11vnc -bg -o %HOME/.x11vnc.log.%VNCDISPLAY -auth /var/run/lightdm/root/:0 -forever 

On the Host computer start Ultra VNC (Virtual Network Connection) viewer.

Step 3

Start gnuplot . Open a Terminal window and enter.


Gnuplot can run in interactive or script mode.

Let's try a few interactive commands

plot sin(x)

This will plot display a 'sine' function (with only one command!)

If you do not see any display...try:

set term dumb

This will set the output to text mode, and allow you to see 'something' (although not pretty). You can also use this if you are using a non-graphical interface. You can also direct the output to a file.

set term png
set output "myplot.png"

This will create an output file with your data plot.

We can explore other commands

A title would be nice:
plot sin(x) title "MyTitle"

A label for the x axis:
set xlabel 'time'

And one for the y-axis:
set ylabel 'amplitude'

This will refresh the plot:

This command will show the linetypes support by the current terminal:

Refer to the GnuPlot User Manual for more commands.

Going Further

Now that we have a taste of gnuplot, let's automate the process for consistent and repeatable results.

The BeagleBone has seven 'analog' inputs that can measure up to a maximum of 1.8 volts (for larger voltage we can use a voltage divider).
As a demonstration, we will collect up to seven independent measurements and display the data with gnuplot.


Using a simple program we can automate the data-acquisition and store the results to a file.

Make sure you have Adafruit_BBIO python library installed:

sudo apt-get update && upgrade
sudo apt-get install build-essential python-dev python-pip -y
sudo pip install Adafruit_BBIO

Download File
and save this file as (without the *.txt extension) to a working location on your BeagleBone.

Program Listings

# Logic Supply Tutorial for the BeagleBone Black ARM Cortex A8
# visit 
# Author : Richard St-Pierre
# Version: 1.0  July 1, 2014  Initial version
# -------------------------------------
# This Python program waits for a Button push, 
# then starts recording all 7 analog inputs
# to a user file at fixed time interval.  
# Logging ends with a Button Push.
# The data file can then be plotted with gnuPlot.  
# NOTE:  Unconnected inputs will 'float' 
# unless connected to ground (zero volts).
# ------------------------------------- 

import Adafruit_BBIO.ADC as ADC
import Adafruit_BBIO.GPIO as GPIO
import time

#--- Define Pins ---

led_G    = "P8_19"         # in series with 330 Ohms resistor
buttonSw = "P8_26"         # in series with 330 Ohms resistor 
adcPins  = ["P9_39","P9_40","P9_37","P9_38","P9_33","P9_36","P9_35"] #AIN0-AIN6

#--- Define Variables ---

myfilename = "myData.dat"  #filename to store data
index      = 0             #counts datapoints  
interval   = 1             #seconds

#--- Setup Pins ---

GPIO.setup(buttonSw, GPIO.IN)
GPIO.setup(led_G, GPIO.OUT)
GPIO.output(led_G, GPIO.LOW)

#--- Display Program Info ---

print '\nLogic Supply --- Data Acquisition Tutorial'
print 'visit:\n'
print '\n=== PRESS BUTTON to START dataLogging ==='

#--- Open DataFile ---

with open(myfilename,'w') as f:
    f.write('#Logic Supply Data Acquisition Tutorial')
    f.write('\n#CH  AIN0  AIN1  AIN2  AIN3  AIN4  AIN5  AIN6')

    GPIO.wait_for_edge(buttonSw, GPIO.FALLING)     #wait for button push
    GPIO.wait_for_edge(buttonSw, GPIO.RISING)      #wait for button release

    print 'Datalogging Started.... Press+HOLD BUTTON to STOP'
    GPIO.output(led_G, GPIO.HIGH)
    while (GPIO.input(buttonSw)):

        f.write('\n'+ str(index))                
        print  ('\r'+ str(index))

        for i in range(len(adcPins)):
            adc  =  ADC.read_raw(adcPins[i])
            f.write(', '+ str(adc))

        index +=1
#--- CleanUp and Exit ---

GPIO.output(led_G, GPIO.LOW) 
print ('Data file: '+ myfilename + ' created.\n')

Program Overview

To run the program start putty or open a terminal (in the directory where you downloaded the program) and enter the following:


Press the button to START, data logging.

To STOP press and HOLD the button (for the duration of one full sampling interval).

This will create a data file to plot.

This program uses the Adafruit_BBIO python library. We then declare pins and variables.
'myfilename' and 'interval' values can be changed to suit your needs. Next we setup the required pins.
A few instruction messages are displayed. A heading in the data file is helpful.
The '#' symbol is used as a comment in the data file, so it is not confused for data.

We are ready to begin. Once the button is pushed (and released),
we can go ahead and make data measurements which we store to file. A green LED is turned ON as a reminder that data collection is in progress.

We take raw readings which range from 0-to-1800 (for an input of 0-to-1.8V max) on "AIN0-AIN6".
The first data column is an index or sample number. Finally, we wait a user defined interval,
until it is time for another measurement. This routine will loop until the button is pressed.

With data automatically measured, collected and recorded, we have greatly improved the efficiency of making measurements.

To view the data collected.

more myData.dat

We can do more!!!
Why not plot data automatically too!

Download File
and save this file as (without the *.txt extension) to a working location on your BeagleBone.

GnuPlot Script Example

# Logic Supply Tutorial
# Richard St-Pierre
# Gnuplot demo on BeagleBone Black/Debian
set yrange[0:100]
set mytics 10
set datafile separator ","
set title  "Logic Supply Tutorial\n"
set xlabel "Data points"
set ylabel "Analog Input [mV]"
set timestamp "%B %d %Y %H:%M" top
set grid
plot 'myData.dat' using 1:2 lt 1 lw 2 title "AIN0" with lines
replot 'myData.dat' using 1:3 lt 2 lw 2 title "AIN1" with lines
replot 'myData.dat' using 1:4 lt 3 lw 2 title "AIN2" with lines
replot 'myData.dat' using 1:5 lt 4 lw 2 title "AIN3" with lines
replot 'myData.dat' using 1:6 lt 5 lw 2 title "AIN4" with lines
replot 'myData.dat' using 1:7 lt 6 lw 2 title "AIN5" with lines
replot 'myData.dat' using 1:8 lt 7 lw 2 title "AIN6" with lines

set terminal png
set output "myPlot.png"
pause 10 "Pausing...Done"

Program Overview

This gnuplot script automates the dataplotting. Let's start fresh with a reset.
We then set the range for 'y',and  let gnuplot know our datafile has ',' to separate the data results. We would like to add titles and labels to our graph. Why not include a date and time stamp for our results.
I like a grid on my plot.

Now we can dig into the data. We plot the input file 'myData.dat' using the first column for 'x' and second for 'y'. The linetype is set to 1 (see 'test' above for sample), and the linewidth to 2. The first data series gets a title of "AIN0" and we would like a line to join the points. We repeat this command for each column we want to see on the graph.

Since we like to do more... we will also create a 'png' image file (that we can add to a report, or email). The replot command will refresh and send the output to this images file. After all this we take a pause (so that the plot remains on the display for a few seconds).

Step 5

Now that we have logged our data to a file, we can easily plot it .
(Use a VNCviewer from STEP 2 to allow graphical data to be shown.)


If you would like to simplify even more you can make the 'script' executable:

sudo chmod a+x

And to run it:

sudo ./ 


Putting it all together!
We can perform data-acquisition, data-logging and plotting in a single step.
We will create a script file to link all operations in one place.

Create a new file using 'nano' or your favorite editor

Enter the following
sudo python
The first line identifies the file as a bash file.
We then call the data acquisition python program.
Finally, plot the results and display the graph.

Make the file executable
chmod +x

Run the full data logging, and plotting in one command.

In a single command, you can acquire data, record it and plot it !

Going Even Further

For longer delay between datalogs (hours, days) consider setting up a CRON job.

You can customize the plotting program, by passing in filename variables, sample rates and more.

Hope you are now 'plotting' new data acquisition projects!