Introduction
This is a 2X16 character RGB LCD+Keypad shield for Raspberry Pi. We made improvements in the wiring connection based on the previous LCD display as well as left out the contrast adjustment function, so the product can be pretty easy to use, and users can spend time focusing on the most important projects.
The RGB LCD1602 display is integrated on the shield. It leads out Raspberry Pi’s GPIO ports for connecting more device. Besides, the shield adopts IIC interface, so you can realize the 16 million color combination of the LCD, backlight brightness adjustment, display control etc. To convenient your use on Raspberry Pi, there are 5 push-buttons integrated on the board to help you to switch display and configure functions, then you can easily build up your data monitor and small operating platform.
IIC 16x2 RGB LCD Keypad HAT has two display effects: colorful background or colorful font.
Specification
- Supply Voltage: 5V
- Logic Voltage: 3.3V
- Operating Current: ≤60mA
- 16*2 LCD Display
- Communication: IIC
- Backlight: RGB adjustable backlight (16 million)
- Operating Temperature: -20 ~+70℃
- Storage Temperature: -30 ~ +80℃
- Dimension: 85.0×56.0×13.0mm/3.35×2.20×0.51"
Board Overview


GPIO(BCM Num) | Button Description |
---|---|
16 | Select |
17 | Up |
18 | Down |
19 | Left |
20 | Right |
Note: when using the Keys, the jumper needs to be installed first. For example, connect GPIO16 and Button via jumper cap, then the button Select can be used.
Tutorial
- Learn how to use RGB LCD KeyPad HAT on Raspberry Pi and set the color of the LCD screen.
- Learn the usage of Buttons and the calling of the programs.
Requirements
- Hardware
- Raspberry Pi Controller Board x 1
- RGB LCD KeyPad HAT x1
- HDMI Cable x1
- Display x1
- Keyboard and Mouse x1
IIC 16X2 RGB LCD KeyPad HAT


Operations
Step 1. Install RGB LCD KeyPad HAT onto Raspberry Pi board. The IIC interface of Raspberry Pi board is disabled by default, we have to enable it manually first:
Input the command into the Raspberry Pi terminal: sudo raspi-config
Press "Enter" on the keyboard to select: [Interfacing Options] (or [Advanced Options])->[I2C]->[Yes]->[OK]->[Finish]:





