Lesson 15 LCD1602

Share for us
Share on facebook
Share on twitter
Share on pinterest
Share on whatsapp

Introduction

In this lesson, we will learn how to use LCD1602 to display strings.

Experimental Conditions

– 1*Raspberry Pi

– 1*Breadboard

– 1*Network cable (or USB wireless network adapter)

– 1*LCD1602

– 1*Potentiometer

– Jumper wires

Experimental Principle

LCD1602, also called character type LCD1602, is a dot matrix LCD module that specially used to display letters, figures, symbols, and so on. It consists of 16X2 dot matrixes, and each dot matrix is composed of 5X7 or 5X11 character bit. Each character bit can display a character. There is a dot space between each adjacent character bit. And there is a dot space between each row too. The dot space functions as a character space or a line space, and because of this, LCD1602 cannot display graphics very well. It is widely used in pocket instruments and low power application systems due to its micro power consumption, small size, rich contents display, ultra-thin and lightness.

LCD1602 use the standard 16-pin port, among which:

Pin 1(GND): connected to Ground;

Pin 2(Vcc): connected to 5V positive power supply;

Pin 3(Vo): used to adjust the contrast of LCD1602, lowest when connected to positive power supply, highest when connected to ground (you can connect a 10K potentiometer to adjust its contrast when using LCD1602);

Pin 4 (RS): A Register Selection pin, select data register when supplied high level (1), select instruction register when supplied low level (0);

Pin 5 (R/W): A Read/Write signal pin, read signals when supplied high level (1), write signals when supplied low level (0). Since we only need to write data to LCD1602, we directly connect this pin to ground;

Pin 6 (E): An Enable pin, when supplied low level, the LCD module will execute relevant instructions

Pin 7 (D0-D7)Pins that read and write data;                                        

A and K: LCD backlight power source;

LCD1602 has two operation modes: 4-bit mode and 8-bit mode. When the IOs of the microprocessor (MCU) is nervous, you can choose the 4-bit mode, which only uses D4~D7 pins. After connecting the circuit, you can operate LCD1602 by Raspberry Pi.

Experimental Procedures

Step1: Connect the circuit as shown in the following diagram

Step 2: Edit and save the code (see path/Rpi_LcdStartKit/15_lcd1602/lcd1602_python/Adafruit_CharLCD.py)

Step 3: Run the program      

sudo python Adafruit_CharLCD.py

Press Enter, and you will see two lines of information being displayed on the LCD1602. The first line displays our logo “SUNFOUNDER” and the second line display “Hello World !:)”. The real effect is shown as follow:

Further Exploration

In this experiment, we drive LCD1602 in 4-bit mode. I hope you can program by yourselves to drive LCD1602 in 8-bit mode.

Summary

Through this lesson, you have basically mastered the principle and programming implementation for LCD1602 based on Raspberry Pi. I hope you can create many more fun works on the basis of Raspberry Pi.

Code

