Pages: [1]   Go Down
Author Topic: Reading DS18B20+ temperatures  (Read 1371 times)
0 Members and 1 Guest are viewing this topic.
Czech Republic
Offline Offline
Sr. Member
****
Karma: 4
Posts: 312
Absolute Beginner, yet.
Newbie in Arduino
since May 2013
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

After a few day of "playing" I debugged working program for cyclic reading temperatures from DS18B20+ sensors.

The function
Code:
int my_putc(char c, FILE *t){
  Serial.write(c);
//tft.print(c);
//SoftwareSerialLCD.print(c);
};
function is very interesting and allows output to the any display device without any major modifications of the program.

Code:
// This Arduino sketch reads DS18B20/DS18B20+ "1-Wire" digital temperature sensors at BUS.

//you can use any unused digital input
#define ONE_WIRE_BUS 3

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

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

#define TEMPERATURE_PRECISION 12
//#define TEMPERATURE_PRECISION 11
//#define TEMPERATURE_PRECISION 10
//#define TEMPERATURE_PRECISION 9
#define MAX_SENSORS 3
DeviceAddress Thermometer[MAX_SENSORS];
float Temperature[MAX_SENSORS];
byte numberOfDevices;
byte Limit;
#define FLOAT_A 3
#define FLOAT_B 3
#if !defined(DEVICE_DISCONNECTED)
#define DEVICE_DISCONNECTED -127
#endif

int my_putc(char c, FILE *t){
  Serial.write(c);
};

void printAddress(DeviceAddress deviceAddress){
  printf("%02X-%02X-%02X-%02X-%02X-%02X-%02X-%02X\r\n", deviceAddress[0], deviceAddress[1], deviceAddress[2], deviceAddress[3], deviceAddress[4], deviceAddress[5], deviceAddress[6], deviceAddress[7]);
};

void setup(void){
  Serial.begin(115200);
  fdevopen( &my_putc, 0);
  sensors.begin();

  printf("Locating devices...");
  numberOfDevices = sensors.getDeviceCount();
  printf("Found %d devices.\r\n", numberOfDevices);

  printf("Parasite power is: ");
  if (sensors.isParasitePowerMode()){
    printf("ON\r\n");
  }
  else{
    printf("OFF\r\n");
  };
  if (numberOfDevices > MAX_SENSORS){
    Limit = MAX_SENSORS;
  }
  else{
    Limit = numberOfDevices;
  };
  for(byte index=0; index<Limit; index++){
    if(sensors.getAddress(Thermometer[index], index)){
      printf("\r\nDevice %d address:\r\n", index);
      printAddress(Thermometer[index]);
      printf("Setting resolution to %d\r\n",TEMPERATURE_PRECISION);
      sensors.setResolution(Thermometer[index], TEMPERATURE_PRECISION);
      delay(750/ (1 << (12-TEMPERATURE_PRECISION)));
      printf("Resolution actually set to: %d\r\n",sensors.getResolution(Thermometer[index]));
    }
    else{
      printf("Found ghost device at %d but could not detect address. Check power and cabling!\r\n",String(index));
    };
  };
  printf("\r\nGetting temperatures...\r\n");
};

void loop(void){
  sensors.requestTemperatures();
  for(byte index=0; index<Limit; index++){
    Temperature[index] = sensors.getTempC(Thermometer[index]);
    if (Temperature[index] == DEVICE_DISCONNECTED) {
      printf("Error!");
    }
    else {
      static char dtostrfbuffer[15];
      dtostrf(Temperature[index], FLOAT_A, FLOAT_B, dtostrfbuffer);
      printf("%s \xB0""C  ",dtostrfbuffer);
    };
    switch (TEMPERATURE_PRECISION){
    case 9:
      delay(94);
      break;
    case 10:
      delay(188);
      break;
    case 11:
      delay(375);
      break;
    case 12:
    default:
      delay(750);
      break;
    };
  };
  Serial.println();
};

Wiring is very simply, such as
Logged

Arduino Uno R3, Arduino Mega 2560 R3, Ethermega (R3), and some Ethernet Shields (R3), Chronodots, TMP36 sensors, DS18B20 sensors, LCDs/TFTs, RS232C to learn and play...

Offline Offline
Newbie
*
Karma: 1
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Nice example code!

I checked it, and wondered if you are using the correct location for the delay. In your code, you wait for each sensor to convert, so adding a large delay.
But the delay is already in the library, called with the function requestTemperatures(). So you can get good readings with just one delay after printing all temps. Code change: relocate on singe }; a few lines up in both loop and setup.

Keep up the good work!
Logged

Czech Republic
Offline Offline
Sr. Member
****
Karma: 4
Posts: 312
Absolute Beginner, yet.
Newbie in Arduino
since May 2013
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I checked it, and wondered if you are using the correct location for the delay. In your code, you wait for each sensor to convert, so adding a large delay.
But the delay is already in the library, called with the function requestTemperatures(). So you can get good readings with just one delay after printing all temps. Code change: relocate on singe }; a few lines up in both loop and setup.
I don't know if the delay is necessary. I took a part of another program and implement it in my loop.

Note:
Program requires OneWire.h and DallasTemperature.h (download latest version)
Logged

Arduino Uno R3, Arduino Mega 2560 R3, Ethermega (R3), and some Ethernet Shields (R3), Chronodots, TMP36 sensors, DS18B20 sensors, LCDs/TFTs, RS232C to learn and play...

Pages: [1]   Go Up
Jump to: