From ArduinoInfo
Jump to navigation Jump to search

Multiple Temperature Sensors To 2x16 OR 4x20 LCD

The example sketch below shows how to read up to 4 DS18B20 temperature sensors on the same cable and display the results on a 2-line 16 character OR 4-line 20 character-per-line LCD Display with the I2C 'Backpack'. Bill Perry's LCD library makes that magic work... See THIS PAGE for more LCD info.

NOTE! THIS sketch uses pin 10 to connect to the DS18B20 signal wire!!

You MUST find the internal serial numbers of the DS18B20's first and enter them into the sketch. You can start with one sensor.. Scroll down to " Test Sketch to read DS18B20 addresses:" on this page: arduino-info/Brick-Temperature-DS18B20 Use that sketch uploaded to your Arduino to find your sensors addresses. You could cut/paste with two windows of Arduino IDE to put the addresses into your copy of the sketch below.

The sketch will display each sensor's reading in degrees C and F. If there is no sensor on a channel, an unmatched address or a connection problem Sensor Error will be displayed.

NOTE: Read the comments in the sketch below to know where to connect things and where to get the needed libraries. (Library help here:)

EXAMPLE SKETCH (Copy and Paste into Arduino IDE window).

/* Example: Multiple DS18B20 Temperature Sensors
   Displayed on 2x16 OR 4x20 character LCD display 
   *  YD_DS18B20_2or4LINE_LCD.ino
   DS18B20 Pinout (Left to Right, pins down, flat side toward you)
  - Left   = Ground
  - Center = Signal (Pin 10):  (with 3.3K to 4.7K resistor to +5 or 3.3 )
  - Right  = +5 or +3.3 V
  DS18B20 Stainless Steel end with cable:  BLACK=GND RED=+5V  WHITEorYELLOW=Signal
   Questions? */

/*-----( Import needed libraries )-----*/
//NOTE:  INSTALL OneWire and DallasTemperature libraries from Arduino Library Manager  
// See:
#include <OneWire.h>
#include <DallasTemperature.h>

// LCD Libraries
#include <Wire.h>  // Wire (I2C) Library (Preinstalled on Arduino IDE) 
//NOTE:  INSTALL hd44780e library from Arduino Library Manager 
// Move original LiquidCrystal library elsewhere, install this in it's place
#include <hd44780.h>                       // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header

/*-----( Declare Constants and Pin Numbers )-----*/
// Sensor Data wire is plugged into port 10 on the Arduino (can be changed)
#define ONE_WIRE_BUS 10    // NOTE: No ";" on #define  

/*-----( Declare objects )-----*/
// Setup a oneWire instance to communicate with any OneWire devices 
// (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass address of our oneWire instance to Dallas Temperature. 
DallasTemperature sensors(&oneWire);

// Start the LCD display library
hd44780_I2Cexp lcd; // declare lcd object: auto locate & auto config expander chip

/*-----( Declare Variables )-----*/
// Assign the addresses of your 1-Wire temp sensors.
// See the tutorial on how to obtain these addresses:

DeviceAddress Probe01 = { 0x28, 0xFF, 0xA6, 0x0A, 0x94, 0x16, 0x04, 0x6D }; //
DeviceAddress Probe02 = { 0x28, 0xE1, 0xC7, 0x40, 0x04, 0x00, 0x00, 0x0D }; // "5"
DeviceAddress Probe03 = { 0x28, 0x9A, 0x80, 0x40, 0x04, 0x00, 0x00, 0xD5 }; // "4" Again for test
DeviceAddress Probe04 = { 0x28, 0x10, 0xA4, 0x57, 0x04, 0x00, 0x00, 0xA9 };

float tempC;  // Temporary Results
float tempF;

void setup()   /****** SETUP: RUNS ONCE ******/
//------- Initialize the Temperature measurement library--------------
  // set the resolution to 10 bit (Can be 9 to 12 bits .. lower is faster)
  sensors.setResolution(Probe01, 10);
  sensors.setResolution(Probe02, 10);
  sensors.setResolution(Probe03, 10);
  sensors.setResolution(Probe04, 10);

//---------------- Initialize the lcd ------------------  
  lcd.begin (20,4);  // 20 characters, 4 lines

}//--(end setup )---

void loop()   /****** LOOP: RUNS CONSTANTLY ******/
  sensors.requestTemperatures(); // Send the command to get temperatures

  lcd.clear();  // Reset the display  
  lcd.backlight();  //Backlight ON if under program control  

// Print our characters on the LCD
// NOTE: Line number and character number start at 0 not 1

  lcd.setCursor(0,0); //Start at character 0 on line 0
  lcd.print("1: ");

  lcd.setCursor(0,1); //Start at character 0 on line 1
  lcd.print("2: ");

  lcd.setCursor(0,2); //Start at character 0 on line 2
  lcd.print("3: ");

  lcd.setCursor(0,3); //Start at character 0 on line 3
  lcd.print("4: ");


}//--(end main loop )---

/*-----( Declare User-written Functions )-----*/
void displayTemperature(DeviceAddress deviceAddress)

tempC = sensors.getTempC(deviceAddress);
tempF = sensors.getTempF(deviceAddress);

   if (tempC == -127.00) // Measurement failed or no device found
    lcd.print("Sensor Error");
   lcd.print(" F=");
}// End printTemperature

//*********( THE END )***********