This tutorial shows how to interface PIC18F46K22 microcontroller with Nokia 5110 LCD display and LM35 analog temperature sensor from Texas Instruments.
In this project the Nokia 5110 LCD (84×48 pixel resolution) is used to display environment temperature in degree Celsius and Kelvin.
The compiler used in this project is MikroElektronika mikroC PRO for PIC.
To see how to interface PIC18F46K22 MCU with Nokia 5110 LCD using mikroC compiler, visit this post:
Interfacing PIC MCU with Nokia 5110 LCD | mikroC Projects
About the LM35 temperature sensor:
The LM35 temperature sensor is a three pin device (VCC, OUT and GND) with an output voltage linearly related to Centigrade temperature. Since the LM35 output varies with dependent to the temperature, we need an ADC (Analog-to-Digital Converter) module to measure this voltage. The PIC18F46K22 microcontroller has one ADC module with 10-bit resolution.
The LM35 output has linear +10mV/°C scale factor means the following:
If the output voltage = 10mV —> temperature = 1°C
If the output voltage = 100mV —> temperature = 10°C
If the output voltage = 200mV —> temperature = 20°C
If the output voltage = 370mV —> temperature = 37°C
and so on.
LM35 Futures (from datasheet):
- Calibrated Directly in ° Celsius (Centigrade)
- Linear + 10 mV/°C Scale Factor
- 0.5°C Ensured Accuracy (at +25°C)
- Rated for Full −55°C to +150°C Range
- Suitable for Remote Applications
- Low Cost Due to Wafer-Level Trimming
- Operates from 4 to 30 V
- Less than 60-μA Current Drain
- Low Self-Heating, 0.08°C in Still Air
- Nonlinearity Only ±¼°C Typical
- Low Impedance Output, 0.1 Ω for 1 mA Load
The ADC module converts analog data into digital data. The PIC18F46K22 MCU has a 10-bit ADC module and a built-in fixed voltage reference (FVR) module which makes it a good choice for this application. With the fixed voltage reference we get a very good results.
Normally negative and positive references of the ADC module are VSS and VDD respectively, but VDD is not exactly equal to 5.00V, hence 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 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 FVR = 1.024V because the LM35 output is generally less than 1V and also it gave me better result (let’s say higher resolution). Now the ADC module works in the interval between 0 and 1.024V.
Hardware Required:
- PIC18F46K22 microcontroller —-> datasheet
- Nokia 5110 LCD screen
- LM35 temperature sensor —-> datasheet
- AMS1117 3V3 voltage regulator
- 10 uF capacitor
- 100 nF ceramic capacitor
- 5 x 3.3k ohm resistor
- 5 x 2.2k ohm resistor
- 5V source
- Breadboard
- Jumper wires
PIC18F46K22 MCU with Nokia 5110 LCD and LM35 sensor circuit:
The image below shows project circuit diagram.
The LM35 sensor has 3 pins (from left to right):
Pin 1 is power supply pin, connected to circuit +5V,
Pin 2: output pin,
Pin 3: GND (ground), connected to circuit ground.
The output pin of the LM35 sensor is connected to pin RA0 which is analog channel 0 (AN0).
All the grounded terminals are connected together.
The Nokia 5110 LCD module which is shown in the circuit diagram has 8 pins (from left to right): RST (reset), CE (chip enable), DC (or D/C: data/command), Din (data in), Clk (clock), VCC (3.3V), BL (back light) and Gnd (ground).
The Nokia 5110 LCD works with 3.3V only (power supply and control lines). The LCD module is supplied with 3.3V which comes from the AMS1117 3V3 voltage regulator, this regulator steps down the 5V into 3.3V (supplies the LCD controller PCD8544 with regulated 3V3).
All PIC18F46K22 microcontroller output pins are 5V, connecting a 5V pin to the Nokia 5110 LCD may damage its controller circuit!
To connect the PIC18F46K22 to the LCD module, I used voltage divider for each line which means there are 5 voltage dividers. Each voltage divider consists of 2.2k and 3.3k resistors, this drops the 5V into 3V which is sufficient.
So, the Nokia 5110 LCD pins are connected to PIC18F46K22 MCU as follows (each one through voltage divider):
RST (reset) pin is connected to pin RD0 (#19),
CE (chip enable) pin is connected to pin RD1 (#20),
DC (data/command) pin is connected to pin RD2 (#21),
DIN (data in) pin is connected to pin RD3 (#22),
CLK (clock) pin is connected to pin RD4 (#27),
VCC and BL are connected to AMS1117 3V3 regulator output pin and GND is connected to circuit ground (0V).
In this project the PIC18F46K22 microcontroller runs with its internal oscillator @ 16 MHz, MCLR pin is configured as an input pin.
PIC18F46K22 with Nokia 5110 LCD and LM35 sensor C code:
The following C code is for mikroC PRO for PIC compiler, it was tested with version 7.2.0.
To be able to compile project C code, a driver for the Nokia 5110 LCD is required, download link is below. After you download the driver file which named NOKIA5110.C, add it to your project folder:
Nokia 5110 LCD library for mikroC compiler
The connection of the LCD pins with the microcontroller are defined in the C code as shown below:
1 2 3 4 5 6 7 8 | // Nokia 5110 LCD module connections // use software SPI #define LCD_RST RD0_bit // reset pin, optional! #define LCD_CS RD1_bit // chip select pin, optional! #define LCD_DC RD2_bit // data/command pin #define LCD_DAT RD3_bit // data-in pin (MOSI) #define LCD_CLK RD4_bit // clock pin // end LCD module connections |
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 set to 1.024V:
1 2 | // initialize ADC module with voltage references: VSS - FVR(1.024V) ADC_Init_Advanced(_ADC_INTERNAL_VREFL | _ADC_INTERNAL_FVRH1); |
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 (1.024V).
The digital value represents the temperature in tenths °Celsius (output value of “274” equals 27.4 °Celsius).
The temperature in tenths Kelvin = (tenth °Celsius) + 2732 (because: K = °C + 273.16).
To get the actual value of each quantity we’ve to divide it by 10. The line below shows an example for temperature in Kelvin:
1 | sprinti(buffer, "%03u.%1u K", tKelvin / 10, tKelvin % 10); |
We get the first 3 digits by dividing the tenths value by 10, and the tenths number (number after the decimal point) of the actual temperature value is equal to the reminder of that division (tenths value % 10).
The resolution of this thermometer is 0.1°C.
Full mikroC 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 | /************************************************************************************** Interfacing PIC18F46K22 microcontroller with Nokia 5110 LCD (84x48 Pixel) and LM35 analog temperature sensor. C Code for mikroC PRO for PIC compiler. Internal oscillator used @ 16MHz Configuration words: CONFIG1H = 0x0028 CONFIG2L = 0x0018 CONFIG2H = 0x003C CONFIG3H = 0x0037 CONFIG4L = 0x0081 CONFIG5L = 0x000F CONFIG5H = 0x00C0 CONFIG6L = 0x000F CONFIG6H = 0x00E0 CONFIG7L = 0x000F CONFIG7H = 0x0040 This is a free software with NO WARRANTY. http://simple-circuit.com/ ***************************************************************************************/ // Nokia 5110 LCD module connections // use software SPI #define LCD_RST RD0_bit // reset pin, optional! #define LCD_CS RD1_bit // chip select pin, optional! #define LCD_DC RD2_bit // data/command pin #define LCD_DAT RD3_bit // data-in pin (MOSI) #define LCD_CLK RD4_bit // clock pin // end LCD module connections // include Nokia 5110 LCD driver source file #include <NOKIA5110.c> // main function void main() { OSCCON = 0x70; // set internal oscillator to 16MHz ANSELD = 0; // configure all PORTD pins as digital TRISD = 0; // configure all PORTD pins as outputs // initialize ADC module with voltage references: VSS - FVR(1.024V) ADC_Init_Advanced(_ADC_INTERNAL_VREFL | _ADC_INTERNAL_FVRH1); LCD_Begin(); // initialize the LCD LCD_Clear(); // clear the display buffer LCD_SetContrast(60); // set LCD contrast LCD_TextWrap(false); // disable text wrap LCD_TextSize(1); // set text size to 1 LCD_GotoXY(6, 0); // move cursor to position (6, 0) pixel LCD_Print("TEMPERATURE="); // print text LCD_TextSize(2); // set text size to 2 LCD_Display(); // update the screen while(1) { int tKelvin, tCelsius; char buffer[8]; // read analog voltage ( = tenths degree Celsius) tCelsius = ADC_Get_Sample(0); tKelvin = tCelsius + 2732; // convert °C to Kelvin // print temperature in degree Celsius if (tCelsius >= 1000) sprinti(buffer, "%03u.%1u C", tCelsius / 10, tCelsius % 10); else sprinti(buffer, " %02u.%1u C", tCelsius / 10, tCelsius % 10); LCD_GotoXY(0, 13); LCD_Print(buffer); LCD_DrawCircle(66, 15, 2, BLACK); // print degree symbol ( ° ) // print temperature in Kelvin sprinti(buffer, "%03u.%1u K", tKelvin / 10, tKelvin % 10); LCD_GotoXY(0, 34); LCD_Print(buffer); LCD_Display(); // update the screen delay_ms(1000); // wait a second } } // end of code. |
Discover more from Simple Circuit
Subscribe to get the latest posts sent to your email.