Golang and the Arduino

It is beyond me why we don’t give every child an Arduino Uno, and get them coding. Imagine the great engineers that we could create?

Golang and the Arduino

It is beyond me why we don’t give every child an Arduino Uno, and get them coding. Imagine the great engineers that we could create?

So, here’s my Arduino Uno:

And, as my favouriate programming language is Golang, there’s only thing for me to install first … Golang [here]:

And, as I have Macbbook, 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 can then 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 is then a simple task to flash the code onto the Uno with:

(base):~/tinygo$ tinygo flash -target=arduino -port=/dev/tty.usbmodem1411 blinky1.goavrdude: AVR device initialized and ready to accept instructionsReading | ################################################## | 100% 0.00savrdude: 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.10savrdude: 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.08savrdude: verifying …
avrdude: 558 bytes of flash verifiedavrdude 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, the LED stays 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

Get serious about your coding …