This Arduino project shows how to read temperature from LM335 analog temperature sensor and print its value on a 3-digit seven-segment display.
The LM335 sensor is a 3-pin analog device which can measure temperature (converts temperature to analog voltage). This sensor requires an ADC module to convert the analog voltage into digital data. Arduino UNO microcontroller (ATmega328P) has one ADC module with 10-bit resolution.
The LM335 sensor has the following features (from LM335 datasheet):
- Directly Calibrated to the Kelvin Temperature Scale
- 1°C Initial Accuracy Available
- Operates from 400 μA to 5 mA
- Less than 1-Ω Dynamic Impedance
- Easily Calibrated
- Wide Operating Temperature Range
- 200°C Overrange
- Low Cost
The LM335 has a breakdown voltage directly proportional to absolute temperature at 10 mV/°K. For example if the LM335 output voltage is equal to 3.03 (3030 mV) that means the temperature is: 303 Kelvin = 30 °Celsius.
To see how to interface Arduino with 7-segment display visit the following post:
Interfacing Arduino with 7-segment display | 4-Digit counter example
Parts Required:
- Arduino UNO board
- 3-Digit (or 4-digit) common anode 7-segment display
- LM335 temperature sensor —-> datasheet
- 3 x PNP transistor (2SA1015, 2S9015, 2N3906 …)
- 7 x 100 ohm resistor
- 3 x 4.7k ohm resistor
- 2.2k ohm resistor
- Breadboard
- Jumper wires
Arduino with LM335 sensor and 7-segment display circuit:
Project circuit schematic diagram is shown below.
The LM335 sensor has 3 pins (from left to right):
Pin 1 for calibration, not used in this example
Pin 2: output
Pin 3: GND (ground).
The output pin of the LM335 sensor is connected to analog channel 0 (A0) of the Arduino. I chose the 2.2K ohm because as written in the datasheet for optimum accuracy the current flows through the LM335 should be 1mA. For example if the temperature = 27°C, the output will be 3.00V and assume the supply voltage is exactly 5.00V that means the current flows through the sensor is ( 5 – 3)/2.2 = 0.90mA which is good enough. Also the value 2.2K is a standard value and well used.
The 3 transistors are of the same type (PNP).
Arduino with LM335 sensor and 7-segment display code:
The Arduino code below doesn’t use any library for the 7-segment display.
Arduino ADC module is used with 10-bit resolution which means the digital value of the input analog voltage varies between 0 (0V) and 1023 (5V). By multiplying the digital value by 0.489 we get the temperature in Kelvin (0.489 = 500/1023). The temperature in degree Celsius = Kelvin – 273.
The connection of LM335 output pin with Arduino is defined in the code as:
1 2 | // define LM335 data pin connection #define LM335_pin A0 |
Full Arduino code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | /* * Interfacing Arduino with LM335 temperature sensor and 7-segment display. * Common anode 7-segment display is used. * This is a free software with NO WARRANTY. * http://simple-circuit.com/ */ // define LM335 data pin connection #define LM335_pin A0 // segment pins definitions #define SegA 12 #define SegB 11 #define SegC 10 #define SegD 9 #define SegE 8 #define SegF 7 #define SegG 6 // common pins of the three digits definitions #define Dig1 4 #define Dig2 3 #define Dig3 2 // variable declarations byte current_digit; int temp; void setup() { pinMode(SegA, OUTPUT); pinMode(SegB, OUTPUT); pinMode(SegC, OUTPUT); pinMode(SegD, OUTPUT); pinMode(SegE, OUTPUT); pinMode(SegF, OUTPUT); pinMode(SegG, OUTPUT); pinMode(Dig1, OUTPUT); pinMode(Dig2, OUTPUT); pinMode(Dig3, OUTPUT); disp_off(); // turn off the display // Timer1 module overflow interrupt configuration TCCR1A = 0; TCCR1B = 1; // enable Timer1 with prescaler = 1 ( 16 ticks each 1 µs) TCNT1 = 0; // set Timer1 preload value to 0 (reset) TIMSK1 = 1; // enable Timer1 overflow interrupt } ISR(TIMER1_OVF_vect) // Timer1 interrupt service routine (ISR) { disp_off(); // turn off the display byte abs_temp = abs(temp); // abs: absolute value switch (current_digit) { case 1: if(temp < 0 || temp >= 100){ if(temp < 0) disp(10); // print minus sign (-) else disp(1); // print 1 digitalWrite(Dig1, LOW); // turn on digit 1 } break; case 2: disp( (abs_temp / 10) % 10); // prepare to display digit 2 digitalWrite(Dig2, LOW); // turn on digit 2 break; case 3: disp(abs_temp % 10); // prepare to display digit 3 digitalWrite(Dig3, LOW); // turn on digit 3 } current_digit = (current_digit % 3) + 1; } // main loop void loop() { int kelvin = analogRead(LM335_pin) * 0.489; // get temperature in degree Kelvin temp = kelvin - 273; // convert to degree Celsius delay(1000); // wait 1 second } void disp(byte number) { switch (number) { case 0: // print 0 digitalWrite(SegA, LOW); digitalWrite(SegB, LOW); digitalWrite(SegC, LOW); digitalWrite(SegD, LOW); digitalWrite(SegE, LOW); digitalWrite(SegF, LOW); digitalWrite(SegG, HIGH); break; case 1: // print 1 digitalWrite(SegA, HIGH); digitalWrite(SegB, LOW); digitalWrite(SegC, LOW); digitalWrite(SegD, HIGH); digitalWrite(SegE, HIGH); digitalWrite(SegF, HIGH); digitalWrite(SegG, HIGH); break; case 2: // print 2 digitalWrite(SegA, LOW); digitalWrite(SegB, LOW); digitalWrite(SegC, HIGH); digitalWrite(SegD, LOW); digitalWrite(SegE, LOW); digitalWrite(SegF, HIGH); digitalWrite(SegG, LOW); break; case 3: // print 3 digitalWrite(SegA, LOW); digitalWrite(SegB, LOW); digitalWrite(SegC, LOW); digitalWrite(SegD, LOW); digitalWrite(SegE, HIGH); digitalWrite(SegF, HIGH); digitalWrite(SegG, LOW); break; case 4: // print 4 digitalWrite(SegA, HIGH); digitalWrite(SegB, LOW); digitalWrite(SegC, LOW); digitalWrite(SegD, HIGH); digitalWrite(SegE, HIGH); digitalWrite(SegF, LOW); digitalWrite(SegG, LOW); break; case 5: // print 5 digitalWrite(SegA, LOW); digitalWrite(SegB, HIGH); digitalWrite(SegC, LOW); digitalWrite(SegD, LOW); digitalWrite(SegE, HIGH); digitalWrite(SegF, LOW); digitalWrite(SegG, LOW); break; case 6: // print 6 digitalWrite(SegA, LOW); digitalWrite(SegB, HIGH); digitalWrite(SegC, LOW); digitalWrite(SegD, LOW); digitalWrite(SegE, LOW); digitalWrite(SegF, LOW); digitalWrite(SegG, LOW); break; case 7: // print 7 digitalWrite(SegA, LOW); digitalWrite(SegB, LOW); digitalWrite(SegC, LOW); digitalWrite(SegD, HIGH); digitalWrite(SegE, HIGH); digitalWrite(SegF, HIGH); digitalWrite(SegG, HIGH); break; case 8: // print 8 digitalWrite(SegA, LOW); digitalWrite(SegB, LOW); digitalWrite(SegC, LOW); digitalWrite(SegD, LOW); digitalWrite(SegE, LOW); digitalWrite(SegF, LOW); digitalWrite(SegG, LOW); break; case 9: // print 9 digitalWrite(SegA, LOW); digitalWrite(SegB, LOW); digitalWrite(SegC, LOW); digitalWrite(SegD, LOW); digitalWrite(SegE, HIGH); digitalWrite(SegF, LOW); digitalWrite(SegG, LOW); break; case 10: // print - digitalWrite(SegA, HIGH); digitalWrite(SegB, HIGH); digitalWrite(SegC, HIGH); digitalWrite(SegD, HIGH); digitalWrite(SegE, HIGH); digitalWrite(SegF, HIGH); digitalWrite(SegG, LOW); } } void disp_off() { digitalWrite(Dig1, HIGH); digitalWrite(Dig2, HIGH); digitalWrite(Dig3, HIGH); } // end of code. |
The following video shows Proteus simulation of this project (simulation circuit is not the same as real hardware circuit, example circuit diagram is shown above):
Proteus simulation file download:
Arduino + LM335 sensor + 7-segment display
Discover more from Simple Circuit
Subscribe to get the latest posts sent to your email.