Knowledge Base

The AB Electronics UK Knowledge Base provides support solutions, tutorials and troubleshooting guides.

Knowledge Base

1 Wire with DS18S20 digital thermometer

Using 1 Wire with DS18S20 digital thermometer
Created: 02/10/2015 | Last Updated: 03/05/2021

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 Part 2 - Enabling I²C on the Raspberry Pi

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:

sudo mkdir /mnt/1wire
sudo 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.

head /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
head /mnt/1wire/28.5B7DC4030000/temperature
sleep 1
killall owfs
umount /mnt/1wire

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

sudo chmod +x

Run it in a command terminal as root using

sudo ./

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

(images created with Fritzing)

Was this article helpful? Yes / No