Loop DS18B20 reading

I am in the process of my first Arduino project, a temperature display for my computer.

I have been able to set up the temperature sensors, and get the readings to output to the tft display, unfortunately it will only send the temps once, and the program ends (it seems), I am obviously new to this, and I do not know how to get the temperatures to continue to update with a 1000ms delay...

I am not presently at the computer with my sketch, but I will gladly upload it if needed for troubleshooting. I suspect it is an easy fix and I just do not have the experience to get it sorted.

Thank you in advance!

reever:
I am not presently at the computer with my sketch, but I will gladly upload it if needed for troubleshooting. I suspect it is an easy fix and I just do not have the experience to get it sorted.

Hi, welcome to the forum.

It is indeed best to post the code you have questions about, it's hard to answer your question without it :slight_smile:

If you click the 7th menu-button from the right, and paste your code between the the tags it's easiest to read.

It seems I lost my progress from a couple of months ago during a data migration...
Here are the two sketches I have at present...

This one displays my text, and polls a single probe and sends the data to the serial monitor:

#include <OneWire.h>
#include <UTFT.h>
// OneWire DS18S20, DS18B20, DS1822 Temperature Example
//
// http://www.pjrc.com/teensy/td_libs_OneWire.html
//
// The DallasTemperature library can do all this work for you!
// http://milesburton.com/Dallas_Temperature_Control_Library

OneWire  ds(10);  // on pin 10

void setup(void) {
  Serial.begin(9600);
}

void loop(void) {
  byte i;
  byte present = 0;
  byte type_s;
  byte data[12];
  byte addr[8];
  float celsius, fahrenheit;
  
  if ( !ds.search(addr)) {
    Serial.println("No more addresses.");
    Serial.println();
    ds.reset_search();
    delay(250);
    return;
  }
  
  Serial.print("ROM =");
  for( i = 0; i < 8; i++) {
    Serial.write(' ');
    Serial.print(addr[i], HEX);
  }

  if (OneWire::crc8(addr, 7) != addr[7]) {
      Serial.println("CRC is not valid!");
      return;
  }
  Serial.println();
 
  // the first ROM byte indicates which chip
  switch (addr[0]) {
    case 0x10:
      Serial.println("  Chip = DS18S20");  // or old DS1820
      type_s = 1;
      break;
    case 0x28:
      Serial.println("  Chip = DS18B20");
      type_s = 0;
      break;
    case 0x22:
      Serial.println("  Chip = DS1822");
      type_s = 0;
      break;
    default:
      Serial.println("Device is not a DS18x20 family device.");
      return;
  } 

  ds.reset();
  ds.select(addr);
  ds.write(0x44,1);         // start conversion, with parasite power on at the end
  
  delay(1000);     // maybe 750ms is enough, maybe not
  // we might do a ds.depower() here, but the reset will take care of it.
  
  present = ds.reset();
  ds.select(addr);    
  ds.write(0xBE);         // Read Scratchpad

  Serial.print("  Data = ");
  Serial.print(present,HEX);
  Serial.print(" ");
  for ( i = 0; i < 9; i++) {           // we need 9 bytes
    data[i] = ds.read();
    Serial.print(data[i], HEX);
    Serial.print(" ");
  }
  Serial.print(" CRC=");
  Serial.print(OneWire::crc8(data, 8), HEX);
  Serial.println();

  // convert the data to actual temperature

  unsigned int raw = (data[1] << 8) | data[0];
  if (type_s) {
    raw = raw << 3; // 9 bit resolution default
    if (data[7] == 0x10) {
      // count remain gives full 12 bit resolution
      raw = (raw & 0xFFF0) + 12 - data[6];
    }
  } else {
    byte cfg = (data[4] & 0x60);
    if (cfg == 0x00) raw = raw << 3;  // 9 bit resolution, 93.75 ms
    else if (cfg == 0x20) raw = raw << 2; // 10 bit res, 187.5 ms
    else if (cfg == 0x40) raw = raw << 1; // 11 bit res, 375 ms
    // default is 12 bit resolution, 750 ms conversion time
  }
  celsius = (float)raw / 16.0;
  fahrenheit = celsius * 1.8 + 32.0;
  Serial.print("  Temperature = ");
  Serial.print(celsius);
  Serial.print(" Celsius, ");
  Serial.print(fahrenheit);
  Serial.println(" Fahrenheit");


extern uint8_t BigFont[];
extern uint8_t SevenSegNumFont[];

// Set the pins to the correct ones for your development shield
// ------------------------------------------------------------
// Arduino Uno / 2009:
// -------------------
// Standard Arduino Uno/2009 shield            : <display model>,A5,A4,A3,A2
// DisplayModule Arduino Uno TFT shield        : <display model>,A5,A4,A3,A2
//
// Arduino Mega:
// -------------------
// Standard Arduino Mega/Due shield            : <display model>,38,39,40,41
// CTE TFT LCD/SD Shield for Arduino Mega      : <display model>,38,39,40,41
//
// Remember to change the model parameter to suit your display module!
UTFT myGLCD(ITDB32S,38,39,40,41);


  myGLCD.InitLCD();
  myGLCD.clrScr();
  myGLCD.setFont(BigFont);


void loop();

    myGLCD.setColor(255, 0, 0);
    myGLCD.print("Probe Location:", 319, 239, 180);
    myGLCD.setColor(255, 255, 255);
    myGLCD.print("Intake - ", 319, 220, 180);
    myGLCD.print("560mm Res 1 - ", 319, 205, 180);
    myGLCD.print("560mm Res 2 - ", 319, 190, 180);
    myGLCD.print("VRM - ", 319, 175, 180);
    myGLCD.print("BackPlate - ", 319, 160, 180);
    myGLCD.print("PSU - ", 319, 145, 180);
    myGLCD.print("PCH - ", 319, 130, 180);
    myGLCD.print("Pump 1 - ", 319, 115, 180);
    myGLCD.print("Pump 2 - ", 319, 100, 180);
    myGLCD.print("Coolant - ", 319, 85, 180);
    

//    myGLCD.setColor(0, 0, 255);
//    myGLCD.print("0 degrees", 0, 16, 0);
//    myGLCD.print("90 degrees", 319, 0, 90);
//    myGLCD.print("180 degrees", 319, 239, 180);
//    myGLCD.print("270 degrees", 0, 239, 270);

//    myGLCD.setFont(SevenSegNumFont);
//    myGLCD.setColor(0, 255, 0);
//    myGLCD.print("45", 90, 100, 45);
//    myGLCD.print("90", 200, 50, 90);
//    myGLCD.print("180", 300, 200, 180);

  while (true) {};
}

