< up >
2025-06-11

Gokrazy with Raspberry Pi Zero

Default gokrazty output.

I started my IoT journey with a Raspberry Pi 1 B and have since settled on the Arduino Pico/Esp32 so far. Recently, someone showed me their small GPIO code in Go and the remarkably simple CLI to build and update an image on a remote IoT device.

This post is a summary of the steps I did to let an LED blink with gokrazy and a Raspberry Pi Zero 2W, hence I want to do this again.

Setup

Let’s set up a new gokrazy project and flash it onto the SD card.

  1. Install gokrazy cli: go install github.com/gokrazy/tools/cmd/gok@main
  2. Initialize repo, will create ~/gokrazy/hello: gok new
  3. Goto directory and add the gpio repo: gok add github.com/RaphaelPour/gpio
  4. Replace the hostname with the ip address in config.json (in case the hostname doesn’t get resolved properly)
  5. Add Wifi Module: gok add github.com/gokrazy/wifi and follow configuration instructions
  6. Add your project having a main.go via gok add ~/dev/mygokrazypkg or github.com/RaphaelPour/mygokrazypackage
  7. Insert SD card and locate the device (e.g. via lsblk). Mine is under /dev/sda1
  8. Build and flash: gok overwrite --full /dev/sda
  9. Any further update: gok update

Notes:

In-use

After flashing the image properly, the raspi connects to the specified WLan and provides a website. Credentials can be obtained from the gok overwrite output (see above) or config.json. Username is gokrazy.

Run own program

Now I want to run my own program and do the ultimate hardware test: can it blink?

But first some general steps to set up a new repo with custom code:

  1. create new folder or repo (doesn’t need to be within the gokrazy directory), ~/dev/gpio in my case
  2. add custom program with regular main function
  3. go back to gokrazy dir and add the repo via gok add <url|path>, gok add ~/dev/gpio in my case
  4. build image as usual, gokrazy will pull the repo and run it on boot

GPIO program

The following Go program is a minimal example for letting an LED blink. It uses GPIO pin 17 and the LED stays on (and off) for 100ms. GPIO Pin and the duration can be altered in the variable section.

package main

import (
	"log"
	"time"

	"periph.io/x/conn/v3/gpio"
	"periph.io/x/conn/v3/gpio/gpioreg"
	host "periph.io/x/host/v3"
)

var (
	delay = 100 * time.Millisecond
	pin   = gpioreg.ByName("GPIO17")
)

func main() {
	if _, err := host.Init(); err != nil {
		panic(err)
	}

	for {
		pin.Out(gpio.High)
		log.Println("high")
		time.Sleep(delay)

		pin.Out(gpio.Low)
		log.Printf("low\n")
		time.Sleep(delay)
	}
}

Advantages

I’ve found several advantages in contrast to my Arduino/ESP32 projects:

Challenges

In general there were some challenges on setting up the project:

Impressions

References


  1. Because my laptop has an NVMe, the SD card gets assigned the sda device name.
  2. Limit the maximal current the LED can draw to protect it from overheating.
  3. See periph.io.