Skip to content
systemcoding.dev
Embedded

Flash a Blinky to a Raspberry Pi Pico With TinyGo

Install TinyGo 0.37, write a Go blinky for the Raspberry Pi Pico, put the board in BOOTSEL mode and flash it with tinygo flash -target=pico.

6 min read
Summarize in AI
Flat illustration of a microcontroller wired to an LED and a sensor, with the Raspberry Pi, Go and Arm logos.

A first microcontroller program is a blinking LED, and with TinyGo it is an ordinary Go file. There is no vendor SDK to install, no linker script to write and no Makefile. You install one compiler, plug the board in while holding a button, and run one command. This post takes you from an empty folder to a blinking LED on a Raspberry Pi Pico, with TinyGo 0.37 and Go 1.24, and explains each step so that the second program is easier than the first. It is the beginner entry point of the TinyGo, Zig and Ada guide, and it assumes only that you can open a terminal and have written a few lines of Go or any other language.

TinyGo is a separate compiler that accepts Go source and produces firmware for small chips. It is not the standard go tool, and it does not support the whole language: it omits some reflection and parts of the standard library. For a blinky that does not matter.

What you need

  • A Raspberry Pi Pico (the original board with the RP2040 chip) and a micro-USB cable that carries data, not only power.

  • A computer running macOS, Linux or Windows.

  • Go 1.24 installed, because TinyGo uses the Go toolchain's front end. Check with go version.

The Pico has a single button, labelled BOOTSEL, and a green LED wired to GPIO pin 25. When you power the board up while holding BOOTSEL, the RP2040's built-in ROM bootloader presents the board as a USB drive named RPI-RP2, and you copy firmware onto it. TinyGo can do the copy for you.

Install TinyGo

On macOS with Homebrew:

Shell
brew tap tinygo-org/tools
brew install tinygo

On Debian or Ubuntu, download the .deb package for your CPU from the TinyGo releases page and install it with sudo dpkg -i. The getting started pages list the commands for every platform, including Windows and Docker.

Confirm the install:

Shell
tinygo version
Text
tinygo version 0.37.0 darwin/arm64 (using go version go1.24.0 and LLVM version 19.1.2)

Your platform and the LLVM version differ. The line shows three things worth reading: the TinyGo version, the Go version it found, and the LLVM version it carries. If TinyGo reports that it cannot find Go, or that the Go version is too new, install a Go release that your TinyGo version supports, because each TinyGo release supports a range of Go versions.

Create the project

Shell
mkdir blinky && cd blinky
go mod init example.test/blinky

go mod init writes a go.mod file. TinyGo uses the Go module system even for firmware, so the file is required. The module name is arbitrary; example.test/blinky is a placeholder that will never collide with a real address.

Create main.go:

main.gogo
package main

import (
	"machine"
	"time"
)

func main() {
	led := machine.LED
	led.Configure(machine.PinConfig{Mode: machine.PinOutput})

	for {
		led.High()
		time.Sleep(500 * time.Millisecond)
		led.Low()
		time.Sleep(500 * time.Millisecond)
	}
}

Read it as three parts.

The machine package exists only in TinyGo. It exposes the chip's pins and peripherals. machine.LED is a constant defined by the board's target description: for the Pico it is GPIO 25, and for other boards it is whichever pin carries the user LED, which lets the same source run on several boards.

led.Configure sets the pin's mode. A pin starts as an input, and writing to an input does nothing visible, so a program that skips this line compiles and runs and the LED stays dark. machine.PinOutput makes it drive its level.

The loop sets the pin high, which lights the LED, waits half a second, sets it low, and waits again. time.Sleep takes a time.Duration, as it does in desktop Go, and TinyGo implements it with the chip's timer. While the program sleeps, the processor is idle and consumes little power.

Flash it

Unplug the Pico. Hold the BOOTSEL button down, plug the cable into the board and your computer, then release the button. A drive called RPI-RP2 appears. Now run, from the project folder:

Shell
tinygo flash -target=pico
Text
(no output; the drive disappears, the board resets and the LED starts blinking)

The -target=pico flag selects the board definition, which names the chip, the memory layout, the pins and the way to flash. tinygo flash compiles the program, converts it into the UF2 file format that the RP2040 bootloader understands, copies it to the drive and waits for the board to reboot. After the copy the bootloader jumps to your program, so the green LED should blink once a second: on for 500 ms, off for 500 ms.

To see what is available without a board in hand, list the supported targets:

Shell
tinygo targets
Text
arduino
arduino-nano
circuitplay-express
feather-m4
microbit
pico
...

The list is long and your version may print more entries. Each name is a board or a chip.

Build without flashing

Sometimes you want the file and not the flash, for example to copy it from another machine. Ask for a UF2 output:

Shell
tinygo build -o blinky.uf2 -target=pico

Hold BOOTSEL, plug in, and drag blinky.uf2 onto the RPI-RP2 drive in your file manager. The result is the same. To see how big the program is, add -size short:

Shell
tinygo build -o blinky.uf2 -target=pico -size short
Text
   code    data     bss |   flash     ram
   6192     128    4224 |    6320    4352

The figures are illustrative and will differ with the compiler version. The columns are the same ones a C toolchain's size command prints: flash is code plus data, and RAM is data plus bss. A blinky using a few kilobytes of flash shows how little the Go runtime costs on a chip with 2 MB.

Add a button

A program that only blinks teaches little about input. The Pico has no user button besides BOOTSEL, which is not readable by user code, so wire a push button between GPIO 15 and ground. The chip's internal pull-up resistor holds the pin high until the button pulls it low:

main.gogo
package main

import "machine"

func main() {
	led := machine.LED
	led.Configure(machine.PinConfig{Mode: machine.PinOutput})

	button := machine.GPIO15
	button.Configure(machine.PinConfig{Mode: machine.PinInputPullup})

	for {
		// the button reads false while pressed because it connects the pin to ground
		led.Set(!button.Get())
	}
}

button.Get() returns the logic level as a bool. Pressed means low, so the program inverts it before setting the LED: the LED lights while the button is held. The loop has no sleep, so it spins at full speed, which is fine for a demonstration and wasteful for a battery product; the interrupts post shows how to avoid polling.

Send text to your computer

The Pico enumerates a USB serial port when running TinyGo. println writes to it:

main.gogo
package main

import "time"

func main() {
	for i := 0; ; i++ {
		println("tick", i)
		time.Sleep(time.Second)
	}
}

Flash it, then open the port with tinygo monitor:

Shell
tinygo monitor
Text
Connected to /dev/cu.usbmodem1101. Press Ctrl-C to exit.
tick 0
tick 1
tick 2

The port name differs on every computer, and the first lines may be cut if the board started before the monitor connected. println is the built-in Go function, not fmt.Println: it needs no import and it adds little code to the firmware.

When it does not work

SymptomLikely causeFix
No RPI-RP2 drive appearsThe cable carries power only, or BOOTSEL was released too earlyTry another cable; hold the button until after the cable is plugged in
tinygo flash cannot find a driveThe board is not in bootloader modeRe-enter BOOTSEL mode before running the command
The program compiles and nothing blinksConfigure was skipped, or the board is a Pico WAdd the Configure call; the Pico W's LED is on the wireless chip and needs its own target
Go version mismatch errorThe Go toolchain is newer than your TinyGo supportsInstall a supported Go, or a newer TinyGo
Package fmt makes the image largefmt pulls in reflectionUse println for output on small chips

Two more habits help. Keep a second cable on hand, because bad cables cause more first-day failures than any code. And flash the unchanged blinky whenever something strange happens, which separates a hardware or toolchain problem from a bug in your program.

Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.