This PIC18F46K22 microcontroller project shows how to read temperature from LM335 analog temperature sensor and print its value on a three-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. The PIC18F46K22 microcontroller has one ADC module with 10-bit resolution.
ADC: Analog-to-Digital Converter.
The LM335 sensor has the following features (from 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) this means the temperature is: 303 Kelvin = 30 °Celsius.
The PIC18F46K22 is an 8-bit microcontroller which has 10-bit ADC module. The good thing with this microcontroller is the fixed voltage references. With the fixed voltage reference we get a very good result. Normally negative and positive references of the ADC module are VSS and VDD, but VDD is not exactly equal to 5.00V and here we should use the fixed voltage reference as a positive reference of the ADC module.
The PIC18F46K22 has 3 fixed voltage references: 1.024V, 2.048V and 4.096V. For example if we set the fixed voltage reference to 4.096V and the ADC module is configured so that the negative and the positive references are VSS and FVR (Fixed Voltage Reference) respectively, in this case the equivalent 10-bit digital value of 4.096 is 1023 and 3.00V is 3.00 * 1023/4.096 = 749 , and so on.
In this project I used 4.096 because the LM335 output is between 2.23V (temperature = -50°C) and 3.98V (temperature = +125°C).
The value of the temperature is displayed on 3-digit common anode 7-segment display, to see how to interface the PIC18F46K22 microcontroller with 7-segment display (4-digit counter example), visit the following post:
Interfacing PIC18F46K22 with 7-segment display | 4-Digit counter example
Parts Required:
- PIC18F46K22 microcontroller —-> datasheet
- 3 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
- 5V source
- Breadboard
- Jumper wires
- PIC MCU Programmer (PICkit 3, PICkit 4…)
PIC18F46K22 with LM335 sensor and 7-segment display circuit:
Project circuit schematic diagram is shown below.
All the grounded terminals are connected together.
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 pin RA0 which is analog channel 0 (AN0). 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).
In this project the PIC18F46K22 microcontroller runs with its internal oscillator @ 8 MHz, MCLR pin is configured as an input pin.
PIC18F46K22 with LM335 sensor and 7-segment display C code:
The following C code is for CCS C compiler, it was tested with version 5.051.
The internal fixed voltage reference (FVR) of the PIC18F46K22 microcontroller is set to 4.096V using the following line:
1 | setup_vref(VREF_4v096); // set FVR to 4.096V // FVR: Fixed Voltage Reference |
The ADC module is configured so that it uses its internal clock, voltage references (negative and positive) are set to VSS and FVR respectively where the FVR is the one previously set to 4.096V.
The PIC18F46K22 ADC (Analog-to-Digital Converter) module reads analog voltage applied to analog channel AN0 (#2) which is the output signal of the LM335 sensor.
The PIC18F46K22 microcontroller has a built-in ADC module with 10-bit resolution. With a positive reference voltage of 4.096V, a 0V is represented by 0 and 4.096V is represented by a digital value of 1023.
The LM335 output pin is connected to AN0 channel. All these configurations are done in the code as shown below:
1 2 3 | setup_adc(ADC_CLOCK_INTERNAL); // set ADC module clock to internal setup_adc_ports(sAN0 | VSS_FVR); // configure AN0 pin as analog & Voltage references: VSS-FVR set_adc_channel(sAN0); // select AN0 channel |
PIC18F46K22 ADC module is used with 10-bit resolution which means the digital value of the input analog voltage varies between 0 (0V) and 1023 (4.096V). By multiplying the digital value by 0.4 we get the temperature in Kelvin (0.4 = 100*4.096/1024). The temperature in degree Celsius = Kelvin – 273.
CCS C 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 | /* * Interfacing PIC18F46K22 MCU with LM335 temperature sensor and 7-segment display. * Common anode 7-segment display is used. * C Code for CCS C compiler. * This is a free software with NO WARRANTY. * http://simple-circuit.com/ */ // segment pin definitions #define Seg_A PIN_D6 #define Seg_B PIN_D5 #define Seg_C PIN_D4 #define Seg_D PIN_D3 #define Seg_E PIN_D2 #define Seg_F PIN_D1 #define Seg_G PIN_D0 // common pins of the three digits definitions #define Com_1 PIN_B2 #define Com_2 PIN_B1 #define Com_3 PIN_B0 #include <18F46K22.h> #device ADC = 10 #fuses NOMCLR,NOLVP,NOBROWNOUT,PUT,NOXINST #use delay(internal = 8MHz) // variable declaration signed int8 Celsius; const char seg_maps[] = { 0x40, // 0 0x79, // 1 0x24, // 2 0x30, // 3 0x19, // 4 0x12, // 5 0x02, // 6 0x78, // 7 0x00, // 8 0x10, // 9 0x3F // - }; void seg_out(int8 nbr) { nbr = seg_maps[nbr]; output_bit(Seg_A, bit_test(nbr, 0)); output_bit(Seg_B, bit_test(nbr, 1)); output_bit(Seg_C, bit_test(nbr, 2)); output_bit(Seg_D, bit_test(nbr, 3)); output_bit(Seg_E, bit_test(nbr, 4)); output_bit(Seg_F, bit_test(nbr, 5)); output_bit(Seg_G, bit_test(nbr, 6)); } #INT_TIMER2 // Timer2 ISR void Timer2_ISR(void) { static int8 current_digit; int8 abs_temp = abs(Celsius); // abs: absolute value // turn off all segments output_high(Com_1); output_high(Com_2); output_high(Com_3); if( (current_digit == 1) && (Celsius < 0 || Celsius >= 100) ) { if(Celsius < 0) // if the temperature < 0 seg_out(10); // print minus sign (-) else // if the tempearture >= 100 °C seg_out(1); // print 1 output_low(Com_1); // turn on digit 1 (most left) } if(current_digit == 2) { seg_out( (abs_temp / 10) % 10 ); output_low(Com_2); // turn on digit 2 } if(current_digit == 3) { seg_out(abs_temp % 10); output_low(Com_3); // turn on digit 3 } current_digit = (current_digit % 3) + 1; } // main function void main() { setup_oscillator(OSC_8MHZ); // set internal oscillator to 8MHz setup_vref(VREF_4v096); // set FVR to 4.096V // FVR: Fixed Voltage Reference setup_adc(ADC_CLOCK_INTERNAL); // set ADC module clock to internal setup_adc_ports(sAN0 | VSS_FVR); // configure AN0 pin as analog & Voltage references: VSS - FVR(4.096V) set_adc_channel(sAN0); // select AN0 channel enable_interrupts(GLOBAL); // enable global interrupts clear_interrupt(INT_TIMER2); // clear Timer2 interrupt flag bit enable_interrupts(INT_TIMER2); // enable Timer2 interrupt setup_timer_2( T2_DIV_BY_16, 255, 2 ); // enable Timer2 with prescaler=16 & postoscaler=2 while(TRUE) { // read analog voltage and convert it to Kelvin (0.4 = 100*4.096/1024) int16 Kelvin = (read_adc() + 1) * 0.4; Celsius = Kelvin - 273; // convert °Kelvin to °Celsius delay_ms(1000); // wait 1 second } } // end of code. |
Proteus simulation of the project should give a result near to the one shown below where Arduino UNO board is used (simulation circuit is not the same as real hardware circuit, project hardware circuit is shown above):
Discover more from Simple Circuit
Subscribe to get the latest posts sent to your email.