[View]  [Edit]  [Lock]  [References]  [Attachments]  [History]  [Home]  [Changes]  [Search]  [Help] 

[pi8] Using DS18B20 Temperature Sensor

Please take a look at GPIO(General Purpose Input/Output ports).
See also GPIO pinout to explore different pin interfaces.

Requirements

In addition to your Raspberry Pi running Raspbian & S8, what you will need is:
BreadBoardUploaded Image: breadboard.png
A DS18B20 Temperature Sensor x1Uploaded Image: ds18b20.jpg
4.7KOhms resistor x1Uploaded Image: 4.7k-resistor.jpg
Male-Female jumper wiresUploaded Image: jWires.jpg

J8 Expansion Header

Check the GPIO pins layout on your pi device.

Making connections

Please take a look at the wire color -and pins- for DS18B20 Temperature Sensor
In order to try a minimal sample we need to do the connections as follow:
Uploaded Image: ds18b20Connetions.png



First try with DS18B20

  1. System setup.
    1. Enable Raspberry Pi to recognize the sensor. Enter the following at the command prompt
      $ sudo nano /boot/config.txt
      
      open the /boot/config.txt file for editing. Then scroll down to the bottom of the file, and add the line:
      dtoverlay=w1-gpio

    2. Ctrl-X to save the amended file.
    3. Finally reboot the Raspberry Pi so that the changes take effect.
  2. Detecting device
    1. Now at the command prompt enter
      $ ls -l /sys/bus/w1/devices/
      
      to see a list of the devices currently connected to your Raspberry Pi. Your temperature sensor will appear with an address in the format 28-xxxxxxxxxxx. Below you can see that our temperature sensor address was 28-011617c6c3ee. Each DS18B20 temperature sensor has a unique hard-coded address so that you can connect up multiple temperature sensors when required and still read them individually.
      Uploaded Image: screen1.png
  3. Reading a sample value
    1. type in the following at the command prompt to open the sensor file and view the temperature reading
      $ cat /sys/bus/w1/devices/28-011617c6c3ee/w1_slave
      

      Uploaded Image: screen2.png
      Above we can see that the output from this command is two lines of data. At the end of the second line after t= is a numerical value. This value is 1000 times the temperature of the sensor in degrees Celcius. Therefore in our example where we see t=26125, the temperature readings was 26125/1000=26.125 degrees Celcius.

Using DS18B20 sensor with S8

  1. Start your S8 system server executing
    $ sudo node n8.snapshot.js
    take note of the server address e.g. SystemServer running at http://192.168.1.???:8088
  2. connect from U8 tools image to S8 system running in your Raspberry Pi.
  3. Finally copy this few lines to your remote workspace. Just select/paint all this code and click on #doIt button
    | delay count max readInput options temperature |
    delay := 100.
    count := 0.
    max   := 15.
    options := ((#encoding -> #utf8),(#flag -> #r)).
    
    temperature := [:data|
    	| parts temp |
    	data isEmpty ifTrue:[ data ] ifFalse: [
    		parts := data asArrayOfSubstringsSeparatedBy: String lf.
    		parts := (parts at:2) asArrayOfSubstringsSeparatedBy: $=.
    		temp := ((parts at:2) asNumber) / 1000.
    		temp.
    	]
    ].
    
    readInput := [	
    		NodeJS fs 
    			readFile: ('/sys/bus/w1/devices/28-011617c6c3ee/w1_slave') 
    			options: options 
    			with: [:error :data|
    				(count > max) ifFalse: [
    					count := count + 1.
    					error isNil ifFalse: [ ^self error: 'Error reading file: ' ,error ].				
    					self print:  (temperature value: data) asString. 
    					readInput valueDeferred: delay.
    				] ifTrue: [self print: 'End test'].
    			]	
    ]. 
    readInput value.
    What you'll see on standard output (console) are 15 values from DS18B20.
    Uploaded Image: screen3.png

Congratulations!!



References