Definition

pid·dle [pid-l] verb, pid·dled, pid·dling.

To spend time in a wasteful, trifling, or ineffective way; dawdle (often followed by around ): He wasted the day piddling around.

Sunday, December 2, 2012

Arduino Based Temperature Sensor

Overview

I mentioned quite some time ago that I had ordered an Arduino UNO to play around with.  From looking at the left over parts I had laying around I got the idea to throw together a quick and dirty temperature monitor using a couple seven segment displays and a thermistor.  After many months I finally got around to actually completing this and documenting it.  One of the things I wanted to do, even thought I didn't really have to, was document the project using Fritzing.  In an earlier post I showed how to make a custom seven segment display so I continued using Fritzing to complete the breadboard layout and schematic.  

After reading through this project, my hope is that you will get a better understanding of how to:
  • Control multiple LEDs with a minimum number of micro-controller pins
  • Take a multiple digit number and split it into individual digits for display
  • Display two different numbers using the same control lines

Reference Material

Before reading the rest of this post, I would recommend that you jump over to the following links to gather some background information.  My program uses ideas and methods that are found in the following tutorials.

Hardware

This temperature sensor is made with some fairly simple components but provides a nice example of monitoring a simple input and displaying the appropriate output on a simple LED display.  With the exception of the thermistor, most of these parts are readily available at your local Radio Shack.

  • 2 Common Cathode 7 Segment Displays
  • 2 NPN Transistors
  • 7 Resistors (I used 220 Ohm - doesn't have to be exact)
  • 1 Resistor (10 K Ohm for thermistor voltage divider circuit)
  • 1 NTC Thermistor
This project was built up and prototyped on a breadboard.  Just for documentation purposes I built up a model of it in Fritzing.  A screenshot of the board is shown below.  I tried to use different color wires to denote the different functions of the signals.  The yellow wires are the digit select lines, the green wires are to drive the segments of the display, and the gray wire is the thermistor input.


A schematic of the design is shown below for reference as well.

Software

Most of the software is heavily re-used from the tutorials I linked to at the beginning of the posting.  However, the main loop is a little bit unique.  The idea behind this program is that it will read the thermistor, determine the temperature, and then display it on the two 7 segment displays.  One thing you may notice from the hardware section is the each display shares the same anode connections at the Arduino.  The benefit in this type of arrangement is that you only need one incremental pin to drive a new 7 segment display.  The downside is that only one display can be active at a time since the individual LED activated will be present on all displays.  So if you want to show a number, such as 19 on the two displays the ideas is that you set the LEDs to show a 1, then activate the first display for a few milliseconds.  Next you turn off the first display, set the LEDs to display a 9 and then activate the second display for a few milliseconds.  As long as you repeat this procedure quickly enough the human eye will not perceive a blink and it will look like a smooth, consistent digit display.

The full listing is shown below for reference.


#include <math.h>

void setup() 
{
  pinMode(10, OUTPUT);  // Segment C, Pin 7
  pinMode(9, OUTPUT);  // Segment G, Pin 1
  pinMode(8, OUTPUT);  // Segment B, Pin 9
  pinMode(7, OUTPUT);  // Segment D, Pin 5
  pinMode(6, OUTPUT);  // Segment F, Pin 2
  pinMode(5, OUTPUT);  // Segment A, Pin 10
  pinMode(4, OUTPUT);  // Segment E, Pin 4
  pinMode(3, OUTPUT);  // Display 2 
  pinMode(2, OUTPUT);  // Display 1

}

void Zero() // A, B, C, D, E, F
{
  digitalWrite(5, HIGH);  // Segment A
  digitalWrite(8, HIGH);  // Segment B
  digitalWrite(10, HIGH);  // Segment C
  digitalWrite(7, HIGH);  // Segment D
  digitalWrite(4, HIGH);  // Segment E
  digitalWrite(6, HIGH);  // Segment F
  digitalWrite(9, LOW);  // Segment G
}

void One()  // B, C
{
  digitalWrite(8, HIGH);  // Segment B
  digitalWrite(10, HIGH);  // Segment C
  digitalWrite(5, LOW);  // Segment A
  digitalWrite(7, LOW);  // Segment D
  digitalWrite(4, LOW);  // Segment E
  digitalWrite(6, LOW);  // Segment F
  digitalWrite(9, LOW);  // Segment G
}

void Two() // A, B, D, E, G
{
  digitalWrite(5, HIGH);  // Segment A
  digitalWrite(8, HIGH);  // Segment B
  digitalWrite(7, HIGH);  // Segment D
  digitalWrite(4, HIGH);  // Segment E
  digitalWrite(9, HIGH);  // Segment G
  digitalWrite(10, LOW);  // Segment C
  digitalWrite(6, LOW);  // Segment F
}

void Three() // A, B, C, D, G
{
  digitalWrite(5, HIGH);  // Segment A
  digitalWrite(8, HIGH);  // Segment B
  digitalWrite(10, HIGH);  // Segment C
  digitalWrite(7, HIGH);  // Segment D
  digitalWrite(9, HIGH);  // Segment G  
  digitalWrite(4, LOW);  // Segment E
  digitalWrite(6, LOW);  // Segment F
}

void Four() // B, C, F, G
{
  digitalWrite(8, HIGH);  // Segment B
  digitalWrite(10, HIGH);  // Segment C
  digitalWrite(6, HIGH);  // Segment F
  digitalWrite(9, HIGH);  // Segment G
  digitalWrite(5, LOW);  // Segment A
  digitalWrite(7, LOW);  // Segment D
  digitalWrite(4, LOW);  // Segment E
}

void Five() // A, C, D, F, G
{
  digitalWrite(5, HIGH);  // Segment A
  digitalWrite(10, HIGH);  // Segment C
  digitalWrite(7, HIGH);  // Segment D
  digitalWrite(6, HIGH);  // Segment F
  digitalWrite(9, HIGH);  // Segment G
  digitalWrite(8, LOW);  // Segment B
  digitalWrite(4, LOW);  // Segment E
}

void Six() // A, C, D, E, F, G
{
  digitalWrite(5, HIGH);  // Segment A
  digitalWrite(10, HIGH);  // Segment C
  digitalWrite(7, HIGH);  // Segment D
  digitalWrite(4, HIGH);  // Segment E
  digitalWrite(6, HIGH);  // Segment F
  digitalWrite(9, HIGH);  // Segment G
  digitalWrite(8, LOW);  // Segment B
}

void Seven() // A, B, C
{
  digitalWrite(5, HIGH);  // Segment A
  digitalWrite(8, HIGH);  // Segment B
  digitalWrite(10, HIGH);  // Segment C
  digitalWrite(7, LOW);  // Segment D
  digitalWrite(4, LOW);  // Segment E
  digitalWrite(6, LOW);  // Segment F
  digitalWrite(9, LOW);  // Segment G
}

void Eight() // A, B, C, D, E, F, G
{
  digitalWrite(5, HIGH);  // Segment A
  digitalWrite(8, HIGH);  // Segment B
  digitalWrite(10, HIGH);  // Segment C
  digitalWrite(7, HIGH);  // Segment D
  digitalWrite(4, HIGH);  // Segment E
  digitalWrite(6, HIGH);  // Segment F
  digitalWrite(9, HIGH);  // Segment G
}

void Nine() // A, B, C, F, G
{
  digitalWrite(5, HIGH);  // Segment A
  digitalWrite(8, HIGH);  // Segment B
  digitalWrite(10, HIGH);  // Segment C
  digitalWrite(6, HIGH);  // Segment F
  digitalWrite(9, HIGH);  // Segment G
  digitalWrite(7, LOW);  // Segment D
  digitalWrite(4, LOW);  // Segment E
}

void Blank()
{
  digitalWrite(10, LOW);  // Segment C
  digitalWrite(9, LOW);  // Segment G
  digitalWrite(8, LOW);  // Segment B
  digitalWrite(7, LOW);  // Segment D
  digitalWrite(6, LOW);  // Segment F
  digitalWrite(5, LOW);  // Segment A
  digitalWrite(4, LOW);  // Segment E
}

void displayDigit(int digit)
{
  if (digit == 0){
    Zero();
  }
  else if (digit == 1){
    One();
  }
  else if (digit == 2){
    Two();
  }
  else if (digit == 3){
    Three();
  }
  else if (digit == 4){
    Four();
  }
  else if (digit == 5){
    Five();
  }
  else if (digit == 6){
    Six();
  }
  else if (digit == 7){
    Seven();
  }
  else if (digit == 8){
    Eight();
  }
  else if (digit == 9){
    Nine();
  }
  else{
    Blank();
  }
}

double Thermistor(int RawADC){
  double Temp;
  // See http://en.wikipedia.org/wiki/Thermistor for explanation of formula
  Temp = log(((10240000/RawADC) - 10000));
  Temp = 1 / (0.001129148 + (0.000234125 * Temp) + (0.0000000876741 * Temp * Temp * Temp));
  Temp = Temp - 273.15; // Convert Kelvin to Celcius
  return Temp;
}

void loop()
{
  int count = 0;
  int displayLoop = 25;
  int digOne;
  int digTwo;
  int ThermistorInput = A0;
  int ThermistorValue = 0;
  double fTemp; // Temp in degrees Fahrenheit
  double temp;

  while(count < 10){
    // Read thermistor and determine temperature in degrees F
    temp = Thermistor(analogRead(ThermistorInput)); // Read thermistor value
    fTemp = (temp * 1.8) + 32 - 7; // Convert degrees C to F
    
    // Split two digit fTemp variable into individual digits to display
    digOne = fTemp / 10;  // Determine what to display for digit # 1
    digTwo = fTemp - digOne*10;  // Determine what to display for digit # 2

    // Alternate between displaying each digit for 10 mS 
    while (displayLoop > 0){
      digitalWrite(3, LOW);
      digitalWrite(2, HIGH);
      displayDigit(digOne);
      delay(10);

      digitalWrite(3, HIGH);
      digitalWrite(2, LOW);
      displayDigit(digTwo);

      delay(10);
      displayLoop --;
    }
    displayLoop = 250;
  }
  count = 0;  // Reset counter */
}