herosdevices.hardware.raspberrypi.max318xx.max31865 - Heros Devices 0.10.3 documentation

Skip to content
Back to top
View this page

# herosdevices.hardware.raspberrypi.max318xx.max31865

MAX31865 Resistance to Digital Converter.

This module implements an interface for the MAX31865 resistance temperature detector (RTD) converter. The chip communicates over SPI. This implementation uses GPIO bit-banging on a Raspberry Pi. Using spidev would be more efficient, but initial attempts with spidev were unsuccessful.

This code is based on the work of steve71: https://github.com/steve71/MAX31865

Classes: - MAX31865: Provides methods to read resistance and temperature from PT100/PT1000 sensors.

> - Initialization allows configuration of SPI pins, reference resistance, and wiring type.

> - Methods to fetch resistance, convert resistance to temperature, and handle errors.

Usage: - Instantiate multiple sensor objects for different chip select pins. - Loop to read and print temperature readings. - Gracefully handle interruption and clean GPIO resources.

Notes: - Proper wiring and pull-down resistors are essential. - Optimized for use with a Raspberry Pi and RPi.GPIO.

## Module Contents

herosdevices.hardware.raspberrypi.max318xx.max31865.A = 0.003908

herosdevices.hardware.raspberrypi.max318xx.max31865.B = -5.775e-07

herosdevices.hardware.raspberrypi.max318xx.max31865.C = -4.183e-12

herosdevices.hardware.raspberrypi.max318xx.max31865.c_v_d(r: float, r_0: float) → float

  Calculate temperature using Callendar-Van Dusen equation.

  This function converts resistance to temperature based on the Calendar-Van Dusen equation, which relates resistance in Ohms to temperature in Celsius. It uses numpy for polynomial root solving.

  Parameters:

    - r (float) – Resistance in Ohms.

    - r_0 (float) – Resistance at 0°C in Ohms.

  Returns:

    Estimated temperature in degrees Celsius.

  Return type:

    float

  Raises:

    - ImportError – If numpy is not installed.

    - ValueError – If the polynomial root calculation encounters issues (e.g., negative discriminant).

herosdevices.hardware.raspberrypi.max318xx.max31865.c_v_d_quad(r: float, r_0: float) → float

  Approximate temperature using a quadratic form of the Callendar-Van Dusen equation.

  This method offers a fast and reasonably accurate way to estimate temperature from resistance, suitable for class A PT100 sensors with about 0.2°C accuracy.

  Parameters:

    - r (float) – Resistance in Ohms.

    - r_0 (float) – Resistance at 0°C in Ohms.

  Returns:

    Estimated temperature in degrees Celsius.

  Return type:

    float

  Raises:

    ValueError – If the discriminant in the quadratic formula is negative.

class herosdevices.hardware.raspberrypi.max318xx.max31865.MAX31865(cs_pin: int = 8, miso_pin: int = 9, mosi_pin: int = 10, clk_pin: int = 11, r_0: float = 1000.0, r_ref: float = 400.0, three_wire: bool = False)

  Bases: `herosdevices.hardware.raspberrypi.max318xx.max318xx.MAX318xx`

  Interface class for MAX31865 resistance temperature sensor.

  Parameters:

    - cs_pin (int) – Chip select GPIO pin (default: 8).

    - miso_pin (int) – MISO GPIO pin (default: 9).

    - mosi_pin (int) – MOSI GPIO pin (default: 10).

    - clk_pin (int) – Clock GPIO pin (default: 11).

    - r_0 (float) – Resistance at 0°C, in Ohms (default: 1000 for PT1000).

    - r_ref (float) – Reference resistor resistance, in Ohms (default: 400).

    - three_wire (bool) – Use 3-wire configuration (default: False).

    - log (logging.Logger) – Optional logger for debug messages.

  r_ref = 400.0
  r_0 = 1000.0
  config = 162
  read_resistance(cs_pin: int) → float

    Initiate resistance measurement and return resistance value.

    Returns:

      Resistance in Ohms.

    Return type:

      float

  read_temp(convert: collections.abc.Callable[[float, float], float] = c_v_d_quad) → dict

    Read resistance and convert to temperature.

    Parameters:

      convert (function) – Function to convert resistance to temperature. Defaults to c_v_d_quad.

    Returns:

      Temperature in Celsius.

    Return type:

      float

  cs_pins
  miso_pin = 9
  mosi_pin = 10
  clk_pin = 11
  write_register(reg_num: int, data_byte: int) → None

    Write a byte to a specified register.

    Parameters:

      - regNum (int) – Register number to write to.

      - dataByte (int) – Data byte to write.

  read_register(cs_pin: int, num_registers: int, reg_num_start: int | None = None) → list[int]

    Read one or more bytes starting from a specified register.

    Parameters:

      - cs_pin (int) – Chip select GPIO pin number.

      - numRegisters (int) – Number of bytes to read.

      - regNumStart (int, optional) – Starting register number for the read operation.

    Returns:

      List of bytes read from the device.

    Return type:

      list

  send_byte(byte: int) → None

    Send a byte via SPI by bit-banging.

    Parameters:

      byte (int) – Byte value to send.

  recv_byte() → int

    Receive a byte via SPI by bit-banging.

    Returns:

      Byte received.

    Return type:

      int

  create_dict(values: list[float], observable: str, unit: str) → dict

    Build dictionary of observables to be returned by the sensor.