EngineersGarage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise
You are here: Home / Topics / Temperature Sensor : Interfacing LM35 and LCD Display to PIC18F4550

Temperature Sensor : Interfacing LM35 and LCD Display to PIC18F4550

|

Microcontroller › PIC › Temperature Sensor : Interfacing LM35 and LCD Display to PIC18F4550

  • This topic has 8 replies, 9 voices, and was last updated 9 years ago by Anonymous.
Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • October 5, 2011 at 7:42 am #4743
    alysha rahayu
    Participant

    Hey.

     

    I need some help with my programming for Temperature Sensor. I used LM35 and connected it to PORTAbits.RA0 so as to make use of the ADC module. I connected my LCD display to PORTD. However, I can’t display the temperature=t on my LCD display. Can someone check my source code? I managed to build it successfully but unable to show the temperature. Below is the result of my Temperature Sensor. As you can see, the black block was supposed to show the temperature. Here’s my code :

     

     

    //Temperature Sensor –  Convert the Analog signal to Digital and Display it to the LCD Display Board
     
    #include <p18F4550.h>
    #include <delays.h>
    #include “lcd.h” // Include file is located in the project directory
     
     
     
    // Include this when using Bootloader Program ================================
    #pragma udata
     
    extern void _startup (void);        // See c018i.c in your C18 compiler dir
    #pragma code _RESET_INTERRUPT_VECTOR = 0x000800
    void _reset (void)
    {
        _asm goto _startup _endasm
    }
    #pragma code
     
    #pragma code _HIGH_INTERRUPT_VECTOR = 0x000808
    void _high_ISR (void)
    {
        ;
    }
     
    #pragma code _LOW_INTERRUPT_VECTOR = 0x000818
    void _low_ISR (void)
    {
        ;
    }
    #pragma code
     
    #pragma code
     
    // additional codes ends here ===============================================================
     
    // Your program declarations start here:====
     
     
    #define LCD_RS PORTDbits.RD6    //  Register Select on LCD
    #define LCD_EN PORTDbits.RD4    //  Enable on LCD controller
    #define LCD_WR PORTDbits.RD5    //  Write on LCD controller
     
     
    //— Function for writing a command byte to the LCD in 4 bit mode


     
    void lcd_write_cmd(signed char cmd)
    {
        unsigned char temp2;
        LCD_RS = 0; // Select LCD for command mode
        Delay10TCYx(4); // 40us delay for LCD to settle down
        temp2 = cmd;
        temp2 = temp2 >> 4; // Output upper 4 bits, by shifting out lower 4 bits
        PORTD = temp2 & 0x0F; // Output to PORTD which is connected to LCD
     
        Delay1KTCYx(10); // 10ms – Delay at least 1 ms before strobing
        lcd_strobe();
        
    Delay1KTCYx(10); // 10ms – Delay at least 1 ms after strobing
     
        temp2 = cmd; // Re-initialise temp2 
        PORTD = temp2 & 0x0F; // Mask out upper 4 bits
     
        Delay1KTCYx(10); // 10ms – Delay at least 1 ms before strobing
        lcd_strobe();
        Delay1KTCYx(10); // 10ms – Delay at least 1 ms before strobing
     
    }
     
    //—- Function to write a character data to the LCD


     
    void lcd_write_data(char data)
    {
      char temp1;
     
        LCD_RS = 1; // Select LCD for data mode
        Delay10TCYx(4); // 40us delay for LCD to settle down
     
        temp1 = data;
        temp1 = temp1 >> 4;
        PORTD = temp1 & 0x0F;
    Delay1KTCYx(10); 
        LCD_RS = 1;
        Delay1KTCYx(10); //_-_ strobe data in
     
        lcd_strobe();
        Delay1KTCYx(10);
     
        temp1 = data;
        PORTD = temp1 & 0x0F;
        Delay1KTCYx(10);
    LCD_RS = 1;
        Delay1KTCYx(10); //_-_ strobe data in
     
        lcd_strobe();
        Delay1KTCYx(10);
    }
     
    //– Function to generate the strobe signal for command and character


     
    void lcd_strobe(void) // Generate the E pulse
    {
        LCD_EN = 1; // E = 0
        Delay1KTCYx(100); // 10ms delay for LCD_EN to settle
        LCD_EN = 0; // E = 1
        Delay1KTCYx(100); // 10ms delay for LCD_EN to settle
    }
     
     
    //—- Function to initialise LCD module


    void lcd_init(void)
    {
        TRISD = 0x00;
        PORTD = 0x00; // PORTD is connected to LCD data pin
        LCD_EN = 0;
        LCD_RS = 0; // Select LCD for command mode
        LCD_WR = 0; // Select LCD for write mode
       
        Delay10KTCYx(250); // Delay a total of 1 s for LCD module to
        Delay10KTCYx(250); //
        Delay10KTCYx(250); //
        Delay10KTCYx(250); // finish its own internal initialisation
     
        /* The data sheets warn that the LCD module may fail to initialise properly when
           power is first applied. This is particularly likely if the Vdd
           supply does not rise to its correct operating voltage quickly enough.
     
           It is recommended that after power is applied, a command sequence of
           3 bytes of 30h be sent to the module. This will ensure that the module is in
           8-bit mode and is properly initialised. Following this, the LCD module can be
           switched to 4-bit mode.
        */
     
        lcd_write_cmd(0x33);
        lcd_write_cmd(0x32);
          
        lcd_write_cmd(0x28); // 001010xx – Function Set instruction
        // DL=0 :4-bit interface,N=1 :2 lines,F=0 :5×7 dots
       
        lcd_write_cmd(0x0E); // 00001110 – Display On/Off Control instruction
        // D=1 :Display on,C=1 :Cursor on,B=0 :Cursor Blink on
       
        lcd_write_cmd(0x06); // 00000110 – Entry Mode Set instruction
        // I/D=1 :Increment Cursor position
        // S=0 : No display shift
       
        lcd_write_cmd(0x01); // 00000001 Clear Display instruction
     
        Delay1KTCYx(20); // 20 ms delay
     
    }
     
    void main(void)
    {
    // Do not remove these as well=============
    ADCON1 = 0x0F; // Configure PORTA to be digital I/O
    CMCON = 0x07;
    // ========================================
    // Your MAIN program Starts here: =========
     
        
    lcd_init(); // Initialise LCD module
     
    LCD_RS = 1; // Select LCD for character data mode
    Delay1KTCYx(1); // 1 ms delay
     
    /* Initialise analog to digital conversion setting */
     
    ADCON0 = 0b00000001;    // bit5-2 0000 select channel 0 conversion 
    // bit1  A/D conversion status bit
    //      1- GO to starts the conversion
    //      0 – DONE when A/D is completed
    // bit0   Set to 1 to power up A/D
     
    ADCON1 = 0b00001100; // bit5   reference is VSS
    // bit4   reference is VDD
    // bit3-0 AN2 to AN0 Analog, the rest Digital
     
    ADCON2 = 0b00010110; // bit7   : A/D Result Format. 0 Left, 1 Right justified ( Set it to 1, Right Justified)
    // bit5-3 : 010 acquisition time = 4 TAD
    // bit2-0 : 110 conversion clock = Tosc / 16
     
    for(;;)
    {
           unsigned int t; // variable for temperature
           
     
    ADCON0bits.GO = 1; // This is bit2 of ADCON0, START CONVERSION NOW
           
    while(ADCON0bits.GO == 1); // Waiting for DONE
     
            t=(ADRESH * 0.48876); //Convert to Degree Celcius – ADRES (16bit) is the output of ADC
            
     
    while(1)
    {
         lcd_write_cmd(0x80); // Move cursor to line 1 position 1
     
            lcd_write_data(‘T’);
            lcd_write_data(‘E’);
            lcd_write_data(‘M’);
            lcd_write_data(‘P’);
            lcd_write_data(‘E’);
            lcd_write_data(‘R’);
            lcd_write_data(‘A’);
            lcd_write_data(‘T’);
            lcd_write_data(‘U’);
            lcd_write_data(‘R’);
            lcd_write_data(‘E’);
            lcd_write_data(‘:’);
     
         lcd_write_cmd(0xC0); // Move cursor to line 2 position 1
     
            lcd_write_data(t);          // Display the temperature 
            lcd_write_data(0xDF);
            lcd_write_data(‘C’);
     
      
     
     
     
     
       while(1); //stop here for now
     
     
    }
    }
    }
     
     
     
     

     

    January 5, 2012 at 9:41 am #6998
    Shivaji P. Patil
    Participant

    Hello,

    I hope you have solved problem as it is posted 2 months before.

     

    If not,

    1) you have to convert your temepature in string format to display on LCD.

    2) you have to make calculation in float variable.

     

     

    ex:

         lcd_write_cmd(0xC0); // Move cursor to line 2 position 1

     
    lcd_write_data(t); // Display the temperature 


      Instead of direct ‘t’ it should be converted to string. 
                                                                            


    Instead of  ‘int’ ‘t’ it should be ‘float’.
    lcd_write_data(0xDF);
    lcd_write_data(‘C’);
     
     

     

    February 13, 2012 at 1:27 am #7154
    faizal
    Participant

    i use c18 “long time” ago. if i’m not mistaken, itoa() is a built in function in c18. you can use this to convert the integer to string straight away.

    have a nice day :)

    March 12, 2012 at 2:38 pm #7291
    muhammad
    Participant

    Can you share with me schematic circuit for this project…it can help me for my project…thank you 

    March 10, 2013 at 7:12 pm #9289
    himanshu singh
    Participant

    help me rectifying error in this code for ADC 

      // program to read analog input from TEMPERATURE SENSOR AND PRODUCE EQUIVALENT OUTPUT AND PRINT IT ON LCD

    #include <avr/io.h>
    #include<util/delay.h>
    #include “lcd.h”
    #include “lcd.c”
    #define F_CPU 1000000UL
    void main()
    {
     
    char ch; // declaring ch as character variable of length 4, used to select 1 channel out of 7
    unsigned long int adc_result; // declaring adc_result variable as unsigned integer used to to store adc result which willbe passed onto lcd
     
    lcd_init (LCD_DISP_ON);
    lcd_clrscr() ;
     
    initADC (); // initialise adc funtion
     
     
    lcd_gotoxy(0,0);
    lcd_puts(“ADC TEST”);
    lcd_gotoxy(0,1);
    lcd_puts(“ADC= “);
     
    while (1)
    {
     
    adc_result= ReadADC(); // store adc result in adc_result variable
    sprintf(ch,”%4d”,adc_result); // to print this result in lcd, first store it in ch variable
    lcd_gotoxy(4,1);
    lcd_puts(ch); // display the result on lcd
    _delay_ms(100);
    }
    }
     
    void initADC() // ADC INITIALISATION FUNCTION
    {
    ADMUX=(1<<REFS0)|(1<<REFS1); // VOLTAGE REFERENCE AS 2.56
    ADCSRA= (1<<ADEN)|(1<<ADPS1)|(1<<ADPS0);// ENABLING ADC AND SELECTING PRESCALING OF 8
    }
     
    unsigned long int ReadADC(void) // ADC READING FUNCTION, RETURN VALUE IS OF 16 BITS SO UINT16_T AND CHANNEL LENGTH AS INPUT ARGUMENT
    {
                          // enabling 0to 7 channels
    ADMUX|= (1<<MUX0);
     
    ADCSRA |= (1<<ADSC); // start adc conversion
     
    while (!(ADCSRA & (1<<ADIF))); // wait until conversion is complete
    ADCSRA|= (1<<ADIF); // clear adif flag after completion, yes its 1 to clear it
    return (ADC); // return the digital output
    }
     
    March 12, 2013 at 8:06 am #9301
    Dhilip
    Participant

    same problem i am facing ….

    can any one help me to control the temperature using any kind of pic….and display the corresponding temperature on LCD…

    June 16, 2013 at 6:54 pm #9970
    aqeel raza
    Participant

    Can any one help me i want to interface LM35 through PLC 

    August 2, 2014 at 12:51 pm #11968
    CIRCUITPROBLEM
    Participant

    can anyone help to solve this problem??i am facing the same problem.

    July 14, 2016 at 10:53 am #14064
    Anonymous
    Guest

    hello , can any one help me i m facing problem with LM35 interfacing with p18f4520 . The readings changing from 24 to 25 degree

  • Author
    Posts
Viewing 9 posts - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.
Log In

RSS Recent Posts

  • Faulty heat air gun (dc motor) - problem to locate fault due to Intermittent fault July 19, 2025
  • Does US electric code allow branching ? July 19, 2025
  • Fun with AI and swordfish basic July 19, 2025
  • Sump pit water alarm - Kicad 9 July 19, 2025
  • turbo jet fan - feedback appreciated. July 19, 2025

Stay Up To Date

Newsletter Signup
EngineersGarage

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise