Nuffnang Ads

Tuesday, 16 September 2014

16x2 LCD Hello World (using C + WiringPi)

Thanks to WiringPi who made the GPIO porting become simple. This entry will show the "hello world" LCD tutorial in C using WiringPi.

Basic info about 16x2 LCD:-

16x2 LCD

16x2 LCD pinout:-
1   = VSS (GND)
2   = VDD (VCC,5V)
3   = VE (connect with variable resistor)
4   = RS, Register Select
5   = RW, Read/Write
6   = E, Enable
7   = Data0, D0
8   = Data1, D1
9   = Data2, D2
10 = Data3, D3
11 = Data4, D4
12 = Data5, D5
13 = Data6, D6
14 = Data7, D7
15 = Backlight Anode (VCC)
16 = Backlight Cathode (GND)

For 16x2 LCD, usually  requires 8 data lines to provide data to Bits 0-7. However it also can enable “4 bit” mode which allows you to send data in parallel of 4 bits. This approach can reduces the number of GPIO connections when connection with Raspberry Pi.


1. You can found plenty of site that show the LCD to GPIO wiring. This tutorial will use 4 bit mode. The wiring can follow shown as below:-



2. After done the wiring, make sure you already installed WiringPi, if not, please refer to the previous post. Run "gpio readall" command to see the WiringPi pins.

GPIO mapping with WiringPi
Pin 12 = wPi 1 = D7
Pin 16 = wPi 4 = D6
Pin 18 = wPi 5 = D5
Pin 22 = wPi 6 = D4
Pin 24 = wPi 10 = E
Pin 26 = wPi 11 = RS


3. Place the code below to display "HELLO WORLD" text on the top line of the LCD. Make sure the WiringPi pinout and LCD pinout are correct. Refer to the 2nd step for the pin mapping.

#vi lcd-test.c
in lcd-test.c,
#include <wiringPi.h>           //WiringPi headers
#include <lcd.h>                //LCD headers from WiringPi
#include <stdio.h>              //Needed for the printf function below

//Pin numbers below are the WiringPi pin numbers

#define LCD_RS  11               //Register select pin
#define LCD_E   10               //Enable Pin
#define LCD_D4  6               //Data pin 4
#define LCD_D5  5               //Data pin 5
#define LCD_D6  4               //Data pin 6
#define LCD_D7  1               //Data pin 7

int main()
{
    int lcd;                //Handle for LCD
    wiringPiSetup();        //Initialise WiringPi

    //Initialise LCD(int rows, int cols, int bits, int rs, int enable, int d0, int d1, int d2, int d3, int d4, int d5, int d6, int d7)
    if (lcd = lcdInit (2, 16,4, LCD_RS, LCD_E ,LCD_D4 , LCD_D5, LCD_D6,LCD_D7,0,0,0,0)){
            printf ("lcd init failed! \n");
            return -1 ;
    }
    lcdPosition(lcd,0,0);  //Position cursor on the first line in the first column
    lcdPuts(lcd, "HELLO WORLD");  //Print the text on the LCD at the current cursor postion
    getchar();                      //Wait for key press
}

4. Compile lcd-test.c using


5. Run using

6. "HELLO WORLD" text will be displayed at LCD


References
[1] http://www.raspberrypi-spy.co.uk/2012/07/16x2-lcd-module-control-using-python/
[2] http://ozzmaker.com/2013/10/04/interface-16x2-lcd-with-the-raspberry-pi/?utm_source=feedly
[3] http://www.instructables.com/id/How-to-drive-a-character-LCD-displays-using-DIP-sw/step2/HD44780-pinout/





13 comments:

  1. sir this program is running successefully but problem is the HELLO WORLD is not showing on screan.

    ReplyDelete
    Replies
    1. put some potentiometer to adjust the contrast.

      Delete
  2. and connection is also ok.show please tell me the problem on my e-mai- er.saurabh2890@gmail.com

    ReplyDelete
  3. sir still it is not working.can you please post video

    ReplyDelete
  4. sir,thanks program is running.
    very much thanks.

    ReplyDelete
  5. and,all things are proper working

    ReplyDelete
    Replies
    1. Hello Sir,
      Would you please let me know how did you succeed to make the hello world message appear in the scrren. I had the same problem as you: Program rrunning without errors but no message appears in the LCD screen Thanks

      Delete
    2. PLEASE PUT the 10k preset to set contrast of lcd....you can see connection from many image posted on google....

      Delete
    3. PLEASE PUT the 10k preset to set contrast of lcd....you can see connection from many image posted on google....

      Delete
    4. use of 5k preset to set contrast of lcd....
      use this program
      int main()
      {
      int lcd; //Handle for LCD
      wiringPiSetup(); //Initialise WiringPi

      //Initialise LCD(int rows, int cols, int bits, int rs, int enable, int d0, int d1, int d2, int d3, int d4, int d5, int d6, int d7)
      lcd = lcdInit (2, 16,4, LCD_RS, LCD_E ,LCD_D4 , LCD_D5, LCD_D6,LCD_D7,0,0,0,0);
      lcdClear(lcd);
      lcdPosition(lcd,0,0); //Position cursor on the first line in the first column
      lcdPuts(lcd, "HELLO WORLD"); //Print the text on the LCD at the current cursor postion
      getchar(); //Wait for key press
      }
      and hurray here is your output

      Delete
  6. Hello Sir,
    Would you please let me know how did you succeed to make the hello world message appear in the scrren. I had the same problem as you: Program rrunning without errors but no message appears in the LCD screen Thanks

    ReplyDelete
  7. PLEASE PUT the 10k preset to set contrast of lcd....you can see connection from many image posted on google....

    ReplyDelete