The popular BeagleBone Black single board computer has proven itself useful in a wide range of applications, both for amateur tinkerers and professional engineers alike (as we touched on in a previous blog post). Our latest BeagleBone Black tutorial uses the popular portable graphing program Gnuplot, to gather and visually represent data from any connected sensor.
Whether you simply want to plot a few data points collected from a basic temperature sensor, or are looking to visualize more complex information from a custom embedded application, this tutorial will show you the important steps in setting up a simple data acquisition and plotting system using the BeagleBone Black.
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!).
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.
Open a terminal window to enter the installation commands.
Update and upgrade the Debian distribution (if you have not done this in a while).
On the Host computer start Ultra VNC (Virtual Network Connection) viewer.
Start gnuplot . Open a Terminal window and enter.
Gnuplot can run in interactive or script mode.
Let’s Try A Few Interactive Commands
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:
print 'Datalogging Started.... Press+HOLD BUTTON to STOP'
print ('\r'+ str(index))
adc = ADC.read_raw(adcPins[i])
f.write(', '+ str(adc))
#--- CleanUp and Exit ---
print ('Data file: '+ myfilename + ' created.\n')
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.
We can do more!!!
Why not plot data automatically too!
Download plotMyData.gp File
and save this file as plotMyData.gp (without the *.txt extension) to a working location on your BeagleBone.
plot 'myData.dat'using1:2 lt 1 lw 2 title "AIN0"with lines
replot 'myData.dat'using1:3 lt 2 lw 2 title "AIN1"with lines
replot 'myData.dat'using1:4 lt 3 lw 2 title "AIN2"with lines
replot 'myData.dat'using1:5 lt 4 lw 2 title "AIN3"with lines
replot 'myData.dat'using1:6 lt 5 lw 2 title "AIN4"with lines
replot 'myData.dat'using1:7 lt 6 lw 2 title "AIN5"with lines
replot 'myData.dat'using1:8 lt 7 lw 2 title "AIN6"with lines
pause 10 "Pausing...Done"
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).
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 plotMyData.gp
And to run it:
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
Richard worked as the Senior Electrical Engineer at Logic Supply before relocating with his family in 2015. A general problem solver and well-known "Lab MacGyver", Richard is always on the lookout for creative approaches to design solutions and continues to explore new opportunities to build creative solutions to common challenges.