BLOG# 5 -Edible Algae System - (Growing Spirulina in space)

Temperature Sensor - DS18B20

This is my 5th blog post in a series of blog post for the Design Challenge 1 Meter of Pi

version 1.3


Blog# 4 EAS - Research and Unboxing of the challenge kit

Blog# 6 EAS - System Designs -- Block diagram



In order to maintain a consistent temperature of the water in the growing tank, a waterproof temperature Sensor will be used.

The Sensor that I found on the internet, is the 1-Wire DS18B20

I bought a set of five DS18B20  from Amazon here

How to Setup the DS18B20 - Experiment 2

OK, since the 1st setup experiment did not work, I've decided to try another option that element14 member Adrian Christiansen recommended.

I've decided to give the package on GitHub a try W1ThermSensor

You can reference the GitHub, but I'll document what I did to get this sensor connected to the Raspberry PI.



These are the Materials needed:

  • Raspberry Pi 4model B 2GB
  • DB18B20 (waterproof temperature sensor)
  • 10kΩ resistor
  • Breadboard
  • Jumper Wires


Edit the /boot/config.txt and I added dtoverlay=w1-gpio,pullup="y" to the end.

edit with sudo nano /boot/config.txt


NOTE: the article suggest that you can use dtoverlay=w1-gpio , but it did not work for me.

Attaching to the PI

connected the jumpers, resistor, and sensor to the breadboard an RaspPI as described in the article.



install it from the official repository

sudo apt-get install python3-w1thermsensor


you can check the connection from the command line

List all available sensors:

$ w1thermsensor ls


here's the code that worked for me


import Unit from w1thermsensor but this did not work for me

from w1thermsensor import W1ThermSensor


sensor = W1ThermSensor()

temperature_in_celsius = sensor.get_temperature()

temperature_in_fahrenheit = sensor.get_temperature(.DEGREES_F)

here it is running


The water in the glass is almost the sane as the room here. a cowincidence.

SUCCESSThis sensor works great.



How to Setup the DS18B20 - Research experiment 1

NOTE 10/21/2020 --- Problem on  step 3.2


  • Folder not created after a reboot
  • Will try alternative suggestion from ajcc

I was able to find an article on the  DS18B20 on  the internet at Set Up a Raspberry Pi DS18B20 Temperature Sensor, which I will attempt to experiment with.

Using this article as a guide, The following steps are taken:

  1. Show how to enable the w1-gpio in config.txt.
  2. Describe how to connect the DS18B20 to the Raspberry Pi 4
  3. Describe how to load, configure, test the drivers.
  4. Python code example to read the sensor


These are the Materials that were recommended:

  • Raspberry Pi 4model B 2GB
  • DB18B20 (waterproof temperature sensor)
  • 4.7kΩ (or 10kΩ) resistor
  • Breadboard
  • Jumper Wires


1.Enable the w1-gpio in config.txt

Enable the w1-gpio by editing the /boot/config.txt file.

  1. Enter the following command:  sudo nano /boot/config.txt
  2. Then add this line to the bottom of the file if not already present dtoverlay=w1-gpio

2. Connect the Sensor

  1. Connect GPIO GND on the Pi to the negative rail on the breadboard and connect GPIO 3.3V on the Pi to the positive rail on the breadboard.
  2. Connect the black (-)wire of your DS18B20 to the breadboard negative rail.
  3. Connect the red (+)wire of your DS18B20 to the breadboard positive rail.
  4. Connect one end of your 4.7kΩ resistor to the positive rail and the other end to an empty row on your breadboard.
  5. Connect the yellow (Data) wire of your DS18B20 to the same row on your breadboard as the 4.7kΩ resistor.
  6. Finally, connect GPIO4 on the RPi to the same row on your breadboard as the 4.7kΩ resistor and yellow (Data) wire.

     If all has gone well then your setup should look like the diagram below.

<DIAGRAM of the Breadboard HERE>

3. Load, Configure and Test the drivers

     Now , we will need to identify the registered serial number of our sensor. To do this follow the next steps:

     1. Add the 1-Wire and Thermometer drivers to your RasPI.

        To load the drivers, use the modprobe command.

        From the command prompt enter the following:


     2. We then need to check that the DB18B20 has connected correctly to our Pi, change directory to your 1-Wire device folder and list the devices.

     cd /sys/bus/w1/devices/


          In the device drivers folder, your sensor should have created a folder using its mac address.

          If you can’t see any folder then perform a reboot of the Pi and check again.

     sudo reboot



  • Folder not created even after a reboot


