|
| 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. |
0 commit comments