DS18b20 notes

Active today
Viewed 6 times

I have set up 3 temperature sensors (DS18B20) and connected them to what I believe is a Raspberry Pi 4. I have only been using the Pi 4 for easy testing before I move everything over to the Pi zero headless. (I have not been able to successfully use SSH with the Pi zero. So please don’t ask me to see what settings and information are on the Raspberry Pi zero, although if you have an easy way to get access to the Pi zero without SSH that would also help!) On the Pi 4 everything works together flawlessly. But, once I move it over to the Raspberry Pi zero, I get no data.

So here is where I think the problem is:

sensor_1 = '/sys/bus/w1/devices/28-000004b91a2d/w1_slave'
sensor_2 = '/sys/bus/w1/devices/28-000004b8a26d/w1_slave'
sensor_3 = '/sys/bus/w1/devices/28-000004b8fb4c/w1_slave'

Does the device ID change after moving the sensors to the PI zero? (The device ID’s were found on the Raspberry Pi 4)

The rest of the code in case it helps.

import time
import sys
from datetime import datetime
import csv

# System path to the sensor, further system paths could be via an array
# or other variables can be added here.
# 28-02161f5a48ee you have to replace with your sensor!
sensor_1 = '/sys/bus/w1/devices/28-000004b91a2d/w1_slave'
sensor_2 = '/sys/bus/w1/devices/28-000004b8a26d/w1_slave'
sensor_3 = '/sys/bus/w1/devices/28-000004b8fb4c/w1_slave'

#now = str(datetime.now())

def readTempSensor(sensorName):
    """I read the temperature of the DS18B20 from the system bus."""
    f = open(sensorName, 'r')
    lines = f.readlines()
    return lines

def readTempLines(sensorName):
    lines = readTempSensor(sensorName)
    # As long as the data could not be read, I am in an endless loop
    while lines[0].strip()[-3:] != 'YES':
        lines = readTempSensor(sensorName)
    temperaturStr = lines[1].find('t=')
    # I check if the temperature was found.
    if temperaturStr != -1:
        tempData = lines[1][temperaturStr + 2:]
        tempCelsius = float(tempData) / 1000.0
        tempKelvin = 273 + float(tempData) / 1000
        tempFahrenheit = float(tempData) / 1000 * 9.0 / 5.0 + 32.0
        # Return as an array - [0] tempCelsius => Celsius ...
        return [tempCelsius, tempKelvin, tempFahrenheit]

# where the script starts
def main():
    # creates header for CSV file
    f = open('/home/pi/Mamdau2DATA/TempDATA.csv', 'a')
    f.write('Date and Time')
    f.write('Temperature Sensor 1')
    f.write('Temperature Sensor 2')
    f.write('Temperature Sensor 3')
    f.write(' ')

        while True:
            now = str(datetime.now())

            # I provide my measurement with a timestamp and have it displayed in the console.
            print("Temperature Sensor 1 at " + time.strftime('%H:%M:%S') + "   Temp. : " + str(readTempLines(sensor_1)[0]) + " °C")
            print("Temperature Sensor 2 at " + time.strftime('%H:%M:%S') + "   Temp. : " + str(readTempLines(sensor_2)[0]) + " °C")
            print("Temperature Sensor 3 at " + time.strftime('%H:%M:%S') + "   Temp. : " + str(readTempLines(sensor_3)[0]) + " °C")
            temperature_1 = readTempLines(sensor_1)[0]
            temperature_2 = readTempLines(sensor_2)[0]
            temperature_3 = readTempLines(sensor_3)[0]
            # The next measurement takes place after 10 seconds

            # Datenlogger
            f = open('/home/pi/Mamdau2DATA/TempDATA.csv', 'a')
            #f.write(time.strftime('%Y-%m-%d %H:%M:%S'))
            f.write('{0:0.2f}'.format(temperature_1))  # Temperature
            f.write(' ')  # Abstand/Leerzeichen
            f.write('\n')  # Neue Zeile

            # and so on

    except KeyboardInterrupt:
        # Program ends when CTRL + C is pressed.
        print('Temperature measurement is ended')
    except Exception as e:
        # The program ends here so that no error is written to the console.
        print('Program has ended.')

if __name__ == '__main__':
  • Hello @ViperSniper0501, your program looks OK. Perhaps you can try my demo program to compare and contrast with yours. My completely listed program is fully debugged and plug and play. You just copy and run it without setting any parameter. The DS18B20s are auto detected and IDs listed: (1) “DS18B20 Temperature Sensor – Rpi 3/4 Driver, Wiring, Detection, and Python Programming”: raspberrypi.stackexchange.com/questions/100203/…. Happy temperature sensing and cheers. – tlfong01 3 mins ago   Edit   


No, the device id does not change, it is hardwired into the DS18B20.

Categories: Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: