Heat meter - Arduino
- alejandro escobar
- Nov 6, 2016
- 1 min read

This is another basic exercise from the Arduino starter kit. In this case, I am using a temperature sensor to measure room temperature. As the temperature rises (by touching the sensor), the lights are turned on.
This is the code:
/*We set two constants, one with the name of the analog pin and
another for the ambient temperature
*/
const int sensorPin =A0;
/* to adjust the right temperature, you should check the initial
value that the micropocessor sends back in the variable volts
*/
const float tempAmbient = 22.0;
void setup() {
Serial.begin(9600);
//with this loop, we set ports 2 to 4 to work as output and make shure the lights are off
for(int pinNumber = 2; pinNumber<5; pinNumber ++){
pinMode(pinNumber, OUTPUT);
digitalWrite(pinNumber,LOW);
}
}
void loop() {
/*with this loop we constantly check and print the values returned from the temperature
sensor, the volts (with a little math) on that pin and the temperature.
*/
int sensorVal = analogRead(sensorPin);
Serial.print("Value of the sensor: ");
Serial.print(sensorVal);
float volts = (sensorVal/1024.0)*5;
Serial.print(", Volts: ");
Serial.print(volts);
Serial.print(", Degrees C: ");
/*the values used for this equation are particular to the sensor provided in the kit,
this is how whe calculate the temperature.
*/
float temperature = (volts- .5)*100;
Serial.println(temperature);
//this conditional structure allows to determine mow many lights need to be turned on
if(temperature < tempAmbient){
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
}
else if(temperature >= tempAmbient+1 && temperature < tempAmbient+2){
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
}
else if(temperature >= tempAmbient+2 && temperature < tempAmbient+3){
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
}
else if(temperature >= tempAmbient+3){
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
}
delay(1);
}


Comments