nanoFramework.Iot.Device.Ds18b20 1.0.803

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package nanoFramework.Iot.Device.Ds18b20 --version 1.0.803
                    
NuGet\Install-Package nanoFramework.Iot.Device.Ds18b20 -Version 1.0.803
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="nanoFramework.Iot.Device.Ds18b20" Version="1.0.803" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="nanoFramework.Iot.Device.Ds18b20" Version="1.0.803" />
                    
Directory.Packages.props
<PackageReference Include="nanoFramework.Iot.Device.Ds18b20" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add nanoFramework.Iot.Device.Ds18b20 --version 1.0.803
                    
#r "nuget: nanoFramework.Iot.Device.Ds18b20, 1.0.803"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package nanoFramework.Iot.Device.Ds18b20@1.0.803
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=nanoFramework.Iot.Device.Ds18b20&version=1.0.803
                    
Install as a Cake Addin
#tool nuget:?package=nanoFramework.Iot.Device.Ds18b20&version=1.0.803
                    
Install as a Cake Tool

Ds18b20 - Temperature Sensor

The DS18B20 digital thermometer provides 9-bit to 12-bit Celsius temperature measurements and has an alarm function with nonvolatile user-programmable upper and lower trigger points.

Documentation

Product datasheet can be found here

Sensor Image

sensor

Circuit

circuit

  • DATA - RX2 + TX2 shorted
  • VCC - 3.3V
  • GND - GND

Important: make sure you've connected data pin of sensor to two pins on the board!

Usage

Important: make sure you properly setup the Rx/Tx pins especially for ESP32 before creating the OneWireHost, make sure you install the nanoFramework.Hardware.ESP32 nuget:

Configuration.SetPinFunction(16, DeviceFunction.COM3_RX);
Configuration.SetPinFunction(17, DeviceFunction.COM3_TX);

Library supports three approaches to read the data from Ds18b20 sensors:

  1. Reading from first found sensor on OneWire bus or sensor with address passed as second parameter to the constructor:
 private static void ReadingFromOneSensor()
 {
     OneWireHost oneWire = new OneWireHost();


     Ds18b20 ds18b20 = new Ds18b20(oneWire, null, false, TemperatureResolution.VeryHigh);

     ds18b20.IsAlarmSearchCommandEnabled = false;
     if (ds18b20.Initialize())
     {
         Console.WriteLine($"Is sensor parasite powered?:{ds18b20.IsParasitePowered}");
         string devAddrStr = "";
         foreach (var addrByte in ds18b20.Address)
         {
             devAddrStr += addrByte.ToString("X2");
         }

         Console.WriteLine($"Sensor address:{devAddrStr}");

         while (true)
         {
             if (!ds18b20.TryReadTemperature(out var currentTemperature))
             {
                 Console.WriteLine("Can't read!");
             }
             else
             {
                 Console.WriteLine($"Temperature: {currentTemperature.DegreesCelsius.ToString("F")}\u00B0C");
             }

             Thread.Sleep(5000);
         }
     }


     oneWire.Dispose();
 }
  1. Notification mode from first found sensor on OneWire bus or sensor with address passed as second parameter to the constructor:
private static void NotificationWhenValueHasChanged()
{
    OneWireHost oneWire = new OneWireHost();

    Ds18b20 ds18b20 = new Ds18b20(oneWire, null, false, TemperatureResolution.VeryHigh);

    if (ds18b20.Initialize())
    {
        ds18b20.SensorValueChanged += (currentTemperature) =>
        {
            Console.WriteLine($"Temperature: {currentTemperature.DegreesCelsius.ToString("F")}\u00B0C");
        };
        ds18b20.BeginTrackChanges(TimeSpan.FromMilliseconds(2000));
        // do whatever you want or sleep
        Thread.Sleep(60000);
        ds18b20.EndTrackChanges();
    }

    oneWire.Dispose();
}
  1. Using alarms - alarm is a mode when sensor has value either higher or lower than defined tresholds.
