Main Site Documentation

DS18S20 Working code example


In case anyone else has some DS18S20 sensors (instead of the newer DS18B20), here is some working code. I used many of the resources in the forum and code examples.

You can use the debug statements to find out the ROM code of your sensors.

I have this working in both the parasite and powered modes. Be careful, some of the sensors seem to only work in parasite mode. If you are having trouble working in parasite mode, try a different resistor value. The datasheet says to use a 4.7K but I have found that lower values seem to work when the 4.7k will not.

I am using a specific wait time after requesting the temperature conversion (1 sec). The datasheet says the maximum conversion time is 750 ms so you might be able to shorten this. This method is different than most of the examples which use a “while” or “if” statement waiting on the sensor to send a 1 to indicate the conversion is finished. My method will work in parasite and powered mode whereas the examples will only work in powered mode according to the datasheet.

I have also included code to increase the resolution of the reading. You can find this information at the bottom of page 3 in the datasheet.

One last thing, the examples I found here did not take into account the sensor being in a sub-freezing environment. I have included code to check for this and give the correct temperature.

using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;

using GHIElectronics.NETMF.Hardware;
using GHIElectronics.NETMF.FEZ;

namespace SimpeOneWire
    public class Program
        public static OneWire ow = new OneWire((Cpu.Pin)FEZ_Pin.Digital.Di6);
        public static void Main()
            int temp;
            int sign;
            float tempc;
            float tempf;
            byte[] romid = new byte[8];
            byte[] readall = new byte[9];

            while (ow.Reset())
                ow.Read(romid, 0, 8);
                for (int x = 0; x < 8; x++)
                ow.Write(romid, 0, 8);
                ow.WriteByte(0x44); // Start temperature conversion
                Thread.Sleep(1000); //wait for conversion to finish
                ow.Write(romid, 0, 8);
                ow.WriteByte(0xBE); // Read Scratchpad
                ow.Read(readall, 0, 9);               
                for (int x = 0; x < 9; x++)
                temp = readall[0];    // LSB
                temp |= (ushort)(readall[1] << 8);   // MSB
                //determine if the reading is < 0
                sign = temp & 0x8000;
                if (sign != 0)
                { // value is < 0
                    temp = (temp ^ 0xFFFF) + 1;
                    tempc = (float)(((temp >> 1) - 0.25 + ((16.00 - readall[6]) / 16.00)) * -1.00); //See datasheet - way to increase resolution
                    tempc = (float)((temp >> 1) - 0.25 + ((16.00 - readall[6]) / 16.00)); //See datasheet - way to increase resolution
                tempf = (float)((1.80 * tempc) + 32.00);
                Debug.Print("Temperature in C: " + tempc.ToString("F2"));
                Debug.Print("Temperature in F: " + tempf.ToString("F2"));



This will be lost over time. Please share on


Good point. I have created a code page: “DS18S20 Working Code”