# Digital Thermometer with A seven-Segment Display

## Introduction

For this project, we will be learning how to set up our own digital thermometer and displaying the temperature on a seven-segment display. For more information about the seven segment display, feel free to refer to the corresponding link on the right.

##### Before you begin, you should:
• Know how to use a seven-segment display.
##### After you're done, you should:
• Be able to show off your thermometer.

## Inventory:

Qty Description Typical Image Schematic Symbol Breadboard Image
1 seven-segment Display
1 NTC Thermistor
1 10k Ohm Resistor
4 220 Ohm Resistor

## Basic Theory

The thermometer that we'll be using is a component called a thermistor. A thermistor is a type of resistor that changes resistance with changes in temperature. This is in contrast to a “normal” resistor which maintains a constant resistance at a large range of temperatures. We will be able to detect this change in voltage across the thermistor and determine the temperature by using an equation that relates resistance to temperature.

Once we have determined the temperature, we will then be able to display the temperature on either a seven-segment display or an LCD screen. We will also decide how often the displayed temperature should be updated.

## Step 1: Wiring

### Wiring of the 10 kΩ Thermistor:

• Place the thermistor somewhere on the breadboard. Using a 10 kΩ resistor, connect one of the leads to the power bus strip.
• Connect the power bus to the 3.3V supply on the chipKIT™ Uno32™ board which is labeled as “3V3”.
• Attach the other lead of the thermistor to the negative bus on the breadboard. Connect this bus to either one of the ground pins on the chipKIT Uno32, which are both labeled as “GND”.
• Now, run a wire from the leg of the thermistor that has the 10 kΩ resistor to pin A0 on the chipKIT Uno32.

### Wiring of the Seven-Segment Display:

• Place the seven-segment display on the breadboard.
• Now make the following connections:
1. Attach a 220Ω resistor to pin 12 of the seven-segment display. Then wire the other end of the resistor to pin 34 on the chipKIT Uno32
2. Attach a 220Ω resistor to pin 9 of the seven-segment display. Then wire the other end of the resistor to pin 35 on the chipKIT Uno32
3. Attach a 220Ω resistor to pin 8 of the seven-segment display. Then wire the other end of the resistor to pin 36 on the chipKIT Uno32
4. Attach a 220Ω resistor to pin 6 of the seven-segment display. Then wire the other end of the resistor to pin 37 on the chipKIT Uno32.
5. Create a connection between pin 11 of the seven-segment display to pin 30 on the chipKIT Uno32.
6. Create a connection between pin 7 of the seven-segment display to pin 2 on the chipKIT Uno32.
7. Create a connection between pin 4 of the seven-segment display to pin 4 on the chipKIT Uno32.
8. Create a connection between pin 2 of the seven-segment display to pin 6 on the chipKIT Uno32.
9. Create a connection between pin 1 of the seven-segment display to pin 7 on the chipKIT Uno32.
10. Create a connection between pin 10 of the seven-segment display to pin 29 on the chipKIT Uno32.
11. Create a connection between pin 5 of the seven-segment display to pin 3 on the chipKIT Uno32.
12. Create a connection between pin 3 of the seven-segment display to pin 5 on the chipKIT Uno32.

For a refresher on how the pins internally interact in the seven-segment, check out the link on seven-segment displays located at the top of the page.

Althogether, the final setup of our circuit should look something like this:

## Determining the Temperature

As we mentioned earlier, we cannot measure the temperature directly from the thermistor. Instead, we will determine the temperature by reading the voltage and correlating this to the correct temperature. This will take multiple steps to do. The analog pin that the thermistor is connected to, A0, does not directly record the change of resistance in the thermistor. Rather, the analog-to-digital (ADC) value that is somewhere between a value of 0 and a value of 1023 will change because the resistance, and thus the output voltage, changes.

What we will do is determine what the resistance (R) of the thermistor based on the ADC value that we receive, mathematically.

The output voltage of the thermistor (V0) is found by:

$\Large V_{0} = \frac{R}{R + 10k\Omega} \times V_{cc}$

Where Vcc is the power supply voltage, which in our case happens to be 3.3V, and the 10 kΩ is the resistor between the breadboard power supply and the thermistor.

The ADC value reported by the chipKIT Uno32 is found by:

$\Large ADC = \frac{V_{i} \times 1023}{V_{cc}}$

Where Vi is the input voltage to the chipKIT Uno32.

Since the output voltage of the thermistor is the input voltage to the chipKIT Uno32, we can substitute the V0 equation into Vi in the ADC equation and see that:

$\Large ADC = \frac{R}{R + 10k\Omega} \times 1023$

You will notice that the power supply voltage has been nicely cancelled from the equation so we don't have to worry about it. Additionally, with this equation we will be able to figure out the resistance of the thermistor and (eventually) the temperature!

We will rearrange our ADC equation for the resistance of the thermistor, R.

$\Large \frac{ADC}{1023} = \frac{R}{R + 10k\Omega}$

$\Large\frac{R \times ADC}{1023} + \frac{10k\Omega \times ADC}{1023} = R$

