DS18B29 Temprature sensor digital data to integer value

Hi everyone. i was trying to create a temprature alarm system using DS18B20 sensor. the problem im having is i can read temprature value in serial monito with the code which ill mention further, but i want to convert that value to an integer so i can take some actions like alarm or turning on led on certain temprature. i dont know how to convert the "Serial.print(sensors.getTempCByIndex(0));" to a value so i would have seprate temprature integer with which i can assign funtion to it. for example
" if (Temprature > 40) {
digitalWrite(Led5, HIGH);
}
else{
digitalWrite(Led5,LOW)}

here is the code:
#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged into digital pin 2 on the Arduino
#define ONE_WIRE_BUS 2

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

// Pass oneWire reference to DallasTemperature library
DallasTemperature sensors(&oneWire);

void setup(void)
{
sensors.begin(); // Start up the library
Serial.begin(9600);
}

void loop(void)
{
// Send the command to get temperatures
sensors.requestTemperatures();

//print the temperature in Celsius
Serial.print("Temperature: ");
Serial.print(sensors.getTempCByIndex(0));
Serial.print((char)176);//shows degrees character
Serial.print("C | ");

//print the temperature in Fahrenheit
Serial.print((sensors.getTempCByIndex(0) * 9.0) / 5.0 + 32.0);
Serial.print((char)176);//shows degrees character
Serial.println("F");

delay(500);
}

Since you are currently just printing them, you just have to change the line to this:
Serial.print((sensors.getTempCByIndex(0) * 9.0) / 5.0 + 32.0,0);

But what you need to do is capture the sensor value into a variable. You can declare it as global in the beginning of your sketch if you want to do something with it the next time through or local. If only local, you would change it to this:

void loop(void)
{
// Send the command to get temperatures
sensors.requestTemperatures();

//print the temperature in Celsius
int temp = sensors.getTempCByIndex(0).toInt();
Serial.print("Temperature: ");
Serial.print(temp);
Serial.print((char)176);//shows degrees character
Serial.print("C | ");

//print the temperature in Fahrenheit
Serial.print((sensors.getTempCByIndex(0) * 9.0) / 5.0 + 32.0);
Serial.print((char)176);//shows degrees character
Serial.println("F");

if(temp....WHATEVER YOU WANT HERE)

delay(500);
}

But you don't have to get it to an integer to compare it to 40. You could leave it in it's native "float" type and not worry about converting to an integer.

1 Like
#include <OneWire.h>
#include <DallasTemperature.h>
OneWire oneWire(2); // pin 2
DallasTemperature sensors(&oneWire);
float tempC, tempF; // use these variables in your if() statements

void setup() {
  Serial.begin(9600);
  sensors.begin();
}

void loop() {
  sensors.requestTemperatures(); // get temperatures
  tempC = sensors.getTempCByIndex(0);
  tempF = sensors.getTempFByIndex(0); // note the "F" in the middle


  Serial.print("Temperature: ");
  Serial.print(tempC);
  Serial.print((char)176);
  Serial.print("C | ");
  Serial.print(tempF);
  Serial.print((char)176);
  Serial.println("F");

  delay(1000);
}
1 Like

thankyou guys for the help. one more question, is that possible if i can add two more temprature sensors to this program, basically im working on a power supply and i have to measure main MOSFET temprature and two SHOTTKEY diodes temprature.

Then you could write something like:

if(tempC > 40.0)
{
  digitalWrite(fanPin,HIGH); // fan ON
}
else if(tempC < 39.0)
{
  digitalWrite(fanPin,LOW); // fan OFF
}
1 Like

sorry i didnt get it. how would arduino know which sensor reading what data and how is it assigned to led and eventually turning the fan on.
so basically whats happening is im going to assign 3 led to 1 mosfet and two diodes respectively. so i would know which thing is running hotter. which will eventually turn the fan on

Use the OneWire address finder to get the address of the individual sensors:
one_wire_address_finder.ino (1.1 KB)
Then study this sketch:
arduino_ds18b20_temperature_sensor.ino (2.0 KB)

No need to find the serial numbers. Just print them by index.


#include <OneWire.h>
#include <DallasTemperature.h>
OneWire oneWire(2); // pin 2
DallasTemperature sensors(&oneWire);
float tempC0, tempC1, tempC2, tempF0, tempF1, tempF2; // use these variables in your if() statements

void setup() {
  Serial.begin(9600);
  sensors.begin();
}

void loop() {
  sensors.requestTemperatures(); // get temperatures

  tempC0 = sensors.getTempCByIndex(0); // sensor with lowest serial number
  tempF0 = sensors.getTempFByIndex(0); // note the "F" in the middle
  tempC1 = sensors.getTempCByIndex(1);
  tempF1 = sensors.getTempFByIndex(1);
  tempC2 = sensors.getTempCByIndex(2); // sensor with the highest serial number
  tempF2 = sensors.getTempFByIndex(2);

  Serial.print("Temperature: ");
  Serial.print(tempC0);
  Serial.print((char)176);
  Serial.print("C | ");
  // print two other sensors
  Serial.print(tempF0);
  Serial.print((char)176);
  Serial.println("F");

  delay(1000);
}
1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.