Step 2. Detect if the IIC address of the RGB LCD KeyPad HAT is detected by the Raspberry Pi bus:
Input the command into the Raspberry Pi terminal:
Install the test tool: sudo apt-get install i2c-tools
Detect the IIC device on the bus: sudo i2cdetect -y 1
Step 3 When finishing the above two steps, we can start to use RGB LCD KeyPad HAT now. To begin with, we must copy the library files into Raspberry Pi Board, and there are two ways to realize that:
a. Click to download DFRobot_RGB1602_RaspberryPi, copy the downloaded files to Raspberry Pi board via a U-disk;
b. If your Raspberry Pi is connected to network, you can directly use git command to get files through Raspberry Pi terminal, input the command: git clone https://github.com/DFRobot/DFRobot_RGB1602_RaspberryPi.git
Step 4. Run the C++ program[SetColor]: Set the LCD screen color to red.
Input the commands into Raspberry Pi terminal:
a. Switch the catelogue to DFRobot_RGBLCD library: cd DFRobot_RGB1602_RaspberryPi/DFRobot_RGBLCD
b. Check the files in DFRobot_RGBLCD: ls
c. Switch the catelogue to CPP files: cd cpp
e. Check the files in CPP: ls (all the example programs edited in c++ can be seen after this command is executed)
f. Compile the example program: make
g. Run the SetColor example program: ./SetColor
h. As shown below, input r into the terminal: 255 (Enter) g:0 (enter) b:0 (Enter)
i: Exit the program: Ctrl+C
- Note: if it fails to compile, try executing "make clean" command and then running the "make" command.
4-1: Run the python example program [SetColor]: set the color of the LCD screen to red.
a. Install the wiringpi library of python3: pip3 install wiringpi
b. Switch the catelogue to DFRobot_RGBLCD library: cd DFRobot_RGB1602_RaspberryPi/DFRobot_RGBLCD
c. Check the files in DFRobot_RGBLCD: ls
d. Switch the catelogue to python3 example program: cd python3
e. Check the files in python3: ls (all the example programs edited in python can be seen after this command is executed.)
f. Switch the catelogue to SetClolor: cd SetColor
g. Execute the SetColor example program: python3 SetColor.py
h. As shown below, input r into the terminal: 255(Enter) g: 0(Enter) b: 0(Enter)
i. Exit the program: Ctrl+C
Sample Code (c)
Function: set the color of the RGB LCD screen
Result: input r: 255 g: 0 b: 0 then the screen displays red.
- To check or edit codes, you can download them to Windows and then open directly, or input the command vim SetColor.cpp under the catelogue DFRobot_RGB1602_RaspberryPi/DFRobot_RGBLCD/cpp/SetColor in Raspberry Pi system.
Note: vim compiler needs to be installed first then you can continue the above operations. vim install command: sudo apt-get install vim-gtk
Save and exit: press ESC key, then press shift+zz(or switch to uppercase mode and press ZZ);
Exit without saving: press ESC key, then type a colon, and input the command "q!".
#include "../DFRobot_RGBLCD.h"
#include <iostream>
#include <string>
using namespace std;
int main(){
int r,g,b;
DFRobot_RGBLCD lcd(16, 2);
// initialize
lcd.init();
// Print a message to the LCD.
lcd.print("set cllor");
while(1){
cout << "r:";
cin >> r;
cout << "g:";
cin >> g;
cout << "b:";
cin >> b;
lcd.setRGB(r, g, b);
cout << "r = " << r
<< "g = " << g
<< "b = " << b
<< endl;
}
return 0;
}
Sample Code(Python)
Fucntion: detect the functions of the buttons
Result:
- When press the "Up" button, the LCD display Push the buttons UP
- When press the "Down" button, the LCD display Push the buttons DOWN
- When press the "Left" button, the LCD display Push the buttons LEFT
- When press the "Right" button, the LCD display Push the buttons RIGHT
- When press the "Select" button, the LCD display Push the buttons SELECT
To check or edit the codes, you can download them to Windows and open directly, or input the command vim Button.py under the catelogue DFRobot_RGB1602_RaspberryPi/DFRobot_RGBLCD/python3/Button in raspberry pi system.
Save and exit: press ESC key, then press shift+zz(or switch to uppercase mode and press ZZ);
Exit without saving: press ESC key, then type a colon, and input the command "q!".
import sys
sys.path.append('../')
import RPi.GPIO as GPIO
import rgb1602
import time
lcd = rgb1602.RGB1602(16,2)
GPIO.setmode(GPIO.BCM)
# Define keys
lcd_key = 0
key_in = 0
btnRIGHT = 0
btnUP = 1
btnDOWN = 2
btnLEFT = 3
btnSELECT = 4
GPIO.setup(16, GPIO.IN)
GPIO.setup(17, GPIO.IN)
GPIO.setup(18, GPIO.IN)
GPIO.setup(19, GPIO.IN)
GPIO.setup(20, GPIO.IN)
#Read the key value
def read_LCD_buttons():
key_in16 = GPIO.input(16)
key_in17 = GPIO.input(17)
key_in18 = GPIO.input(18)
key_in19 = GPIO.input(19)
key_in20 = GPIO.input(20)
if (key_in16 == 1):
return btnSELECT
if (key_in17 == 1):
return btnUP
if (key_in18 == 1):
return btnDOWN
if (key_in19 == 1):
return btnLEFT
if (key_in20 == 1):
return btnRIGHT
lcd.setCursor(0,0)
lcd.print("Push the buttons"); #print“Push the buttons”
while True:
lcd.setCursor(0,1)
lcd_key = read_LCD_buttons() # Reading keys
if (lcd_key == btnRIGHT):
lcd.print("RIGHT ")
elif (lcd_key == btnLEFT):
lcd.print("LEFT ")
elif (lcd_key == btnUP):
lcd.print("UP ")
elif (lcd_key == btnDOWN):
lcd.print("DOWN ")
elif (lcd_key == btnSELECT):
lcd.print("SELECT")
FAQ
For any questions, advice or cool ideas to share, please visit the DFRobot Forum
More Documents
Get IIC 16X2 RGB LCD KeyPad HAT (V1.0) from DFRobot Store or DFRobot Distributor.