Problem with DS18B20 with arduino mega 2560

hey i wish that ur all fine ( i m sorry for some mistakes because i dont have a good english);
i wish that u gonna hepl me to solve this problem;
in my project i have to commande the DS18B20 by ARDUINO MEGA 2560 and if if the Temperature > Tempmax = 26 than the LED-Green ( i called in the programme Vent) is high and if the Temperature < Tempmin = 10 than the LED-Red ( i called in the programme Resi) is high .
But that problem is when i do the project in ISIS (Proteus ) that the Led-Green(i called Vent) is always high and the Led-Red ( i calledd Resi) is high just in the case that the temperature is negative.

This is the programme :

#include<OneWire.h> // librairie du capteur DS18B20

// declaration
float Tempmax = 26;
float Tempmin = 10;
int Resi = 3; //LED qui represente la resistance au pin3
int Ven = 4; //LED qui represente le ventilateur au pin4

int HighByte, LowByte, TReading, Temperature;

OneWire ds(10);

void setup() {
Serial.begin(9600);
pinMode(Resi,OUTPUT);
pinMode(Ven,OUTPUT);

}

void loop() {
byte i;
byte present = 0;
byte data[12];
byte addr[8];

if ( !ds.search(addr)) {
Serial.print("No more addresses.\n");
ds.reset_search();
return;
}

Serial.print("R=");
for( i = 0; i < 8; i++) {
Serial.print(addr*, HEX);*

  • Serial.print(" ");*
  • }*
  • if ( OneWire::crc8( addr, 7) != addr[7]) {*
  • Serial.print("CRC is not valid!\n");*
  • return;*
  • }*
  • if ( addr[0] == 0x10) {*
  • Serial.print("Device is a DS18S20 family device.\n");*
  • }*
  • else if ( addr[0] == 0x28) {*
  • Serial.print("Device is a DS18B20 family device.\n");*
  • }*
  • else {*
  • Serial.print("Device family is not recognized: 0x");*
  • Serial.println(addr[0],HEX);*
  • return;*
  • }*
  • ds.reset();*
  • ds.select(addr);*
  • ds.write(0x44,1); *
  • delay(1000); *
  • present = ds.reset();*
  • ds.select(addr); *
  • ds.write(0xBE); *
  • Serial.print("P=");*
  • Serial.print(present,HEX);*
  • Serial.print(" ");*
  • for ( i = 0; i < 9; i++) { // on a besoin de 9 bit*
    _ data = ds.read();_
    _ Serial.print(data*, HEX);
    Serial.print(" ");
    }
    Serial.print(" CRC=");
    Serial.print( OneWire::crc8( data, 8 ), HEX);
    Serial.println();*_

//Convertir la temperature en degres Celsius

* LowByte = data[0];*
* HighByte = data[1];*
* TReading = (HighByte << 8 ) + LowByte;*
_ Temperature = (6 * TReading) + TReading / 4; // La valeur de temperature_
* Serial.print(" Temperature");*
* Serial.print(Temperature);*

* //Comparer la Temperature au Tempmin et Tempmax*

* if( Temperature < Tempmin ){*
* digitalWrite(Resi,HIGH);*
* delay(1000);*
* digitalWrite(Resi,LOW); *
* }*
* else if( Temperature > Tempmax ){*
* digitalWrite(Ven,HIGH);*
* delay(1000);*
* digitalWrite(Ven,LOW); *
* }*

* delay(5000);*
}
This is the schema of the project in ISIS :

amoultii:
i dont have a good english......
i wish that u gonna hepl me to solve this problem;

Your English is OK although it might be better to refrain from using "gonna" which is not English. I recognise that the president of the United States uses it, but he is American, and therefore excused.

You picture shows incorrect wiring for the DS18B20. The Vcc pin should be connected to 5v and the signal pin to 5v via the 4k7 resistor which serves as a pull-up. At the moment , both pins are connected via the 4k7. I don't know if it is going to fix your problem, but it will certainly help.

I don't use that code, but no comment on it other than that

The DS18B20 normally returns degrees C

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

and 2.
It seems unnecessarily long. This tutorial actually involves two programmes but I think it might be simpler

EDIT

I have just realised that your code looks strange because it does not use the DallasTemperature library. I don't believe that explains your temperature conversion exercise. I suggest it would be a good idea to use the proper library, and the above link addresses that.

Nick_Pyner:

thanks for ur help :slight_smile: