Giter Site home page Giter Site logo

dzsoni / nonblockingdallas Goto Github PK

View Code? Open in Web Editor NEW

This project forked from gbertaz/nonblockingdallas

0.0 0.0 0.0 72 KB

Arduino library for the DS18B20 temperature sensor. Avoid blocking the sketch while reading the sensor.

License: MIT License

C++ 99.25% C 0.75%

nonblockingdallas's Introduction

arduino-library-badge

Non blocking temperature sensor library for Arduino

This simple library for Arduino implements a machine state for reading the Maxim Integrated DS18B20 temperature sensor without blocking the main loop() of the sketch. It is designed for a continuos sensor reading every amount of time configurable by the developer. It is also possible to request a new sensor reading on the fly by calling the requestTemperature() function.

While the conversion is in progress, the main loop() continues to run so that the sketch can execute other tasks. When the temperature reading is ready, a callback is invoked. At full resolution the conversion time takes up to 750 milliseconds, a huge amount of time, thus the importance of the library to avoid blocking the sketch execution.

Features

Supports up to 15 sensors on the same ONE WIRE bus. To get some debug information, simply remove the comment on following line in NonBlockingDallas.h:

#define DEBUG_DS18B20

The output will be like the following:

DS18B20: 3 sensors found on the bus
DS18B20: parasite power is OFF
DS18B20: requested new reading
DS18B20 (0): 29.37 °C
DS18B20 (1): 29.12 °C
DS18B20 (2): 29.26 °C
DS18B20: requested new reading
DS18B20 (0): 29.37 °C
DS18B20 (1): 29.12 °C
DS18B20 (2): 29.26 °C
...

Callbacks

The library is callback driven: onIntervalElapsed is invoked every time the timer interval is elapsed, onTemperatureChange is invoked only when the temperature value changes between two sensor readings. Both of them provide the temperature, a bool value which indicates wether the readout is valid and the sensor index from 0 to 14.

void onIntervalElapsed(void(*callback)(float temperature, bool valid, int deviceIndex)) {
	cb_onIntervalElapsed = callback;
}

void onTemperatureChange(void(*callback)(float temperature, bool valid, int deviceIndex)) {
	cb_onTemperatureChange = callback;
}

Sensor Resolution

The conversion time of the DS18B20 temperature sensor depends on its resolution, thus the time interval parameter passed to the begin function must be greater than or equal to the conversion time.

Resolution Conversion time
9 bit 93 ms
10 bit 187 ms
11 bit 375 ms
12 bit 750 ms

Prerequisites

This library uses OneWire and DallasTemperature libraries, so you will need to have those installed.

Installation

The library is available from the Arduino Library Manager: load the Arduino IDE, then use the menu at the top to select Sketch -> Include Library -> Manage Libraries. Type NonBlockingDallas in the search box.

Click the following badge for a complete installation guide

arduino-library-badge

Usage

Step 1

Include the required libraries:

#include <OneWire.h>
#include <DallasTemperature.h>
#include <NonBlockingDallas.h>

Step 2

Create the instance of the classes:

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature dallasTemp(&oneWire);
NonBlockingDallas sensorDs18b20(&dallasTemp);

Step 3

Initialize the sensor and set the callbacks. The parameters of the begin function are the sensor resolution, unit of measure (Celsius or Fahrenheit) and time interval in milliseconds.

sensorDs18b20.begin(NonBlockingDallas::resolution_12, NonBlockingDallas::unit_C, 1500);

Possible values are:

  • resolution_9

  • resolution_10

  • resolution_11

  • resolution_12

  • unit_C for degrees Celsius

  • unit_F for Fahrenheit

Step 4

Implement the callbacks' functions and call the update function inside the main loop()


void loop() {
 sensorDs18b20.update();
}

void handleIntervalElapsed(float temperature, bool valid, int deviceIndex){

}

void handleTemperatureChange(float temperature, bool valid, int deviceIndex){

}

Please see the Example for a complete working sketch

nonblockingdallas's People

Contributors

gbertaz avatar dzsoni avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.