🎁📬❄️📦 Holiday Shipping Deadlines are approaching! International customers and domestic ground - get your orders in 12/9/2020 📦❄️📬🎁
0

MAX31865-PT100
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

MAX31865-PT100

by kevkeva on Thu Oct 29, 2020 1:04 pm

Hi all,

I want to use the Max31865 with a PT100 on my Raspberry by SPI1, but if I connect it with the SPI1 Pins and change the pins in the software, it won't work.

How can I use SPI1?

Thanks for your time.

kevkeva
 
Posts: 10
Joined: Thu Oct 29, 2020 12:52 pm

Re: MAX31865-PT100

by mikeysklar on Fri Oct 30, 2020 9:18 pm

@kevkeva,

How do you have the sensors wired 2-wire, 3-wire, 4-wire etc? Does the code and MAX31865 both reflect that?

Which model of Pi are you using?

mikeysklar
 
Posts: 2398
Joined: Mon Aug 01, 2016 8:10 pm

Re: MAX31865-PT100

by kevkeva on Sun Nov 01, 2020 5:07 pm

I have a 4-wire and a RPI 4.
If i use the SPI0 Pins it works, but i want to use the spi1 because the spi0 were used by an ADC board.

kevkeva
 
Posts: 10
Joined: Thu Oct 29, 2020 12:52 pm

Re: MAX31865-PT100

by mikeysklar on Tue Nov 03, 2020 3:15 pm

@kevkeva,

Have you enabled spi1 in /boot/config.txt also make sure you are not outputing the console over serial as that will also conflict and take up sp1.

Code: Select all | TOGGLE FULL SIZE
dtoverlay=spi1-3cs


Most people just have sp1 enabled which is why spi0 is working for you with:

Code: Select all | TOGGLE FULL SIZE
dtparam=spi=on


check the contents of:

Code: Select all | TOGGLE FULL SIZE
ls /dev/spidev*


before and after reboot when changing this option.

mikeysklar
 
Posts: 2398
Joined: Mon Aug 01, 2016 8:10 pm

Re: MAX31865-PT100

by kevkeva on Wed Nov 04, 2020 4:37 am

I have changed the code like this ->
Code: Select all | TOGGLE FULL SIZE
#MAX31865 thermocouple amplifier
# Will print the temperature every second.
import time
import board
import busio
import digitalio
import adafruit_max31865

# Initialize SPI bus and sensor.
#spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
spi = busio.SPI(board.SCK_1, MOSI=board.MOSI_1, MISO=board.MISO_1)
cs = digitalio.DigitalInOut(board.D18) # Chip select of the MAX31865 board.

sensor = adafruit_max31865.MAX31865(spi, cs, rtd_nominal=100, ref_resistor=430, wires=4)
# 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=2)

# Main loop to print the temperature every second.
while True:
    # Print the value.
    print("Temperature: {0:0.3f}C".format(sensor.temperature))
    print('Resistance: {0:0.3f} Ohms'.format(sensor.resistance))
    # Delay for a second.
    time.sleep(1.0)

, but I am getting an error
Code: Select all | TOGGLE FULL SIZE
Traceback (most recent call last):
  File "PT100.py", line 18, in <module>
    sensor = adafruit_max31865.MAX31865(spi, cs, rtd_nominal=100, ref_resistor=430, wires=4)
  File "/usr/local/lib/python3.7/dist-packages/adafruit_max31865.py", line 115, in __init__
    config = self._read_u8(_MAX31865_CONFIG_REG)
  File "/usr/local/lib/python3.7/dist-packages/adafruit_max31865.py", line 139, in _read_u8
    device.write(self._BUFFER, end=1)
  File "/usr/local/lib/python3.7/dist-packages/busio.py", line 300, in write
    return self._spi.write(buf, start, end)
  File "/usr/local/lib/python3.7/dist-packages/adafruit_blinka/microcontroller/generic_linux/spi.py", line 81, in write
    self._spi.mode = self.mode
  File "/usr/local/lib/python3.7/dist-packages/Adafruit_PureIO/spi.py", line 355, in mode
    self._ioctl(SPI._IOC_WR_MODE, mode)
  File "/usr/local/lib/python3.7/dist-packages/Adafruit_PureIO/spi.py", line 224, in _ioctl
    ioctl(self.handle, ioctl_bytes, arg)
OSError: [Errno 22] Invalid argument


What can I do to get no Error?

kevkeva
 
Posts: 10
Joined: Thu Oct 29, 2020 12:52 pm

Re: MAX31865-PT100

by mikeysklar on Wed Nov 04, 2020 2:43 pm

@kevkeva,

Your changes in the code to use:

Code: Select all | TOGGLE FULL SIZE
spi = busio.SPI(board.SCK_1, MOSI=board.MOSI_1, MISO=board.MISO_1)


look good, but did you get the
dtoverlay=sp13cs
into /boot/config.txt and verify
/dev/spidev1*
are showing up?

Also in our guide we recommend CS1 be used as GPIO17 so I'd update your CS line.

https://learn.adafruit.com/circuitpytho ... rs-devices

mikeysklar
 
Posts: 2398
Joined: Mon Aug 01, 2016 8:10 pm

Re: MAX31865-PT100

by kevkeva on Thu Nov 05, 2020 12:55 pm

I changed the following code
Code: Select all | TOGGLE FULL SIZE
#MAX31865 thermocouple amplifier
# Will print the temperature every second.
import time
import board
import busio
import digitalio
import adafruit_max31865


# Initialize SPI bus and sensor.
#spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
spi = busio.SPI(board.SCK_1, MOSI=board.MOSI_1, MISO=board.MISO_1)
cs = digitalio.DigitalInOut(board.D17) # Chip select of the MAX31865 board.

sensor = adafruit_max31865.MAX31865(spi, cs, rtd_nominal=100, ref_resistor=430, wires=4)
# 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=2)

# Main loop to print the temperature every second.
while True:
    # Print the value.
    print("Temperature: {0:0.3f}C".format(sensor.temperature))
    print('Resistance: {0:0.3f} Ohms'.format(sensor.resistance))
    # Delay for a second.
    time.sleep(1.0)

and got this Error
Code: Select all | TOGGLE FULL SIZE
Traceback (most recent call last):
  File "PT100.py", line 15, in <module>
    sensor = adafruit_max31865.MAX31865(spi, cs, rtd_nominal=100, ref_resistor=430, wires=4)
  File "/usr/local/lib/python3.7/dist-packages/adafruit_max31865.py", line 115, in __init__
    config = self._read_u8(_MAX31865_CONFIG_REG)
  File "/usr/local/lib/python3.7/dist-packages/adafruit_max31865.py", line 139, in _read_u8
    device.write(self._BUFFER, end=1)
  File "/usr/local/lib/python3.7/dist-packages/busio.py", line 300, in write
    return self._spi.write(buf, start, end)
  File "/usr/local/lib/python3.7/dist-packages/adafruit_blinka/microcontroller/generic_linux/spi.py", line 81, in write
    self._spi.mode = self.mode
  File "/usr/local/lib/python3.7/dist-packages/Adafruit_PureIO/spi.py", line 355, in mode
    self._ioctl(SPI._IOC_WR_MODE, mode)
  File "/usr/local/lib/python3.7/dist-packages/Adafruit_PureIO/spi.py", line 224, in _ioctl
    ioctl(self.handle, ioctl_bytes, arg)
OSError: [Errno 22] Invalid argument

kevkeva
 
Posts: 10
Joined: Thu Oct 29, 2020 12:52 pm

Re: MAX31865-PT100

by mikeysklar on Thu Nov 05, 2020 5:16 pm

It sounds like you are only changing the python script and not the necessary OS configuration files /boot/config.txt and confirming that /dev/spidev* shows up in the filesystem as mentioned in the previous comment.

mikeysklar
 
Posts: 2398
Joined: Mon Aug 01, 2016 8:10 pm

Re: MAX31865-PT100

by kevkeva on Fri Nov 06, 2020 4:51 am

I have changed it as you said and the filesystem shows me
Code: Select all | TOGGLE FULL SIZE
/dev/spidev0.0  /dev/spidev0.1  /dev/spidev1.0  /dev/spidev1.1  /dev/spidev1.2

kevkeva
 
Posts: 10
Joined: Thu Oct 29, 2020 12:52 pm

Re: MAX31865-PT100

by mikeysklar on Sun Nov 08, 2020 12:25 pm

@kevkeva,

Great to see the /dev/spidev0* and /dev/spidev1* devices are present.

You can also use the raspi-gpio get to confirm that SP1 is up and configured.

