1 Wire with DS18S20 digital thermometer

Created 02/10/2015 | Using 1 Wire with DS18S20 digital thermometer

In this tutorial we will use the 1 Wire Pi Zero or 1 Wire Pi Plus with a maxim integrated DS18S20 digital thermometer. For this you will need your Raspberry Pi, an 1 Wire Pi Zero or 1 Wire Pi Plus, and a maxim integrated DS18S20 digital thermometer.

You will need to enable i2c on your Raspberry Pi, see our other tutorial on i2c: I2C, SMBus and Raspbian Linux

Next install the OWFS (one wire file system) on your Raspberry Pi, see our other tutorial on owfs: OWFS with I2C support on Raspberry Pi

You need to reboot the Raspberry Pi once OWFS in installed before you can use the one wire file system.

Now all of the modules are installed and the Raspberry Pi is configured to use i2c we can begin building our project.

Parts Used:

1 Wire Pi Zero or 1 Wire Pi Plus

Maxim integrated DS18S20 digital thermometer

Connecting wire

Connecting the Sensor to the 1 Wire Pi Plus

Click to enlargeIf you haven’t done so install your 1 Wire Pi Plus onto the Raspberry Pi by connecting it onto the GPIO header.  Make sure your Raspberry Pi is turned off when you do this to minimise the risk of damaging the Raspberry Pi or the 1 Wire Pi Plus.

Next connect the DS18S20 sensor to the 1 Wire Pi Plus. Pin 1 connects to GND, Pin 2 to OW on the 1 Wire Pi, Pin 3 connects to 5V.

For this tutorial we will start by creating a shell script to read the temperature from the sensor. You can use your favourite text editor for writing the program.

We need to create the mount point for the 1wire device with the following commands:

mkdir /mnt/1wire
/opt/owfs/bin/owfs --i2c=ALL:ALL --allow_other /mnt/1wire/

Next we need to list all connected 1wire devices:

cd /mnt/1wire/
ls -la

This will return a list of all the 1wire devices detected on the 1wire bus.

The DS18S20 sensors will appear in the list of devices with a number starting with 28. such as 28.0844C4030000 as shown below:

Terminal Screen

Using the sensors ID number we can now query the sensor to obtain the temperature. You must replace the 28.xxxxx number with the ID of your connected sensor.

cat /mnt/1wire/28.0844C4030000/temperature

Next we need to kill the owfs service and unmount the 1wire mountpoint

killall owfs
umount /mnt/1wire

We also need to add sleep commands to pause the script to allow the OWFS file system to mount and unmount as the script runs.

Your script should now look like this.

mkdir /mnt/1wire
/opt/owfs/bin/owfs --i2c=ALL:ALL --allow_other /mnt/1wire/
sleep 1
cat /mnt/1wire/28.5B7DC4030000/temperature
sleep 1
killall owfs
umount /mnt/1wire

Save your program as "read1wire.sh" and change the permissions on the script:

sudo chmod +x read1wire.sh

Run it in a command terminal as root using

sudo ./read1wire.sh

You will now have a temperature reading from your sensor on the console.

Shop

Support

Click to contact us.
X

Have any Questions?

Send us any questions about this page or product and we will reply within 24 hours.

Submit your Message

X

Have any Questions?

Thank you for contacting us.

We will respond to your enquiry in the next 24 hours.

You can find answers to many questions and additional product support in our Knowledge Base and the Community Forum.