3

I need to read temperature data with using MAX31865 SPI communication. First of all, I tried to read 4 byte data:

import machine
import ubinascii

spi = machine.SPI(1, baudrate=5000000, polarity=0, phase=0)

#baudrate controls the speed of the clock line in hertz.
#polarity controls the polarity of the clock line, i.e. if it's idle at a low or high level.
#phase controls the phase of the clock line, i.e. when data is read and written during a clock cycle
cs = machine.Pin(15, machine.Pin.OUT)
cs.off()


cs.on()
data = spi.read(4)
cs.off()


print(ubinascii.hexlify(data))

I tried many times with different codes but result is always similar b'00000000'.

I am using ESP32 WROOM.

I used this pins:

ESP32 : D12 - D14 - 3V3 - GND - D15

Max31865: SDO - CLK - VIN - GND - CS

I am new on micropython and esp32.

I don't know what should I do. Is there any suggestions , recommended tutorials or idea?

noobinmath
  • 175
  • 2
  • 12

1 Answers1

2

Short answer: see if you can use CircuitPython and its drivers for MAX31865.

Long answer: a bunch of stuff. I suspect you've been following the Adafruit tutorial for MAX31855, but its SPI interface is very different from the MAX31865.

Your SPI connection is missing the SDI pin. You have to connect it, as communication is bidirectional. Also, I suggest using the default SPI pinout on ESP32 side as described in the micropython documetation for ESP32.

The SPI startup looks to be missing stuff. Looking at the SPI documentation a call to machine.SPI() requires that you assign values to arguments sck, mosi, miso. Those would probably be the pins on ESP32 side where you've connected SCLK, SDI, SDO on MAX31865 (note mosi means "master out, slave in" and miso is "master in, slave out").

The chip select signal on the MAX is inverted (that's what the line above CS input in the datasheet means). You have to set it low to activate the chip and high to disable it.

You can't just read data out of the chip, it has a protocol you must follow. First you have to request a temperature-to-resistance conversion from the chip. The datasheet for your chip explains how to do that, the relevant info starts on page 13 (it's a bit difficult to read for a beginner, but try anyway as it's the authoritative source of information for this chip). On a high level, it works like this:

  1. Write to Configuration register a value which initiates the conversion.
  2. Wait for the conversion to complete.
  3. Read from the RTD (Resistance-To-Digital) registers to get the conversion result.
  4. Calculate the temperature value from the conversion result.

The code might be closer to this (not tested, and very likely to not work off the bat - but it should convey the idea):

import ubinascii, time
from machine import Pin, SPI

cs = Pin(15, Pin.OUT)
# Assuming you've rewired according to default SPI pinout
spi = machine.SPI(1, baudrate=100000, polarity=0, phase=0, sck=Pin(14), mosi=Pin(13), miso=Pin(12))

# Enable chip
cs.off()
# Prime a 1-shot read by writing 0x40 to Configration register 0x00
spi.write(b'\x00\x40')
# Wait for conversion to complete (up to 66 ms)
time.sleep_ms(100)
# Select the RTD MSBs register (0x01) and read 1 byte from it
spi.write(b'\x01')
msb = spi.read(1)
# Select the RTD LSBs register (0x02) and read 1 byte from it
spi.write(b'\x02')
lsb = spi.read(1)
# Disable chip
cs.on()
# Join the 2 bytes
result = msb * 256 + lsb
print(ubinascii.hexlify(result))

Convert result to temperature according to section "Converting RTD Data Register Values to Temperature" in datasheet.

Side note 1: here spi = machine.SPI(1, baudrate=5000000, polarity=0, phase=0) you've configured a baud rate of 5MHz which is the maximum for this chip. Depending on how you've connected your devices, it may not work. The SPI protocol is synchronous and driven by master device, so you can set any baud rate you want. Start with a much, much lower value, maybe 100KHz or so. Increase this after you've figured out how to talk to the chip.

Side note 2: if you want your conversion result faster than the 100ms sleep in my code, connect the DRDY line from MAX to ESP32 and wait for it to go low. This means the conversion is finished and you can read out the result immediately.

Tarmo
  • 3,728
  • 1
  • 8
  • 25
  • I am grateful for your help in this matter, thank you very much. Actually when I tried the corrected code , result was the same.It may be because of micropython and esp32. Should I use circuitPython with ESP WROOM 32 ? – noobinmath Feb 26 '21 at 13:11
  • I need to learn spi and micropython because after this, I must write to code for [this](https://www.fujitsu.com/uk/Images/MB85RS2MTA.pdf) – noobinmath Feb 26 '21 at 13:14
  • 1
    Well, getting a new peripheral to work is always a struggle. First double- and triple-check your connections. If possible, buy or borrow a logical analyzer or a digital oscilloscope, hook it up to the SPI lines and verify that the signals are actually hitting the wires. Re-check the protocol in the datasheet (I only had a 5-minute look into it, likely missed something) etc. – Tarmo Feb 26 '21 at 13:34