Go Down

Topic: 2 x I2C LCD and 2 x Dsb18b20 Probes (Read 375 times) previous topic - next topic

J_Erbe

Hi all, I have managed to show one probe to both LCD's but am struggling with displaying one on each LCD, any help/ advice is much appreciated.

Code: [Select]
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

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

/*Define for DS18S20 data pins
*  This data pins is to connect 4.7K Om resister and to 5V
*/
#define ONE_WIRE_BUS D4

//Define for LCD display pins
#define SCL_CLOCK_PIN D1
#define SDA_DATA_PIN D2

//Initialize OneWire library for temperature sensor
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

// set the LCD address
LiquidCrystal_I2C lcd1(0x27,16,2); 
LiquidCrystal_I2C lcd2(0x3F,16,2);


float HighestTemp1 = 0.0;
float CurrentTemp1 = 0.0;
float LowestTemp1 = 0.0;
float HighestTemp2 = 0.0;
float CurrentTemp2 = 0.0;
float LowestTemp2 = 0.0;

// Set probe address
DeviceAddress Probe01 = { 0x28, 0xFF, 0x27, 0xC9, 0xC1, 0x17, 0x04, 0x50 };
DeviceAddress Probe02 = { 0x28, 0xFF, 0x41, 0x00, 0xC2, 0x17, 0x04, 0x93 };

void setup()
{
  // start temperature sensor
  sensors.begin();
  sensors.requestTemperatures(); //get temperature

 
 
  // Start the LCD
  lcd1.init();
  lcd2.init();
  //lcd display screen backlight
  lcd1.backlight(); 
  lcd2.backlight();
  //clear lcd display
  lcd1.clear();
  lcd1.home();
  lcd2.clear();
  lcd2.home();
 
  //Set lowest, highest temperatues
  CurrentTemp1  = sensors.getTempCByIndex(0);
  HighestTemp1 = CurrentTemp1;
  LowestTemp1 = CurrentTemp1;
  CurrentTemp2  = sensors.getTempCByIndex(0);
  HighestTemp2 = CurrentTemp2;
  LowestTemp2 = CurrentTemp2;

// LCD display information
  lcd1.setCursor(0,0);
  lcd1.print(F("Current: "));
  LCDshowNum(9,0,CurrentTemp1); 

  lcd1.setCursor(0,1);
  lcd1.print(F("Ma: "));
  LCDshowNum(3,1,HighestTemp1);   
     
  lcd1.setCursor(9,1);
  lcd1.print(F("Mi: "));
  LCDshowNum(12,1,LowestTemp1);
       
  lcd2.setCursor(0,0);
  lcd2.print(F("Current: "));
  LCDshowNum(9,0,CurrentTemp2); 

  lcd2.setCursor(0,1);
  lcd2.print(F("Ma: "));
  LCDshowNum(3,1,HighestTemp2);   
     
  lcd2.setCursor(9,1);
  lcd2.print(F("Mi: "));
  LCDshowNum(12,1,LowestTemp2);
}


void loop()
{
  sensors.requestTemperatures(); //get temperature

  //Get Current
  CurrentTemp1 = sensors.getTempCByIndex(0);
  LCDshowNum(9,0,CurrentTemp1);   
  CurrentTemp2 = sensors.getTempCByIndex(0);
  LCDshowNum(9,0,CurrentTemp1);   
 
  //Check if Hightest
  if(CurrentTemp1 > HighestTemp1)
     HighestTemp1 = CurrentTemp1;
     LCDshowNum(3,1,HighestTemp1);
  if(CurrentTemp2 > HighestTemp2)
     HighestTemp2 = CurrentTemp2;
     LCDshowNum(3,1,HighestTemp2);
     
  //Chekc if Lowest
  if(CurrentTemp1 < LowestTemp1)
    LowestTemp1 = CurrentTemp1;
    LCDshowNum(12,1,LowestTemp1);
 if(CurrentTemp2 < LowestTemp2)
    LowestTemp2 = CurrentTemp2;
    LCDshowNum(12,1,LowestTemp2);
 
 
  delay(1);
}

/*
* LCDshowNum
* LCD display location: x, y
* Float n: temperature to display
*/
void LCDshowNum(int x, int y, float n){
  lcd1.setCursor(x,y);
  lcd1.print(n);
  lcd2.setCursor(x,y);
  lcd2.print(n);
}

david_prentice

#1
Mar 08, 2018, 12:24 pm Last Edit: Mar 08, 2018, 12:55 pm by david_prentice
You can distinguish different LCDs by using the individual Slave address e.g. 0x27, 0x3F
You can distinguish different DS18B20 devices by their unique addresses e.g. Probe1, Probe2

Many OneWire projects just assume a single device i.e. ignore the unique 8-byte DeviceAddress