$\Large \frac{10k\Omega \times ADC}{1023} = R \times (1 - \frac{ADC}{1023})$

$\Large R = \frac{10k\Omega \times ADC}{1023 - ADC}$

This equation can also be rearranged into a form more commonly known as a voltage divider. For more information on voltage dividers, please see the link on the right.

$\Large R = \frac{10k\Omega}{\frac{1023}{ADC} - 1}$

We now have the equation for the corresponding resistance of the thermistor based on the ADC value that the chipKIT Uno32 board reports to us. Naturally, this is not the temperature; we have to utilize another equation to be able to determine the temperature. This equation is known as the B parameter equation, which is a modified (and simplified) version of a more robust temperature equation known as the Steinhart-Hart equation and can be used for a Negative Temperature Coefficient (NTC) thermistor such as ours. The B parameter equation is as follows:

$\Large \frac{1}{T} = \frac{1}{T_{0}} + \frac{1}{B} \times \ln{\frac{R}{R_{0}}}$

Where T is the temperature in Kelvin, T0 is the standard room temperature (presumed to be at 298K or 25C), B is the B parameter which is 4100 for our particular thermistor, R is the resistance of the thermistor at various temperatures, and R0 is the resistance of the thermistor at room temperature. For our thermistor this is 10 kΩ.

Naturally, we want to rearrange this equation for temperature instead of the inverse temperature. We can do this by multiplying both sides by T0*4100 and then isolating the variable T to get the equation:

$\Large T = \frac{T_{0} \times 4100}{4100 + T_{0} \times ln{\frac{R}{R_{0}}}}$

We finally have our equation for our temperature! This temperature is still in the Kelvin scale though, but we will correct this to the Fahrenheit (or Celsius) temperature scale inside our mpide code.

## Step 2: Write some code

Now we get to write our code to make our digital thermometer actually work. When we worked with the seven-segment display, what we did was display a continually increasing variable (time). In this project, we will be getting input from part of our system (the thermistor), analyzing the input (converting the thermistor data to temperature), and then displaying this information on the seven-segment display.

//Note: much of the code related to the seven-segment display is
//adapted from the project “Seven-Segment Display”

//globally defining the Anode pins for the 7-segment display
const int anode1=34;
const int anode2=35;
const int anode3=36;
const int anode4=37;

//globally defining the Cathode pins for the 7 segment display
const int A=30;//Section 'A' of the digit
const int B=2;//Section 'B' of the digit
const int C=4;//Section 'C' of the digit
const int D=6;//Section 'D' of the digit
const int E=7;//Section 'E' of the digit
const int F=29;//Section 'F' of the digit
const int G=3;//Section 'G' of the digit
const int DP=5;//Decimal point of the digit

void setup() {
Serial.begin(9600);

//Setting up the pins of our seven-segment display to respond
//our inputs we give them
pinMode(anode1, OUTPUT);
pinMode(anode2, OUTPUT);
pinMode(anode3, OUTPUT);
pinMode(anode4, OUTPUT);
pinMode(A,OUTPUT);
pinMode(B,OUTPUT);
pinMode(C,OUTPUT);
pinMode(D,OUTPUT);
pinMode(E,OUTPUT);
pinMode(F,OUTPUT);
pinMode(G,OUTPUT);
pinMode(DP,OUTPUT);

*	each digit in the seven-segment on HIGH (i.e., “off”)
*	and then depending on what “value” variable
*	(one of the numbers in the temperature we calculate)
*	we will set the segment to LOW so that that particular
*	section is "on".
*/
digitalWrite(A,HIGH);
digitalWrite(B,HIGH);
digitalWrite(C,HIGH);
digitalWrite(D,HIGH);
digitalWrite(E,HIGH);
digitalWrite(F,HIGH);
digitalWrite(G,HIGH);
digitalWrite(DP,HIGH);
}//end of setup

