void setup()
{
delay(1000);
Serial.begin(115200);
Serial.println("Misura la temperatura con sensore DS18B20");
delay(1000);
sensors.begin();
}
void loop()
{
Serial.println();
sensors.requestTemperatures();
Serial.print("Temperature = ");
Serial.print(sensors.getTempCByIndex(0));
Serial.println(" C degrees");
delay(100);
}
but I constantly see on the screen "-127 C degrees". I connect the sensor to GND, 5V and Pin 11. I tried two differents sensors and different Pins, so I guess the only problem could be in the code.
Hope someone can help, thank you
Welcome to the Arduino forum. I suspect you did not try the example in the IDE, nor did you read the documentation on the temperature sensor.
This is code from my drying oven control program. Can you see the difference?
void check_current_Temp() {
sensors.requestTemperatures(); // Send the command to get temperatures
current_Temp = sensors.getTempCByIndex(0);
running_Temp = current_Temp; // use only the integer part of returned temperature value
if (running_Temp >= max_Temperature) { // have we met or exceeded the max temperature limit?
turnOven_off(); // yes!
temp_Emergency = true;
long_beep = true;
reset_the_Parameters();
show_Operator_message(5);
}
Can you please read the How to get the best out of this forum. Try to modify your original post to use code tags. It should look like the code from Paul.
I recommend you get trough some of the examples that come with the IDE, especially look at the BlinkWithoutDelay
This will be the best thing you can learn about Arduino now. The use of delay is going to bring you nothing but trouble soon.
Additionally, I recommend you avoid calling function inside other function calls for now. e.g.
Serial.print(sensors.getTempCByIndex(0));
You do not know what data type is returned by the get function and what data type the print function can take. In the long run, understanding what is going on will help you a lot.
Nick_Pyner:
You might also check that your DS18B20 is kosher. There are a lot of fakes about and, if it looks like a transistor, it may indeed be one.
I bought 10 or more of the DS18b20's off ebay. Instead of being £5 each, they were £1 each, so I knew what I was letting myself in for. Surprisingly, they worked fine in most of my projects. However, in some projects, I initially had problems with the temperature readings and thought it was something to do with them being fakes.
I downloaded a sketch from the following Chris Petrich github page:-
The Chris Petrich sketch is great. He has done a lot of research and testing with the ds18b20s. The sketch reports if a sensor is a clone or not, and there is a lot of info about the devices. The sketch did in fact report that all my ebay sensors were clones/fakes, and that the one expensive RS Components device I had was kosher.
In my case, it turned out that I had wiring and interference problems in the problem projects, and the fake clone sensors were in fact working ok.
Steve