Googling for the DallasTemperature library:
Code: [Select]


 // sends command for one device to perform a temperature conversion by address
 bool requestTemperaturesByAddress(const uint8_t*);

 // sends command for one device to perform a temperature conversion by index
 bool requestTemperaturesByIndex(uint8_t);
        ...
 // Get temperature for device index (slow)
 float getTempCByIndex(uint8_t);




You would use these to find how many devices are on the bus:
Code: [Select]


 // returns the number of devices found on the bus
 uint8_t getDeviceCount(void);

 // returns the number of DS18xxx Family devices on bus
 uint8_t getDS18Count(void);


You can either select the DS18B20 by its index or by its actual ID.
Your program should:
Code: [Select]

  CurrentTemp2  = sensors.getTempCByIndex(1);    //if there are 2 sensors, index is 0 or 1


Untested.

David.

J_Erbe

Hello David

I knew I had to set the probes to their respective Index I was just unsure of how to ha ha.

I have re coded to suite and again it will either show one or the other on both LCD's, even if I have one as 0 and the other as 1, maybe there is something somewhere else I have gone wrong.

Code: [Select]
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

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

/*Define for DS18S20 data pins
*  This data pins is to connect 4.7K Ohm resister and to 5V
*/
#define ONE_WIRE_BUS D4

//Define for LCD display pins
#define SCL_CLOCK_PIN D1
#define SDA_DATA_PIN D2

//Initialize OneWire library for temperature sensor
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

// set the LCD address
LiquidCrystal_I2C lcd1(0x27,16,2); 
LiquidCrystal_I2C lcd2(0x3F,16,2);


float HighestTemp1 = 0.0;
float CurrentTemp1 = 0.0;
float LowestTemp1 = 0.0;
float HighestTemp2 = 0.0;
float CurrentTemp2 = 0.0;
float LowestTemp2 = 0.0;

// Set probe address
DeviceAddress Probe01 = { 0x28, 0xFF, 0x27, 0xC9, 0xC1, 0x17, 0x04, 0x50 };
DeviceAddress Probe02 = { 0x28, 0xFF, 0x41, 0x00, 0xC2, 0x17, 0x04, 0x93 };

 // returns the number of devices found on the bus
 uint8_t getDeviceCount(void);

 // returns the number of DS18xxx Family devices on bus
 uint8_t getDS18Count(void);
 
void setup()
{
    Serial.print("Locating devices...");
  Serial.print("Found ");
  Serial.print(sensors.getDeviceCount(), DEC);
  Serial.println(" devices.");
 
  // start temperature sensor
  sensors.begin();
  sensors.requestTemperatures(); //get temperature
sensors.setResolution(Probe01, 10);
sensors.setResolution(Probe02, 10);
 
 
  // Start the LCD
  lcd1.init();
  lcd2.init();
  //lcd display screen backlight
  lcd1.backlight(); 
  lcd2.backlight();
  //clear lcd display
  lcd1.clear();
  lcd1.home();
  lcd2.clear();
  lcd2.home();
 
  //Set lowest, highest temperatues
  CurrentTemp1  = sensors.getTempCByIndex(0);
  HighestTemp1 = CurrentTemp1;
  LowestTemp1 = CurrentTemp1;
  CurrentTemp2  = sensors.getTempCByIndex(1);
  HighestTemp2 = CurrentTemp2;
  LowestTemp2 = CurrentTemp2;

// LCD display information
  lcd1.setCursor(0,0);
  lcd1.print(F("Current: "));
  LCDshowNum(9,0,CurrentTemp1); 

  lcd1.setCursor(0,1);
  lcd1.print(F("Ma: "));
  LCDshowNum(3,1,HighestTemp1);   
     
  lcd1.setCursor(9,1);
  lcd1.print(F("Mi: "));
  LCDshowNum(12,1,LowestTemp1);
       
  lcd2.setCursor(0,0);
  lcd2.print(F("Current: "));
  LCDshowNum(9,0,CurrentTemp2); 

  lcd2.setCursor(0,1);
  lcd2.print(F("Ma: "));
  LCDshowNum(3,1,HighestTemp2);   
     
  lcd2.setCursor(9,1);
  lcd2.print(F("Mi: "));
  LCDshowNum(12,1,LowestTemp2);
}


void loop()
{
  sensors.requestTemperatures(); //get temperature

  //Get Current
  CurrentTemp1 = sensors.getTempCByIndex(0);
  LCDshowNum(9,0,CurrentTemp1);   
  CurrentTemp2 = sensors.getTempCByIndex(1);
  LCDshowNum(9,0,CurrentTemp2);   
 
  //Check if Hightest
  if(CurrentTemp1 > HighestTemp1)
     HighestTemp1 = CurrentTemp1;
     LCDshowNum(3,1,HighestTemp1);
  if(CurrentTemp2 > HighestTemp2)
     HighestTemp2 = CurrentTemp2;
     LCDshowNum(3,1,HighestTemp2);
     
  //Chekc if Lowest
  if(CurrentTemp1 < LowestTemp1)
    LowestTemp1 = CurrentTemp1;
    LCDshowNum(12,1,LowestTemp1);
  if(CurrentTemp2 < LowestTemp2)
    LowestTemp2 = CurrentTemp2;
    LCDshowNum(12,1,LowestTemp2);
 
 
  delay(1);
}

/*
* LCDshowNum
* LCD display location: x, y
* Float n: temperature to display
*/
void LCDshowNum(int x, int y, float n){
  lcd1.setCursor(x,y);
  lcd1.print(n);
  lcd2.setCursor(x,y);
  lcd2.print(n);
}

david_prentice

Surely the library has examples for multiple probes.

Codevision C has examples.   There is a GCC onewire library with examples.
But Arduino libraries are generally easier to use.

Although an individual conversion is slow,    you can start the conversion on each probe.    Come back later when the temperatures are all ready to read.

David.

GolamMostafa

#4
Mar 09, 2018, 05:35 am Last Edit: Mar 09, 2018, 07:12 am by GolamMostafa
Which board are you using? Is it UNO or ?

I have a working project based on UNO-2xDS18B20.

J_Erbe

Which board are you using? Is it UNO or ?

I have a working project based on UNO-2xDS18B20.

I will be using a Wemos D1 Mini so I have WiFi available should I want to expand at a later date, But I do have 2 uno r3's should they work better.

Could you copy some code and a drawing the wires look a bit like spaghetti junction lmao

GolamMostafa

#6
Mar 09, 2018, 01:00 pm Last Edit: Mar 10, 2018, 02:48 am by GolamMostafa
Quote
Could you copy some code and a drawing the wires look a bit like spaghetti junction lmao
1. I have one I2C LCD and one PLCD (no second I2C in my stock!)
2. The following is the connection diagram among UNO, I2C LCD, and PLCD. IO Extender of I2C LCD has built-in 10k pull-ups for thr TWI Bus.


3.  The program codes for the set up of Step-2 are given below. These are working codes; but, not optimized. The codes are based on OneWire.h Library. I have not used the DallasTemperature.h Library.
Code: [Select]
//12-bit default resolution; external power supply

#include<OneWire.h>
#include<LiquidCrystal.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal lcd(8, 3, 4, 5, 6, 7);
LiquidCrystal_I2C lcdi(0x27, 16, 2);

OneWire ds(10);
byte addr1[8];         //to hold 64-bit ROM Codes of DS1
byte addr2[8];        //to ho;d 64-bit ROM-code DS2
byte data[9];        //buffer to hold data coming from DS18B20
float celsius;

void setup()
{
  lcdi.init();
  lcdi.backlight();
  lcdi.print("OK!");
  Serial.begin(9600);
  lcd.begin(16, 2);
  
  ds.reset();
  ds.search(addr1);  //collect 64-bit ROM code from sensor (DS1)
  ds.search(addr2);
  
  lcdi.setCursor(0, 1);
  for (int i=0; i<8; i++)
  {
    lcdi.print(addr1[i], HEX);   //show on I2C LCD
  }

  lcd.setCursor(0, 1);
  for (int i=0; i<8; i++)
  {
    lcd.print(addr2[i], HEX);
  }
}

void loop()
{
  probe1();
  delay(2000);
  probe2();
  delay(2000);
}

void probe1()
{
 //----------------------------
 ds.reset();       //bring 1-Wire into idle state
 ds.select(addr1); //slect with DS-1 with address addr1
 ds.write(0x44);    //conversion command
 delay(1000);   //data ready withinh DS18B20 or poll status word
 //---------------------------

 ds.reset();
 ds.select(addr1);  //selectimg the desired DS18B20
 ds.write(0xBE);    //Function command to read Scratchpad Memory (9Byte)
 ds.read_bytes(data, 9); //data comes from DS and are saved into buffer data[8]
 //---------------------------------

  int16_t raw = (data[1] << 8) | data[0]; //---data[0] and data[1] contains temperature data : 12-bit resolution-----
  celsius = (float)raw / 16.0;  //12-bit resolution
  lcdi.setCursor(0, 0);
  lcdi.print(celsius);
}


void probe2()
{
 ds.reset();       //bring 1-Wire into idle state
 ds.select(addr2); //slect with DS-1 with address addr1
 ds.write(0x44);    //conversion command
 delay(1000);   //data ready withinh DS18B20 or poll status word
 //---------------------------

 ds.reset();
 ds.select(addr2);  //selectimg the desired DS18B20
 ds.write(0xBE);    //Function command to read Scratchpad Memory (9Byte)
 ds.read_bytes(data, 9); //data comes from DS and are saved into buffer data[8]
  //---------------------------------

  int16_t raw = (data[1] << 8) | data[0]; //---data[0] and data[1] contains temperature data : 12-bit resolution-----
  celsius = (float)raw / 16.0;  //12-bit resolution
  lcd.setCursor(0, 0);
  lcd.print(celsius);
 
}


BTW: Please correct the Title -- replace Dsb18b20 by DS18B20

Go Up