1.02 Use temperature sensor DS18B20

I bought this sensors on amazon. Great sensor. It’s works since 3 years, also outside and under-water.

This sensor have 3 pin. One for VCC +3,3volt (red wire), one for GND (black wire), and the third for DATA (yellow wire). You can use more sensors at same time on your raspberry and it works very well from -20°C to 100°C with precision 0,5°C.

Note: you must use 3,3V for VCC and not 5V

Choose a PIN on raspberry

Before connect it on our raspberry we have to choose a PIN for the data. I chose the GPIO 4 (number 7 in normal numeration).

In the image above, I have colored in yellow the GPIO4 pin. In fact there are many numeration system… But I use every time the GPIO/BCM numeration… Well… This sensors using 1-wire system and send data serializing many information. We have to configure our raspberry to use the right PIN.

Open the file /boot/config.txt and add a line

sudo nano /boot/config.txt

Ok, at next reboot, the raspberry know that you are using the gpio4 for communicating with your sensor.

Important note: you can connect more than one sensor to the same pin! I have connected 4 sensors in parallel. If you want to use more that one sensor, you MUST connects all in parallel.


[adinserter name=”InArticle1″]

Hardware circuit

This sensor needs a pull-up 4,7kΩ resistor. In my experience I use only one resistor for all 4 sensors and it’s works perfectly from 3 years.

This is easy. These are some photo of my work where I have 3 temperature sensor DS18B20.

my raspberryPI and my connection with 3 sensor DS18B20 and pull-up circuit
my raspberryPI and my connection with 3 sensor DS18B20 and pull-up circuit


pull-up resitor, 3,3v and line data for the DS18B20
pull-up resitor, 3,3v and line data for the DS18B20

This is clearly that I have build a unique connection with the GPIO4, and a unique PULL-UP resistor. The various corrugated tubes arrived from first floor, from ground floor and external.

Test! How to read with Raspberry

I suggest to connect the sensors one by one because every sensors has a UNIQUE IDENTIFIER, and the only mode to discover it is reading by command line. I mean you connect the first sensor, and write on a paper the unique identifier of that sensor… after , you will connect the second and so on.

[adinserter name=”InArticle2″]

In linux, you know, everything is a file. When you connect your DS18B20, appear files and paths in /sys/bus/w1/devices. Every sensor is identificated by a path like “28-00000[…]”. In my experince, image above, I have 4 sensors.

You can enter in the path 28-000********* and read the file w1_slave. The output is many information, and the temperature in Celsius. In my example the temperature is 21,625 °C (you must divide by 1000).
If you want to realize a simple program to read it, you can simple parse this output searching the string “t=”.

In the next post we write some script for automate all things (in php7 and bash)

[adinserter name=”InArticle3″]

Please follow and like us:

Leave a Reply

Your email address will not be published. Required fields are marked *

Fork me on GitHub