#!/usr/bin/python # # based on code from lrvick and LiquidCrystal # lrvic – https://github.com/lrvick/raspi-hd44780/blob/master/hd44780.py # LiquidCrystal – https://github.com/arduino/Arduino/blob/master/libraries/LiquidCrystal/LiquidCrystal.cpp # from time import sleep class Adafruit_CharLCD: # commands LCD_CLEARDISPLAY = 0x01 LCD_RETURNHOME = 0x02 LCD_ENTRYMODESET = 0x04 LCD_DISPLAYCONTROL = 0x08 LCD_CURSORSHIFT = 0x10 LCD_FUNCTIONSET = 0x20 LCD_SETCGRAMADDR = 0x40 LCD_SETDDRAMADDR = 0x80 # flags for display entry mode LCD_ENTRYRIGHT = 0x00 LCD_ENTRYLEFT = 0x02 LCD_ENTRYSHIFTINCREMENT = 0x01 LCD_ENTRYSHIFTDECREMENT = 0x00 # flags for display on/off control LCD_DISPLAYON = 0x04 LCD_DISPLAYOFF = 0x00 LCD_CURSORON = 0x02 LCD_CURSOROFF = 0x00 LCD_BLINKON = 0x01 LCD_BLINKOFF = 0x00 # flags for display/cursor shift LCD_DISPLAYMOVE = 0x08 LCD_CURSORMOVE = 0x00 # flags for display/cursor shift LCD_DISPLAYMOVE = 0x08 LCD_CURSORMOVE = 0x00 LCD_MOVERIGHT = 0x04 LCD_MOVELEFT = 0x00 # flags for function set LCD_8BITMODE = 0x10 LCD_4BITMODE = 0x00 LCD_2LINE = 0x08 LCD_1LINE = 0x00 LCD_5x10DOTS = 0x04 LCD_5x8DOTS = 0x00 def __init__(self, pin_rs=14, pin_e=15, pins_db=[17, 18, 27, 22], GPIO = None): # Emulate the old behavior of using RPi.GPIO if we haven’t been given # an explicit GPIO interface to use if not GPIO: import RPi.GPIO as GPIO self.GPIO = GPIO self.pin_rs = pin_rs self.pin_e = pin_e self.pins_db = pins_db self.GPIO.setmode(GPIO.BCM) self.GPIO.setup(self.pin_e, GPIO.OUT) self.GPIO.setup(self.pin_rs, GPIO.OUT) for pin in self.pins_db: self.GPIO.setup(pin, GPIO.OUT) self.write4bits(0x33) # initialization self.write4bits(0x32) # initialization self.write4bits(0x28) # 2 line 5×7 matrix self.write4bits(0x0C) # turn cursor off 0x0E to enable cursor self.write4bits(0x06) # shift cursor right self.displaycontrol = self.LCD_DISPLAYON | self.LCD_CURSOROFF | self.LCD_BLINKOFF self.displayfunction = self.LCD_4BITMODE | self.LCD_1LINE | self.LCD_5x8DOTS self.displayfunction |= self.LCD_2LINE “”” Initialize to default text direction (for romance languages) “”” self.displaymode = self.LCD_ENTRYLEFT | self.LCD_ENTRYSHIFTDECREMENT self.write4bits(self.LCD_ENTRYMODESET | self.displaymode) # set the entry mode self.clear() def begin(self, cols, lines): if (lines > 1): self.numlines = lines self.displayfunction |= self.LCD_2LINE self.currline = 0 def home(self): self.write4bits(self.LCD_RETURNHOME) # set cursor position to zero self.delayMicroseconds(3000) # this command takes a long time! def clear(self): self.write4bits(self.LCD_CLEARDISPLAY) # command to clear display self.delayMicroseconds(3000) # 3000 microsecond sleep, clearing the display takes a long time def setCursor(self, col, row): self.row_offsets = [ 0x00, 0x40, 0x14, 0x54 ] if ( row > self.numlines ): row = self.numlines – 1 # we count rows starting w/0 self.write4bits(self.LCD_SETDDRAMADDR | (col + self.row_offsets[row])) def noDisplay(self): “”” Turn the display off (quickly) “”” self.displaycontrol &= ~self.LCD_DISPLAYON self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol) def display(self): “”” Turn the display on (quickly) “”” self.displaycontrol |= self.LCD_DISPLAYON self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol) def noCursor(self): “”” Turns the underline cursor on/off “”” self.displaycontrol &= ~self.LCD_CURSORON self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol) def cursor(self): “”” Cursor On “”” self.displaycontrol |= self.LCD_CURSORON self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol) def noBlink(self): “”” Turn on and off the blinking cursor “”” self.displaycontrol &= ~self.LCD_BLINKON self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol) def noBlink(self): “”” Turn on and off the blinking cursor “”” self.displaycontrol &= ~self.LCD_BLINKON self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol) def DisplayLeft(self): “”” These commands scroll the display without changing the RAM “”” self.write4bits(self.LCD_CURSORSHIFT | self.LCD_DISPLAYMOVE | self.LCD_MOVELEFT) def scrollDisplayRight(self): “”” These commands scroll the display without changing the RAM “”” self.write4bits(self.LCD_CURSORSHIFT | self.LCD_DISPLAYMOVE | self.LCD_MOVERIGHT); def leftToRight(self): “”” This is for text that flows Left to Right “”” self.displaymode |= self.LCD_ENTRYLEFT self.write4bits(self.LCD_ENTRYMODESET | self.displaymode); def rightToLeft(self): “”” This is for text that flows Right to Left “”” self.displaymode &= ~self.LCD_ENTRYLEFT self.write4bits(self.LCD_ENTRYMODESET | self.displaymode) def autoscroll(self): “”” This will ‘right justify’ text from the cursor “”” self.displaymode |= self.LCD_ENTRYSHIFTINCREMENT self.write4bits(self.LCD_ENTRYMODESET | self.displaymode) def noAutoscroll(self): “”” This will ‘left justify’ text from the cursor “”” self.displaymode &= ~self.LCD_ENTRYSHIFTINCREMENT self.write4bits(self.LCD_ENTRYMODESET | self.displaymode) def write4bits(self, bits, char_mode=False): “”” Send command to LCD “”” self.delayMicroseconds(1000) # 1000 microsecond sleep bits=bin(bits)[2:].zfill(8) self.GPIO.output(self.pin_rs, char_mode) for pin in self.pins_db: self.GPIO.output(pin, False) for i in range(4): if bits[i] == “1”: self.GPIO.output(self.pins_db[::-1][i], True) self.pulseEnable() for pin in self.pins_db: self.GPIO.output(pin, False) for i in range(4,8): if bits[i] == “1”: self.GPIO.output(self.pins_db[::-1][i-4], True) self.pulseEnable() def delayMicroseconds(self, microseconds): seconds = microseconds / float(1000000) # divide microseconds by 1 million for seconds sleep(seconds) def pulseEnable(self): self.GPIO.output(self.pin_e, False) self.delayMicroseconds(1) # 1 microsecond pause – enable pulse must be > 450ns self.GPIO.output(self.pin_e, True) self.delayMicroseconds(1) # 1 microsecond pause – enable pulse must be > 450ns self.GPIO.output(self.pin_e, False) self.delayMicroseconds(1) # commands need > 37us to settle def message(self, text): “”” Send string to LCD. Newline wraps to second line””” for char in text: if char == ‘\n’: self.write4bits(0xC0) # next line else: self.write4bits(ord(char),True) if __name__ == ‘__main__’: lcd = Adafruit_CharLCD() lcd.clear() #lcd.message(” LCD 1602 Test \n123456789ABCDEF”) lcd.message(” SUNFOUNDER \nHello World ! :)”)