Programming Trouble DS18B20

Hi, I have Arduino UNO R3 and one DS18B20 temperature sensor.

I m trying to create a sketch but I can't. In this sketch when the program is starting up, pin 10, pin 11 & pin 12 is HIGH. Then the sensor will read the temp. and
if the temp. is equal or bigger than 50Celsius degrees then the pin 10 switch to LOW
if the temp. is equal or bigger than 55 Celsius degrees then the pin 11 switch to LOW
& if the temp. is equal or bigger than 60 Celsius degrees then the pin 12 switch LOW

I know that is very annoying to ask for the sketch, but I have read a lot of posts about OneWire lib. & DallasTemp. lib. and a lots of DS18B20 examples but I can't make it work. If anyone can help me I will be very greatful.

Thanks for your time.

We're not going to write your program.
Google "arduino ONEWIRE" You have to do your own research.

man Im not trying to be programmer... I just want to finish a school project... I tried to understand and I made a research but I just running out of time...
the only thing ? need is the right program to compare it with mine and make the corrections..

but I just running out of time...

How can you be running out of time ?. You posted yesterday
If you can't be bothered to do a simple Google search do think we are going to believe that you actually tried to do anything without first posting on the forum ? If you're behind schedule, it certainly isn't our fault.

I know that is very annoying to ask for the sketch,

Yes , it is.

but I have read a lot of posts about OneWire lib. & DallasTemp. lib. and a lots of DS18B20 examples but I can't make it work.

Really ?
Since you read so much about it perhaps you can enlighten us on why it is called ONEWIRE ?

Why don't your tell us what you have tried to get it working
Post a photo of your circuit. Draw a schematic of how you wired the sensor and post a photo of that.
Why don't your explain to us what you tried (how did you connected it and post the code you tried)

I just want to finish a school project

Do you think you should get credit for a project you didn't do ?
Is that fair to the other students who actually DID their own work ?

