DEV Community

Cover image for ๐Ÿ”ฅ The Hidden Superpowers of TinyGo: How to Run Go Code on Microcontrollers and Beyond!
Yevhen Kozachenko ๐Ÿ‡บ๐Ÿ‡ฆ
Yevhen Kozachenko ๐Ÿ‡บ๐Ÿ‡ฆ

Posted on • Originally published at ekwoster.dev

๐Ÿ”ฅ The Hidden Superpowers of TinyGo: How to Run Go Code on Microcontrollers and Beyond!

๐Ÿ”ฅ The Hidden Superpowers of TinyGo: How to Run Go Code on Microcontrollers and Beyond!

When most developers hear โ€œGoโ€, they think high-performance backend services. When they hear โ€œembedded programmingโ€, they think C/C++. But what if I told you that you could write embedded firmware using Go? Welcome to the brave new world of TinyGo โ€” the Go that fits into the tiniest chips and also works on the bleeding edge of WebAssembly!

๐Ÿง  TL;DR: TinyGo lets you write Go code that compiles down to run on ARM Cortex-M microcontrollers and in the browser using WebAssembly. Itโ€™s faster than MicroPython, easier than C, and itโ€™s real, idiomatic Go.

In this post, we're diving deep into TinyGo, uncovering its incredible potential for IoT, edge computing, and WASM. Not only are we going to explore working examples on Arduino and WebAssembly, but we'll also see how TinyGo can be the go-to (pun intended) tool for writing safe, maintainable embedded code without sacrificing performance.


๐Ÿš€ What Is TinyGo?

TinyGo is a Go compiler that targets tiny devices and WebAssembly using LLVM.

  • Supports ARM Cortex-M (e.g., STM32, Arduino Nano 33 IoT)
  • WebAssembly (WASM) that runs in browsers or standalone
  • Esperanto for Go devs entering embedded space

Unlike regular Go, the Go runtime in TinyGo is a tiny subset โ€” necessary to keep binaries small (you know, under 32 KB kinda needs that ๐Ÿค).


โšก TinyGo vs. MicroPython vs. C โ€” A Quick Showdown

Language Performance Memory Footprint Dev ergonomics Community
C/C++ ๐Ÿš€๐Ÿš€ ๐Ÿชถ Lowest ๐Ÿ˜ตโ€๐Ÿ’ซ Steep learning Huge
MicroPython ๐ŸŒ Slow ๐Ÿง  Medium ๐Ÿ˜Š Easy Great
TinyGo โšก Fast ๐Ÿง  Low ๐Ÿ˜Ž Smooth (Go!) Growing

What's the catch? Not all Go features work (e.g., reflect, interface{} can be limited).


๐Ÿงช Real Example #1: Blinking an LED on Arduino Nano 33 IoT with TinyGo

Letโ€™s start with a classic: the Blink.

๐Ÿ”ง Requirements

โœ… Code Example (blink.go)

package main

import (
    "machine"
    "time"
)

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

    for {
        led.High()
        time.Sleep(time.Millisecond * 500)
        led.Low()
        time.Sleep(time.Millisecond * 500)
    }
}
Enter fullscreen mode Exit fullscreen mode

๐Ÿ—๏ธ Build and Flash

tinygo flash -target=arduino-nano33iot blink.go
Enter fullscreen mode Exit fullscreen mode

And voilร  โ€” blinking like a champ ๐ŸŽ‰


๐Ÿง  Real Example #2: WebAssembly and TinyGo in the Browser

TinyGo compiles to WASM with a much smaller output compared to Goโ€™s default WASM toolchain.

Sample Code (main.go)

package main

import (
    "syscall/js"
)

func main() {
    alert := js.Global().Get("alert")
    alert.Invoke("Hello from TinyGo WASM ๐Ÿš€")
}
Enter fullscreen mode Exit fullscreen mode

Build It:

tinygo build -o main.wasm -target wasm main.go
Enter fullscreen mode Exit fullscreen mode

Embed in HTML:

<!DOCTYPE html>
<html>
<head><meta charset="utf-8"></head>
<body>
  <script>
    const go = new Go();
    WebAssembly.instantiateStreaming(fetch("main.wasm"), go.importObject).then(result => {
      go.run(result.instance);
    });
  </script>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

๐Ÿ‘‰ When you load the page โ€” BAM! Alert pops up from Go-WASM.

๐Ÿ’Ž Real World Usage?

  • Create WASM libraries in Go
  • Replace heavy JavaScript modules with TinyGo-WASM
  • Target both embedded devices and browsers with the same code ๐Ÿ”ฅ

๐Ÿ•น๏ธ Bonus: Accessing Sensors on Microcontrollers

Letโ€™s read real-world data โ€” say from a temperature sensor like the DHT11.

Sample Code:

import (
    "time"
    "machine"
    "tinygo.org/x/drivers/dht"
)

func main() {
    sensor := dht.New(machine.D2, dht.DHT11)
    sensor.Configure()

    for {
        temp, humidity, err := sensor.ReadRetry(10)
        if err == nil {
            println("Temp:", temp, "ยฐC",
                " Humidity:", humidity, "%")
        }
        time.Sleep(time.Second * 2)
    }
}
Enter fullscreen mode Exit fullscreen mode

Build and flash again โ€” now your Go code reads real-world data โœจ


๐Ÿง  Why TinyGo Matters

Letโ€™s break the paradigm:

  • Go is safe, typed, and modern
  • Embedded dev is bug-prone, manual, and niche
  • TinyGo bridges them โ€” making embedded and web apps safer and more accessible

๐Ÿ’ก Use TinyGo to write libraries usable across:

  • A microcontroller firmware (e.g. Arduino)
  • A browser via WebAssembly
  • A command-line diagnostic tool (std Go)

๐Ÿ‘ทโ€โ™‚๏ธ What's Missing?

  • Third-party libraries + driver support is growing but less than C or MicroPython
  • No net/http unless you're on appropriate targets
  • Async concurrency and goroutines are limited (but supported)

Contribute and grow the ecosystem if you love Go!


๐Ÿšจ Final Verdict: Use Cases in the Wild

Use Case Why TinyGo?
IoT Device SDKs One codebase for browser + device testing
Plants watering systems ๐ŸŒฟ Go + DHT + Servo control
WASM-Libs for JavaScript Replace bloated JS with tiny fast WASM
EdTech for kids Easier than C, stronger than MicroPython
Low-power Wearables Long battery life with low memory footprint

๐Ÿ“Ž Resources


โ™ป๏ธ Wrapping Up

TinyGo is not here to โ€œreplace Cโ€. Itโ€™s here to open doors. If you love Go, and you want to get your hands dirty in hardware or compile blazing fast WebAssembly apps, give TinyGo a try.

Donโ€™t just code APIs. Build electric cats. Laser bots. Air-qual sensors in your kitchen. And make the browser do what JS never could.

Go tiny โ€” think big.

Are you using TinyGo in production? Got questions about drivers or WebAssembly? Share them in the comments below!


โœจ If you're exploring embedded Go or experimenting with TinyGo + WASM for real-world applications โ€” we offer tailored research and development services to support your innovation!

Top comments (0)