While this one polls all probes and sends the data to the serial monitor, but does not show my text (Obviously has not been input):

#include <OneWire.h>
#include <UTFT.h>
// OneWire DS18S20, DS18B20, DS1822 Temperature Example
//
// http://www.pjrc.com/teensy/td_libs_OneWire.html
//
// The DallasTemperature library can do all this work for you!
// http://milesburton.com/Dallas_Temperature_Control_Library

// UTFT_Textrotation_Demo (C)2014 Henning Karlsen
// web: http://www.henningkarlsen.com/electronics
//
// This program is a demo of the textrotation-functions.
//
// This demo was made for modules with a screen resolution 
// of 320x240 pixels.
//
// This program requires the UTFT library.
//
OneWire  ds(10);  // on pin 10

void setup(void) {
  Serial.begin(9600);
}

void loop(void) {
  byte i;
  byte present = 0;
  byte type_s;
  byte data[12];
  byte addr[8];
  float celsius, fahrenheit;
  
  if ( !ds.search(addr)) {
    Serial.println("No more addresses.");
    Serial.println();
    ds.reset_search();
    delay(250);
    return;
  }
  
  Serial.print("ROM =");
  for( i = 0; i < 8; i++) {
    Serial.write(' ');
    Serial.print(addr[i], HEX);
  }

  if (OneWire::crc8(addr, 7) != addr[7]) {
      Serial.println("CRC is not valid!");
      return;
  }
  Serial.println();
 
  // the first ROM byte indicates which chip
  switch (addr[0]) {
    case 0x10:
      Serial.println("  Chip = DS18S20");  // or old DS1820
      type_s = 1;
      break;
    case 0x28:
      Serial.println("  Chip = DS18B20");
      type_s = 0;
      break;
    case 0x22:
      Serial.println("  Chip = DS1822");
      type_s = 0;
      break;
    default:
      Serial.println("Device is not a DS18x20 family device.");
      return;
  } 

  ds.reset();
  ds.select(addr);
  ds.write(0x44,1);         // start conversion, with parasite power on at the end
  
  delay(1000);     // maybe 750ms is enough, maybe not
  // we might do a ds.depower() here, but the reset will take care of it.
  
  present = ds.reset();
  ds.select(addr);    
  ds.write(0xBE);         // Read Scratchpad

  Serial.print("  Data = ");
  Serial.print(present,HEX);
  Serial.print(" ");
  for ( i = 0; i < 9; i++) {           // we need 9 bytes
    data[i] = ds.read();
    Serial.print(data[i], HEX);
    Serial.print(" ");
  }
  Serial.print(" CRC=");
  Serial.print(OneWire::crc8(data, 8), HEX);
  Serial.println();

  // convert the data to actual temperature

  unsigned int raw = (data[1] << 8) | data[0];
  if (type_s) {
    raw = raw << 3; // 9 bit resolution default
    if (data[7] == 0x10) {
      // count remain gives full 12 bit resolution
      raw = (raw & 0xFFF0) + 12 - data[6];
    }
  } else {
    byte cfg = (data[4] & 0x60);
    if (cfg == 0x00) raw = raw << 3;  // 9 bit resolution, 93.75 ms
    else if (cfg == 0x20) raw = raw << 2; // 10 bit res, 187.5 ms
    else if (cfg == 0x40) raw = raw << 1; // 11 bit res, 375 ms
    // default is 12 bit resolution, 750 ms conversion time
  }
  celsius = (float)raw / 16.0;
  fahrenheit = celsius * 1.8 + 32.0;
  Serial.print("  Intake Temperature = ");
  Serial.print(celsius);
  Serial.print(" Celsius, ");
// Declare which fonts we will be using
extern uint8_t BigFont[];
extern uint8_t SevenSegNumFont[];

}

reever:
// The DallasTemperature library can do all this work for you!

This is the only useful line in your temperature programme, and it isn't even a command, it's a comment. The rest is junk.

Try this to get get something sensible

You can hardly go wrong with UTFT as the manual for it is about the best you will ever see, although I wouldn't have chosen it for a first attempt. Typical commands might be

Setup........
    myGLCD.print("Output            C",82, 180);
    myGLCD.drawCircle(220, 182, 2);

Loop..............

   myGLCD.printNumF(OutTemp, 2, 174, 180);

I imagine you will want to do some graphics, so you might find this mildly entertaining. It's for DS18B20s but most of the figures are dummies at this point!

Not looking to do any graphics yet, although down the road I am hoping to do some logging and make graphing the temperature logs possible. Really the only issue I think I am having is combining the two things. It seems like it would be easy, but I don't know how to make both the temps log and the display work. I can do them individually, but trying to combine the two isn't working...

I used the Onewire library successfully until I discovered the Dallas Temperature library and it is much more user friendly. That's all I use now.

reever:
how to make both the temps log and the display work. I can do them individually, but trying to combine the two isn't working...

It's essentially an exercise in combining the two - setup with setupo, loop with loop, and then adding the subroutines afterwards. No particular order needed for the subroutines. The example I linked to is not the best to start with - too involved.

That below is a bit easier to read and may be of help. It combines:

Read 3xDS18B20
display to 5110 LCD
send to Bluetooth Graphics Terminal
send every tenth reading to SD
change SD filename at midnight

Most imortantly, it reads multiple DS18B20s in a sensible and bulletproof manner. Strip out that which is not relevant to you.

#include <OneWire.h>
#include <DallasTemperature.h>
#include <PCD8544.h>             // Nokia 5110
#include "Wire.h"                     // 
#include <SD.h>
#include <SPI.h>                 // SD
#include "RTClib.h"              //Date As Filename
#include <string.h>              //Date As Filename 


#define DS1307_ADDRESS 0x68

RTC_DS1307 RTC;
char filename[] = "00000000.CSV";
File myFile;

static PCD8544 lcd;

// Custom symbols
static const byte DEGREES_CHAR = 1;
static const byte degrees_glyph[] = { 0x00, 0x07, 0x05, 0x07, 0x00 };
static const byte SLASH_CHAR = 2;
static const byte slash_glyph[] = {0x00,0x20,0x10,0x08};

// Green group Dee Why (red or amber LED shields)
byte InThermo[8] =  {0x28, 0x69, 0xC2, 0xB0, 0x03, 0x00, 0x00, 0x9F };
byte OutThermo[8] = {0x28, 0x7A, 0x8B, 0xC0, 0x03, 0x00, 0x00, 0x2F};
byte DrainThermo[8] = {0x28, 0x54, 0xF7, 0x2D, 0x04, 0x00, 0x00, 0x68}; 
  
#define ONE_WIRE_BUS 3
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
 
int  second, minute, hour, weekDay, monthDay, month, year;
int k=0;

const int chipSelect = 4;

float tempC, InTemp, OutTemp, DrainTemp, diff; 

// Define the strings for our datastream IDs
char sensorId0[] = "InThermo";
char sensorId1[] = "OutThermo";
char sensorId2[] = "DrainThermo";

char strIn[8];
char strOut[8];
char strDrain[8];
char strdiff[8];

String stringOne, stringTwo, stringThree, stringFour;
//___________________________________________________________________________________________________________

