Using TinyGo to program the Arduino Nano 33 IoT, demonstrating hardware control and internet connectivity.
TinyGo uses Go to build. And Go's a great thing to have around anyway.
sudo apt update
sudo apt-get install golang-1.13-go
brew update
brew install go
Install from MSI or source https://golang.org/dl/
Use the links below to understand how to get TinyGo and the AVR microcontroller dependencies installed on your system.
https://tinygo.org/getting-started/linux/
From that page follow instructions to:
- Install TinyGo itself
- Install Clang 9
- Install AVR tools for Arduino
brew tap tinygo-org/tools
brew install tinygo
brew tap osx-cross/avr
brew install avr-gcc avrdude
https://tinygo.org/getting-started/windows/
go get -u tinygo.org/x/drivers
go get -u github.com/eclipse/paho.mqtt.golang
sudo apt install \
libwxgtk3.0-dev
libreadline-dev
git clone https://github.com/shumatech/BOSSA.git
cd BOSSA
make
The Linux and macOS device subsystems have subtle differences. In order to talk to the Arduino board on a Mac, you need to discover how macOS system has named it. Plug it in and follow these commands:
ls /dev | grep usb
Should produce entries like (you may have different numbers after
usbmodem
):
/dev/cu.usbmodem141201
/dev/tty.usbmodem141201
The entry with tty
is going into an environment variable. We'll use this below in the macOS version of the steps.
Make sure that you use a full path. It will look something like the above. Then export it into your shell:
export NANO33_DEV_PATH=/dev/tty.YOUR_USBMODEM_ID
"hello, world!" - blinking the onboard LED on an interval
- Flashing basics
- Using the machine abstraction
Running it - Linux
tinygo flash -target arduino-nano33 ./lesson0/main.go
Running it - macOS
tinygo flash -target arduino-nano33 -port=$NANO33_DEV_PATH ./lesson0/main.go
Goroutines and UART print
- goroutines let you do more than one thing at once
- TinyGo uses UART for built-in debugging b/c of its ubquity
screen
to connect to serial interface
Running it - Linux
tinygo flash -target arduino-nano33 ./lesson1/main.go
Running it - macOS
tinygo flash -target arduino-nano33 -port=$NANO33_DEV_PATH ./lesson1/main.go
Screen command to read println statements
screen $NANO33_DEV_PATH 9600
Connecting an external LED to a pin
- Breadboards and their rails
- Using jumper wires and pins
- Blinking the LED with a button
Steps:
- Run a red jumper wire from the 3.3v power pin on the board to the + rail on the breadboard.
- Run a black jumper wire from the GND pin on the board to the - rail on the breadboard.
- NOTE: you only need to do the above once to power the entire breadboard.
- Plug the LED bulb (gently!) into the socket. If it doesn't light up in later steps, pull it out and turn it around.
- Plug a jumper wire into pin D11 on the board and run it to a rail in the middle of the breadboard (5 is a good one to start)
- Plug your LED component's red and black lines into power and ground slots on the breadboard.
- Plug your LED's yellow wire in-line with the jumper you ran to D11
- Replicate the above with the button component, adding power and ground and then running your control jumper to D12 on the 10 line of the breadboard and plugging your yellow wire in-line.
- Flash the board and use the button to control the LED
Running it - Linux
tinygo flash -target arduino-nano33 ./lesson2/main.go
Running it - macOS
tinygo flash -target arduino-nano33 -port=$NANO33_DEV_PATH ./lesson2/main.go
Analog rotation sensor to control an LED, understanding PWM and ADC
- Use rotation sensor as a dimmer switch
- Send rotation info to UART for debugging
Steps
- Run a jumper wire from pin A0 on the board to row 15 on your breadboard and then wire up your rotary sensor
- Flash the board and then use the rotary sensor to brighten/dim the LED
- Connect to the UART interface with
screen $NANO33_DEV_PATH 9600
and watch the values change as you rotate the sensor
Running it - Linux
tinygo flash -target arduino-nano33 ./lesson3/main.go
Running it - macOS
tinygo flash -target arduino-nano33 -port=$NANO33_DEV_PATH ./lesson3/main.go
Buzzer to create rotation threshold
- Transforming analog values
- Understanding ADC range
Steps
- Run a jumper wire from pin D11 on the board to row 20 on your breadboard and then wire up your buzzer
- Flash the board. When you turn the rotary sensor enough, the buzzer will sound. If the buzzer is sounding when your board is flashed, turn the rotary sensor until it stops making noise.
Running it - Linux
tinygo flash -target arduino-nano33 ./lesson4/main.go
Running it - macOS
tinygo flash -target arduino-nano33 -port=$NANO33_DEV_PATH ./lesson4/main.go
Sending data to an MQTT broker
- Connect to wifi
- Send MQTT message when pressing a button
Steps
- Open
lesson6/main.go
in an editor and change all instances of the string "unit1" to your first name. Save the file. - Flash the board, wait a couple seconds, and then connect to the UART interface: (
screen $NANO33_DEV_PATH 9600
). You should see the device get provisioned with an IP and also see the gateway address and netmask. - Press the button to send an MQTT message to the broker on the local network.
Running it - Linux
tinygo flash -target arduino-nano33 ./lesson5/main.go
Running it - macOS
tinygo flash -target arduino-nano33 -port=$NANO33_DEV_PATH ./lesson5/main.go
docker run -it -p 1883:1883 -p 9001:9001 eclipse-mosquitto
- Arduino Nano33 IoT with headers
- Tiny breadboard
- Small breadboard
- Jumpwires
- Seeed Studios Grove LED (modified with male terminators for breadboard)
- Seeed Studios Grove button (modified with male terminators for breadboard)
- Seeed Studios Grove buzzer (modified with male terminators for breadboard)
- Seeed Studios Grove rotation sensor (modified with male terminators for breadboard)
Thanks to TinyGo creator Ayke van Laethem (@aykevl) and Ron Evans (@deadprogram) of the HybridGroup for their work on TinyGo, related workshop content, and valuable input into these lessons.