Skip to content

Instantly share code, notes, and snippets.

@brooksware2000
Created September 5, 2012 00:46
Show Gist options
  • Save brooksware2000/3628594 to your computer and use it in GitHub Desktop.
Save brooksware2000/3628594 to your computer and use it in GitHub Desktop.
Example sketch for DS18B20 temperature sensors using the DallasTemperature library. Prints results to an I2C LCD.
/*
Demonstration sketch for DS18B20 temperature sensors using the DallasTemperature library.
Reads temperature (Celsius) and temperature (Fahrenheit). Displays results to I2C LCD.
*/
#include <LCD.h> // I2C LCD
#include <Wire.h> // I2C
#include <OneWire.h> // One-wire devices
#include <DallasTemperature.h> // DS18B20
#define DS18B20_BUS A0 // Dallas DS18B20 temperature sensors
const char degree = 223; // Degree symbol
/*-----------------------------------------------------------------------------------------------
* Create object references
* ----------------------------------------------------------------------------------------------*/
// create LCD object
LCD lcd;
// Setup One-wire instance to communicate with DS18B20 sensors
OneWire oneWire(DS18B20_BUS);
// Pass oneWire reference to DallasTemperature library
DallasTemperature temperatureSensors(&oneWire);
// Create an array for DS18B20 sensor addresses
DeviceAddress DS18B20[3];
/*-----------------------------------------------------------------------------------------------
* Function: INIT_DS18B20
* Description: Initialize DS18B20 Temperature sensor(s)
* Ins: Integer value for sensor precision. Can be 9, 10, 11 or 12 bits
* Outs: none
* ----------------------------------------------------------------------------------------------*/
void INIT_DS18B20(int precision)
{
temperatureSensors.begin();
int available = temperatureSensors.getDeviceCount();
for(int x = 0; x!= available; x++)
{
if(temperatureSensors.getAddress(DS18B20[x], x))
{
temperatureSensors.setResolution(DS18B20[x], precision);
}
}
}
/*-----------------------------------------------------------------------------------------------
* Function: DS18B20_CELSIUS
* Description: Get celsius reading from DS18B20 Temperature sensor
* Ins: Integer value for sensor address
* Outs: Returns celsius reading
* ----------------------------------------------------------------------------------------------*/
int DS18B20_CELSIUS(int address)
{
if (temperatureSensors.getAddress(DS18B20[address], address))
{
temperatureSensors.requestTemperatures();
return temperatureSensors.getTempC(DS18B20[address]);
}
else
return 0;
}
/*-----------------------------------------------------------------------------------------------
* Function: DS18B20_FAHRENHEIT
* Description: Get fahrenheit reading from DS18B20 Temperature sensor
* Ins: Integer value for sensor address
* Outs: Returns fahrenheit reading
* ----------------------------------------------------------------------------------------------*/
int DS18B20_FAHRENHEIT(int address)
{
if (temperatureSensors.getAddress(DS18B20[address], address))
{
temperatureSensors.requestTemperatures();
return DallasTemperature::toFahrenheit(temperatureSensors.getTempC(DS18B20[address]));
}
else
return 0;
}
/*-----------------------------------------------------------------------------------------------
* Main routines
* ----------------------------------------------------------------------------------------------*/
void setup()
{
// Initialize DS18B20 temperature sensors with precision set to 9
INIT_DS18B20(9);
// Initialize LCD to 20 characters by 4 lines
lcd.begin(20, 4);
lcd.clear();
lcd.print("--DS18B20 Demo--");
lcd.setCursor(0, 1);
lcd.print("Temp C: ");
lcd.setCursor(0, 2);
lcd.print("Temp F: ");
}
void loop()
{
float temp_c;
float temp_f;
// Read values from the sensor at address 0
temp_c = DS18B20_CELSIUS(0);
temp_f = DS18B20_FAHRENHEIT(0);
// Print results to LCD
lcd.setCursor(10, 1);
lcd.print(temp_c);
lcd.print(degree);
lcd.print("C");
lcd.setCursor(10, 2);
lcd.print(temp_f);
lcd.print(degree);
lcd.print("F");
delay(1000);
}
@jessey63
Copy link

Can anyone tell me where it says in the code or anywhere as to what pin to wire the 18B20 sensor?

@jessey63
Copy link

is this line of code below saying the sensor is connected to pin 9?
// Initialize DS18B20 temperature sensors with precision set to 9
does anyone monitor this page?

@gmkyte
Copy link

gmkyte commented Oct 1, 2015

Hello,

The 1-Wire bus line is connected to Pin A0 and is defined in the code in this line:

define DS18B20_BUS A0 // Dallas DS18B20 temperature sensors

Hope this helps.

@sondauta2
Copy link

How is posible, add more DS18B20 sensors? I need add 2 sensors, but no more pins availables. I use a Wemos D1 R2 card.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment