Arduino library · Sensors

microDS18B20: an example that runs

Yes. You can use the microDS18B20 library online in Mokxi with nothing to install, and the sketch runs in your browser against the simulated part (Temperature sensor, DS18B20). The example below, "DS18B20s by address and alone on a pin, the microDS18B20 way", runs on the Arduino Uno R3.

Built in on Mokxi: #include <microDS18B20.h> and it builds, with nothing to install. Free, and no account needed.

What microDS18B20 does

DS18B20 thermometers through the MicroDS18B20<pin> template, alone on a pin or by address: requestTemp(), readTemp(), getTemp(), getTempInt(), setResolution(), readAddress() and online(). That is what microDS18B20, by AlexGyver, covers. The sketch below uses it on the Arduino Uno: “DS18B20s by address and alone on a pin, the microDS18B20 way”.

The microDS18B20 library's interface (AlexGyver and Egor 'Nich1con' Zakharov, 3.10), for DS18B20 thermometers.

DS_CHECK_CRC set false before the include turns the CRC check off, as upstream.

The calls follow release 3.10.0 of the library, so a sketch written here builds on a real board too. The example is the one Mokxi's own tests build and run, so what is printed here is what runs.

Library Manager namemicroDS18B20
AuthorAlexGyver
Include#include <microDS18B20.h>
Boardsevery board Mokxi compiles for but ATtiny85
In the simulatorSimulated: it runs against the part the way it does on the bench.
Parts it drivesTemperature sensor, DS18B20

Install it

It builds on every board Mokxi compiles for but ATtiny85.

In Mokxi it is built in: #include <microDS18B20.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "microDS18B20" by AlexGyver from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add microDS18B20 to lib_deps in PlatformIO. Mokxi follows the calls of release 3.10.0, so a sketch written here builds there too.

The calls you will use

In the order a sketch meets them.

MicroDS18B20<2, addr1> sensor1

Makes sensor1, the MicroDS18B20 object the calls below are made on.

sensor3.readAddress(address)

Returns the address as true or false.

sensor3.setResolution(10)

Sets the resolution to the value you pass in.

sensor1.requestTemp()

Requests the temp.

sensor1.getTemp()

Returns the temp as a decimal number.

sensor2.readTemp()

Returns the temp as true or false.

Example: DS18B20s by address and alone on a pin, the microDS18B20 way

On the Arduino Uno R3. It runs in the browser exactly as printed.

sketch.ino · Arduino Uno R3
#include <microDS18B20.h>

// Two sensors on one pin, by their addresses.
uint8_t addr1[] = {0x28, 0xFF, 0x78, 0x5B, 0x50, 0x17, 0x04, 0xCF};
uint8_t addr2[] = {0x28, 0xFF, 0x7B, 0x5B, 0x50, 0x17, 0x04, 0x81};
MicroDS18B20<2, addr1> sensor1;
MicroDS18B20<2, addr2> sensor2;

// A third alone on its own pin: no address needed.
MicroDS18B20<3> sensor3;

void setup() {
  Serial.begin(9600);
  uint8_t address[8];
  if (sensor3.readAddress(address)) {
    Serial.print("sensor3: {");
    for (uint8_t i = 0; i < 8; i++) {
      Serial.print("0x");
      Serial.print(address[i], HEX);
      if (i < 7) Serial.print(", ");
    }
    Serial.println("}");
  } else {
    Serial.println("Not connected");
  }
  sensor3.setResolution(10);
}

void loop() {
  sensor1.requestTemp();
  sensor2.requestTemp();
  sensor3.requestTemp();
  delay(1000);

  Serial.print("t1: ");
  Serial.print(sensor1.getTemp());
  Serial.print(", t2: ");
  if (sensor2.readTemp()) Serial.print(sensor2.getTemp());
  else Serial.print("error");
  Serial.print(", t3: ");
  if (sensor3.readTemp()) Serial.println(sensor3.getTemp());
  else Serial.println("error");
}

Wiring

  • Arduino Uno R3 2 to Temperature sensor, DS18B20 DQ
  • Arduino Uno R3 2 to Temperature sensor, DS18B20 DQ
  • Arduino Uno R3 3 to Temperature sensor, DS18B20 DQ
  • Arduino Uno R3 5V to Temperature sensor, DS18B20 VDD
  • Arduino Uno R3 GND to Temperature sensor, DS18B20 GND
  • Arduino Uno R3 5V to Temperature sensor, DS18B20 VDD
  • Arduino Uno R3 GND to Temperature sensor, DS18B20 GND
  • Arduino Uno R3 5V to Temperature sensor, DS18B20 VDD
  • Arduino Uno R3 GND to Temperature sensor, DS18B20 GND

Open it in the editor, wired and ready to run

Try microDS18B20 without installing anything

Open the example, change the sketch, and keep your version in a free account.