raspberry pi lcd display 16x2 free sample
In the previous project of the Raspberry Pi Series, I have shown you how to blink an LED using Raspberry Pi and Python Program. Moving forward in the series, in this project, I’ll show you the interfacing 16×2 LCD with Raspberry Pi.
In this project, you can see all the steps for Interfacing a 16×2 LCD with Raspberry Pi like circuit diagram, components, working, Python Program and explanation of the code.
Even though the Raspberry Pi computer is capable of doing many tasks, it doesn’t have a display for implementing it in simple projects. A 16×2 Alphanumeric Character LCD Display is a very important types of display for displaying some basic and vital information.
A 16×2 LCD is one of the most popular display modules among hobbyists, students and even electronics professionals. It supports 16 characters per row and has two such rows. Almost all the 16×2 LCD Display Modules that are available in the market are based on the Hitachi’s HD44780 LCD Controller.
The pin description in the above table shows that a 16×2 LCD has 8 data pins. Using these data pins, we can configure the 16×2 LCD in either 8 – bit mode or 4 – bit mode. I’ll show the circuit diagram for both the modes.
In 8 – bit mode, all the 8 data pins i.e. D0 to D7 are used for transferring data. This type of connection requires more pins on the Raspberry Pi. Hence, we have opted for 4 – bit mode of LCD. The circuit diagram (with Fritzing parts) is shown below.
The following image shows the wiring diagram of the featured circuit of this project i.e. LCD in 4 – bit mode. In this mode, only 4 data pins i.e. D4 to D7 of the LCD are used.
NOTE: In this project, we have used the 4 – bit mode of the 16×2 LCD display. The Python code explained here is also related to this configuration. Slight modifications are needed in the Python Program if the circuit is configured in 8 – bit mode.
The design of the circuit for Interfacing 16×2 LCD with Raspberry Pi is very simple. First, connect pins 1 and 16 of the LCD to GND and pins 2 and 15 to 5V supply.
Then connect a 10KΩ Potentiometer to pin 3 of the LCD, which is the contrast adjust pin. The three control pins of the LCD i.e. RS (Pin 4), RW (Pin 5) and E (Pin 6) are connected to GPIO Pin 7 (Physical Pin 26), GND and GPIO Pin 8 (Physical Pin 24).
Now, the data pins of the LCD. Since we are configuring the LCD in 4 – bit mode, we need only 4 data pins (D4 to D7). D4 of LCD is connected to GPIO25 (Physical Pin 22), D5 to GPIO24 (Physical Pin 18), D6 to GPIO24 (Physical Pin 16) and D7 to GPIO18 (Physical Pin 12).
The working of project for Interfacing 16×2 LCD with Raspberry Pi is very simple. After making the connections as per the circuit diagram, login to your Raspberry Pi using SSH Client like Putty in Windows.
Alternatively, you can use any VNC Viewer software like RealVNC. (NOTE: I’ve used RealVNC Software for accessing the Raspberry Pi’s Desktop on my personal computer).
I’ve created a folder named “Python_Progs” on the desktop of the Raspberry Pi. So, I’ll be saving my Python Program for Interfacing 16 x 2 LCD with Raspberry Pi in this folder.
Using “cd” commands in the terminal, change to this directory. After that, open an empty Python file with name “lcdPi.py” using the following command in the terminal.
Now, copy the above code and paste it in the editor. It is important to properly use the Tab characters as they help in grouping the instructions in Python.
Save the file and close the editor. To test the code, type the following command in the terminal. If everything is fine with your connections and Python Program, you should be able to see the text on the 16×2 LCD.
First, I’ve imported the RPi.GPIO Python Package as GPIO (here after called as GPIO Package) and sleep from time package. Then, I have assigned the pin for LCD i.e. RS, E, D4, D5, D6 and D7. The numbering scheme I followed is GPIO or BCM Scheme.
Finally, using some own functions like lcd_init, lcd_string, lcd_display, etc. I’ve transmitted the data to be printed from the Raspberry Pi to the 16×2 LCD Module.
By interfacing 16×2 LCD with Raspberry Pi, we can have a simple display option for our raspberry Pi which can display some basic information like Date, Time, Status of a GPIO Pin, etc.
Many simple and complex application of Raspberry Pi like weather station, temperature control, robotic vehicles, etc. needs this small 16×2 LCD Display.
I was searching for the 16x2 LCD Interfacing with Raspberry Pi and found that there are not many Instructions About interfacing the simple Connection anywhere in the Community Blogs Hence, I decided to create a small instruction to interface the LCD with Raspberry Pi using Adafruit CharLCD Library.
To interface the LCD with Raspberry Pi we need to install The Circuit Python and CharLCD Library.Firstly, you need to install the circuit python Libraries. The following Link shows How to Install Circuit Python On the raspberry pi platform
This repository contains all the code for interfacing with a 16x2 character I2C liquid-crystal display (LCD). This accompanies my Youtube tutorial: Raspberry Pi - Mini LCD Display Tutorial.
During the installation, pay attention to any messages about python and python3 usage, as they inform which version you should use to interface with the LCD driver. For example:
It is possible to define in CG RAM memory up to 8 custom characters. These characters can be prompted on LCD the same way as any characters from the characters table. Codes for the custom characters are unique and as follows:
For example, the hex code of the symbol ö is 0xEF, and so this symbol could be printed on the second row of the display by using the {0xEF} placeholder, as follows:
This demo uses ping and nc (netcat) to monitor the network status of hosts and services, respectively. Hosts and services can be modified by editing their respective dictionaries:
exchangerate-api.com / free.currencyconverterapi.com: There are a lot of currency apis but these ones offer free currency exchange info. Both are used, one as main, the other as backup. Requires an API key to use.
In order to use the script, you need to get API key tokens for both exchange rate services and the weather api. Once you"ve done that, edit the script to put your tokens in the USER VARIABLES section.
Pin 4The register select signal (RS) determines whether the Data Bit values are interpreted as a command (E.g. clear screen) or data (aka: a character to display).
Pin 5Is the Read/Write pin. In read mode, this pin is used to get feedback from the LCD to work out if the LCD can accept commands or to indicate it is too busy.
If read is enabled and Pin4 on the LCD is connected to a pin on your Raspberry Pi, there is a chance that you can destroy your Pi. We only ever want to write to the LCD, we never want to read from it. So this should always be connected to ground.
Pin 6The enable pin (E)functions as the command/data latching signal for the LCD. The LCD will latch in whatever is on the Data Bits and process it on the falling edge of the E signal
Below shows how to wire up the LCD to the Raspberry Pi. We will be using 4 pin mode, so there is no need to connect pins 7 to 10. This LCD doesn’t use the backlight pins, pins 15 and 16. It also doesn’t use the contrast pin, pin 3.
In the past, you would have had to know the registers used by the controller to setup the display, position the cursor or even write a single character. There was also timing issues to take into consideration. This is no more, with thanks to WiringPi.
The code below is a very simple example of displaying some text on the top line of the LCD. There is a complete list of all the functions in the LCD library on the WiringPiwebsite.#include
d0-d7 = 8 or 4 bit mode. In for 4 bit mode, only specificity pins from d0 to d3. If using 8 bit mode, you need to specify pins from d0 to d7. In the example code above, we are using 4 bit mode.
In this tutorial we"ll take you through how to connect a 16x2 LCD display up to your Raspberry Pi using GPIO pins. Being able to display a message on the LCD is not only very cool but can be pretty useful too, for example in this tutorial we"ll cover how to get your LCD display to display the IP address of your raspberry Pi.
For this exercise we are going to control the LCD display using 4-bit mode. Whilst is is possible to connect to it in other ways using I2C or the UART this is the most direct method. In order to control the display in this way will we need to use 6 pins on the GPIO port, 4 data pins and 2 control pins.
Register Select – This toggles the Lcd display between two modes, Command mode (high) and Data mode (low). Command mode gives a Instruction to the LCD. Example – “Clear the display” , “Move cursor to home” etc and Data tells the LCD to display characters.
In order for the LCD to work we will wire the circuit up in a fashion similar to the diagram above, but hold off connecting everything together for now! The list below tells you exactly what the pins on the LCD connect to:
Begin assembling the circuit by inserting the Adafruit cobbler into the breadboard. Remember to straddle the cobbler over the centre of the breadboard so that no two pin is in the same row. Next insert the LCD display into the breadboard. Connect the 5V and GND pins from the cobbler to the top of breadboard and also connect Pins 1, 2, 15 on the LCD and 16 to their respective power rails. Your circuit should look similar to the picture below:
Connect the GPIO ribbon cable from the cobbler to the Pi, if everything is working correctly the back light on the LCD should turn on like on the picture above. If it doesn"t work check everything is wired up correctly
Next wire up the potentiometer. The middle pin of the potentiometer is connected to Pin 3 on the LCD display and the other two pins are connected to ground and 5V (it doesn"t matter which way round). Check the potentiometer is working by twisting the nob until you see boxes appear in the first line of the display like in the picture below:
In order to utilize the GPIO pins within Python you will need to Install the GPIO python library. Instructions on how to install the GPIO Python library can be found here.
To get the Python code to run the LCD display we are going to "grab" it from adafruit using GitHub. Make sure your Rasp berry Pi is connected to internet and we"ll use the git command to clone the python code. Run the following commands in the terminal to download the files.
Now we can test the display is working and it is wired up correctly. One of the files we downloaded Adafruit_CharLCD.py contains python class for LCD display control. It also contains a small piece of code so when the program is run it will display a message on the LCD.
If you are using Version 2 of the Raspberry pi you will need to edit the program slightly since pin #21 has now been changed to pin #27. Open the file Adafruit_CharLCD.py with Python or use nano Adafruit_CharLCD.py command to edit the program within the terminal. Go to line 57 of the code and replace:
Feel free to dive into the code of the program and change what"s displayed on the LCD. To do so open the program to edit like before and scroll to the last line of the code:
Simply change what is typed in the brackets after lcd.message() to display the text you want. The command is used to wrap the text onto a new line. A neater way of doing this is to change the last part of the program to look like the following:
This way when you run the program you will be prompted by "type your message here" to enter a message via your keyboard, which will then be displayed on the LCD. This was done by defining a new variable "message" that is equal to the command raw_input(), which allows the user to manually enter text. The part within the brackets of the raw_input() command is simply printed on the computer screen to prompt you what to write.
Getting the LCD display to display some text of your choosing is cool but not that useful. Running the program Adafruit_CharLCD_IPclock_example.py will display the date/time and the IP address of the Pi on the LCD. The program calls upon the methods from the previous program Adafruit_CharLCD.py. Feel free to open the program to look at the coding. To do so open the program in python or use the command sudo nano Adafruit_CharLCD_IPclock_example.pyin the terminal.
As an enthusiast, I started working with embedded with Netduino. It was fun but at the same time Netduino was not offering that much things that I can do now. For a person like me who has no knowledge of electronics and C/C++, this was a gift. Now here I am using Raspberry PI’s latest version and I have everything I ever wanted on a single piece of hardware.
To get started with Python and Raspberry PI, I looked into the “Hello World” sort of LED blinking example. Using the LCD is the second thing I would like to test and so here it is. Before you start, here are the things you will need.
As you can see from the wiring diagram, the LCD will take up around 6 GPIO pins on your Raspberry PI. If you have some modules plugged in or you are planning to then there are chances that you will fall short of the GPIO pins. To save GPIO pins on your board, you can use MCP23008 or MCP23017 . To keep it simple, I am not using any port expanders for now.
Now comes the code, I have a very little knowledge of Python at the moment. So I am going to stick with what I have read and tested. For controlling the LCD, I am going to use Adafruit’s LCD library which you can get it from Github here. I am going to use Adafruit_CharLCD.py to control my LCD. The thing to keep in mind is that you cannot use this library out of the box. You have to make a change to set the correct GPIO pins in the __init__ function. Open the file using the below command.
The reason I am using Adafruit library to control the display is because it has other useful functions to control LCD. Here is the list of functions that you can try.
I hope this will be useful to the people who are just getting started with Raspberry Pi. You can also download the complete source code from Github here.
It’s not only the devices that have experienced rapid development. The development boards used for them have started to become more and more commercial and accessible.
For this demo, we will use the ClimaCell Weather API as a weather data provider, as they have a large number of indicators, including air quality indicators, for us to use.
As soon as we have this API key, we can move to the hardware configuration and connect the LCD screen to our Raspberry Pi. You should turn the Raspberry Pi off while you make the wire connection.
This hardware connection will make the LCD screen be on full brightness and full contrast. The brightness level is not a problem, but contrast is because we won’t be able to see the characters on the screen.
At this point, we can turn on our Raspberry Pi and we should see the LCD screen alive. With the help of variable resistance we should be able to control the contrast.
As a programming language, we’ll use NodeJS to write the code. If you don’t already have NodeJS installed on your Raspberry then you can follow these simple instructions.
In a new folder, run the command npm init -y to set up a new npm package, followed by the command npm install lcd node-fetch to install these 2 necessary dependencies.lcd will be used to communicate with the LCD Screen
We said that we need an API key to communicate with the weather data provider. You place your secret API key directly in the main code, or you can create a config.json file in which you can place this key and any other code-related configuration you may have.
Writing on the screen is a piece of cake using the lcd module. This library acts as a layer of abstraction over how we communicate with the device. In this way we don’t need to micro-manage each command individually.
The keys cols and rows represent the number of columns and rows of our LCD display. 16x2 is the one I used in this example. If your LCD has just 8 columns and 1 row, then replace 16 and 2 with your values.
At this point, you can use this function and print something on your display. writeToLcd(0,0,"Hello World") should print the message Hello World on the first row starting from the first column.
ClimaCell provides a lot of weather data information, but also air quality and pollen, fire and other information. The data is vast, but keep in mind that your LCD screen only has 16 columns and 2 rows – that’s just 32 characters.
To find your city’s coordinates, you can use a free tool like latlong.net and then you can save them in config.json file along with your API key, or you can write them directly in the code.
The weather data is updated every 5 minutes. But because we have a limit of 100 API Calls / Hour imposed by ClimaCell, we can go even further and update the weather data each minute.
To print the time in the upper right corner, we must first calculate the starting column so that the text fits snugly. For this we can use the next formula total columns number minus text to display length
The LCD setting is asynchronous, so we must use the method lcd.on() provided by the related library, so we know when the LCD has been initialized and is ready to be used.
Another best practice in embedded systems is to close and free the resources that you use. That’s why we use the SIGNINT event to close the LCD screen when the program is stopped. Other events like this one include:SIGUSR1 and SIGUSR2 - to catch "kill pid” like nodemon restart
At this point you’re probably connected to your Raspberry Pi using SSH or directly with an HDMI cable and a monitor. No matter what, when you close your terminal the program will stop.
From this point you can customize your new device however you want. If you find this weather data important for you (or any other data from ClimaCell, like air pollution, pollen, fire index or road risk), you can create a custom case to put the Raspberry Pi and the LCD display in it. Then after you added a battery you can place the device in your house.
Raspberry Pi is like a personal computer, so you can do much more on it than you would normally do on a microcontroller like Arduino. Because of this, it"s easy to combine it with other devices you have in your house.
Add any 16x2 or 20x4 LCD-screen with a Hitachi HD44780 controller using either a port expander connected through I2c or just wire through GPIO 4 or 8 bit.
1 LCD-screen Hitachi HD44780 controller (PCF8574, or MCP23008 or MCP23017) with an I2c port expander. Or just wire through GPIO 4 or 8 bit. We recommend using a LCD-screen with an I2c port expander as it uses less wire (only 4) and is faster and more stable.
If your LCD has a PCF8574T chip from Texas Instruments, its default I2C address is 0x27Hex. If your LCD has a PCF8574AT chip from NXP semiconductors, its default I2C address is 0x3FHex. So your LCD probably has an I2C address 0x27Hex or 0x3FHex.
Since the Raspberry Pi GPIO only handle 3.3v, it will therefore be a good idea to use a I2C-safe Bi-directional Logic Level Converter so you don’t fryed your pi.
R2: Potentiometers: 10K Ohms. Controls the contrast and brightness of the LCD. Using a simple voltage divider with a potentiometer, we can make fine adjustments to the contrast.
I finally freed up one of my breadboards. I got my semi-permanent temperature sensing interface fully up and running with the Pi Cobbler – logging to COSM.
The next thing I wanted to get working was a 16 x 2 LCD panel. (£6 from Tandy) Having seen other people get these working, I figured it couldn’t be all that hard and it wasn’t too bad actually. But I did make one small mistake along the way. I got it working the second time I tried it.
The mistake I made was trying to run it from a separate 5 Volt supply instead of directly from the Pi. I hadn’t connected it to the Pi’s earth, which I think is why it didn’t work first time round. Properly grounded, I think it would run from a separate supply (but don’t connect the +ves together or the regulators will have a fight).
There are 3V3 (3.3 Volt) versions of these LCDs available, but the 5V ones are more common. According to my flavour-of-the-month site, Adafruit, it’s safe enough to use a 5 Volt LCD with the Raspberry Pi as long as the read-write pin (pin 5) is connected to ground. As long as we only “write” to the screen and don’t try to take input from it (like you would on some embedded device with input buttons e.g. a battery charger).
That way, you won’t “send” a 5 Volt signal to the Pi’s GPIO (General Purpose Input Output) ports, which run on 3V3. Sending a 5 Volt signal to a 3V3 port would be very likely to toast the port. This is the basis of the instructions I followed…
…but I didn’t use the Pi Cobbler this time as it’s already in use with my semi-permanent temperature logging setup. Once I get that onto a permanent board, the Cobbler will be free again.
I’ve noticed a few times that this screen sometimes needs the scripts to be run a couple of times before it works properly. I don’t have an explanation for that. Once or twice it has displayed what looks like Japanese characters instead of Roman alpha-numerics.
Now this little LCD is displaying data pulled from my COSM temperature feed every 40 seconds. As I progressively add more sensors, I’ll be able to alternate the display, showing each set of readings for a few seconds before going on to the next. I’ve got plans for barometric pressure and light sensors already – who knows what else will crop up to occupy the remaining channels on the ADC? :rotfl: (Currently four channels available).
This tutorial shows how to use the I2C LCD (Liquid Crystal Display) with the ESP32 using Arduino IDE. We’ll show you how to wire the display, install the library and try sample code to write text on the LCD: static text, and scroll long messages. You can also use this guide with the ESP8266.
Additionally, it comes with a built-in potentiometer you can use to adjust the contrast between the background and the characters on the LCD. On a “regular” LCD you need to add a potentiometer to the circuit to adjust the contrast.
Before displaying text on the LCD, you need to find the LCD I2C address. With the LCD properly wired to the ESP32, upload the following I2C Scanner sketch.
After uploading the code, open the Serial Monitor at a baud rate of 115200. Press the ESP32 EN button. The I2C address should be displayed in the Serial Monitor.
Displaying static text on the LCD is very simple. All you have to do is select where you want the characters to be displayed on the screen, and then send the message to the display.
The next two lines set the number of columns and rows of your LCD display. If you’re using a display with another size, you should modify those variables.
Then, you need to set the display address, the number of columns and number of rows. You should use the display address you’ve found in the previous step.
To display a message on the screen, first you need to set the cursor to where you want your message to be written. The following line sets the cursor to the first column, first row.
Scrolling text on the LCD is specially useful when you want to display messages longer than 16 characters. The library comes with built-in functions that allows you to scroll text. However, many people experience problems with those functions because:
The messageToScroll variable is displayed in the second row (1 corresponds to the second row), with a delay time of 250 ms (the GIF image is speed up 1.5x).
In a 16×2 LCD there are 32 blocks where you can display characters. Each block is made out of 5×8 tiny pixels. You can display custom characters by defining the state of each tiny pixel. For that, you can create a byte variable to hold the state of each pixel.
In summary, in this tutorial we’ve shown you how to use an I2C LCD display with the ESP32/ESP8266 with Arduino IDE: how to display static text, scrolling text and custom characters. This tutorial also works with the Arduino board, you just need to change the pin assignment to use the Arduino I2C pins.