TinyGo example code causes Pico to frezze
I have tried running the example code for the max6675 temp sensor on a pi pico, resulting in the pico freezing and needing a reboot. I have tried changing the cables and switching the pico out, with no change.
Some simple troubleshooting showed, that the program hangs when it tries to read the sensor. Getting desperate, i changed the SDI to SDO, with no luck.
In the following code, I have switched the pins from adafruit to pico
`func main() { machine.GPIO17.Configure(machine.PinConfig{Mode: machine.PinOutput}) machine.GPIO17.High()
machine.SPI0.Configure(machine.SPIConfig{
Frequency: 1_000_000,
SCK: machine.GPIO18,
SDO: machine.GPIO16,
})
thermocouple := max6675.NewDevice(machine.SPI0, machine.GPIO17)
for {
temp, err := thermocouple.Read()
if err != nil {
println(err)
return
}
println("%0.02f C : %0.02f F\n", temp, (temp*9/5)+32)
time.Sleep(time.Second)
}
}` Any ideas what could cause this in the example code?
Perhaps this is a scheduler problem. Try the -scheduler tasks flag as noted in a potentially related issue. See also https://github.com/tinygo-org/tinygo/issues/4975.
I have now tried
tinygo flash -target=pico -scheduler tasks
But no luck.
I have also looked at the related issue and have tried using gdb, but I am not that well versed in it.
I get errors
"monitor" command not supported by this target. You can't do that when your target is exec'
"monitor" command not supported by this target.Probably obvious errors, not sure ifgdb` is the solution.
I do not have one of the specific sensors to test with. However, just eyeballing differences in your code vs the example.
The example configures the pin used for the CS as an output, and sets it on:
machine.D5.Configure(machine.PinConfig{Mode: machine.PinOutput})
machine.D5.High()
You probably need to do the same on pin GPIO17.
As far as the SPI interface, I would suggest trying the defaults.
machine.SPI0.Configure(machine.SPIConfig{})
Lastly, make sure you have the sensor connected to the correct pins. See https://datasheets.raspberrypi.com/pico/Pico-R3-A4-Pinout.pdf
For example, GPIO17 is the pin numbered 22 on the Pico.
Hope that helps!
I have this MAX6675 module and have tested it successfully. See the necessary changes.
Raspberry Pi Pinout: https://www.raspberrypi.com/documentation/microcontrollers/images/pico-2-r4-pinout.svg
Connections:
Sensor terminal Red ===> MAX6675 Sensor +
Sensor terminal Blue ===> MAX6675 Sensor -
RaspPico 3V3(OUT) ===> MAX6675 VCC pin
RaspPico GND(any) ===> MAX6675 GND pin
SPI0_SCK_PIN = GPIO18 ===> MAX6675 SCK pin
SPI0_RX_PIN = GPIO16 ===> MAX6675 SO pin
SPI0_CS_PIN = GPIO17 ===> MAX6675 CS pin
/*
K-Thermocouple to Digital Converter MAX6675
Raspberry Pi Pico - RP2040
tinygo version 0.39.0 windows/amd64 (using go version go1.25.1 and LLVM version 19.1.2)
tinygo flash -target pico main.go
https://github.com/tinygo-org/drivers/blob/release/examples/max6675/main.go
"tinygo.org/x/drivers/max6675"
*/
package main
import (
"fmt"
"machine"
"time"
"tinygo.org/x/drivers/max6675"
)
// example for reading temperature from a thermocouple
func main() {
machine.InitSerial() // Initialize serial for debug output
time.Sleep(3 * time.Second) // Sleep to catch prints on serial
fmt.Printf("Pico Thermo sensor MAX6675 \n")
// Pins are for an Raspberry Pico RP2040
machine.Pin(17).Configure(machine.PinConfig{Mode: machine.PinOutput}) // CS pin = GPIO17
machine.Pin(17).High()
machine.SPI0.Configure(machine.SPIConfig{
Frequency: 1000000, // clock 1 MHz
Mode: 0}) // SPI mode 0,0
thermocouple := max6675.NewDevice(machine.SPI0, machine.Pin(17))
for {
temp, err := thermocouple.Read()
if err != nil {
println(err)
return
}
fmt.Printf("%0.02f C : %0.02f F\n", temp, (temp*9/5)+32)
time.Sleep(time.Millisecond * 250)
}
}
Serial Console output:
Pico Thermo sensor MAX6675
25.00 C : 77.00 F
24.75 C : 76.55 F
24.00 C : 75.20 F
24.50 C : 76.10 F
24.50 C : 76.10 F
24.50 C : 76.10 F
24.50 C : 76.10 F
References:
https://tinygo.org/docs/reference/microcontrollers/machine/pico/#type-spi
Configure is intended to setup/initialize the SPI interface. Default baudrate of 4MHz is used if Frequency == 0. Default word length (data bits) is 8. Below is a list of GPIO pins corresponding to SPI0 bus on the rp2040:
corrections = GPIO17 is SPIO0 CSn not SPIO RX GPIO16 is SPIO0 RX
SI : 0, 4, 17 a.k.a RX and MISO (if rp2040 is master) SO : 3, 7, 19 a.k.a TX and MOSI (if rp2040 is master) SCK: 2, 6, 18