03 Jul 2012

Circuit: temperature sensor

Got some time to play with electronics again. This time I’ve had some fun with a temperature sensor. I’ve improved the tenth sample circuit from the Arduino Starter Kit.

The original circuit/program just reads and transforms the voltage output of a temperature sensor and logs the temperatures to the serial port. I’ve added a couple of leds so when the temperature goes above certain value it lights a red led. If the temperature goes bellow that value then it lights a green led.

I’ve also added a potentiometer so I can adjust the switching temperature value.

    //TMP36 Pin Variables
    int temperaturePin = 0; //the analog pin the TMP36's Vout (sense) pin is connected to
    //the resolution is 10 mV / degree centigrade
    //(500 mV offset) to make negative temperatures an option
    int potentiometerPin = 5;

    int redLedPin = 9;
    int greenLedPin = 13;
    boolean tempOk = false;

    void setup()
    {
        Serial.begin(9600);
        pinMode(redLedPin, OUTPUT);
        pinMode(greenLedPin, OUTPUT);
    }

    void loop()
    {
        float temperature = getVoltage(temperaturePin);
        temperature = (temperature - .5) * 100;

        //to degrees ((volatge - 500mV) times 100)

        float tempSwitch = getVoltage(potentiometerPin);
        tempSwitch = tempSwitch * 40 / 5;

        if(temperature>tempSwitch)
        {
            if(tempOk)
            {
                Serial.println("RED");
                tempOk = false;
                digitalWrite(redLedPin, HIGH);
                digitalWrite(greenLedPin, LOW);
            }
        }
        else
        {
            if(!tempOk)
            {
                Serial.println("GREEN");
                tempOk = true;
                digitalWrite(redLedPin, LOW);
                digitalWrite(greenLedPin, HIGH);
            }
        }

        Serial.println(temperature);
        Serial.println(tempSwitch);
        delay(1000);
    }

    /*
     * getVoltage() - returns the voltage on the analog input defined by
     * pin
     */
    float getVoltage(int pin)
    {
        //converting from a 0 to 1023 digital range
        // to 0 to 5 volts (each 1 reading equals ~ 5 millivolts
        return (analogRead(pin) * .004882814);
    }

Finally, I connected pins 0 and 1 to a serial RS232 cable to log the temperatures. This way I power the circuit with a battery instead of using the USB port.