Using 1-wire Dallas DS18B20 thermal sensor

This article illustrates how to wire a Dallas DS18B20 thermal sensor to the FOX Board G20 and how to read the temperatures using the default 1-wire kernel driver.

The DS18B20 digital thermometer provides 9-bit to 12-bit Celsius temperature measurements. It 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” (read more...)

1-Wire® is a registered trademark of Dallas Semiconductor Corp. for a device communications bus system designed by Dallas Semiconductor that provides low-speed data, signaling and power over a single signal, albeit using two wires, one for ground, one for power and data” (read more...).

WARNING To try this tutorial you have to update your Debian Linux kernel image: How to update the kernel image.

Wirings

This is the basic scheme to wire a DS18B20 thermal sensor to the FOX Board G20. The resistor used to pull-up the 1-wire bus is a 4,7 KOhm.

DS18B20 thermal sensor wiring

It is possible to link more than one sensor. I tried up to five. The driver will acknoledge automatically the sensors linked.

Multi DS18B20 thermal sensor wiring

How to read the temperature

The 1-wire driver automatically scans each 10 seconds if new sensors are plugged on the 1-wire bus.

For each 1-wire device detected a new directory is created on /sys/bus/w1/devices/w1 bus master.

Type:

debarm:~# cd "/sys/bus/w1/devices/w1 bus master"
debarm:/sys/bus/w1/devices/w1 bus master# ls
28-0000028f6667  w1_master_add              w1_master_remove
28-0000028fa89c  w1_master_attempts         w1_master_search
driver           w1_master_max_slave_count  w1_master_slave_count
power            w1_master_name             w1_master_slaves
subsystem        w1_master_pointer          w1_master_timeout
uevent           w1_master_pullup

The two directory 28-…“ indicates the two DS18B20 thermal sensor are wired to the bus (28 is the family ID) and they unique ID are 0000028f6667 and 0000028fa89c.

The file w1_master_slaves contains an uodated list:

debarm:/sys/bus/w1/devices/w1 bus master# ls
debarm:/sys/bus/w1/devices/w1 bus master# cat w1_master_slaves
28-0000028fa89c
28-0000028f6667

To read the temperature for each sensor type:

debarm:/sys/bus/w1/devices/w1 bus master# cat 28-0000028f6667/w1_slave
49 01 4b 46 7f ff 07 10 f6 : crc=f6 YES
49 01 4b 46 7f ff 07 10 f6 t=20562

t=20562 indicates that the temperature read is 20.562 °C

How to read the temperature in Python

These simple programs in Python scan the 1-wire bus to detect the thermal sensors available:

File: http://foxg20.acmesystems.it/download/examples/scan1w.py -

#!/usr/bin/python
import time
import fox
 
print "Scan for the available thermal sensors"
 
for device in fox.w1buslist():
	print "Sensor ID = " + device
 
 
 

To download it type:

debarm:/# wget http://foxg20.acmesystems.it/download/examples/fox.py
debarm:/# wget http://foxg20.acmesystems.it/download/examples/scan1w.py
debarm:/# chmod +x scan1w.py

Then launch it typing:

debarm:/# ./scan1w.py
Scan for the available thermal sensors
Sensor ID = 0000028fa89c
Sensor ID = 0000028f6667

This other example read the temperature from a specific sensor.

File: http://foxg20.acmesystems.it/download/examples/tread.py -

#!/usr/bin/python
import time
import fox
 
indoor = fox.DS18B20("0000028fa89c")
outdoor = fox.DS18B20("0000028f6667")
 
print "Indoor temp=%.2f C" % (indoor.getTemp())
print "Outdoor temp=%.2f C" % (outdoor.getTemp())
 
 

To download it type:

debarm:/# wget http://foxg20.acmesystems.it/download/examples/tread.py
debarm:/# chmod +x tread.py

Change the sensor ID in the source and try it:

debarm:/# ./tread.py
Indoor temp=20.38 C
Outdoor temp=20.44 C

Related links


1-wire Dallas DS18B20 thermal sensor
The DS18B20 digital thermometer provides 9-bit to 12-bit Celsius temperature measurements.

A 4.7 KOhm is provided with this sensor.

 
tutorial/1wire.txt · Last modified: 2010/04/21 10:25 by tanzox
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki