Golang and Ruby on the Arduino

Raza Z Sayed
3 min readMar 13, 2021

The arduino microcontroller board comes with its own programming language which is used to program the microcontroller on the board. This default language is based on a language called Wiring.

The programs or sketches as they are called, written using the arduino programming language can be uploaded to the microcontroller and get stored in its flash memory. Now depending on the type of the microcontroller on the board the max program size can vary, for example from 16 KB to 256 KB for the ATMega series of microcontrollers. For example,the arduino i have has a ATMega328P microcontroller,so the max program size for it is 32 KB.

This much memory is sufficient for programs written for most embedded systems, however sometimes you wish to write programs with a much larger memory footprint. So, in this scenario wouldnt it be great if we could harness the power of our PC to do the complex processing while the Arduino can be the platform for the sensors and motor controls ?.

Well, this is certainly possible !.However, the drawback to this approach is that you now cannot run the arduino in a standalone mode and it needs to be tethered to a PC/laptop using USB cable or over Wifi since the program is running on your PC instead of directly on the microcontroller.The advantage is that you now can write programs of size much larger than the flash memory storage limit and can also use different programming languages for programming the arduino. For example below i show two examples, one written in Go and the other in Ruby.Now, isnt this fun ? 😊

The technology that allows us to do this is the Firmata protocol.The Firmata website describes it as a generic protocol for communicating with microcontrollers from software running on a host computer.Since its a generic protocol so in addition to arduino it is also available for other physical computing platforms like beaglebone, rasberrypi, edison etc. The way this works is that the firmata firmware is first uploaded to the microcontroller.Then any programming language with a firmata adapter can be used to communicate with the microcontroller using this protocol over the serial port.This adapter is available for different programming languages through frameworks like Gobot for Go, Artoo for Ruby, Cylon.js for Javascript etc.

So,in order to write programs in Go,Ruby or any other supported language one first needs to install the firmata firmware on the arduino. Note that doing this will overwrite any other program previously loaded on the arduino as a microcontroller can run only one program at a time.There are atleast a couple of ways of doing this. One is by using the arduino ide and going to File->Examples->Firmata->StandardFirmata and then uploading this program on the microcontroller.

The other way is to use something called Gort which is a command line tool to scan for connected devices,upload firmware etc. On my machine i was able to scan for connected devices but had no luck uploading the firmata firmware to my arduino duemilanove, though this might work with other arduino versions. I used the arduino ide to upload the firmata firmware on the arduino microcontroller board.Once this is done we can then write the program in any supported language of our choice and run it the way programs are run for that particular language.

And,now for the fun part. Below are two code snippets, one written in Go using the Gobot framework and the other in Ruby using the Artoo framework. Both do the same thing, that is blink the LED on pin 13 on the microcontroller board on and off every 1 second.

Gobot

Artoo

Since im on an OSX machine, in Artoo i had to use a cu prefixed address for my serial port instead of a tty prefixed one as in Gobot. This has been mentioned on the artoo-arduino github project page.Hope this post gives you some insight into programming this powerful hardware platform called arduino using languages other than the default one. Have fun and happy hacking ! 😊

--

--