//Creating a function to display the temperature on the 7 segment
void decodeAndWrite(unsigned char value, unsigned int digit){

//Telling the seven-segment how to switch between digits
switch(digit){
//for our anode pins which control the digits, the opposite
//is true: HIGH means “on” and LOW means “off”
//Remember that DP is one of the cathodes
case 1:
digitalWrite(anode1,HIGH);
digitalWrite(anode2,LOW);
digitalWrite(anode3,LOW);
digitalWrite(anode4,LOW);
digitalWrite(DP,HIGH);
break;

case 2: //Turns “on” the decimal point
digitalWrite(anode1,LOW);
digitalWrite(anode2,HIGH);
digitalWrite(anode3,LOW);
digitalWrite(anode4,LOW);
digitalWrite(DP,LOW);
break;

case 3:
digitalWrite(anode1,LOW);
digitalWrite(anode2,LOW);
digitalWrite(anode3,HIGH);
digitalWrite(anode4,LOW);
digitalWrite(DP,HIGH);
break;

case 4:
digitalWrite(anode1,LOW);
digitalWrite(anode2,LOW);
digitalWrite(anode3,LOW);
digitalWrite(anode4,HIGH);
digitalWrite(DP,HIGH);
break;

default:
digitalWrite(anode1,LOW);
digitalWrite(anode2,LOW);
digitalWrite(anode3,LOW);
digitalWrite(anode4,LOW);
digitalWrite(DP,HIGH);
break;

}//end of switching between digits

//Telling the seven-segment how to display each number
switch(value){

case 0:
digitalWrite(A,LOW);
digitalWrite(F,LOW);
digitalWrite(E,LOW);
digitalWrite(D,LOW);
digitalWrite(C,LOW);
digitalWrite(B,LOW);
digitalWrite(G,HIGH);
break;

case 1:
digitalWrite(C,LOW);
digitalWrite(B,LOW);
digitalWrite(A,HIGH);
digitalWrite(D,HIGH);
digitalWrite(E,HIGH);
digitalWrite(F,HIGH);
digitalWrite(G,HIGH);
break;

case 2:
digitalWrite(A,LOW);
digitalWrite(G,LOW);
digitalWrite(E,LOW);
digitalWrite(D,LOW);
digitalWrite(B,LOW);
digitalWrite(C,HIGH);
digitalWrite(F,HIGH);
break;

case 3:
digitalWrite(C,LOW);
digitalWrite(B,LOW);
digitalWrite(A,LOW);
digitalWrite(G,LOW);
digitalWrite(D,LOW);
digitalWrite(E,HIGH);
digitalWrite(F,HIGH);
break;

case 4:
digitalWrite(F,LOW);
digitalWrite(B,LOW);
digitalWrite(G,LOW);
digitalWrite(C,LOW);
digitalWrite(A,HIGH);
digitalWrite(D,HIGH);
digitalWrite(E,HIGH);
break;

case 5:
digitalWrite(A,LOW);
digitalWrite(F,LOW);
digitalWrite(G,LOW);
digitalWrite(C,LOW);
digitalWrite(D,LOW);
digitalWrite(B,HIGH);
digitalWrite(E,HIGH);
break;

case 6:
digitalWrite(C,LOW);
digitalWrite(G,LOW);
digitalWrite(E,LOW);
digitalWrite(D,LOW);
digitalWrite(F,LOW);
digitalWrite(A,HIGH);
digitalWrite(B,HIGH);
break;

case 7:
digitalWrite(A,LOW);
digitalWrite(B,LOW);
digitalWrite(C,LOW);
digitalWrite(D,HIGH);
digitalWrite(E,HIGH);
digitalWrite(F,HIGH);
digitalWrite(G,HIGH);
break;

case 8:
digitalWrite(A,LOW);
digitalWrite(F,LOW);
digitalWrite(B,LOW);
digitalWrite(G,LOW);
digitalWrite(C,LOW);
digitalWrite(D,LOW);
digitalWrite(E,LOW);
break;

case 9:
digitalWrite(A,LOW);
digitalWrite(F,LOW);
digitalWrite(B,LOW);
digitalWrite(G,LOW);
digitalWrite(C,LOW);
digitalWrite(D,HIGH);
digitalWrite(E,HIGH);
break;

case 'F':
digitalWrite(A,LOW);
digitalWrite(F,LOW);
digitalWrite(E,LOW);
digitalWrite(G,LOW);
digitalWrite(B,HIGH);
digitalWrite(C,HIGH);
digitalWrite(D,HIGH);
break;

default:
// do not have anything be lit up for the default case
break;
}//end of displaying of various characters
}//end of decodeAndWrite function

void loop() {
//defining necessary variables for our data collection
//and subsequent calculations
int i=0;
float thermResistance=0.0;
float temperatureK=0.0;
float temperatureF=0.0;
float temperatureC=0.0;
int hundredsPlace=0;
int tensPlace=0;
int onesPlace=0;
int tenthsPlace=0;
int segment=1;
char temp;

//collecting a total sum of thermistor resistance values
//to use in an average
for(i=0; i<3; ++i) {
}

//calculating the resistance

//calculating the temperatures in various units
temperatureK= 298*4100/(4110.0+298.0*log(thermResistance/10000.0));
temp='F';
temperatureF= (temperatureK-273.0)*9.0/5.0+32.0;

//removing the hundreds place since we don't have room for it
hundredsPlace= temperatureF/100;
temperatureF= temperatureF-hundredsPlace;

/* Assigning the temperature we have to variables to show on
*  the seven-segment display and removing each subsequent
*  place holding
*/

//calculating the “place”
tensPlace= temperatureF/10;
//stating the digit on the 7 segment
segment=1;
//call to the function we defined eariler
decodeAndWrite(tensPlace, segment);
//a brief pause so that we can see the number
delay(3);
//removing the tens place
temperatureF= temperatureF-tensPlace*10;
//repeating for the other “places”
onesPlace= temperatureF;
segment=2;
decodeAndWrite(onesPlace, segment);
delay(3);
temperatureF= temperatureF-onesPlace;
tenthsPlace= temperatureF*10;
segment=3;
decodeAndWrite(tenthsPlace, segment);
delay(3);
segment=4;
decodeAndWrite(temp, segment);

}//end of loop