Skip to content

Commit bd2b42b

Browse files
committed
add tinygo example
Signed-off-by: Simon Waldherr <[email protected]>
1 parent 33ad29e commit bd2b42b

File tree

3 files changed

+99
-2
lines changed

3 files changed

+99
-2
lines changed

readme.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ From the root directory (`$GOPATH/github.com/SimonWaldherr/golang-examples`), ru
256256
go test -bench=. -benchmem advanced/json_bench/main_test.go
257257
```
258258

259-
Make pipeable unix applications with os.Stdin ([Golang Playground](https://play.golang.org/p/NqrUOfBmJtt))
259+
Make pipe-able unix applications with os.Stdin ([Golang Playground](https://play.golang.org/p/NqrUOfBmJtt))
260260

261261
```Shell
262262
go run pipe.go
@@ -451,7 +451,7 @@ List files in working directory
451451
go run explorer.go
452452
```
453453

454-
run assemply code from golang
454+
run assembly code from golang
455455

456456
```Shell
457457
go run assembly.go
@@ -581,6 +581,17 @@ Hash Map standard functions in golang
581581
go run map.go
582582
```
583583

584+
### TinyGo
585+
586+
You can even use Go on microcontrollers, the keyword here is [TinyGo](https://tinygo.org/), a go compiler specially developed for SBCs and MCUs.
587+
If you want to blink the LED of your Raspberry Pi Pico, try this:
588+
589+
```Shell
590+
tinygo build -o firmware.uf2 -target=pico ./tinygo/blink.go
591+
```
592+
593+
and then upload it to the pico.
594+
584595
## Compile
585596

586597
One great aspect of Golang is, that you can start go applications via ```go run name.go```, but also compile it to an executable with ```go build name.go```. After that you can start the compiled version which starts much faster.

tinygo/README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Using TinyGo on a Raspberry Pi Pico
2+
3+
## 1. **Download and Install TinyGo**
4+
- **Download TinyGo:** Visit the [official TinyGo website](https://tinygo.org/getting-started/). Choose the installation method suitable for your operating system (Windows, macOS, or Linux).
5+
- **Install TinyGo:** Follow the installation instructions provided on the website for your platform. Typically, you can install TinyGo via package managers like `brew` for macOS, `apt` for Linux, or directly from the release binaries.
6+
7+
For Linux (e.g., Ubuntu/Debian):
8+
```bash
9+
wget https://github.com/tinygo-org/tinygo/releases/download/v0.28.1/tinygo_0.28.1_amd64.deb
10+
sudo dpkg -i tinygo_0.28.1_amd64.deb
11+
```
12+
13+
- **Set Environment Variables:** Add the TinyGo installation directory to your system's `PATH` and set the `GOROOT` environment variable. For example:
14+
```bash
15+
export PATH=$PATH:/usr/local/tinygo/bin
16+
export GOROOT=/usr/local/go
17+
```
18+
19+
## 2. **Compiling a Program**
20+
- **Write a Program:** Create a simple `main.go` file:
21+
```go
22+
package main
23+
24+
import "machine"
25+
import "time"
26+
27+
func main() {
28+
led := machine.LED
29+
led.Configure(machine.PinConfig{Mode: machine.PinOutput})
30+
31+
for {
32+
led.High()
33+
time.Sleep(time.Millisecond * 500)
34+
led.Low()
35+
time.Sleep(time.Millisecond * 500)
36+
}
37+
}
38+
```
39+
40+
- **Compile with TinyGo:** To compile the Go program for the Raspberry Pi Pico, run:
41+
```bash
42+
tinygo build -o firmware.uf2 -target=pico main.go
43+
```
44+
This command generates a `firmware.uf2` file compatible with the Pico.
45+
46+
## 3. **Uploading to Raspberry Pi Pico**
47+
- **Put Pico in Bootloader Mode:** Hold down the BOOTSEL button on the Pico, then plug it into your computer. Release the button once connected. The Pico will mount as a USB storage device.
48+
- **Upload the Firmware:** Copy the generated `firmware.uf2` file to the mounted Pico drive. Once copied, the Pico will reboot and start running the program.
49+
50+
## 4. **Example Programs**
51+
- **Blink an LED:** The provided code example makes the onboard LED blink every half a second.
52+
- **Button Control:** Extend the example by reading a button input to control the LED.
53+
54+
```go
55+
button := machine.BUTTON
56+
button.Configure(machine.PinConfig{Mode: machine.PinInputPullup})
57+
58+
for {
59+
if !button.Get() {
60+
led.High()
61+
} else {
62+
led.Low()
63+
}
64+
time.Sleep(time.Millisecond * 10)
65+
}
66+
```
67+
68+
This setup lets you quickly develop and deploy Go programs on a Raspberry Pi Pico using TinyGo, making it a powerful tool for microcontroller projects.

tinygo/blink.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package main
2+
3+
import (
4+
"machine"
5+
"time"
6+
)
7+
8+
func main() {
9+
led := machine.Pin(25) // Use Pin 19 on Pico W
10+
led.Configure(machine.PinConfig{Mode: machine.PinOutput})
11+
12+
for {
13+
led.High()
14+
time.Sleep(time.Millisecond * 500)
15+
led.Low()
16+
time.Sleep(time.Millisecond * 500)
17+
}
18+
}

0 commit comments

Comments
 (0)