Controlling a GPIO input/output pin

In this guide, we are using, a library for peripheral I/O in Go, to set one of the Raspberry Pi’s General Purpose I/O (GPIO) pins to a logical high (3.3V) or low (0V) signal. supports the Raspberry Pi 3 and Raspberry Pi 4, starting with version v3.6.4.

Connect GPIO pins based on pinout

To verify the code is doing what we expect, let’s connect a multimeter as per’s pinout:

  • pin number 18 (signal BCM24, labeled 24 in the pinout above)
  • pin number 20 (signal GND)

We need to set the multimeter to “Voltage measurement, DC (direct current)”.

Setting an output pin signal

To set the pin high and low, alternatingly, with a 5 second frequency, we will be using the hello-gpio program, which is a slightly modified version of the example at

package main

import (


func doGPIO() error {
	log.Printf("Loading drivers")
	// Load drivers:
	if _, err := host.Init(); err != nil {
		return err
	log.Printf("Toggling GPIO forever")
	t := time.NewTicker(5 * time.Second)
	for l := gpio.Low; ; l = !l {
		log.Printf("setting GPIO pin number 18 (signal BCM24) to %v", l)
		// Lookup a pin by its location on the board:
		if err := rpi.P1_18.Out(l); err != nil {
			return err
	return nil

func main() {
	if err := doGPIO(); err != nil {

You have two options to run this program on your Raspberry Pi:

  1. Use gok run to temporarily run this program on a running gokrazy instance.
  2. Use gok add to permanently include this program in your gokrazy instance:
# From the hello-gpio directory, run:
gok add .
# Then, deploy as usual:
gok update

At this point, we should be able to see the high/low signal on the multimeter, alternating between 3.3V (high) and 0V (low) every 5 seconds: