DS1820 help.

hello guys …
i have a problem, I got connected DS1820 sensor to arduino and an SD card module to store sensor readings to card.
So i used a sketch and a tutorial from book “Beginning Arduino, 2nd Edition” , in example is a DS1307 real time clock , and one more DS1820 sensor, so i have only one DS1820 for inside temperature, and i removed DS1307 real time clock.
So now in my serial monitor temperature is always -127.00° C.
I don’t know have i messed something in the sketch or is something else.

// Project 43

#include <SD.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 3
#define TEMPERATURE_PRECISION 12
File File1;

OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
// arrays to hold device addresses
DeviceAddress insideThermometer = { 0x28, 0x44, 0x12, 0xC2, 0x03, 0x00, 0x00, 0x92 };
float tempC, tempF;
// Init the DS1307

void setup() {
Serial.println("Initializing the SD Card...");
if (!SD.begin()) {
Serial.println("Initialization Failed!");
return;
}
Serial.println("Initialization Complete.\n");
// Set the clock to run-mode

Serial.begin(9600);
Serial.println("Type any character to start");
while (!Serial.available());
Serial.println();
// Start up the sensors library
sensors.begin();
Serial.println("Initialising Sensors.\n");
// set the resolution
sensors.setResolution(insideThermometer, TEMPERATURE_PRECISION);
delay(100);
// Set the time on the RTC.
// Comment out this section if you have already set the time and have a battery backup
// The following lines can be commented out to use the values already stored in the DS1307



}
void getTemperature(DeviceAddress deviceAddress)
{
sensors.requestTemperatures();
tempC = sensors.getTempC(deviceAddress);
tempF = DallasTemperature::toFahrenheit(tempC);
}
void loop() {
File1 = SD.open("TEMPLOG.txt", FILE_WRITE);
Serial.println("File Opened.");
if (File1) {


File1.print(", ");
Serial.print(", ");


File1.print(": Inside: ");
Serial.print(": Inside: ");
getTemperature(insideThermometer);
File1.print(tempC);
Serial.print(tempC);
File1.println(" C");
Serial.println(" C");
Serial.println("Data written to file.");
}
File1.close();
Serial.println("File Closed.\n");
Serial.println("Safe to disconnect card");
delay(10000);
Serial.println("Card in use, do not disconnect!!");
}

Thanks for help :slight_smile:

TEMPLOG.TXT (2.02 KB)

Wiring for the DS1820 looks ok. Might be an idea to remove the RTC wiring until the sensor is working. Post your code too.

But RTC are not here i dont have that sensor, i trying to use only one DS1820 and store data on SD card.
That is my goal, i can post code for two DS1820 sensor, and RTC.

// Project 43
// DS1307 library by Henning Karlsen
#include <SD.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <DS1307.h> // written by Henningh Karlsen
#define ONE_WIRE_BUS 3
#define TEMPERATURE_PRECISION 12
File File1;
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas

OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
// arrays to hold device addresses
DeviceAddress insideThermometer = { 0x28, 0x44, 0x12, 0xC2, 0x03, 0x00, 0x00, 0x92 };
DeviceAddress outsideThermometer = { 0x28, 0xA5, 0x02, 0xC2, 0x03, 0x00, 0x00, 0xF0 };
float tempC, tempF;
// Init the DS1307
DS1307 rtc(4, 5);
void setup() {
Serial.println("Initializing the SD Card...");
if (!SD.begin()) {
Serial.println("Initialization Failed!");
return;
}
Serial.println("Initialization Complete.\n");
// Set the clock to run-mode
rtc.halt(false);
Serial.begin(9600);
Serial.println("Type any character to start");
while (!Serial.available());
Serial.println();
// Start up the sensors library
sensors.begin();
Serial.println("Initialising Sensors.\n");
// set the resolution
sensors.setResolution(insideThermometer, TEMPERATURE_PRECISION);
sensors.setResolution(outsideThermometer, TEMPERATURE_PRECISION);
delay(100);
// Set the time on the RTC.
// Comment out this section if you have already set the time and have a battery backup
// The following lines can be commented out to use the values already stored in the DS1307
rtc.setDOW(TUESDAY); // Set Day-of-Week to TUESDAY
rtc.setTime(9, 27, 00); // Set the time HH,MM,SS
rtc.setDate(30, 04, 2013); // Set the date DD,MM,YYYY
}
void getTemperature(DeviceAddress deviceAddress)
{
sensors.requestTemperatures();
tempC = sensors.getTempC(deviceAddress);
tempF = DallasTemperature::toFahrenheit(tempC);
}
void loop() {
File1 = SD.open("TEMPLOG.txt", FILE_WRITE);
Serial.println("File Opened.");
if (File1) {
File1.print(rtc.getDateStr());
Serial.print(rtc.getDateStr());
File1.print(", ");
Serial.print(", ");
File1.print(rtc.getTimeStr());
Serial.print(rtc.getTimeStr());
File1.print(": Inside: ");
Serial.print(": Inside: ");
getTemperature(insideThermometer);
File1.print(tempC);
Serial.print(tempC);
File1.print("C Outside: ");
Serial.print("C Outside: ");
getTemperature(outsideThermometer);
File1.print(tempC);
Serial.print(tempC);
File1.println(" C");
Serial.println(" C");
Serial.println("Data written to file.");
}
File1.close();
Serial.println("File Closed.\n");
Serial.println("Safe to disconnect card");
delay(10000);
Serial.println("Card in use, do not disconnect!!");
}

I have changed the code for only one sensor, but sensor readings are -127.00° C always. :S

If you don’t have an RTC, you need to remove all the code which accesses one.
It would make your code much easier to read if you used the Tools|Auto Format item in the IDE menu.

Pete

I'd suggest you try running the sample code that spits out the addresses and see if they match yours - it'll be a good test of your wiring. Also, along those lines I'd try getting the temperatures with a position number rather than an address. Most of all though I'd drop back to a minimal sketch that just exercises the temperature sensor.

here i tried this example code for DS1820 temperature and it works.

#include <OneWire.h>
#include <DallasTemperature.h>
 
// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 2
 
// Setup a oneWire instance to communicate with any OneWire devices 
// (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
 
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
 
void setup(void)
{
  // start serial port
  Serial.begin(9600);
  Serial.println("Dallas Temperature IC Control Library Demo");

  // Start up the library
  sensors.begin();
}
 
 
void loop(void)
{
  // call sensors.requestTemperatures() to issue a global temperature
  // request to all devices on the bus
  Serial.print(" Requesting temperatures...");
  sensors.requestTemperatures(); // Send the command to get temperatures
  Serial.println("DONE");

  Serial.print("Temperature for Device 1 is: ");
  Serial.print(sensors.getTempCByIndex(0)); // Why "byIndex"? 
    // You can have more than one IC on the same bus. 
    // 0 refers to the first IC on the wire
 
}

But now code for store on SD card , looks like this.

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

#define ONE_WIRE_BUS 2
File File1;
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas

OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
// arrays to hold device addresses
DeviceAddress insideThermometer = { 
  0x28, 0x44, 0x12, 0xC2, 0x03, 0x00, 0x00, 0x92 };
float tempC, tempF;
// Init the DS1307

void setup() {
  Serial.println("Initializing the SD Card...");
  if (!SD.begin()) {
    Serial.println("Initialization Failed!");
    return;
  }
  Serial.println("Initialization Complete.\n");
  // Set the clock to run-mode

  Serial.begin(9600);
  Serial.println("Type any character to start");
  while (!Serial.available());
  Serial.println();
  // Start up the sensors library
  sensors.begin();
  Serial.println("Initialising Sensors.\n");
  // set the resolution


  delay(100);
  // Set the time on the RTC.
  // Comment out this section if you have already set the time and have a battery backup
  // The following lines can be commented out to use the values already stored in the DS1307



}
void getTemperature(DeviceAddress deviceAddress)
{
  sensors.requestTemperatures();
  tempC = sensors.getTempC(deviceAddress);
  tempF = DallasTemperature::toFahrenheit(tempC);
}
void loop() {
  File1 = SD.open("TEMPLOG.txt", FILE_WRITE);
  Serial.println("File Opened.");
  if (File1) {


    File1.print(", ");
    Serial.print(", ");


    File1.print(": Inside: ");
    Serial.print(": Inside: ");
    getTemperature(insideThermometer);
    File1.print(tempC);
    Serial.print(tempC);
    File1.println(" C");
    Serial.println(" C");
    Serial.println("Data written to file.");
  }
  File1.close();
  Serial.println("File Closed.\n");
  Serial.println("Safe to disconnect card");
  delay(10000);
  Serial.println("Card in use, do not disconnect!!");
}

I removed outside sensor and a DS1307 in code. And still i get -127°C
Can mistake be in the?

DallasTemperature sensors(&oneWire);
// arrays to hold device addresses
DeviceAddress insideThermometer = { 
  0x28, 0x44, 0x12, 0xC2, 0x03, 0x00, 0x00, 0x92 };
float tempC, tempF;

Where did you get that address from? Each of those things has a unique address - if you copied that sketch from somewhere and kept the addresses, no surprise that it doesn't work.

if you copied that sketch from somewhere and kept the addresses

Yes, I copied from the book “Beginning Arduino, 2nd Edition”
So i didn’t know for those addresses how that work, can you tell me how to change sketch now?
Thanks alot!

There's an example sketch that usually comes with the library which will enumerate your sensors and tell you their addresses. Run that, grab them from serial and paste them into your sketch instead of the ones you have.

These are good tutorials on the DS18xxx http://www.hacktronics.com/Tutorials/arduino-1-wire-tutorial.html http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html

Yes, I did it and it works,. Thanks a lot! :D

Just one more thing how to change this

Serial.begin(9600);
  Serial.println("Type any character to start");
  while (!Serial.available());
  Serial.println();

i need that program runs just when is Arduino connected on power supply ..

Just delete the last 3 lines of the posted code.

hah. It Don’t work like that at all. :confused:

Post your code again and tell us what went wrong.

Everyting is fine but i wanted to change this line of the code

Serial.begin(9600);
  Serial.println("Type any character to start");
  while (!Serial.available());
  Serial.println();

Because I need PC to start program over the serial monitor. I what to start program , and Arduino to collect sensor values and store them on SD, when is Arduino powered. Or i can put one button and a 10k ressistor , but for now I need program that starts immediately and whit no PC action. When i delete those part of the code, program is uploaded, but it doesn't work.