The Biomaker starter kit contains a number of sensor devices that allow the construction of a variety of simple instruments for monitoring laboratory reactions, culture conditions and environment. In this tutorial, we will examine some ways that time-series of sensor data can be displayed and stored. The tutorial draws on a XOD guide at: https://xod.io/docs/guide/sd-log-example/ - adjusted for Biomaker Starter Kit hardware, and expanded to include the 4D Systems touchscreen.
The example describes how to store sensor data and process it on the computer. We are going to read position data and save a time sequence of values on a microSD card. After this the microSD card can be connected to a computer (or the internet) for analysis and display.
Each Biomaker Starter Kit contains a HC-SR04 sonar device, pictured above. In addition, the xod/common-hardware library contains nodes that support the use of this sensor. The library contains two XOD nodes that allow calculation of (i) the travel time of for an acoustic pulse (hc-sr04-ultrasonic-time) and (ii) the range of an object in metres (hc-sr04-ultrasonic-range).
The sensor device contains both transmitter and receiver elements. The ultrasonic transmitter emits pulses of inaudible 40,000 Hz sound waves which travel through air, and will bounce back to the module if there is an object or obstacle in the path. The module has a receiver that allows measurement of the travel time. Knowing the speed of the sound allows calculation of the distance. The device can be used for measuring water levels, movement of organisms and proximity for robots.
In order to be able to easily connect a number of various devices - first plug the 4D Systems touchscreen adapter Arduino shield to the Biomaker Rich UNO R3 board. Then attach the Starter Kit Prototyping shield (https://www.aliexpress.com/item/32419802380.html) on top of this. The mini prototyping board on the top shield allows easy connection of new components.
Plug the HC-SR04 ultrasonic sonar module into one side of the mini breadboard. (An excellent guide to the use of electronic breadboards can be found at: https://learn.sparkfun.com/tutorials/how-to-use-a-breadboard/all, if necessary). The sonar module has 4 pins, Ground, VCC, Trig and Echo. The GND and the VCC pins of the module need to be connected to GND and 5V pins on the Arduino prototyping board (a series of pin connectors can be found adjacent to the breadboard). The Trig and Echo pins should be connected to digital I/O pins on the Arduino board (e.g ports D2 and D3). For more information about the HC-SR04 device, see: (https://components101.com/ultrasonic-sensor-working-pinout-datasheet).
The standard xod/common-hardware library provides two nodes that can be used to obtain values from the sonar device. The hc-sr04-ultrasonic-range node provides the last measured range in metres. The hc-sr04-ultrasonic-time node provides the roundtrip time of the last pulse in seconds. The identities of the digital ports for the connected Tigger and Echo lines must also be defined, and choden to avoid clashes with other hardware.
Note: Ports D0 and D1 are used for serial communication between computer and Arduino, D2, D3 and D4 are used for communication between the Arduino and the 4D Systems touchscreen. Ports D10 D11 are used for communication with the onboard 4-digit display. Ports D7 and D8 are used for communication between the Arduino and the onboard mp3 player, which we won't use, and can switch off via the Rich UNO R3 DIP switch - so these ports make a good choice for the HC-SR04.
After the HC-SR04 sensor has been wired up to the Arduino prototyping shield, test that it is functioning by uploading simple XOD patches that are provided in the BiomakerTutorial5.xodball file (available to download from a link at the bottom of this page).
The patches include a clock node to set a 0.1 second sampling rate, and read either the range (metres) or the time (seconds) for pulse return measured by the sensor. A format-number node and watch node allow display of the values with 4 decimal places.
Showing data values on the onboard 4-digit display
Two other patches are provided. The 03-sonar-to-4digit-display patch multiplies the output of the hc-sr04-ultrasonic-range node by 100, to provide centimetre units, and sends this to the onboard 4-digit display, using similar code that used in Tutorial 4. The HC-SR04 sensor provides reasonably accurate measurements in the range of 5-120 cm, although this will depend on the size, shape, reflectivity of the target.
Showing data on the touchscreen display
In addition, the 04-sonar-to-touchscreen patch also reuses code introduced in Tutorial 4 to communicate with the 4D Systems Gen4 32DT touchscreen. Here the clock node is used to control the rate of reading the sensor. The multiply node is used to convert the range to centimetre units. This value is then distributed via the bus named DIST (distance) to a watch node and to the onboard TM1637 4-digit 7-segment display, which is connected to ports D10 and D11.
The touchscreen has been reprogrammed to recalibrate the gauges and meters, to allow the display of distances measured by the HC-SR04 sensor, allowing for a range of 0-200 centimetres. Code for the 4D Systems Workshop is provided below.
The raw values from sensors like the HC-SR04 can be noisy, with values varying over time. The gweimer/utils/ XOD library provides a running-avg node that may be useful where successive values can be accumulated, and a running average established for a set number of values (https://xod.io/libs/gweimer/utils/). This can help provide a smoothed set of values, and minimise short term fluctuations that can interfere with some forms of display. Data is collected and sampled each 0.1 seconds in the 05-data-sampling patch, and both raw and 10x averaged data are fed to watch nodes in this example. The rate of sampling and number of accumulated values can be altered to experiment with different datastreams.
The Visi-Genie environment in 4D Workshop allows the insertion of a Scope object into a form that can be loaded into a 4D Systems programmable touchscreen - along with other widgets. The write-scope node from the XOD bradzilla84/visi-genie-extra-library can be used to display a time series of data, similar to an oscilloscope trace. In this example, data from the ultrasonic range-finder sensor was collected at 10 Hz, and a running average of 5 datapoints was sent directly for display on a scope object. The X-axis scale was 1 pixel per datapoint, and the Y-axis scale was 90 pixels per 1 metre distance.
In order to collect a premanent record, data can be bound to a timestamp when it is read. For example, one can use a
system-time node, which outputs time in seconds passed since the start of the XOD program. To update the data continuously we can number each new record. The
count node will do this. Place it onto the patch and set the
STEP value to 1.
A raw data source, system-time and count nodes are placed in the same patch. This example, taken from https://xod.io/docs/guide/sd-log-example/, uses thermometer sensor, but the same principles apply for distance measurements of other sensor data.
We need to set a frequency of readings to limit the data volume and to give some time for the microSD board to flush the file after each record. The easiest way to do this is to use a
clock node. We place it onto the patch and link with all three data sources.
IVAL pin is responsible for the pulse generation frequency and, as a consequence, for extracting a new set of data. We decided to receive and record data 4 times per second, so we set
IVAL value to 0.25 (=¼).
We predict lots of data. For easier processing it is a good idea to format it as a table. Each row may represent a single record: the record number, record timestamp, and a temperature value read at that moment. To combine all data values and store them as a single string, use the
join node. Place
join onto the patch and link it with all data sources.
D pin of the
join node is used to delimit the values. With this pin you can set the a character or string you want to delimit the values.
There are numerous text-based formats for storing and exchanging data. The examples of such formats are CSV (Comma-Separated Values) or TSV (Tab-separated values). These text formats are used to store tabular data and to exchange it between different computer programs. Notably, spreadsheet applications such as Google Spreadsheets, Microsoft Excel, LibreOffice Calc can easily import such files. Look at a CSV file example which defines track durations for a music album:
No.,Title,Length1,Hells Bells,5:122,Shoot to Thrill,5:173,What Do You Do for Money Honey,3:354,Givin’ the Dog a Bone,3:315,Let Me Put My Love into You,4:156,Back in Black,4:157,You Shook Me All Night Long,3:308,Have a Drink on Me,3:589,Shake a Leg,4:0510,Rock And Roll Ain’t Noise Pollution,4:15
So, let’s use the TSV text-format for storing. To delimit the values with tabs place the
\t character into the
D pin of the
join node. The backslash-t is a special escape-sequence to denote the
TABcharacter because it can’t be easily printed and displayed in IDE.
XOD provides an
sd-log node to append text lines to the tail of the specified file stored on an SD card. Add the
sd-log node onto the patch and set up its inputs:
- Link its
LINEpin with the
CSpin is used to set up the number of the chip select pin of the SPI interface on the Arduino board. In this example it is
FILEpin points to a particular file on the microSD card. Let’s name our data file
Wpin triggers a new write. Connect it with the
DONEpin of the thermometer. With this link the
sd-logmakes a new record only when a new temperature value appears.
Here is the resulting program patch.
Upload the program to your board and optionally power it through a battery. You may connect and disconnect the device from the power source as many times as you wish. On every boot the log will be continued, so the data from previous sessions won’t be lost.
The ambient temperature changes slowly. To test the logger we force faster changes: we cool the thermometer in a fridge and heat it with a dryer.
When enough data is collected, take off the microSD card.
temp-log.txt file on your computer to see what is inside.
As you see, we’ve got a table with three columns and many rows. One row is for a single sample. The first column stores the record number (just ignore the
00 fraction part), the second column is the time value in seconds, and the last one is the actual temperature in Celsius. You may use various applications to analyze the data. For our example, let’s use Google Spreadsheets.
You can move data values to the spreadsheet in different ways:
- Select all the data from the
temp-log.txtfile by pressing CTRL+A in your text editor. Copy what you’ve selected by pressing CTRL+C. Create a new Google Spreadsheet document, select the first cell in the table and press CTRL+V.
- Create a new Google Spreadsheet document. Hit
File -> Import.. -> Upload. Select the
temp-log.txtfile from the microSD card or drag-and-drop it into the field. Select the
Tabseparator type and import the data.
Once you input the data, you can visualize it.
- Create a blank chart by hitting
Insert -> Chart.
- Choose the
Chart type. We chose the
Smooth line chart.
- Edit the
X-AXISand choose the time column as data.
- Edit the
SERIESand choose the temperature column as data.
CUSTOMIZE -> Chart & axis title.
- Input the titles for the vertical and horizontal axes. We named them
- Set the minimum and maximum bounds for the vertical and horizontal axes. We set the
(10,90)bound for the
Temperature, Caxis and
- You can change the color of the line by hitting
Seriesand choosing the color.
On the graph, you can clearly see the temperature change after our manipulations with the thermometer.
If you have got problems trying to repeat the guide, download the prepared project and open it in the IDE.
With the microSD card, you can store a significant amount of data that can not be stored in the memory of the controller. You can use any other sensor or even several different sensors to log and observe a physical process of your choice.
To improve a logger, use some
delay nodes linked to
DONE outputs so that your device clearly shows whether the records are coming in without any problems or logging fails due to, for example, out of space, bad SD card or wrong connection.
Load the XOD library: wayland/vl53l1x-time-of-flight. This will provide support for a low-cost pulsed laser rangefinder, which is based on a 930nm IR laser and array of avalanche photodiodes and capable of accurate time-of-flight measurements.
The DS18B20 chip is a digital thermometer that provides 9-bit to 12-bit Celsius temperature measurements (and has an alarm function with nonvolatile user-programmable upper and lower trigger points). It can be obtained as a waterproof sealed unit with a 1M lead. The DS18B20 chip is contained in the stainless steel tip, and communicates over a 1-Wire bus that requires only one data line (and ground) for communication with the Arduino. In addition, the DS18B20 can derive power directly from the data line (“parasite power”), eliminating the need for an external power supply. Each DS18B20 has a unique 64-bit serial code, which allows multiple sensors to be placed on the same 1-Wire bus. XOD provides a node in the "common hardware" library that can read single 18DSB20 devices in a circuit.
Download Arduino UNO and Multipurpose Shield pinouts (PDF. 1. MB)
The sensor measures temperatures from -55°C to +125°C with better than ±0.5°C Accuracy from -10°C to +85°C (see graph) - the chip is internally calibrated and returns a digital measurement that can be displayed directly. (For more information, see: https://create.arduino.cc/projecthub/TheGadgetBoy/ds18b20-digital-temperature-sensor-and-arduino-9cc806)
One challenge is to devise and implement in XOD a temperature alarm system using the Arduino prototyping system, DS18B20 sensor, LCD panel readout and buzzer.
How to wire the DS18B20 sensor to an Arduino. (You will be using a waterproof, encapsulated version with connecting wires: red = 5V. black = GND, yellow = DATA)
XOD node for DS18B20 digital thermometer