We will need The LM35 temperature sensor.
From the datasheet by Texas Instruments:
If the temperature is -10 °C and the voltage on the GND pin of LM35 is 0 Volts, then the voltage produced on the Out pin of LM35 is -100 millivolts.
But let us consider, that the voltage provided on the GND pin of LM35 is 1 Volt, then the voltage produced on the OUT pin of LM35 is 900 millivolts.
This means that the voltage at the Out pin is shifted up by 1 Volt.
Now, the shifted positive values can be easily detected by Arduino’s ADC.
Let’s write code.:
const int lm35_pin = A0; /* LM35 O/P pin */
const int offset_pin = A1; /* LM35 GND pin */
float offset_voltage = 0; /* offset voltage stored in milli-volts */
float temp_val;
void setup() {
Serial.begin(9600);
}
void loop() {
offset_voltage = analogRead(offset_pin) * (5000 / 1024); /* read offset voltage on A1 pin and convert it to milli-volts */
temp_val = analogRead(lm35_pin) * (5000 / 1024); /* Convert adc value to equivalent voltage (milli-volts) */
temp_val = temp_val - offset_voltage; /* remove the offset voltage */
temp_val = (temp_val / 10); /* LM35 gives output of 10mv/°C */
Serial.print("Temperature = "); /* Print the temprature*/
Serial.print(temp_val);
Serial.println(" Degree Celsius");
delay(1000);
}
float temp_val = adcValue * (5000 / 1024)
: converts ADC_value to voltage (milli volts). temp_val = temp_val - offset_voltage
: removes the offset voltage, that is given to the GND pin.temp_val = (temp_val/10)
: converts into degree Celsius (1 degree Celsius = 10mV).
Negative temperature detection