An Uno and Golang

Computer Science in Scotland is now falling faster than virtually any other subject, but the demand for software and cyber skills…

An Uno and Golang

Recently, I saw a posting about installing a light weight version of Golang on an Arduino Uno, and I was hooked. Overall, it’s beautiful, and takes me back to the 1980s when electronics was fun:

And so, I don’t want to develop using Sketch and with the default GUI, so it was off to Golang for me [here]:

And, as I have Mac, it’s an easy install:

$ brew tap tinygo-org/tools
$ brew install tinygo
$ brew tap osx-cross/avr
$ brew install avr-gcc
$ brew install avrdude
$ git clone --recursive https://github.com/tinygo-org/tinygo.git
$ cd tinygo
tinygo$ brew install llvm
tinygo$ go install

And so with the code of:

package main
// This is the most minimal blinky example and should run almost everywhere.
import (
“machine”
“time”
)
func main() {
led := machine.LED
led.Configure(machine.PinConfig{Mode: machine.PinOutput})
for {
led.Low()
time.Sleep(time.Millisecond * 500)
led.High()
time.Sleep(time.Millisecond * 500)
}
}

We have a quick look at the devices at /dev, and identify the serial port for USB. In my case, it was /dev/tty.usbmodem14411. It’s then a simple task to flash the Uno with:

(base):~/tinygo$ tinygo flash -target=arduino -port=/dev/tty.usbmodem1411 blinky1.go
avrdude: AVR device initialized and ready to accept instructions
Reading | ################################################## | 100% 0.00s
avrdude: Device signature = 0x1e950f (probably m328p)
avrdude: NOTE: “flash” memory has been specified, an erase cycle will be performed
To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: reading input file “/var/folders/ft/yt166pln0pdf36_vh8lhh_w80000gn/T/tinygo828716304/main.hex”
avrdude: writing flash (558 bytes):
Writing | ################################################## | 100% 0.10s
avrdude: 558 bytes of flash written
avrdude: verifying flash memory against /var/folders/ft/yt166pln0pdf36_vh8lhh_w80000gn/T/tinygo828716304/main.hex:
avrdude: load data flash data from input file /var/folders/ft/yt166pln0pdf36_vh8lhh_w80000gn/T/tinygo828716304/main.hex:
avrdude: input file /var/folders/ft/yt166pln0pdf36_vh8lhh_w80000gn/T/tinygo828716304/main.hex contains 558 bytes
avrdude: reading on-chip flash data:
Reading | ################################################## | 100% 0.08s
avrdude: verifying …
avrdude: 558 bytes of flash verified
avrdude done. Thank you.

And the Uno flashes! Pweh … in just minutes, I had a flashing light on my Uno … and which was 100 times more interesting than creating a database to order pizzas.

And then, with this program, an LED is on, until you detect a button press connected to the output pins, and it will go off:

package main

import (
"machine"
"time"
)

// This example assumes that the button is connected to pin 8. Change the value
// below to use a different pin.
const (
led = machine.LED
button = machine.Pin(8)
)

func main() {
led.Configure(machine.PinConfig{Mode: machine.PinOutput})
button.Configure(machine.PinConfig{Mode: machine.PinInput})

for {
if button.Get() {
led.Low()
} else {
led.High()
}

time.Sleep(time.Millisecond * 10)
}
}

Conclusions

Go do go …