What is LCD1602: 1602 crystal is also called 1602 character crystal. 1602 LCD means that the display content is 16×2, that is, it can display two lines, each line of 16 characters LCD module, it is a kind of dot matrix type specially used to display letters, numbers, symbols, etc. It consists of several 5×7 or 5×11 dot matrix characters, each of which can display one character. The character bits are separated by one dot pitch and there's a gap between each line. Therefore, characters are spaced within and between lines. Pins of an LCD screen are as follows We can clearly see on the table above that 1, 2 are the power wires. The voltage of the pin 3 can be used to adjust the contrast of the screen. Pin 4 RS is data and command selection; pin 5 RW is read and write control. Pin 6 EN is a data transfer enable pin, like a clock signal, when a pulse is generated, the data on the data pin is transferred to the LCD. DB0-DB8 is the data port of the liquid crystal. Users can choose the 8-bit transmission mode or switch to the 4-bit transmission mode.
VSS | GND |
VDD | 3.3V |
V0 | Analog input |
RS | Digital input |
RW | Digital input |
E | Digital input |
D0 | Digital input |
D1 | Digital input |
D2 | Digital input |
D3 | Digital input |
D4 | Digital input |
D5 | Digital input |
D6 | Digital input |
D7 | Digital input |
A | 3.3V |
K | GND |
What is IIC interface?
The IO ports of the micro: bit is limited. We need a lot of ports if we want
to directly drive the LCD1602. But there may be not enough ports to
connect other sensors. In order to solve this problem, a PCF8574-based I2C
interface module is used here. It is to expand the IO ports of the micro: bit
so that only two pins (SDA and SCL) are needed to control the LCD1602 and
to save many ports to connect more sensors.
PCF8574 pin information
IIC communication protocol Two bus lines, one data line SDA and one clock line SCL. The IIC bus has three types of signals in the process of transmitting data: start signal, end signal, and response signal. Start signal: When SCL is high, SDA transfers from high level to low level and starts transmitting data. End signal: When SCL is high, SDA transfers from low level to high level and ends transmitting data.
Response signal: After receiving the 8-bit data, the receiving IC sends a specific low-level pulse to the sending IC, indicating that the data has been received.
After the CPU sends a signal to the controlled unit, it waits for the controlled
unit to send a response signal. After receiving the response signal, the CPU
makes a judgment as to whether to continue transmitting the signal
according to the actual situation. If the response signal is not received, it is
judged to be an error occurs in the controlled unit.
IIC interface module:
SDA | Data Pin |
SCL | Clock Pin |
+ | 3.3V |
- | GND |
1 * microbit
1 * microbit expansion board
1 * USB cable
1 * IIC interface module
1 * lcd 1602
- Several Jumper wires
connection diagram:
Python code:
#----------------------------------------------------------- # File name : LCD1602.py. # Description : LCD1602 Display # Author : jason # E-mail : jason@adeept.com # Website : www.adeept.com # Date : 2019/01/07 #----------------------------------------------------------- from microbit import * # define lcd_i2c_addr 0x27 LCD_I2C_ADDR = 0x27 class LCD1620(): def __init__(self): self.buf = bytearray(1) self.BK = 0x08 self.RS = 0x00 self.E = 0x04 self.setcmd(0x33) sleep(5) self.send(0x30) sleep(5) self.send(0x20) sleep(5) self.setcmd(0x28) self.setcmd(0x0C) self.setcmd(0x06) self.setcmd(0x01) self.version = '1.0' def setReg(self, dat): self.buf[0] = dat i2c.write(LCD_I2C_ADDR, self.buf) sleep(1) def send(self, dat): d = dat & 0xF0 d |= self.BK d |= self.RS self.setReg(d) self.setReg(d | 0x04) self.setReg(d) def setcmd(self, cmd): self.RS = 0 self.send(cmd) self.send(cmd << 4) def setdat(self, dat): self.RS = 1 self.send(dat) self.send(dat << 4) def clear(self): self.setcmd(1) def backlight(self, on): if on: self.BK = 0x08 else: self.BK = 0 self.setdat(0) def on(self): self.setcmd(0x0C) def off(self): self.setcmd(0x08) def char(self, ch, x=-1, y=0): if x >= 0: a = 0x80 if y > 0: a = 0xC0 a += x self.setcmd(a) self.setdat(ch) def puts(self, s, x=0, y=0): if len(s) > 0: self.char(ord(s[0]), x, y) for i in range(1, len(s)): self.char(ord(s[i])) l = LCD1620() l.puts(" Welcome to") l.puts(" www.adeept.com", 0, 1)
Click “flash” and download the code onto the micro:bit.
We can see that the LCD1602 displays the text set in the codes.
Effect Picture:
Components
- 1 * Adeept Arduino UNO R3 Board
- 1 * LCD1602 Module
- 1 * IIC Interface Module
- 1 * USB Cable
- 1 * 4-Pin Wires
Build the circuit
Code:
/*********************************************************** File name: _49_TimerLCD1602.ino Website: www.adeept.com E-mail: support@adeept.com Author: Tom Date: 2019/01/07 ***********************************************************/ #include <Wire.h> #include <LiquidCrystal_I2C.h> //LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to "0x27"(or "0x3f") for a 16 chars and 2 line display LiquidCrystal_I2C lcd(0x3f,16,2); // set the LCD address to "0x3f"(or "0x27") for a 16 chars and 2 line display #define AR_SIZE( a ) sizeof( a ) / sizeof( a[0] ) char show0[]=" : : "; char show1[]=" www.adeept.com "; unsigned long oldTime=0; unsigned long newTime; unsigned long time; int Time = 17; int Minute = 35; int Second = 10; void setup() { lcd.init(); //initialize the lcd lcd.backlight(); //turn on the backlight lcd.clear(); //clears the LCD screen lcd.setCursor(0,0); // set the cursor to column 0, line 1 for (int positionCounter1 = 0; positionCounter1 < 16; positionCounter1++) { lcd.print(show0[positionCounter1]); // print a message to the LCD. } lcd.setCursor(0,1); // set the cursor to column 0, line 2 for (int positionCounter2 = 0; positionCounter2 < 16; positionCounter2++) { lcd.print(show1[positionCounter2]); // print a message to the LCD. } } void loop() { char strSecond[2]; char strMinute[2]; char strTime[2]; newTime = millis(); time = (newTime - oldTime)/1000; if(time>=1){ oldTime = newTime; Second++; } if(Second >= 60){ Second = 0; Minute++; } if(Minute>=60){ Minute = 0; Time++; } if(Time>=24){ Time = 0; } dtostrf(Second,2,0,strSecond); //Converts a floating-point number to a string lcd.setCursor(10,0); // set the cursor to column 10, line 1 for (int positionCounter1 = 0; positionCounter1 < 2; positionCounter1++) { lcd.print(strSecond[positionCounter1]); // print a message to the LCD. } dtostrf(Minute,2,0,strMinute); //Converts a floating-point number to a string lcd.setCursor(7,0); // set the cursor to column 7, line 1 for (int positionCounter1 = 0; positionCounter1 < 2; positionCounter1++) { lcd.print(strMinute[positionCounter1]); // print a message to the LCD. } dtostrf(Time,2,0,strTime); //Converts a floating-point number to a string lcd.setCursor(4,0); // set the cursor to column 4, line 1 for (int positionCounter1 = 0; positionCounter1 < 2; positionCounter1++) { lcd.print(strTime[positionCounter1]); // print a message to the LCD. } delay(100); }
Compile and download the sketch to the UNO R3 board.
Components
- 1 * Raspberry Pi
- 1 * GPIO Extension Board
- 1 * 40-Pin GPIO Cable
- 1 * Breadboard
- 1 * LCD1602
- 1 * Potentiometer Module
- 1 * I2C Interface Module
- 1 * 3-Pin Wires
- 1 * 4-Pin Wires
- Several Jumper Wires
Step 1: Build the circuit
Step 2: The I2C of the Raspberry Pi is not turned on by default, we can use raspi-config to
enable it
$ sudo raspi-config
Use the down arrow to select 5 Interfacing Options
Arrow down to P5 I2C
Select yes when it asks you to enable I2C.
Also select yes when it tasks about automatically loading the kernel module.
Use the right arrow to select the <Finish> button.
Select yes when it asks to reboot.
The system will reboot. when it comes back up, log in and enter the following
command:
$ ls /dev/*i2c*
The Pi should respond with
/dev/i2c-1
Which represents the user-mode I2C interface.
For C language users:
Step 3: Edit and save the code with vim or nano.
(code path: /home/Adeept_Sensor_Kit_for_RPi_C_Code/35_LCD1602/i2c_lcd1602.c)
Step 4: Compile
$ sudo gcc i2c_lcd1602.c -o i2c_lcd1602 -lwiringPi -lwiringPiDev
Step 5: Run
$ sudo ./i2c_lcd1602
For Python users:
Step 3: Edit and save the code with vim or nano.
(code path: /home/Adeept_Sensor_Kit_for_RPi_Python_Code/35_LCD1602/i2c_lcd1602.py)
Step 4: Run
$ sudo python i2c_lcd1602.py
Reference material:http://www.adeept.com/learn/
Video:http://www.adeept.com/video/