svp
Posts: 5
Joined: Thu Nov 29, 2018 11:05 am

PROBLEM : MAX31850 interfacing with RPi_3B+ using 1-wire protocol in python

Thu Nov 29, 2018 12:13 pm

Hi there,

I m a beginner with both Raspberry and Python.

I interfaced MAX31850J with RPi 3B+. The DQ line of MAX31850 is connected with PIN 38 of RPi. 10 other different thermocouples are connected on different different pins.

I have written 1 code to get temperature reading from the MAX31850J. It always giving the same reading. I am not able to understand how to read the temperature from it. Is there any library that i can use? The code is given below:

Code: Select all

from time import sleep
import RPi.GPIO as GPIO

## to select pin number pattern
GPIO.setmode(GPIO.BOARD)

## to config 38 pin as input
GPIO.setup(38,GPIO.IN)

"""function to initialize the thermocouple"""
def resetSlave():
	## for handshaking 
	GPIO.setup(38,GPIO.IN,pull_up_down=GPIO.PUD_DOWN)
	sleep(480*1/1000000)
	## to wait for response
	sleep(300*1/1000000)

"""function to send command to thermocouple"""
def slaveCommand(romCommand = 0xCC):
	for i in range(8):
		if( (romCommand>>i)&1 == 1 ):
			GPIO.setup(38,GPIO.IN,pull_up_down=GPIO.PUD_UP)
			sleep(60*1/1000000)
		else:
			GPIO.setup(38,GPIO.IN,pull_up_down=GPIO.PUD_DOWN)
			sleep(60*1/1000000)
		sleep(1*1/1000000)		## recovery time

"""function to read temperature from thermocouple"""
def readTemperature():
	tempData = 0
	## slaveCommand(0xBE)
	for i in range(16):
		if( GPIO.input(38) ):
			tempData = tempData | (1<<i)
			sleep(0.000060)
		else:
			tempData = tempData | (0<<i)
			sleep(0.000060)
		sleep(0.000001)
	return tempData

""" to get contineous temperature from thermocouple """
while 1:
	temp = 0
	
	## step_1 : initialization
	resetSlave()
	
	## step_2 : ROM command followed by data exchange format
	slaveCommand()
	slaveCommand(0x44)
	
	## to Reading temperature
	slaveCommand()
	temp = readTemperature()

	print "Temperature = " + str(temp)
	sleep(1)

ghp
Posts: 1254
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: PROBLEM : MAX31850 interfacing with RPi_3B+ using 1-wire protocol in python

Fri Nov 30, 2018 6:35 pm

Hello,
most possible python in linux is not accurate enough to drive this device. Found some c-code in https://github.com/danjperron/BitBangingDS18B20 which might help you, covers also the MAX31850. There are some remarks about this in
the issues "Raspberry Pi 3 and MAX31850 (now working!)".
Basically the w1-gpio driver is used for this type of device, but looks as if there are no success stories using this driver and the chip.

Other solutions:
- use MAX31855 with SPI, which is straightforward, well explained and working with python (did this once here).
- use an arduino board with one-wire lib to scan your MAX31850 devices and let this board report by USB serial to the raspberry.

svp
Posts: 5
Joined: Thu Nov 29, 2018 11:05 am

Re: PROBLEM : MAX31850 interfacing with RPi_3B+ using 1-wire protocol in python

Sat Dec 08, 2018 9:34 am

ghp wrote:
Fri Nov 30, 2018 6:35 pm
Hello,
most possible python in linux is not accurate enough to drive this device. Found some c-code in https://github.com/danjperron/BitBangingDS18B20 which might help you, covers also the MAX31850. There are some remarks about this in
the issues "Raspberry Pi 3 and MAX31850 (now working!)".
Basically the w1-gpio driver is used for this type of device, but looks as if there are no success stories using this driver and the chip.

Other solutions:
- use MAX31855 with SPI, which is straightforward, well explained and working with python (did this once here).
- use an arduino board with one-wire lib to scan your MAX31850 devices and let this board report by USB serial to the raspberry.
Thanks ghp...

Previously, I have used MAX318555. As am using 11 sensors & it is using SPI protocol it is quite tedious task to manually give address to each sensor Ic.

Currently I am trying with 2nd option using arduino. Communication between Pi & Arduino using UART @ 115200 baud. I have programmed the arduino ic that when is receives 'R' letter on Rx then it transmits all 11 data on Tx.

I am sending 'R' continuously using RPi, but sometimes Pi receives proper data and sometimes not or data with error.

svp
Posts: 5
Joined: Thu Nov 29, 2018 11:05 am

Re: PROBLEM : MAX31850 interfacing with RPi_3B+ using 1-wire protocol in python

Mon Dec 31, 2018 9:48 am

Hello Everyone,

I found the solution for the problem stated above. First of all, we have to configure 11 pin for 1-Wire protocol in /boot/config.txt file.
Image.

Instead of reading direct /sys/bus/w1/3b* file we should read through /sys/bus/w1/w1_bus_master*/3b* file. So we can trace out the data of each sensor and not need to modify the program even we change the ic. Because, each ic creates unique file but the folder will remain unchanged.

Code: Select all

# coding: utf-8

import os
import time
import glob

# load 1-wire drivers
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')

# genarates path for perticular function
def filePathGen(sensor_id):
	base_dir = '/sys/bus/w1/devices/w1_bus_master'	# commo path
	base_dir = base_dir + str(sensor_id) + '/'	# different path for each sensor
	device_folder = glob.glob(base_dir + '3b*')[0]	# destination folder
	device_file = device_folder + '/w1_slave'	# destination file
	return device_file

# reads the lines of the 'w1_slave' file
def temp_raw(file_path):
	f = open(file_path, 'r')
	lines = f.readlines()
	f.close()
	return lines

# to calculate temperature from the data
def read_temp(file_path):
    lines = temp_raw(file_path)
    # if data reading fail then wait for 0.2 second and read again
    while lines[0].strip()[-3:] != 'YES':
        time.sleep(0.2)
        lines = temp_raw()
    # find 't=' in the lines and take the data after it
    temp_output = lines[1].find('t=')
    if temp_output != -1:
        temp_string = lines[1].strip()[temp_output+2:]
        temp_c = float(temp_string) / 1000.0
        temp_f = temp_c * 9.0 / 5.0 + 32.0
        return temp_c, temp_f

while True:
	# to continuously read temperature through all 11 sensor
	for i in range(11):
		file_path = filePathGen(i+1)
        	print 'Sensor_{0} : {1}'.format(i+1,read_temp(file_path))
	print '\n'
        time.sleep(1)

Return to “Python”