Skip to content

gherlein/pico-tinygo-template

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Pico TinyGo Template

A Raspberry Pi Pico project template using TinyGo instead of the C/C++ SDK. This is a companion to the pico-template project, providing the same functionality with Go.

Features

  • LED blinking example with serial output
  • Support for both standard Pico and Pico W boards
  • OpenOCD debugging support
  • Multiple flashing methods (picoprobe and UF2)
  • Same Make targets as the C pico-template for consistency

Prerequisites

Required Software

  1. TinyGo (v0.31.0 or later)

    # macOS
    brew tap tinygo-org/tools
    brew install tinygo
    
    # Linux (Ubuntu/Debian)
    wget https://github.com/tinygo-org/tinygo/releases/download/v0.31.0/tinygo_0.31.0_amd64.deb
    sudo dpkg -i tinygo_0.31.0_amd64.deb
    
    # Or see https://tinygo.org/getting-started/install/
  2. Go (1.21 or later)

    # Install from https://golang.org/dl/
  3. ARM GCC Toolchain (for debugging and binary utilities)

    # Ubuntu/Debian
    sudo apt install gcc-arm-none-eabi gdb-multiarch
    
    # macOS
    brew install arm-none-eabi-gcc arm-none-eabi-gdb
  4. OpenOCD (for flashing via picoprobe)

    # Ubuntu/Debian
    sudo apt install openocd
    
    # macOS
    brew install openocd

Optional Hardware

  • Raspberry Pi Pico or Pico W
  • Picoprobe (another Pico configured as a debugger)
  • USB cable

Project Structure

pico-tinygo-template/
├── cmd/
│   └── main/
│       └── main.go         # Main application code
├── pkg/                    # Reusable packages (if needed)
├── scripts/
│   ├── flash              # Flash script using OpenOCD
│   └── reset              # Reset script using OpenOCD
├── build/                 # Build artifacts (created by make)
├── docs/                  # Additional documentation
├── go.mod                 # Go module file
├── Makefile              # Build automation
└── README.md             # This file

Quick Start

1. Clone the Repository

git clone https://github.com/user/pico-tinygo-template.git
cd pico-tinygo-template

2. Build the Project

make build

This creates:

  • build/main.uf2 - For drag-and-drop flashing
  • build/main.hex - Intel hex format
  • build/main.elf - For debugging

3. Flash to Pico

Method A: Using Picoprobe (Recommended for Development)

make flash

Method B: Using BOOTSEL Mode (UF2)

  1. Hold the BOOTSEL button while connecting the Pico via USB
  2. The Pico will appear as a USB mass storage device
  3. Run:
    make flash-uf2

Available Make Targets

All the same targets from the C template are supported:

Target Description
make or make dummy Shows available targets
make build Compiles the TinyGo code
make prep Prepares build directory and shows TinyGo info
make flash Builds and flashes via OpenOCD/picoprobe
make flash-uf2 Builds and flashes via UF2 (BOOTSEL mode)
make clean Removes build artifacts
make debug Starts GDB debugging session
make openocd Starts OpenOCD server
make reset Resets the Pico
make size Shows binary size information
make strip Strips debug symbols from ELF
make monitor Opens serial monitor (115200 baud)
make test-tinygo Verifies TinyGo installation
make git Quick git add, commit, and push

Code Example

The main application (cmd/main/main.go) demonstrates:

package main

import (
    "machine"
    "time"
)

func main() {
    // Configure UART for serial output
    machine.UART0.Configure(machine.UARTConfig{
        BaudRate: 115200,
    })

    // Configure LED pin
    led := machine.LED
    led.Configure(machine.PinConfig{Mode: machine.PinOutput})

    // Blink LED forever
    for {
        led.High()
        println("ON")
        time.Sleep(100 * time.Millisecond)

        led.Low()
        println("OFF")
        time.Sleep(100 * time.Millisecond)
    }
}

Serial Monitor

To view the serial output:

make monitor

Or use your preferred serial terminal:

screen /dev/ttyACM0 115200  # Linux
screen /dev/tty.usbmodem* 115200  # macOS

Debugging

  1. Start OpenOCD in one terminal:

    make openocd
  2. In another terminal, start GDB:

    make debug

Customization

Changing the Target Board

Edit the Makefile:

TARGET = pico        # For standard Pico
# TARGET = pico-w    # For Pico W (WiFi version)

Build Optimization

Modify TINYGO_FLAGS in the Makefile:

TINYGO_FLAGS = -target=$(TARGET) -size short -opt=z  # Size optimized (default)
# TINYGO_FLAGS = -target=$(TARGET) -size short -opt=2  # Speed optimized
# TINYGO_FLAGS = -target=$(TARGET) -size short -opt=s  # Balanced

TinyGo vs C SDK Comparison

Feature C SDK TinyGo
Language C/C++ Go
Build System CMake Go modules + Make
Binary Size Smaller Slightly larger
Development Speed Slower Faster
Memory Management Manual Garbage collected
Hardware Access Direct Via machine package
Libraries Extensive Growing

Troubleshooting

TinyGo Not Found

make test-tinygo  # Check installation
export PATH=$PATH:/usr/local/tinygo/bin  # Add to PATH if needed

Pico Not Detected

  • Check USB cable (must be data cable, not power-only)
  • Try different USB port
  • Verify with lsusb (Linux) or system_profiler SPUSBDataType (macOS)

Build Errors

make clean  # Clean build artifacts
go mod tidy  # Update dependencies
tinygo version  # Verify TinyGo version

Flash Errors

  • Ensure picoprobe is properly connected
  • Check OpenOCD configuration
  • Try BOOTSEL mode as alternative

Contributing

Contributions are welcome! Please:

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Run make build to verify
  5. Submit a pull request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

Resources

About

Simple tempate for a tinygo project for the Raspberry Pi Pico

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published