community! I have a problem as follows. I've been trying to interface the LM19 temperature sensor using Arduino. I made a function to read and compute the temperature from the sensor according to a formula described in its datasheet. The result was pretty good. However, when I added another function for reading ADC value from an LDR, the result of the temperature computation went crazy!!! Here's my code:
/*
Author: M. Chenda
Created on Tue Feb 18, 2020
*/
#define ldr 0 // assign LDR's pin to analog pin A0
#define light 8 // assign LED's pin to digital pin 8
#define lm19 2 // assign LM19's pin to analog pin A2
int ldr_val = 0; // LDR's ADC reading variable
String state; // string representing light's state
int lm19_val = 0; // LM19's ADC reading variable
float Vo = 0.00; // V output of LM19
float temp = 0.00; // temperature variable
void setup() {
pinMode(light, OUTPUT);
Serial.begin(9600);
while(!Serial) {;} // wait for serial port connection
}
void loop() {
lighting();
temp_control();
delay(500);
}
/* LIGHT CONTROL */
void lighting() {
ldr_val = analogRead(ldr); // read LDR's ADC value (brightness)
if (ldr_val < 512) {
digitalWrite(light, LOW);
state = "off";
}
else {
digitalWrite(light, HIGH);
state = "on";
}
Serial.print("LDR's ADC: ");
Serial.println(ldr_val);
}
/* VALUE MAPPING FUNCTION */
float mapping(float x, float in_min, float in_max, float out_min, float out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
/* TEMPERATURE MEASUREMENT */
void temp_control() {
// read LM19's ADC value
lm19_val = analogRead(lm19);
// convert the ADC value to voltage (0 to 5V)
Vo = mapping(lm19_val, 0, 1023, 0, 5);
// compute temperature value in Celsius
temp = -1481.96 + sqrt((2.1962*pow(10,6))+((1.8639-Vo)/(3.88*pow(10,-6))));
Serial.print(" Temperature (Celsius): ");
Serial.println(temp); // print the measured temperature to Serial Terminal
}
lighting()
function in theloop()
, I received random values oftemp
fromtemp_control()
function. And yes, when I commented out thelighting()
function, my temperature computation function worked correctly again. \$\endgroup\$