Now it sounds like you are hitting a known issue with sp1 that it only supports modes 1 and 3 (not the default of 0.

Default (works on spi0, not on spi1)

Code: Select all | TOGGLE FULL SIZE
polarity = 0
phase = 1


needs to be changed to:

Code: Select all | TOGGLE FULL SIZE
polarity = 1
phase = 0


https://www.raspberrypi.org/forums/view ... 9&p=668045

I believe you will need to use spi.configure to get spi1 into a working non-zero mode:

Code: Select all | TOGGLE FULL SIZE
spi.configure(baudrate=5000000, phase=1, polarity=0)


A little more on this here:

https://learn.adafruit.com/circuitpytho ... pi-devices

mikeysklar
 
Posts: 2398
Joined: Mon Aug 01, 2016 8:10 pm

Re: MAX31865-PT100

by kevkeva on Tue Nov 10, 2020 1:44 pm

I added this
Code: Select all | TOGGLE FULL SIZE
spi.configure(baudrate=5000000, phase=1, polarity=0)
to my code an get an error:
Code: Select all | TOGGLE FULL SIZE
spi.configure(baudrate=5000000, phase=1, polarity=0)
NameError: name 'spi' is not defined
.

I tried to change the phase and polarity directly in the adafruit_max31865 library and got values but the values were too high.
Code: Select all | TOGGLE FULL SIZE
Temperature: 317.539C
Resistance: 218.281 Ohms


What can I do?

kevkeva
 
Posts: 10
Joined: Thu Oct 29, 2020 12:52 pm

Re: MAX31865-PT100

by mikeysklar on Wed Nov 11, 2020 6:19 pm

Can you post the full script in CODE brackets. spi.configure() would need to be called after you have setup SPI.

Code: Select all | TOGGLE FULL SIZE
spi = busio.SPI(board.SCK_1, MOSI=board.MOSI_1, MISO=board.MISO_1)

mikeysklar
 
Posts: 2398
Joined: Mon Aug 01, 2016 8:10 pm

Re: MAX31865-PT100

by kevkeva on Fri Nov 13, 2020 9:24 am

This is my code:
Code: Select all | TOGGLE FULL SIZE
#MAX31865 thermocouple amplifier
# Will print the temperature every second.
import time
import board
import busio
import digitalio
import adafruit_max31865

# Initialize SPI bus and sensor.
#spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
spi = busio.SPI(board.SCK_1, MOSI=board.MOSI_1, MISO=board.MISO_1)
cs = digitalio.DigitalInOut(board.D23) # Chip select of the MAX31865 board.
spi.configure(baudrate=5000000, phase=0, polarity=1)

sensor = adafruit_max31865.MAX31865(spi, cs, rtd_nominal=100, ref_resistor=430, wires=4)


fo = open('temperature.csv', 'a')
zeilen = 1

while True:

    string = \
    ";%lf"%(sensor.temperature) + "\n"
    string = string.replace(".",",")
    # Print the value.
   
    print("Temperature: {0:0.3f}C".format(sensor.temperature))
    print('Resistance: {0:0.3f} Ohms'.format(sensor.resistance))
    # Delay for a second.
    time.sleep(1.0)
    zeilen += 1
    if zeilen >= 3:
        zeilen = 0 
        fo.write(string)
        result = ""


I got an ERROR:
Code: Select all | TOGGLE FULL SIZE
Traceback (most recent call last):
  File "PT100.py", line 13, in <module>
    spi.configure(baudrate=5000000, phase=0, polarity=1)
  File "/usr/local/lib/python3.7/dist-packages/busio.py", line 281, in configure
    raise RuntimeError("First call try_lock()")
RuntimeError: First call try_lock()

kevkeva
 
Posts: 10
Joined: Thu Oct 29, 2020 12:52 pm

Re: MAX31865-PT100

by mikeysklar on Fri Nov 13, 2020 2:12 pm

@kevkeva,

Looks like we will need to wait for the SPI bus to not be in use when calling spi.conifgure.

How about this wrapper code around spi.configure().

Code: Select all | TOGGLE FULL SIZE
while not spi.try_lock():
    pass
spi.configure(baudrate=5000000, phase=0, polarity=1)
spi.unlock()

mikeysklar
 
Posts: 2398
Joined: Mon Aug 01, 2016 8:10 pm

Re: MAX31865-PT100

by kevkeva on Fri Nov 13, 2020 2:33 pm

I have used this code and get unhappily this Error:
Code: Select all | TOGGLE FULL SIZE
Traceback (most recent call last):
  File "PT100.py", line 17, in <module>
    sensor = adafruit_max31865.MAX31865(spi, cs, rtd_nominal=100, ref_resistor=                                                                                         430, wires=4)
  File "/usr/local/lib/python3.7/dist-packages/adafruit_max31865.py", line 115,                                                                                          in __init__
    config = self._read_u8(_MAX31865_CONFIG_REG)
  File "/usr/local/lib/python3.7/dist-packages/adafruit_max31865.py", line 139,                                                                                          in _read_u8
    device.write(self._BUFFER, end=1)
  File "/usr/local/lib/python3.7/dist-packages/busio.py", line 300, in write
    return self._spi.write(buf, start, end)
  File "/usr/local/lib/python3.7/dist-packages/adafruit_blinka/microcontroller/                                                                                         generic_linux/spi.py", line 81, in write
    self._spi.mode = self.mode
  File "/usr/local/lib/python3.7/dist-packages/Adafruit_PureIO/spi.py", line 35                                                                                         5, in mode
    self._ioctl(SPI._IOC_WR_MODE, mode)
  File "/usr/local/lib/python3.7/dist-packages/Adafruit_PureIO/spi.py", line 22                                                                                         4, in _ioctl
    ioctl(self.handle, ioctl_bytes, arg)
OSError: [Errno 22] Invalid argument

kevkeva
 
Posts: 10
Joined: Thu Oct 29, 2020 12:52 pm

Please be positive and constructive with your questions and comments.