Raspberry Pi blog
  • Home
  • Raspbian
    • Installation
    • Adjustments
  • Guides
    • DNS resolver
    • VNC server
    • Plotting graphs
    • Streaming with VLC
    • VOD with VLC
    • Stream announcements
    • Samba shares
    • NTP client and server
    • DynDNS client
    • No-IP client
    • Bluetooth
    • Internet via GPRS/EDGE/3G/HSPA
    • OpenSSH server
    • XMPP/Jabber server
    • CryptoCat messaging service
    • Converse.js XMPP/Jabber client
  • GPIO
    • Setup GPIO
    • LEDs and buttons
    • Thermal sensor (1Wire)
    • Thermal sensor (I2C)
    • UART to RS-232
  • Case
  • Arduino
  • Contact
  • Sitemap

Thermal sensor DS18B20 via 1Wire bus


RPi can be used to interact with real world. To interact it needs some input. This input can be temperature. Since it is quite simple to connect thermal sensor to RPi, I will show you how to do that. At first you will need:
  • DS18B20 (thermal sensor)
  • 4.7 kOhm resistor
  • piece of cable
DS18B20 in TO-92 package by Dallas
DS18B20 in TO-92 package by Dallas
Thermal sensor can be bought in any shop with electronics, but be aware of price. At my hometown, one store sells it for 14€/piece, second store sells it for <1€/piece :) DS18B20 is also quite accurate +/- 0.5°C. Temperature range is from -55 to 125°C. Other properties can be found in datasheet. Thermal sensor communicate via 1wire bus. And here is a little problem. RPi doesn`t have support for 1wire bus in hardware, bus is only "emulated" on GPIO. This results in fact that on other side of GPIO pin with connected thermal sensor is RPi`s Broadcom SoC. 1wire bus can be really long (with parasite power supply up to 500m). IT IS VERY DUMB to have 500m "antenna" leading directly to CPU! This way of connecting DS18B20 is only suitable and safe for distances up to 20cm. If you want to have thermal sensor further, follow this guide. Now, you can connect thermal sensor this way. Again, breadboard and soldering version of scheme.
Thermal sensor connected to GPIO
Thermal sensor connected to GPIO. Yellow cable can not be longer than 20cm! High risk of damage to Broadcom SoC
...and if you want to solder...
Thermal sensor connected to GPIO
Thermal sensor connected to GPIO. Yellow cable can not be longer than 20cm! High risk of damage to Broadcom SoC
Now when sensor is connected, we can setup RPi. At first, as I stated, 1Wire implementation is only software, you have to load kernel module.
modprobe w1-gpio
modprobe w1-therm
This modules are not loaded automatically at bootup. To do so, edit "/etc/modules" file:
nano /etc/modules
And add two previous modules
w1-gpio
w1-therm
Now close file (CTRL+X...) and save changes (...press "y" and Enter). Change directory to "/sys/but/w1/devices" and list it:
cd /sys/bus/w1/devices
ls
You should see folder named to something like "28-00000482b245". This number is unique for every thermal DS18B20, something like MAC on you network adapter. "cd" into this folder and "cat w1_slave" file:
cd 28-C2D5C4040000
cat w1_slave
You will get output similar to this:
71 01 4b 46 7f ff 0f 10 56 : crc=56 YES
71 01 4b 46 7f ff 0f 10 56 t=23062
Temperature on my sensor is 23.062°C right now. Output can be easily stripped via "tail" and "cut" commands:
cat w1_slave | tail -1 | cut -d'=' -f2
And this number can be easily used in scripts, programs, alerts...

Connecting more thermal sensors to 1Wire bus


On data link of 1wire bus can be connected large number of sensors simultaneously. Here is a guide how to connect second sensor to 1wire bus. Reading of values is simple, in "/sys/bus/w1/devices" will be created next folder named after sensor`s unique hardware address. And here is electrical scheme:
Two thermal sensor connected to GPIO
Two thermal sensor connected to GPIO. Yellow cable can not be longer than 20cm! High risk of damage to Broadcom SoC
And breadboard-free version...
Two thermal sensor connected to GPIO
Two thermal sensor connected to GPIO. Yellow cable can not be longer than 20cm! High risk of damage to Broadcom SoC
Powered by Create your own unique website with customizable templates.