3. You can read what has been saved to the file by entering

cd /sys/bus/w1/devices/


cd 28-xxxxxxxxxxxx (enter your registered device mac address)

cat w1_slave

4. This will print 2 lines with error checking.

  • In the first line we see that it finishes with the word YES, this indicated that there has been a successful reading.
  • In the second line, there is a t= indicating the temperature in °C.


The temperature sensor is now successfully connected to our Pi so we can start our Python programming.


4. Python Code Example


import os

from time import sleep

First, we need to import the required python modules.

os.system('modprobe w1-gpio')

os.system('modprobe w1-therm')

We then need to load our drivers:
temp_sensor_1 = '/sys/bus/w1/devices/Your_Sensor_Name/w1_slave'We now need to define where our sensors output file is located

def read_temp_raw():

   f = open(temp_sensor, 'r')

   lines = f.readlines()


   return lines

We next need to read into a variable our raw sensor output file,


This function opens, reads and then saves the 2 lines of raw data to the variable “lines” , we use the return function so that the result can be used later in our code.

def read_temp():



lines = read_temp_raw()

while lines[0].strip()[-3:] != 'YES':


  lines = read_temp_raw()

temp_result = lines[1].find('t=')



if temp_result != -1:

  temp_string = lines[1].strip()[temp_result + 2:]

  # Temperature in Celcius

  temp = float(temp_string) / 1000.0

  # Temperature in Fahrenheit

  #temp = ((float(temp_string) / 1000.0) * (9.0 / 5.0)) + 32.0

  return temp

Now we need to analyze our raw data, first we will check for errors


This error check reads the first line from the variable lines (lines[0]) and then strips all but the last 3 digits, it is then compared to the value ‘YES’ (In python “!=” is defined as “not equal to”).  If the value does not equal ‘YES’ then the program pauses for 0.2s then reads the new data and does the comparison again.


Once we have a successful ‘YES’ result we move on to read the second line of data from the variable lines (lines[1]).  We search the string and count how many digits until we find the string ‘t=’ and store it in temp_result.  Provided the result of this is not -1 we proceed to strip all the digits prior to that number plus 2 (to remove the ‘t=’) leaving us with just the temperature stored in temp_string. Finally, we calculate the temperature in Celsius or Fahrenheit ready for printing.

while True:



We now loop or final code to print out the data every 2 seconds



Conclusion and summary



  • Problem

I Tried to hook up the sensor using an article from 2016 but I could not get it to work

  • I had a problem when with the loading of the drivers as described in my reference article.
  • The directory was not created as specified in section 3.1 and 3.2 above.
  • I need to figure out why? i might have to use a different approach.
  • I received a suggestion from ajcc  in a comment below.
  • I will try and follow this members advice to get the sensor working.

Suggestion from a member

Adrian Christiansen Nov 22, 2020 5:33 AM

AC: "The temperature T is in milliCelsius, this is the default unit used by temperature sensors in Linux, that might be obvious from reading the python code but perhaps not from the sysfs output?"

SK: Good to know


AC: "Instead of running modprobe from the Python script (requiring you to it run as root), you automatically load them at boot by add the following to /etc/modules

  1. w1-gpio 
  2. w1-therm 


SK: Good to know


AC: "If you don't want to write your own code to parse the output in the sysfs-file you could pip install W1ThermSensor instead, which also implements a command for setting the resolution from 9 to 12-bits (you can also do this by writing to the sysfs-file, notice that you can save the settings in the device)."

SK: I've decided to give the package on git Hub a try W1ThermSensor

I will document my finding in this blog


I used the package on GitHub W1ThermSensor

I started on a new Raspberry pi and put the same circuit I had for experiment 1.

THis worked out great for me and I will use the circut and software for my design.








GitHub  W1ThermSensor Package
Set Up a Raspberry Pi DS18B20 Temperature Sensor





Blog# 4 EAS - Research and Unboxing of the challenge kit

Blog# 6 EAS - System Designs -- Block diagram