You asked for help. I gave you a suggestion, which you ignored. If you followed my suggestion your program would already be running by now. You have a right to be sceptical, or have your doubts, but what I am telling you is that when you post here asking for someone to write your code (which ,by the way , we generally don't do , at least not entire programs) and the response you get from the forum is a suggestion to google "this" or "that", the people who are making that suggestion have already done it a million times and know what you are going to find and know that if you trust us and follow our suggestion you will be rewarded. But if you are lazy or don't believe us you will not find what we know you will find, and in the end you will end up with nothing. Stop arguing and do what I suggested. Do it now.

to compare it with mine and make the corrections..

Since you have a program, post your code using the CODE TAGS tool button.

I tried to understand and I made a research but I just running out of time...

Google "arduino ONEWIRE" You have to do your own research.

You obviously DID NOT do what I suggested here or you would be reading temps right now. Everyone on the forum knows that you are just one click away from reading temperature and you are still too lazy to do it...

I m trying to create a sketch but I can't.

That's rediculous. You won't. You have to approach it step by step. If you are running out of time I suggest you get busy.
Step-1

In this sketch when the program is starting up, pin 10, pin 11 & pin 12 is HIGH.

Step-2

Then the sensor will read the temp.

Step-3

and if the temp. is equal or bigger than 50 degrees Celsius

Step-4

then the pin 10 switch to LOW

Step-5

if the temp. is equal or bigger than 55 Celsius degrees

Step-61

then the pin 11 switch to LOW

Step-7

then the pin 12 switch LOW

[/quote]

SITREP
The first thing we need is a SITREP (Mil-Speak for Situation Report)
We need to know exactly what steps you have taken toward reaching your objective.
In your case you project involves a DS18B20 temp sensor.
We need to know exactly how much you know about this sensor you have.
We need to see you post the datasheet as proof that you have seen it.
We need you to describe how you intend to connect this sensor to your arduino UNO (or have already done so).
We need to know what you have done toward getting the sensor to read data in to the arduino, specifically whether or not you have read in any temperatures. Nothing further will happen on your project until we get the SITREP.
NOTE-TO ALL OTHER FORUM MEMBERS -PLEASE WAIT UNTIL THE OP HAS POSTED HIS SITREP BEFORE POSTING ANYTHING.

Ok I had read the advice from Faraday and I create this sketch…
I use references from:
http://arduino-info.wikispaces.com/Brick-Temperature-DS18B20
http://ladyada.net/learn/arduino/lesson5.html

#include <OneWire.h>                            //incert OneWire Library
#include <DallasTemperature.h>                    //incert Dallas Temperature Library
#define ONE_WIRE_BUS 2                              //conect to pin 2 (I dont know if this is correct)

OneWire ourWire (ONE_WIRE_BUS);          //honestly I dont know what this mean
DallasTemperature sensors (ourWire);       // tell DallasTemp. Library to use oneWire Library

int R1= 10;                                 //R1 connected to digital pin 10
int R2= 11;                                 //R2 connected to digital pin 11
int R3= 12;                                //R3 connected to digital pin 12

void setub ()
{
 pinMode (R1,OUTPUT);      //set the digital pin 10 as output
 pinMode (R2,OUTPUT);     //set the digital pin 11 as output
 pinMode (R3,OUTPUT);     //set the digital pin 12 as output

 sensors.begin ();                  //start up the DallasTemp. Library
}

void loop ();
{
 digitalWrite (R1, HIGH);       // (STEP 1) switch on the digital pin 10
 digitalWrite (R2, HIGH);      // (STEP 1) switch on the digital pin 11
 digitalWrite (R3, HIGH);      // (STEP 1) switch on the digital pin 12

 sensors.requestTemperatures ();      // (STEP 2) send the commant to get temperatures
 if (getTempC) ==> 50            // (STEP 3 this step is certainly wrong) if the Temp is equal or bigger than 50 degrees Celsius
  {
   digitalWrite (R1, LOW);       // (STEP 4) switch off the digital pin 10
  }
 if (getTempC) ==> 55            // (STEP 5) if the Temp. is equal or bigger than 55 degrees Celsius
  {
   digitalWrite (R2, LOW);      // (STEP 6) switch off the digital pin 11
  }
 if (getTempC) ==> 60          // (STEP 7) if the Temp. is equal or bigger than 60 degrees Celsius
  {
   digitalWrite (R3, LOW)      // (STEP 8) switch off the digital pin 12
  }
}                                                   //end of loop

The STEP 3,5,7 is my biggest problem because I dont know how to tell the arduino << read the temperature in Celsius and then if the temp is equal or bigger than 50 degrees Celsius, do this action>>
If you have noticed any mistakes please let me know.
Right now I dont have my Arduino so please run my sketch and let me know about the errors

Thanks for your time…

int R1= 10;                                 //R1 connected to digital pin 10
int R2= 11;                                 //R2 connected to digital pin 11
int R3= 12;                                //R3 connected to digital pin 12

void setub ()
{
 pinMode (R1,OUTPUT);      //set the digital pin 10 as output
 pinMode (R2,OUTPUT);     //set the digital pin 11 as output
 pinMode (R3,OUTPUT);     //set the digital pin 12 as output

I can’t find anything in your post that explains whar R1,R2, and R3 are ? (what are they ?)

I don’t understand why you are having a problem . All the code you need is in the Onewire Library example.
It’s full of examples that read both celsius and farenheit.
LOOK AT THE EXAMPLES IN THE ONEWIRE LIBRARY
HOW COULD YOU NOT KNOW THAT ?
DO YOU JUST INSTALL LIBRARIES WITHOUT LOOKING AT THEM ?
DOES THAT MAKE ANY SENSE ?

 #include <OneWire.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");
}

R1,R2 and R3 are ohmic (thermal) resistors (producing heating in a boiler), so the pin 10,11,12 they end up in 5V three channel relay module, the relays allow or not the strong current to thermical resistors
the DS18B20 is reading the temperature incide the boiler and open or close the resistors

my sketch is totally wrong? :frowning:

Did you look at the code I posted from the onewire example ?

Answer this question .

yes but I can't understand.. I know you want to help me and this is very easy for you.. If you could add some coments in the code maybe I will be able understand something

look at this example

#include <OneWire.h>
#include <LiquidCrystal.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
LiquidCrystal lcd(12, 11, 9, 8, 7, 6);
int backLight = 13;    // pin 13 will control the backlight
 
OneWire  ds(10);  // on pin 10 (a 4.7K resistor is necessary)

void setup(void)
{
  pinMode(backLight, OUTPUT);
  digitalWrite(backLight, HIGH); // turn backlight on. Replace 'HIGH' with 'LOW' to turn it off.
  lcd.begin(2,16);              // rows, columns.  use 2,16 for a 2x16 LCD, etc.
  lcd.clear();                  // start with a blank screen
  
  Serial.begin(115200);
}

void loop(void) 
{
  byte i;
  byte present = 0;
  byte type_s;
  byte data[12];
  byte addr[8];
  float celsius, fahrenheit;
  
  if ( !ds.search(addr))
  {
    lcd.clear();
    lcd.setCursor(0,0);           // set cursor to column 0, row 0  
    lcd.print("No more addrs");
    delay(1000);
    lcd.clear();
    lcd.setCursor(0,0);   
    Serial.println("No more addresses.");
    Serial.println();
    ds.reset_search();
    delay(250);
    return;
   }
   lcd.clear();
   lcd.setCursor(i,0); 
   lcd.print("ROM =");
   //lcd.write(' ');
   //lcd.print(addr[i], HEX);
   //lcd.setCursor(i+1,0); 
   delay(600);
   
   Serial.print("ROM =");
   
  for( i = 0; i < 8; i++)
  {
    Serial.write(' ');
    Serial.print(addr[i], HEX);
  }
    
    lcd.setCursor(0,1); 
  
    lcd.print(addr[1], HEX);
    lcd.setCursor(2,1); 
  
    lcd.print(addr[2], HEX);
    lcd.setCursor(4,1); 
 
    lcd.print(addr[3], HEX);
    lcd.setCursor(6,1); 

    lcd.print(addr[4], HEX);
    lcd.setCursor(8,1); 
 
    lcd.print(addr[5], HEX);
    lcd.setCursor(10,1); 
 
    lcd.print(addr[6], HEX);
    lcd.setCursor(12,1); 
  
    lcd.print(addr[7], HEX);
    lcd.setCursor(14,1); 
    delay(700);
    
    
    

  if (OneWire::crc8(addr, 7) != addr[7]) 
    {
      lcd.clear();
      lcd.print("CRC not valid!");
     delay(700);
      lcd.clear();
      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:
      // lcd.clear();
      // lcd.setCursor(0,0);
      // lcd.print("Chip = DS18B20 ");
      // delay(1000);
     //  lcd.clear();
     //  lcd.setCursor(0,0);   
       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();
  //   lcd.setCursor(0,0);
   //  lcd.print(data[i], HEX);
  //   lcd.print(" ");
  //    delay(2000);
    Serial.print(data[i], HEX);
    Serial.print(" ");
  }
 // lcd.setCursor(0,0);   
 // lcd.print(" CRC=");
 // lcd.print(OneWire::crc8(data, 8), HEX);
  
   delay(700);
  Serial.print(" CRC=");
  Serial.print(OneWire::crc8(data, 8), HEX);
  Serial.println();

  // Convert the data to actual temperature
  // because the result is a 16 bit signed integer, it should
  // be stored to an "int16_t" type, which is always 16 bits
  // even when compiled on a 32 bit processor.
  int16_t 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);
    // at lower res, the low bits are undefined, so let's zero them
    if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms
    else if (cfg == 0x20) raw = raw & ~3; // 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;
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("  Temperature = ");
  //lcd.setCursor(0,1);
  //lcd.print(celsius);
  //lcd.print(" Celsius,  ");
  delay(700);
  lcd.setCursor(0,1);
  lcd.print(fahrenheit);
  lcd.print(" Fahrenheit");
  delay(2000);
  lcd.clear();
  lcd.setCursor(0,0);
  Serial.print("  Temp: ");
  //Serial.print(celsius);
  //Serial.print(" Celsius,       ");
  Serial.print(fahrenheit);
  Serial.println(" Fahrenheit");
}

