ValueError: bad SCK pin - Can't connect via SPI

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
Post Reply
Abigail_Twitch
Posts: 2
Joined: Sun Jul 24, 2022 12:21 am

ValueError: bad SCK pin - Can't connect via SPI

Post by Abigail_Twitch » Sun Jul 24, 2022 12:56 am

Hello

I'm a beginner with microcontrollers, my first project is simply to hook up a pt100 temperature sensor to my Pico to make a PID controller. I've managed to do this using circuit python and it works great but when I tried with micropython i keep getting a bad pin error. Have not been able to resolve this. The code below is a modified version of the adafruit Max31865 amplifier example code which is supplied with circuit python in mind.

Please see below for answer to "why not just use circuit python then?" :D

---------------------------

Code: Select all

"""Simple demo of the MAX31865 thermocouple amplifier.
Will print the temperature every second."""
import time
import machine
import adafruit_max31865



CS = machine.Pin(22, machine.Pin.OUT)
# Initialize SPI bus and sensor.
SPI = machine.SPI(0, sck=machine.Pin(24, machine.Pin.OUT),
                  mosi=machine.Pin(25, machine.Pin.OUT),
                  miso=machine.Pin(21,machine.Pin.OUT)#<-----"bad SCK pin" error originates here
                  )
SPI.init(baudrate=115200, polarity=0, phase=1)


#SENSOR = adafruit_max31865.MAX31865(SPI, CS)
# Note you can optionally provide the thermocouple RTD nominal, the reference
# resistance, and the number of wires for the sensor (2 the default, 3, or 4)
# with keyword args:
sensor = adafruit_max31865.MAX31865(spi, cs, rtd_nominal=100, ref_resistor=430.0, wires=3)

# Main loop to print the temperature every second.
while True:
    # Read temperature.
    TEMP = SENSOR.temperature
    # Print the value.
    print("Temperature: {0:0.3f}C".format(TEMP))
    # Delay for a second.
    time.sleep(1.0)



Traceback (most recent call last):
File "<stdin>", line 13, in <module>
ValueError: bad SCK pin
>>>


As to why use micro python in the first place if circuit python works - I need to measure the temperature and then calculate a PID value and then send this value to a SSR, but with circuit python I understand you can't run multithreaded operations (unlike micropython) therefore the following is happening

read temperature
calculate PID
tell SSR to turn on/off at given frequency
SSR then turns off whilst a new PID is calculated

If anyone can give me a hint on how to have the SSR still turn on/off uninterrupted whilst calculating a new frequency value in circuit python that would help too.

Many thanks for reading, any help will be really appreciated.

Regards

Jon

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: ValueError: bad SCK pin - Can't connect via SPI

Post by Roberthh » Sun Jul 24, 2022 6:38 am

You mixed up board pins and GPIO pins. For the SPI spec, you have to use the GPIO pin numbers.
So for SPI0, is for instance:

Code: Select all

SPI = machine.SPI(0, sck=machine.Pin(18, machine.Pin.OUT),
                  mosi=machine.Pin(19, machine.Pin.OUT),
                  miso=machine.Pin(16,machine.Pin.OUT)#<-----"bad SCK pin" error originates here

Abigail_Twitch
Posts: 2
Joined: Sun Jul 24, 2022 12:21 am

Re: ValueError: bad SCK pin - Can't connect via SPI

Post by Abigail_Twitch » Sun Jul 24, 2022 10:16 am

Hi Roberthh

Thanks for the quick reply! This solved my issue, can now read temperature fine :)

I think i just saw "machine.pin" and assumed this meant board numbering. I'll pay more attention to the documentation in future!

Many thanks for your help

Jon

Post Reply