Onewire with DS1820

I tried to connect with a DS1820

WireController owc = new OneWireController(GpioController.GetDefault(),SC20260.GpioPin.PA13 );
Thread.Sleep(100);
var devices = owc.FindAllDevices();
// The devices arraylist returns one element that contains the 64 bits code of the DS1820

byte[] sn = new byte[8];
owc.SerialNum(sn, true);
// using owc.SerialNum gets the same values

To read the data, If I am correct, need to reset the sensor, select it , send 0x44 to initiate the temp measurment, wait ,then reset, select, send 0xBE and read the scratchpad and

But I am not sure about the command. final read is always 255

            owc.TouchReset();
            owc.SerialNum(sn, false);   -> select sensor ?

            owc.TouchByte(0x44);  -> send command ?
            Thread.Sleep(1000);

            owc.TouchReset();
            owc.SerialNum(sn, false);

            owc.TouchByte(0xBE);
            Thread.Sleep(1000);

            byte[] data = new byte[8]; -> Read scratch pad
            for (byte d = 0; d < 9; d++)
            {
                data[d] = (byte) owc.ReadByte();
            }

It seems there are some problems with 1-wire in the current release. The following code will work, but it won’t be reliable. Sometimes it won’t find all the sensors or will return incorrect readings. I’m told this will be fixed in the next release (preview 3) which will be out soon.

This code is written for the SC20100 Dev Board with one or more DS18B20s connected to PA1. For each sensor found, it will print the temperature and the remaining seven bytes of the scratch pad.

This code uses the GHIElectronics.TinyCLR.Core, GHIElectronics.TinyCLR.Devices.Onewire, GHIElectronics.TinyCLR.Native, and GHIElectronics.TinyCLR.Pins NuGet packages.

class program {
    private static void Main() {
        var oneWireBus = new GHIElectronics.TinyCLR.Devices.Onewire.OneWireController(GHIElectronics.TinyCLR.Pins.SC20100.GpioPin.PA1);
        oneWireBus.TouchReset();

        var oneWireDevices = oneWireBus.FindAllDevices();
        System.Diagnostics.Debug.WriteLine("Number of sensors found = " + oneWireDevices.Count.ToString());

        foreach (byte[] serialNumber in oneWireDevices) {
            oneWireBus.TouchReset();
            oneWireBus.WriteByte(0x55); //Match ROM command.

            for (int i = 0; i < serialNumber.Length; i++) {
                oneWireBus.WriteByte(serialNumber[i]); //Send serial number of device.
            }

            oneWireBus.WriteByte(0x44); //Convert temperature.

            while (oneWireBus.ReadByte() == 0) { // Wait for conversion to finish.
            }

            oneWireBus.TouchReset();
            oneWireBus.WriteByte(0x55); //Match ROM command.

            for (int i = 0; i < serialNumber.Length; i++) {
                oneWireBus.WriteByte(serialNumber[i]); //Send serial number of device.
            }

            oneWireBus.WriteByte(0xBE); //Read scratchpad command.

            System.Diagnostics.Debug.WriteLine("Temperature: " + ((float)(oneWireBus.ReadByte() + (oneWireBus.ReadByte() << 8)) / 16.0).ToString());
            System.Diagnostics.Debug.WriteLine("Remaining 7 bytes of scratch pad:");

            for (int i = 0; i < 7; i++) {
                System.Diagnostics.Debug.WriteLine(oneWireBus.ReadByte().ToString());
            }

            System.Diagnostics.Debug.WriteLine("--------");
        }

        System.Threading.Thread.Sleep(-1);
    }
}

Sample output:

Number of sensors found = 3
Temperature: 21.125
Remaining 7 bytes of scratch pad:
75
70
127
255
14
16
255
--------
Temperature: 21
Remaining 7 bytes of scratch pad:
75
70
127
255
16
16
73
--------
Temperature: 21.1875
Remaining 7 bytes of scratch pad:
75
70
127
255
13
16
233
--------
1 Like

Thanks Joel.
It is clear now.
I confirm that FindAllDevices() is sometimes returning no devices (I have only one)

Last question, what is the purpose of the TouchByte() method ?

It looks like the TouchByte() method is used to send a one byte command to a 1-Wire device, and then receive a one byte response (the method returns an int). But to be honest, I have yet to use the TouchByte() method.

1 Like

Ok.
According to the datasheet, when a reset pulse is transmitted by the master, the DS1820 transmit a presence pulse(signal low). Maybe better to check the transmission instead of going directly to the next step.

Just tried with preview 3 .It works perfectly fine. All readings are correct :+1:

2 Likes