Arduino library · Sensors

GyverDS18: an example that runs

Yes. You can use the GyverDS18 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, "Two DS18B20s on one pin, found and read by address", runs on the Arduino Uno R3.

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

What GyverDS18 does

DS18B20 thermometers by uint64_t address: GyverDS18Single for one a pin with tick(), GyverDS18 and GyverDS18Array for several, ready() and readTemp() on a timer, setResolution(), readAddress() and gds::Search to scan the bus. That is what GyverDS18, by AlexGyver, covers. The sketch below uses it on the Arduino Uno: “Two DS18B20s on one pin, found and read by address”.

The GyverDS18 library's interface (AlexGyver, 1.2.1), for DS18B20 thermometers on one pin, by address.

GyverDS18 sends requestTemp() and setResolution() to every sensor at once, or to one by its uint64_t address, and reads one sensor at a time into a shared buffer: readTemp(addr), then getTemp(). An addressed read of the power-up 85 C is an error. tick() here only requests, on the timer. gds::Search finds the addresses.

The calls follow release 1.2.1 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 nameGyverDS18
AuthorAlexGyver
Include#include <GyverDS18.h>
#include <GyverDS18Single.h>
#include <GyverDS18Array.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 <GyverDS18.h> and the sketch compiles, with no download, no zip and no Library Manager.

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

What to know in the simulator: Parasite power is on by default, as upstream, and the pin is driven high during a conversion. The part runs on VDD only, so readPower() says external.

The calls you will use

In the order a sketch meets them.

GyverDS18 ds(DS_PIN)

Makes ds, the GyverDS18 object the calls below are made on.

ds.setResolution(12)

Sets the resolution to the value you pass in.

ds.requestTemp()

Requests the temp and returns true or false.

ds.readTemp(addr[i])

Returns the temp as true or false.

ds.getTemp()

Returns the temp as a decimal number.

Example: Two DS18B20s on one pin, found and read by address

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

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

#define DS_PIN 2

GyverDS18 ds(DS_PIN);
uint64_t addr[2];
uint8_t found;

void setup() {
  Serial.begin(115200);

  // Scan the bus for addresses.
  found = gds::Search(DS_PIN).scan(addr, 2);
  Serial.print("found: ");
  Serial.println(found);
  for (uint8_t i = 0; i < found; i++) {
    Serial.print("address: ");
    gds::Addr(addr[i]).printTo(Serial);
  }

  ds.setResolution(12);  // all of them
  ds.requestTemp();      // the first conversion, for all of them
}

void loop() {
  if (ds.ready()) {
    for (uint8_t i = 0; i < found; i++) {
      if (ds.readTemp(addr[i])) {
        Serial.print("temp ");
        Serial.print(i);
        Serial.print(": ");
        Serial.println(ds.getTemp());
      } else {
        Serial.println("error");
      }
    }
    ds.requestTemp();
  }
}

Wiring

  • Arduino Uno R3 2 to Temperature sensor, DS18B20 DQ
  • Arduino Uno R3 2 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

Open it in the editor, wired and ready to run

Try GyverDS18 without installing anything

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