I’m trying to use LM35 and HCSR04 sensors together but as soon as I set up the HCSR04 LM35 starts calculating temperature wrongly (Room temperature is 21 C LM35 calculates 135 C).
I tested LM35 itself alone and it works fine, HCSR04 works fine with other sensors and alone but LM35 doesn’t work. There seems no short circuits or anything. Is there anyway to fix it?
Here’s the code I’m using, it is turkish but you can understand it easily and nothing seems wrong with the code but I’m putting it just in case.
const int lm35 = A0;
const int echo = 2;
const int trig = 4;
float gelen_veri;
float voltaj_deger;
float sure;
float mesafe;
float sicaklik;
float formul;
float hiz;
const int sag_i = 8;
const int sag_g = 9;
const int sol_i = 10;
const int sol_g = 11;
void setup(){
pinMode(echo, INPUT);
pinMode(trig, OUTPUT);
pinMode(sag_i, OUTPUT);
pinMode(sag_g, OUTPUT);
pinMode(sol_i, OUTPUT);
pinMode(sol_g, OUTPUT);
Serial.begin(9600);
}
void loop()
{
gelen_veri = analogRead(lm35);
voltaj_deger = (gelen_veri / 1023.0) * 5000;
sicaklik = voltaj_deger / 10.0;
Serial.println(gelen_veri);
Serial.println(voltaj_deger);
Serial.println(sicaklik);
digitalWrite(trig, HIGH);
delayMicroseconds(1000);
digitalWrite(trig, LOW);
sure = pulseIn(echo,HIGH);
formul = sqrt(1+(sicaklik/273));
mesafe = (sure/2)*formul;
hiz = mesafe*0.03;
Serial.print("mesafe = ");
Serial.println(hiz);
Serial.println (sicaklik);
Serial.print("sure = ");
Serial.println(sure);
delay(1000);
if (hiz < 15) {
digitalWrite(sag_i, HIGH);
digitalWrite(sag_g, LOW);
digitalWrite(sol_i, HIGH);
digitalWrite(sol_g, LOW);
}
else {
digitalWrite(sag_g, HIGH);
digitalWrite(sag_i, LOW);
digitalWrite(sol_i, LOW);
digitalWrite(sol_g, HIGH);
}
}