void setup() {
   lcd.begin(84, 48);
     // Register the custom symbols...
  lcd.createChar(DEGREES_CHAR, degrees_glyph);
  lcd.createChar(SLASH_CHAR, slash_glyph);
  
  Wire.begin();
  Serial.begin(9600);
 
  delay(300);//Wait for newly restarted system to stabilize
  
  sensors.setResolution(InThermo, 12);
  sensors.setResolution(OutThermo, 12);
  sensors.setResolution(DrainThermo, 12);
  
  lcd.setCursor (0,0);
  lcd.println("Init SD CARD");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  //pinMode(10, OUTPUT);// Uno
  pinMode(53, OUTPUT);//MEGA
  
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) 
  {
    lcd.println("Card failed");
    // don't do anything more:
    return;
  }
  lcd.println("CARD OK");
  delay(2000);
  
       getFileName();
      lcd.setCursor(0,3); 
      lcd.println(filename);
        delay(2000);
  
    lcd.clear();
        
  //Sequence for bluetooth
  // "E256,-321,982\n" 
  //so insert three variable between four strings, two are the same twice, 
  //to make a fourth string 
  
  stringOne = String("E");
  stringTwo = String(",");
  stringThree = String("\n");

    running();
  }

//_________________________________________________________________________________________________________

void loop() {

   GetClock();
  if (hour == 0 && minute == 0 && second <2)
  {
    getFileName();
  }
  //get the values from the DS8B20's 
  sensors.requestTemperatures();

  InTemp = sensorValue(InThermo);
  OutTemp = sensorValue(OutThermo);  
  DrainTemp = sensorValue(DrainThermo); 

  diff = OutTemp - InTemp;
  
dtostrf(InTemp,4, 2, strIn);
dtostrf(OutTemp,4, 2, strOut);
dtostrf(diff,4, 2, strdiff);

stringFour = stringOne + strIn + stringTwo + strOut;
stringFour = stringFour + stringTwo + strdiff + stringThree;

  Serial.println(stringFour);

  lcd.setCursor(49,0);
  lcd.print(InTemp);
  lcd.setCursor(49,1);
  lcd.print (OutTemp);
  lcd.setCursor(49,2);
  lcd.print(DrainTemp);
  lcd.setCursor(49,3);
  lcd.print(diff);  
  lcd.print(" ");
  lcd.setCursor(17,5);

  lcd.print(hour);
  lcd.print(":");
  lcd.print(minute);
  lcd.print(":");
  lcd.print(second);
  lcd.print("   ");

  k=k+1;  

  if (k>9 )
  {  
  myFile = SD.open(filename, FILE_WRITE);//<<<<<<<<<<<<< OPEN
  myFile.print(hour);
  myFile.print(":");
  myFile.print(minute);
  myFile.print(":");
  myFile.print(second);
  myFile.print(",");

  myFile.print(InTemp);
  myFile.print(",");
  myFile.print(OutTemp);
  myFile.print(",");
  myFile.print(DrainTemp);
  myFile.print(",");
  myFile.print(diff);
  myFile.println();
       myFile.close();//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>CLOSE
       
      k=0;
  }
  delay(850);
}  // loop ends here

//sensorValue function
float sensorValue (byte deviceAddress[])
{
  tempC = sensors.getTempC (deviceAddress);
  return tempC;
}

byte bcdToDec(byte val)  {
  // Convert binary coded decimal to normal decimal bers
  return ( (val/16*10) + (val%16) );
}

void running(){
  lcd.setCursor(0,0);
  lcd.print("In");
  lcd.setCursor(31,0);
  lcd.print("\001C ");
  lcd.setCursor(0,1);
  lcd.print("Out");
  lcd.setCursor(31,1);
  lcd.print("\001C ");
  lcd.setCursor(0,2);
  lcd.print("Drain");
  lcd.setCursor(31,2);
  lcd.print("\001C ");
  lcd.setCursor(0,3);
  lcd.print("diff");
  lcd.setCursor(31,3);
  lcd.print("\001C "); 
  lcd.setCursor(15,4);
  lcd.print("Bluetooth");  
  }

void getFileName(){

DateTime now = RTC.now();

   // Turns file_today_name into date.txt format
   sprintf(filename, "%02d%02d%02d.csv", now.year(), now.month(), now.day());
}

void GetClock(){
  // Reset the register pointer
  Wire.beginTransmission(DS1307_ADDRESS);
  byte zero = 0x00;
  Wire.write(zero);
  Wire.endTransmission();
  Wire.requestFrom(DS1307_ADDRESS, 7);

  second = bcdToDec(Wire.read());
  minute = bcdToDec(Wire.read());
  hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
  weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
  monthDay = bcdToDec(Wire.read());
  month = bcdToDec(Wire.read());
  year = bcdToDec(Wire.read());
}