Look at the example above.
READ YOUR ORIGINAL POST
DOES YOUR ORIGNAL POST SAY ANYTHING AT ALL ABOUT WHAT YOU HAVE DONE, WHAT WORKS , WHAT DOESN’T WORK,
WHETHER OR NOT YOU CAN READ TEMP, WHETHER OR NOT YOUR SWITCH CODE IS WORKING ?
IN FACT DOES IT SAY ANYTHING AT ALL BESIDES “I NEED HELP?”
How are we supposed to help you if you don’t follow instructions ?

WHAT DOES THIS SAY ?

SITREP
The first thing we need is a SITREP (Mil-Speak for Situation Report)

We need to know exactly what steps you have taken toward reaching your objective.
In your case you project involves a DS18B20 temp sensor.
We need to know exactly how much you know about this sensor you have.
We need to see you post the datasheet as proof that you have seen it.
We need you to describe how you intend to connect this sensor to your arduino UNO (or have already done so).
We need to know what you have done toward getting the sensor to read data in to the arduino, specifically whether or not you have read in any temperatures. Nothing further will happen on your project until we get the SITREP.

  #include <OneWire.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");
}[code]

[/code]

I know your code is not compiling. I tested the code from the instructable you linked and it worked fine but your code has statements that do not appear in either the Onewire or the Dallas Temperature library examples. I don't know where you got your code from but it is totally bogus.

Where did you come up with this ?

 if (getTempC) ==> 55            // (STEP 5) if the Temp. is equal or bigger than 55 degrees Celsius
  {

Do you see anything like that in either of the library examples I posted ?