Adding a temperature sensor
From SquidBee
Reading a temperature sensor
A kind of temperature sensor is NTC or Negative Temperature Coefficient that consists in a simple component that provides a variable resistance that changes with temperature, which we can read into the Arduino board as an analog value. In this example, that value controls the rate at which an LED blinks.
Material nedeed:
- Arduino board
- NTC
- 1 kohm resistor
- Breadboard
- 3 cables
- 1 led connected to pin 13 in Arduino (if your Arduino board doesn't have it already)
We connect three wires to the Arduino board. The first goes to ground from one of the pins of the NTC. The second goes from 5 volts to one of the pins of the 1 kohm resistor, the other pin of the resistor goes to the free pin in the LDR. The third goes from analog input 2 to the NTC pin that is connected to the resistor.
By changing the temperature in the NTC, we change the amount of resistance on the NTC, witch changes the voltage value, giving us a different analog input. We'll read different values between 0 (for 0V) and 1023 (for 5V).
Code
/* Temperature sensor
* ------------------
*
* turns on and off a light emitting diode(LED) connected to digital
* pin 13. The amount of time the LED will be on and off depends on
* the value obtained by analogRead() that is depending of the temperature
* in the NTC
*
*/
int NTC = 2; // select the input pin for the NTC
int ledPin = 13; // select the pin for the LED
int val = 0; // variable to store the value coming from the sensor
void setup() {
pinMode(NTC, INPUT); // declare the NTC as input
pinMode(ledPIN, OUTPUT); // declare the ledPin as an OUTPUT
}
void loop() {
val = analogRead(NTC); // read the value from the sensor
digitalWrite(ledPin, HIGH); // turn the ledPin on
delay(val); // stop the program for some time
digitalWrite(ledPin, LOW); // turn the ledPin off
delay(val); // stop the program for some time
}



