This small post shows how to measure the ambient temperature using LM335 analog temperature sensor and Microchip PIC16F887 microcontroller using mikroC Pro for PIC compiler from MikroElektronika.
Related Projects:
Interfacing PIC16F887 with LM335 temperature sensor – CCS C compiler
Interfacing PIC MCU with DHT11 sensor – mikroC
DHT22 Sensor interface with PIC microcontroller – mikroC Projects
The LM335 is an analog device which requires an ADC module to convert the analog data which is the voltage output from the LM335 into digital data. The LM335 has the following features:
- 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 LM135 has a breakdown voltage directly proportional to absolute temperature at 10 mV/°K. If the LM335 output voltage is for example is 3.03 (3030 mV) that means the temperature is: 303 °Kelvin = 30 °Celsius.
Hardware Required:
- PIC16F887 microcontroller —> datasheet
- LM335 temperature sensor —> datasheet
- 16×2 LCD Screen
- 10k ohm variable resistor (or potentiometer)
- 2.2k ohm resistor
- 5V Power supply source
- Breadboard
- Jumper wires
- PIC MCU Programmer (PICkit 3, PICkit 4…)
LM335 Sensor interface with PIC16F887 MCU circuit:
Circuit schematic diagram is shown below.
(All grounded terminals are connected)
The LM335 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 RA0/AN0 pin (analog pin 0) of the PIC16F887.
The LCD module is connected to PORTD pins with:
RS —> RD0
E —> RD1
D4 —> RD2
D5 —> RD3
D6 —> RD4
D7 —> RD5
In this example the PIC16F887 microcontroller runs with its internal oscillator (@ 8MHz) and MCLR pin is configured as digital input pin (configured in the software).
LM335 Sensor interface with PIC16F887 MCU mikroC code:
mikroC configuration words are:
CONFIG1 : 0x2CD4
CONFIG2 : 0x0700
The configuration words can be changed from: Project –> Edit Project …
The resolution of the ADC (Analog-to-Digital Converter) module of the PIC16F887 is 10 bits which means it converts the analog input voltage (between 0 and 5V) into a digital number between 0 and 1023 (0V —> 0 and 5V —> 1023). To return the digital value into its correspondent voltage we can just multiply that value by 5/1023.
Therefor to get the temperature in °K we have to multiply the digital value of the analog voltage (output of the LM335) by 0.489 ( 0.489 = 500/1023).
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 | // Interfacing LM335 sensor with PIC16F887 mikroC code // Internal oscillator used @ 8MHz // Configuration words: CONFIG1 = 0x2CD4 // CONFIG2 = 0x0700 // http://simple-circuit.com/ // LCD module connections sbit LCD_RS at RD0_bit; sbit LCD_EN at RD1_bit; sbit LCD_D4 at RD2_bit; sbit LCD_D5 at RD3_bit; sbit LCD_D6 at RD4_bit; sbit LCD_D7 at RD5_bit; sbit LCD_RS_Direction at TRISD0_bit; sbit LCD_EN_Direction at TRISD1_bit; sbit LCD_D4_Direction at TRISD2_bit; sbit LCD_D5_Direction at TRISD3_bit; sbit LCD_D6_Direction at TRISD4_bit; sbit LCD_D7_Direction at TRISD5_bit; // End LCD module connections char Temp_C[] = "Temp = 00.0 C"; char Temp_K[] = "= 00.0 K"; int Kelvin, Celsius; void main() { OSCCON = 0X70; // Set internal oscillator to 8MHz ANSEL = 1; // Configure RA0 pin as analog (AN0) Lcd_Init(); // Initialize LCD module Lcd_Cmd(_LCD_CURSOR_OFF); // cursor off Lcd_Cmd(_LCD_CLEAR); // clear LCD while(1) { Kelvin = ADC_Read(0) * 0.489; // Read analog voltage and convert it to Kelvin (0.489 = 500/1023) Celsius = Kelvin - 273; // Convert Kelvin to degree Celsius if(Celsius < 0){ Celsius = abs(Celsius); // Absolute value Temp_C[7] = '-'; // Put minus '-' sign } else Temp_C[7] = ' '; // Put space ' ' if (Celsius > 99) Temp_C[7] = 1 + 48; // Put 1 (of hundred) Temp_C[8] = (Celsius / 10) % 10 + 48; Temp_C[9] = Celsius % 10 + 48; Temp_C[12] = 223; // Put degree symbol Temp_K[2] = (Kelvin / 100) % 10 + 48; Temp_K[3] = (Kelvin / 10) % 10 + 48; Temp_K[4] = Kelvin % 10 + 48; lcd_out(1, 1, Temp_C); lcd_out(2, 6, Temp_K); delay_ms(1000); // Wait 1 second } } // End of code |
Proteus simulation of this project should give the same result as shown in the video below (the compiler used in the video is CCS C):
Reference:
LM335 datasheet
Discover more from Simple Circuit
Subscribe to get the latest posts sent to your email.