Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 5055

Beginners • Re: Need help with temperature sensor and display project

$
0
0
Hello everyone,

I am back with some updates and more questions related to reading the temperature.

I have successfully installed Mosquitto on my server. I am also capable of publishing a topic from the sensor pico, and subscribe with others + the server to insert data into the database.

I have moved onto reading data from the sensor.

I have purchased a MAX31865 Image

Store page: https://start3dprint.si/max31865-rtd-to ... alni-modul

I have found a github repository, that has implemented reading of such sensor(2wire, but for PT100) https://github.com/aalbersJulia/tempera ... ax31865.py.

The only modification I made is a print statement and changed _RTD_0 = const(100) to _RTD_0 = const(1000), Will leave the changed source code under this.

Code:

from micropython import constimport machineimport timeimport math_MAX31865_CONFIGURATION_REG = const(0x00)_MAX31865_RTD_MSB_REG = const(0x01)_MAX31865_RTD_LSB_REG = const(0x02)_MAX31865_HIGH_FAULT_THRESHOLD_MSB_REG = const(0x03)_MAX31865_HIGH_FAULT_THRESHOLD_LSB_REG = const(0x04)_MAX31865_LOW_FAULT_THRESHOLD_MSB_REG = const(0x05)_MAX31865_LOW_FAULT_THRESHOLD_LSB_REG = const(0x06)_MAX31865_FAULT_STATUS_REG = const(0x07)# hidden constants_REFERENCE_RESISTOR = const(430.0)_RTD_0 = const(1000) # RTD resistance at 0degC_RTD_A = const(3.9083e-3)_RTD_B = (-5.775e-7)class MAX31865:    """    MAX31865 thermocouple amplifier driver for the PT100 (2-wire)    :param spi: SPI device, maximum baudrate is 5.0 MHz    :param cs: chip select        usage:    from machine import Pin, SPI    from max31865 import MAX31865        spi = SPI(0, baudrate=5000000, sck=2, mosi=3, miso=4)    cs = Pin(5)        sensor = MAX31865(spi, cs)    temp = sensor.temperature    """    def __init__(self, spi, cs, baudrate=5000000):        if cs is None:            raise ValueError("CS parameter must be provided")                # initalize SPI bus and set CS pin high        self.spi = spi        cs.init(mode=machine.Pin.OUT, value=1)        self.cs = cs                self.configure(0x00)            """"    Configuration Register Definition    D7: Vbias (1 = ON, 0 = OFF)    D6: Conversion mode (1 = Auto, 0 = Normally off)    D5: 1-shot (1 = 1-shot (auto-clear))    D4: 3-wire (1 = 3-wire RTD, 0 = 2-wire or 4-wire)    D3-D2: Fault Detection Cycle Control    D1: Fault Status Clear (1 = Clear (auto-clear))    D0: 50/60Hz filter select (1 = 50 Hz, 0 = 60 Hz)    """    def configure(self, config):        self.write(_MAX31865_CONFIGURATION_REG, config)        time.sleep(0.065)    def read(self, register, number_of_bytes = 1):        # registers are accessed using the 0Xh addresses for reads        try:            self.cs.value(0)            self.spi.write(bytearray([register & 0x7F]))            return self.spi.read(number_of_bytes)        except Exception as e:            raise OSError(f"MAX31865: SPI Error: {e}")        finally:            self.cs.value(1)        def write(self, register, data):        # registers are accessed using the 8Xh addresses for writes        try:            self.cs.value(0)            self.spi.write(bytearray([(register | 0x80) & 0xFF, data & 0xFF]))        except Exception as e:            raise OSError(f"MAX31865: SPI Error: {e}")        finally:            self.cs.value(1)            # read the raw value of the thermocouple    def read_rtd(self):        self.configure(0xA0)        rtd = self.read(_MAX31865_RTD_MSB_REG, 2)        rtd = (rtd[0] << 8) | rtd[1]        rtd >>= 1        return rtd            # read the resistance of the PT100 in Ohms    @property    def resistance(self):        resistance = self.read_rtd()        print(resistance)        resistance /= 32768        resistance *= _REFERENCE_RESISTOR        return resistance        # read the temperature of the PT100 in degrees (math: https://www.analog.com/media/en/technical-documentation/application-notes/AN709_0.pdf)     @property    def temperature(self):        raw = self.resistance        Z1 = -_RTD_A        Z2 = _RTD_A * _RTD_A - (4 * _RTD_B)        Z3 = (4 * _RTD_B) / _RTD_0        Z4 = 2 * _RTD_B                temp = Z2 + (Z3 * raw)        temp = (math.sqrt(temp) + Z1) / Z4                if temp >= 0:            return temp                raw /= _RTD_0        raw *= 100 # normalize to 100 ohms )                rpoly = raw        temp = -242.02        temp += 2.2228 * rpoly        rpoly *= raw  # square        temp += 2.5859e-3 * rpoly        rpoly *= raw  # ^3        temp -= 4.8260e-6 * rpoly        rpoly *= raw  # ^4        temp -= 2.8183e-8 * rpoly        rpoly *= raw  # ^5        temp += 1.5243e-10 * rpoly                return temp        
This is my main.py

Code:

import secretsimport networkfrom time import sleepimport machinefrom machine import Pin, Timerimport mipfrom max31865 import MAX31865def connect():    # Connect to WLAN    wlan = network.WLAN(network.STA_IF)    wlan.active(True)    wlan.connect(secrets.wan_ssid, secrets.wan_password)    while wlan.isconnected() == False:        print('Waiting for connection...')        sleep(1)    ip = wlan.ifconfig()[0]    print(f'Connected on {ip}')    return ipdef install_dependencies():    mip.install('umqtt.simple')    # mip.install('umqtt.robust')def publish_temperature_topic(ip, temperature):    from umqtt.simple import MQTTClient    client = MQTTClient(ip, secrets.mqtt_server, secrets.mqtt_port, secrets.mqtt_user, secrets.mqtt_password)    client.connect()    client.publish('thermal', temperature, retain=False, qos=0)    client.disconnect()try:    ip = connect()    install_dependencies()    spi = machine.SPI(0, baudrate=5000000, phase=1, sck=machine.Pin(18), mosi=machine.Pin(19), miso=machine.Pin(16))    cs = machine.Pin(20)    sensor = MAX31865(spi, cs)    temperature = sensor.temperature    print(f"Current Temperature: {temperature} °C")except KeyboardInterrupt:    machine.reset()
The only results for resistance I am capable of getting are 32767(when CS in on pin 20 ) or 0(when CS is not on pin 20).


The questions:
1. I am unable to install dependencies with Thonny package manager(there are errors when I try to install any dependency with it, not just umqtt.simple). And found a way to do this via mip(runtime download to pico). Not the best, but for start i guess it will do. In the end I would like for these devices not to have a connection the the internet. What would be the best approach to this?

2. I am not sure what I am missing with temperature read. Does the order of 2 wire sensor matter?(some sources say it does, some it does not, that they just need to be on oposite terminals), I have also swapped them aound from F+ and F- terminals but the readings are same.

3. This is not an Adafruit MAX31865, but a clone from what I understand, is this my issue?

Statistics: Posted by crkovinar — Sun Nov 24, 2024 1:15 pm



Viewing all articles
Browse latest Browse all 5055

Trending Articles