SPI conflict?

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
michaelmcs
 
Posts: 17
Joined: Wed Aug 26, 2020 9:10 pm

SPI conflict?

Post by michaelmcs »

setup:
Adafruit CircuitPython 7.2.5 on 2022-04-06; FeatherS2 with ESP32S2
I2C/SPI LCD backpack
PT 1000 amplifier

Code: Select all

import gc
import sys
import time
import board
import digitalio
import busio
import adafruit_max31865
import adafruit_character_lcd.character_lcd_spi as character_lcd
def display(string):
        lcd.clear()
        lcd.message = string

lcd_columns = 20
lcd_rows = 5
spi = busio.SPI(board.SCK, MOSI=board.MOSI)
latch = digitalio.DigitalInOut(board.D5)
lcd = character_lcd.Character_LCD_SPI(spi, latch, lcd_columns,
                                    lcd_rows)

spi = busio.SPI(board.SCK, MOSI=board.MOSI,MISO=board.MISO)
cs = digitalio.DigitalInOut(board.D9)
adafruit_max31865.MAX31865(spi, cs, wires=3, 
                rtd_nominal=1000.0, ref_resistor=4300.0)

lcd.backlight = True
lcd.clear()

def temp():
    try:
        t= temperature * 9 / 5 + 32
        return(int(t))
    except:
        return(0)


currentTemp = 22
setTemp = 55

while True:
    previousTemp = currentTemp
    currentTemp = sensor1.temp()
    currentTemp = 55
    display("%d / %d" % (round(currentTemp), round(setTemp)))
    time.sleep(5)
    setTemp += 1
when I run this code I get the following output:
code.py output:
Traceback (most recent call last):
File "code.py", line 20, in <module>
ValueError: IO36 in use


If I have only the LCD or only the PT1000 in the code, it runs. If I have both, no mater the order, whichever comes second throws the error.

What am I doing wrong?

User avatar
tannewt
 
Posts: 3304
Joined: Thu Oct 06, 2016 8:48 pm

Re: SPI conflict?

Post by tannewt »

Only do `spi = busio.SPI(board.SCK, MOSI=board.MOSI,MISO=board.MISO)` once and use the spi object for both libraries.

User avatar
dastels
 
Posts: 15656
Joined: Tue Oct 20, 2015 3:22 pm

Re: SPI conflict?

Post by dastels »

Better, if you have board.SPI() just use that rather than busio.SPI(). It caches the instance and uses the appropriate pins.

Dave

User avatar
michaelmcs
 
Posts: 17
Joined: Wed Aug 26, 2020 9:10 pm

Re: SPI conflict?

Post by michaelmcs »

Thank-you both. I've got both the lcd display and PT1000 running ... sort of.

I can display to the lcd but following a read of the PT1000, the display just shows garbage. However, the temperature is still being read and is reasonable (I write it to REPL)

I had assumed that the library functions would handle the lock/unlock of the SPI bus. Maybe that was expecting too much?

Suggestions are appreciated.

I put the sleeps in the code so that I would have time to read the display.

Code: Select all

import gc
import sys
import time
import board
import digitalio
import busio
import adafruit_max31865
import adafruit_character_lcd.character_lcd_spi as character_lcd

spi = board.SPI()
cs = digitalio.DigitalInOut(board.D9)
sensor = adafruit_max31865.MAX31865(spi, cs, wires=3, rtd_nominal=1000.0, ref_resistor=4300.0)

def readTemp():
    try:
        t= sensor.temperature * 9 / 5 + 32
        time.sleep(2)
        return(int(t))
    except:
        return(0)
    
lcd_columns = 20
lcd_rows = 4
latch = digitalio.DigitalInOut(board.D5)
lcd = character_lcd.Character_LCD_SPI(spi, latch, lcd_columns, lcd_rows)

def display(string):
    lcd.clear()
    lcd.message = string
    time.sleep(2)

debugMessage = '''Debug check
  Check  %d
  Filler Text
  Temperature now %d'''

display(debugMessage % (0, 0.0))
time.sleep(5)
count = 1
while count:
    setTemp =  readTemp()
    print('loop %d: currentTemp == %d' % (count, setTemp))
    display(debugMessage % (count, setTemp))
    count += 1

Locked
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”