Přidat komentář

Enable one wiring for DS18B20 and DS18S20+ digital thermometer

What is DS18B20 ?

The DS18B20 digital thermometer provides 9-bit to 12-bit Celsius temperature measurements and has an alarm function with nonvolatile userprogrammable upper and lower trigger points. The DS18B20 communicates over a 1-Wire bus that by definition requires only one data line (and ground) for communication with a central microprocessor. It has an operating temperature range of -55°C to +125°C and is accurate to ±0.5°C over the range of -10°C to +85°C. In addition, the DS18B20 can derive power directly from the data line (“parasite power”), eliminating the need for an external power supply. Each DS18B20 has a unique 64-bit serial code, which allows multiple DS18B20s to function on the same 1-Wire bus. Thus, it is simple to use one microprocessor to control many DS18B20s distributed over a large area. Applications that can benefit from this feature include HVAC environmental controls, temperature monitoring systems inside buildings, equipment, or machinery, and process monitoring and control systems.

! The DS18B20 "1-wire" sensors can be connected in parallel. All sensors should share the same pins, but you only need one 4.7-10k pull-up resistor for all of them.

First what you need is enable support for 1-wire serial interface:

Before enable 1-wire support:

root@raspberrypi:~# ls -l /sys/devices/
total 0
drwxr-xr-x  2 root root 0 Sep 15 15:20 armv6_1176
drwxr-xr-x  2 root root 0 Sep 15 15:20 breakpoint
drwxr-xr-x 15 root root 0 Sep 15 14:17 platform
drwxr-xr-x  2 root root 0 Sep 15 15:20 software
drwxr-xr-x  6 root root 0 Sep 15 14:17 system
drwxr-xr-x  2 root root 0 Sep 15 15:20 tracepoint
drwxr-xr-x 21 root root 0 Jan  1  1970 virtual

Enable w1 support adding the following line to end of file /boot/config.txt:

root@raspberrypi:~# nano /boot/config.txt
dtoverlay=w1-gpio

Save file /boot/config.txt and reboot Raspberry Pi:

root@raspberrypi:~# reboot

After reboot check /sys/devices:

root@raspberrypi:~# ls -l /sys/devices/
total 0
drwxr-xr-x  2 root root 0 Sep 15 15:27 armv6_1176
drwxr-xr-x  2 root root 0 Sep 15 15:27 breakpoint
drwxr-xr-x 16 root root 0 Sep 15 15:25 platform
drwxr-xr-x  2 root root 0 Sep 15 15:27 software
drwxr-xr-x  6 root root 0 Sep 15 15:25 system
drwxr-xr-x  2 root root 0 Sep 15 15:27 tracepoint
drwxr-xr-x 21 root root 0 Sep 15 15:25 virtual
drwxr-xr-x  3 root root 0 Sep 15 15:26 w1_bus_master1  <--- here

Nice! Now is here new device: ./w1_bus_master1

Connect DS18B20 or DS18S20+ to Raspberry Pi as in figure (R1 = 4k7):

Connect DS18B20 to RPi

Now check if the temperature sensor is connected

For DS18B20 search 28-xxx dir in:

root@raspberrypi:~# ls -l /sys/devices/w1_bus_master1/
total 0
drwxr-xr-x 2 root root    0 Sep 15 16:11 28-021464bceaff  <--- DS18B20
-rw-rw-r-- 1 root root 4096 Sep 15 16:04 w1_master_add
-r--r--r-- 1 root root 4096 Sep 15 16:04 w1_master_attempts
-rw-rw-r-- 1 root root 4096 Sep 15 16:04 w1_master_max_slave_count
...
...

For DS18S20+ search 10-xxx dir in:

root@raspberrypi:~# ls -l /sys/devices/w1_bus_master1/
total 0
drwxr-xr-x 2 root root    0 Aug 11 09:52 10-000802dc02cd  <--- DS18S20+
-rw-rw-r-- 1 root root 4096 Sep 15 16:06 w1_master_add
-r--r--r-- 1 root root 4096 Sep 15 16:06 w1_master_attempts
-rw-rw-r-- 1 root root 4096 Sep 15 16:06 w1_master_max_slave_count
...
...

If you found it, cat temerature now:

root@raspberrypi:~# cat /sys/devices/w1_bus_master1/10-000802dc02cd/w1_slave 
3e 00 4b 46 ff ff 0a 10 fe : crc=fe YES
3e 00 4b 46 ff ff 0a 10 fe t=31125 <--- actual temperature is here

Ok. Temperature is now 31.125°C

Enjoy!

Useful links and sources:

What is pull-up/pull-down resistor ?

Python module for DS18B20 sensor (Kelvin,Celsius and Fahrenheit)

DS18B20 Datasheet

DS18S20+ Datasheet

Connect DS18B20 to RPi