Temperature Sensor

From STEAMwiki
Jump to: navigation, search

This code shows how to use the temperature sensor with a 3.3V device with a 10-bit ADC such as the Particle Photon.

int sensorInput;    //The variable used to store the temperature sensor input
double temp;        //The variable used to store temperature in degrees. 
int temperature = 0; 

void setup() {
    pinMode(A2, INPUT);     //temperature readout on Arduino destop app serial monitor                              
}


void loop() { 
    sensorInput = analogRead(A2);       //read the analog TEMPERATURE sensor and store it (0-1023)
    temp = (double)sensorInput / 1024;  //find percentage of input reading
    
    temp = temp * 3.3;                  //multiply by 3.3V to get voltage
    temp = temp - 0.5;                  //Subtract the offset (sensor reads -50C to +125C)
    temp = temp * 100;                  //Convert from millivolts to degrees 
    temperature = temp;

    delay(20);
}