The code running on Raspberry PI’s within the gasworks project (An art installation that loosely mimics brain cells as clusters of lights) is all written in Golang. While, the hardware architecture for each of the neurones has a Raspberry PI sending commands to an Arduino over serial. This communication link was one of the first things I prototyped for the project.

The thing that tripped me up when prototyping the communication code was that I wasn’t able to immediately pump data down the serial connection to the Arduino, unless I had the Arduino serial monitor open.

When making a serial connection to an Arduino it automatically (unless it is a new Arduino Leonardo) resets it (similar to what happens when you press the reset button). It then takes about a second for the bootloader on the Arduino to do it’s thing and get into a state where it is able to accept data over the serial port.

I worked around this in Golang a little inelegantly by sleeping for a second, however it is possible to disable the Arduino reset on serial connection with a simple hardware hack.

The communication protocol I used between the Raspberry PI and Arduino was very simple. Each command is five bytes, the first byte being the command identifier, with the four remaining bytes reserved for a single mandatory float argument (that could be ignored if necessary on the Arduino).

Packaging up commands and sending them over the wire was pretty easy with the binary encoding package bundled into the Golang standard library. It was a case of encoding the argument into a byte buffer, then looping over the bytes in both the command and argument byte buffer, writing them to the serial port:

Hi! Subconsciously you already know this, but let's make it obvious. Hopefully this article was helpful. You might also find yourself following a link to Amazon, Lego or eBay to learn more about parts or equipment. If you end up placing an order, I make a couple of dollarydoos. We aren't talking a rapper lifestyle of supercars and yachts, but it does help pay for the stuff you see here. So a massive shoutout to everyone that enables this place. Thanks!