Low-code programming for the Arduino with Snap4Arduino
Cold Snap!
Snap4Arduino brings the power of low-code programming to the Arduino hardware environment.
Scratch [1], from MIT, is a graphical coding environment that was originally designed for young programmers. Scratch is bundled with many Raspberry Pi releases, and it lets you create digital stories, games, and animations that communicate with the Pi's General Purpose Input/Output (GPIO) pins. There also some options for using Scratch with Arduino modules, however most of these implementations are somewhat limiting.
Snap! [2], which was created at the University of California, Berkeley, is an extended implementation of Scratch. The major difference between Snap! and Scratch is that Snap! has a rich set of technical libraries. Some of these Snap! libraries include database and SQL interfaces, graphical trends, matrix manipulation, MQTT (standard messaging protocol for IoT), and Neural Net modeling. These additional libraries and other advanced features mean that Snap! is not just a teaching tool but is also ready to serve as a low-code alternative for IoT solutions.
The graphical low-code model is often useful in IoT environments, where less-experienced programmers are sometimes forced to adapt to the ideosyncracies of unfamiliar hardware. The Snap4Arduino [3] version of Snap! offers a unique set of libraries that will upload and configure Arduino modules without the user needing any Arduino knowledge or software.
You can run Snap4Arduino either from a web page or as a standalone Linux, macOS, or Windows app. In this article, I will show you three projects that will help you get started with Snap4Arduino. The first project allows the user to adjust the colors on a Neopixel array. The second project creates a dynamic bar chart that shows temperature and humidity values from a DHT11 sensor. The final project uses the SciSnap! library to show sensor data in a realtime line plot and then save the results to a CSV file.
Getting Started
The USB/serial communications between Snap4Arduino and the microcontroller module is via the Firmata protocol [4]. Firmata is a protocol designed to support communication between a microcontroller and a computer system. At project design time, the user can select which type of Firmata configuration they would like to install on their Arduino module.
The Snap4Arduino project offers an online web interface [5]. To get full functionality, you'll need to install a Chrome/Chromium plugin.
Desktop versions for 32- and 64-bit GNU/Linux systems are also available. It's important to note that there may be some compatibility issues when switching between the desktop and the web versions, so it's best to stick to one version.
After you launch the Snap4Arduino web page or desktop application, you can reach the reference manual by clicking on the top-left A icon (Figure 1). The Snap4Arduino interface has three main areas. The left pane contains a palette area of blocks. These blocks are grouped and color-coded together based on their function. The center pane is the scripting area, and that is where the logic is created by dragging and dropping blocks together in a downward flowing pattern. The top-right pane is the stage area, which shows the visual output when a script is run.
The first step in any project is to load the required Firmata configuration onto an Arduino module (Figure 2). The Snap4Arduino interface supports Arduino UNO and Mega-compatible modules for uploads. While you're loading the Firmata configuration, you'll also need to enable JavaScript extensions and Extension blocks in the settings drop-down menu.(Note: other modules such as: Nano, Leonardo, Micro, Due, NodeMCU, etc., are supported, but you'll need to install Firmata manually through the Arduino IDE.)
To prove that the communications are working, you can create a test script to blink the module's onboard LED (pin 13). Figure 3 shows a blink example. The script uses four Control
blocks (in orange), and three Arduino
blocks (in light blue). Communications are enabled by clicking on the Connect Arduino
button and then by selecting a port. (For most Linux systems the port will be /dev/ttyACM0
.)
This test script uses a for
block to cycle four times with a set digital pin
to toggle pin 13 on and off. Time delays are enabled with wait
blocks.
The Neopixel Project
Neopixels are individually addressable RGB color pixel arrays that come in strips, rings, and rectangular grids. From the previous test script, the Arduino module has already been loaded with the Neopixel firmata firmware. The next step is to import the Neopixel library into the Snap4Arduino project (Figure 4).
For this project, I used a 24-LED Neopixel ring (Figure 5), however, you could use any Neopixel array type. The alligator clips were used to connect the 5V, GND, and data pins (pin 6). My goal for this project was to allow the user to adjust the background color of the LEDs while a red LED circles around the ring. The script logic (Figure 6) starts by setting the Arduino connect port and defining 24 Neopixel LEDs on data pin 6. Four variables are defined: pin, Red, Green, and Blue. On the right stage pane, the color variables are defined with user-adjustable sliders.
Two for
loops are embedded within a forever
loop. The inner for
sets all the LEDs to the values from the color variable (Red, Green, and Blue) sliders. The outer for
loop moves the red LED around the circle every 0.5 seconds. When the script is running, the pin
variable will show the position of the red circling LED.
On the top right of the Snap4Arduino screen are three icons to start (green flag), pause, and stop the script from running. You can also start scripts by clicking on the top of the logic when green flag clicked
block.
DHT11 Temperature and Humidity Sensor Project
A DHT11 Sensor can be connected via expansion boards or by manually wiring the sensor to Arduino pins. Note, the Firmata firmware sets the DHT11 data pin on pin 4.
For my testing I used a TdR-STEAM-compatible board (Figure 7). These expansion boards are relatively inexpensive (~$12) and they offer two color LEDs, a Neopixel LED, a buzzer, a light sensor, a DHT11 sensor, and two input buttons.
This project uses a different Firmata configuration, so the Arduino firmware needs to be re-uploaded with the (recommended) FirmataSA5 tone firmware.
Next, Snap4Arduino requires you to import two libraries (Figure 8), the Bar charts and TdR STEAM v2 libraries. The Bar charts library expects the data to be in a table or a CSV file. Tables can be created in Snap! by making a variable a list of lists.
A Readings
variable can be made into a 2x2 table by using three list
blocks and a set
block (Figure 9). The plot bar chart
block (which is found in the Pen section) is passed the Readings
variable, along with x-y coordinates, width, and height values.
When these three blocks are run, the stage will show the Readings
variable updated with the table information and a gray, static bar chart will appear. The final step is to make the bar chart dynamic with DHT11 sensor data. For this I create two additional variables, humidity
and tempC
, and I position the variables around the bar plot stage (Figure 10).
The two new variables are updated with sensor data by using set
blocks to read TdR temperature
and TdR humidity
blocks. The chart color is defined by the set pen color
block. Before a new bar chart can be drawn, a clear
block is required.
When the final script is run, the bar chart and variables are refreshed with new sensor data every five seconds. Some good future features could include adding a time value to the chart or a toggle between degrees C and F.
In the next project, I'll look at how to save sensor data to a CSV file.
Buy this article as PDF
(incl. VAT)