private static void UsingAlarms()
{
    using OneWireHost oneWire = new OneWireHost();

    Ds18b20 ds18b20 = new Ds18b20(oneWire, null, false, TemperatureResolution.VeryHigh);

    if (ds18b20.Initialize())
    {
        for (int i = 0; i < ds18b20.AddressNet.Length; i++)
        {
            string devAddrStr = "";
            ds18b20.Address = ds18b20.AddressNet[i];

            foreach (var addrByte in ds18b20.AddressNet[i])
            {
                devAddrStr += addrByte.ToString("X2");
            }

            Console.WriteLine("18b20-" + i.ToString("X2") + " " + devAddrStr);

            ds18b20.ConfigurationRead(false);
            Console.WriteLine("Alarm set-points before changes:");
            Console.WriteLine("Hi alarm = " + ds18b20.TemperatureHighAlarm.DegreesCelsius + " C");
            Console.WriteLine("Lo alarm = " + ds18b20.TemperatureLowAlarm.DegreesCelsius + " C");
            SetAlarmSetting();
        }
        alarmSearch();
    }
    else
    {
        Console.WriteLine("No devices found.");
    }

    oneWire.Dispose();

    void alarmSearch()
    {
        int loopRead = 1000;
        ds18b20.IsAlarmSearchCommandEnabled = true;

        while (loopRead > 0)
        {
            Console.WriteLine("LoopRead " + loopRead);

            if (ds18b20.SearchForAlarmCondition())
            {
                for (int index = 0; index < ds18b20.AddressNet.Length; index++)
                {
                    ds18b20.Address = ds18b20.AddressNet[index];
                    if (ds18b20.TryReadTemperature(out var currentTemperature))
                    {
                        break;
                    }

                    string devAddrStr = "";
                    foreach (var addrByte in ds18b20.AddressNet[index]) devAddrStr += addrByte.ToString("X2");
                    Console.WriteLine("DS18B20[" + devAddrStr + "] Sensor reading in One-Shot-mode; T = " + currentTemperature.DegreesCelsius.ToString("f2") + " C");

                    ds18b20.ConfigurationRead(false);
                    Console.WriteLine("Alarm set-points:");
                    Console.WriteLine("Hi alarm = " + ds18b20.TemperatureHighAlarm.DegreesCelsius + " C");
                    Console.WriteLine("Lo alarm = " + ds18b20.TemperatureLowAlarm.DegreesCelsius + " C");
                }
            }
            else
            {
                Console.WriteLine("***** No devices in alarm ****");
            }

            loopRead--;
        }

        Console.WriteLine("");
    }

    void SetAlarmSetting()
    {
        ds18b20.TemperatureHighAlarm = Temperature.FromDegreesCelsius(30);
        ds18b20.TemperatureLowAlarm = Temperature.FromDegreesCelsius(25);
        // Write configuration on ScratchPad.
        ds18b20.ConfigurationWrite(false);
        // Write configuration on EEPROM too.
        ds18b20.ConfigurationWrite(true);
        // Read configuration to check if changes were applied
        ds18b20.ConfigurationRead(true);
        Console.WriteLine("Alarm set-points after changes:");
        Console.WriteLine("Hi alarm = " + ds18b20.TemperatureHighAlarm.DegreesCelsius.ToString("F") + " C");
        Console.WriteLine("Lo alarm = " + ds18b20.TemperatureLowAlarm.DegreesCelsius.ToString("F") + " C");
    }
}

Check samples project for more usage examples.

Product Compatible and additional computed target framework versions.
.NET Framework net is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.840 175 7/28/2025
1.0.820 286 4/2/2025
1.0.815 218 4/2/2025
1.0.803 235 3/11/2025
1.0.797 228 3/10/2025
1.0.773 184 2/26/2025
1.0.726 200 2/4/2025
1.0.723 169 2/4/2025
1.0.706 175 1/31/2025
1.0.694 171 1/20/2025
1.0.669 181 12/30/2024
1.0.647 177 12/16/2024
1.0.624 193 10/23/2024
1.0.607 177 10/3/2024
1.0.590 181 9/6/2024
1.0.582 183 8/28/2024
1.0.564 213 8/9/2024
1.0.557 154 8/2/2024
1.0.552 163 7/26/2024
1.0.541 181 7/17/2024
1.0.524 190 6/19/2024
1.0.487 227 4/15/2024
1.0.465 206 3/22/2024
1.0.445 199 2/28/2024
1.0.425 224 1/24/2024
1.0.413 233 1/5/2024
1.0.409 218 12/20/2023
1.0.387 192 11/10/2023
1.0.367 170 11/8/2023
1.0.354 224 10/6/2023
1.0.347 202 9/27/2023
1.0.335 247 9/6/2023
1.0.329 248 8/16/2023
1.0.320 235 8/2/2023
1.0.314 248 7/28/2023
1.0.308 244 7/19/2023
1.0.305 228 7/14/2023
1.0.296 237 6/21/2023
1.0.292 225 6/14/2023
1.0.288 248 6/7/2023
1.0.286 262 6/2/2023
1.0.280 247 5/26/2023
1.0.253 277 5/10/2023
1.0.248 235 5/3/2023
1.0.224 377 3/17/2023
1.0.218 372 3/10/2023
1.0.214 379 3/8/2023
1.0.210 399 2/27/2023
1.0.207 359 2/24/2023
1.0.204 386 2/22/2023
1.0.173 433 1/9/2023
1.0.168 444 1/6/2023
1.0.159 429 1/3/2023
1.0.154 422 12/28/2022
1.0.110 518 11/14/2022
1.0.104 522 11/5/2022
1.0.38 636 9/15/2022
1.0.14 563 9/3/2022
1.0.3 578 8/17/2022
1.0.1 545 8/16/2022