Raspberry Pi, 16×2 LCD and Node.js – Print stuff10 min read

In this post, we will interface Raspberry Pi and a 16 X 2 Liquid Crystal Display. And then we will write a Node.js program to print some information on the LCD using the lcd node module. We will see how to print a Digital clock, get the Pi’s IP Address and print the same and how we can interact with the world wide web and display the same on the LCD.

Below is a quick demo of how you can print stuff using a Web Browser

You can find the completed code here.

So let us get started.


If you are new to Raspberry pi and have not yet installed Node.js on it, I would recommend going through Getting Started with Raspberry pi and Node.js.

If you are new to electronics devices and circuits, I would recommend going through the video lectures from All About Circuits.

Components needed

  1. 1 – Raspberry pi B+
  2. 1 – Standard 16×2 LCD
  3. 1 – Breadboard
  4. 1 – 1K Ohm potentiometer

If you are new to Raspberry Pi GPIO, please refer this.

Understanding a 16×2 LCD

A typical 16×2 LCD consists of 16 connectors. There are 5 sets of operational pins.

  1. The Data pins
  2. The Register select pin
  3. The Read/Write pin
  4. The Enable pin
  5. The Contrast pin

The data pins are straight forward. They are sending data to the display (toggled high/low). We will only be using write mode and not reading any data.

The register select pin has two uses. When pulled low it can send commands to the LCD (like position to move to, or clear the screen). This is referred to as writing to the instruction or command register. When toggled the other way (1) the register select pin goes into a data mode and will be used to send data to the screen.

The read/write pin will be pulled low (write only) as we only want to write to the LCD based on this setup.

The enable pin will be toggled to write data to the registers.

The contrast pin is used to control the brightness of the LCD.

Apart from the above, there are 2 pairs of 5v and ground pins. One set is used to power the LCD and the second set is to control the back light.

LCD Pinout

  1. Ground
  2. VCC – 5v not 3.3v
  3. Contrast adjustment (VO) from potentiometer
  4. Register Select (RS). RS=0: Command, RS=1: Data
  5. Read/Write (R/W). R/W=0: Write, R/W=1: Read
  6. Clock (Enable). Falling edge triggered
  7. Bit 0 (Not used in 4-bit operation)
  8. Bit 1 (Not used in 4-bit operation)
  9. Bit 2 (Not used in 4-bit operation)
  10. Bit 3 (Not used in 4-bit operation)
  11. Bit 4
  12. Bit 5
  13. Bit 6
  14. Bit 7
  15. Backlight LED Anode (+)
  16. Backlight LED Cathode (-)

The above information is taken from Wiring the Cobbler to the LCD.

Setup LCD

Now that we have a basic understanding of the LCD, we will set it up with our Raspberry Pi B+.

Screen Shot 2014-11-17 at 9.20.13 pm

As you can see from the above diagram, We will connect

  • Pin 5 from the PI – GND to the first row of the breadboard
  • Pin 2 from the PI – 5V to the second row of the breadboard

From now on, we will use these 2 rows for power and ground.

Next, we will connect

  • Pin 1 and Pin 16 of the LCD to Row 1 – GND
  • Pin 2 and Pin 15 of the LCD to Row 2 – +5V

Next, we will connect the variable potentiometer

  • The negative end of the variable potentiometer to Row 1 of the breadboard – GND
  • The positive end of the variable potentiometer to Row 2 of the breadboard – +5V
  • The third pin on the variable potentiometer to Pin 3 of the LCD

The above setup will help us configure the contrast of the Backlight.

Now, power up the PI and you should see the LCD powered up. Before we proceed further, we need to make sure that contrast settings are correct. Else you will not see any display when you start printing text.

Adjust the variable potentiometer as shown below till you see the black boxes.

If you do not see the black boxes, recheck your connection. There is no point going further if this step fails.

Now that the LCD is setup, we will complete the remaining connections. Connect

  • RS Pin – Pin 4 of LCD to GPIO Pin 12 of the Pi
  • R/W Pin – Pin 5 of LCD to GND Pin – Row 1 of breadboard
  • E Pin – Pin 6 of LCD to GPIO Pin 21 of the Pi
  • Data Pin D4 – Pin 11 of LCD to GPIO Pin 5 of the Pi
  • Data Pin D5 – Pin 12 of LCD to GPIO Pin 6 of the Pi
  • Data Pin D6 – Pin 13 of LCD to GPIO Pin 17 of the Pi
  • Data Pin D7 – Pin 14 of LCD to GPIO Pin 18 of the Pi

Make sure that all connections are proper before proceeding.


Now that we have completed our hardware setup, we will print stuff on the LCD. Yay!!

Login to your pi via ssh – terminal/putty. As soon as you ssh into pi, you will be landing inside the /home/pi folder. We will create a new folder here named node_programs. And inside this folder, we will be maintaining all our programs. Run

mkdir node_programs

To step inside that folder, run

cd node_programs

For this post, we will create a new folder named printToLCD and will step inside this folder. Run

mkdir printToLCD && cd printToLCD

Note : You can run multiple commands separated by a &&.

First we will initialize a new node project here. Run

npm init

Fill it up as applicable.

Now, we will use a node module named lcd to interact with the LCD from inside our Node.js code. This is the simplest way to interact with the LCD from our Pi. Run

npm install lcd --save

Next, we will create a new file named index.js. And we will open the same in the nano editor. Run

nano index.js

The first example we are going to do is to show a Digital Clock.

Digital Clock

Paste the below code into the nano editor

Things to notice

Line 1 : We require the  LCD module

Line 2 : We init the module with our LCD – PI pin config. As you can read, rs stands for Register select, e stands for enable, data stands for the 4 data pins and finally the total rows and cols. Even though we have a 16 x 2 LCD, we will be using only the 8 x 1 part to print.

Line 10 :  LCD takes a few minutes to startup, so we have this handy ready method, inside which we write our printing logic.

Line 11 : Every 1000 milliseconds, we execute the logic on line 12 and 13.

Line 12 : Position the cursor at 0th row and 0th col of the LCD

Line 13 : Prints the time part of the Date object

Line 18 : Cleans resources on exit.

Let us save the file now. To save the program, press (ctrl+x). This will ask you to save the file. Press Y and press enter key to complete the operation.

Execute the program by running

sudo node index.js

And you should see

Simple and easy right!

Scrolling Text

Next, we will see how to scroll a text. First, create a new file

  nano scrollingText.js

And paste the below code into the nano editor

We init the LCD as we did in the above program and instead of using the  lcd.print() we have written our own method here that will print one character at a time (line 23) and when we reach the end, we set the position back to the first character (line 19). And we repeat the process of printing a character every 300 milliseconds (line 25) emulating a marquee.

Save the program and run

sudo node scrollingText.js

And you should see

Print Two Rows

Next, we will print text in 2 rows, one after the other. Create a new file

nano printTwoRows.js

And paste the below code inside the nano editor

Once we have init the LCD, we will wait for the ready event. Next, we set the cursor to first row and first col and print the time. We wait on the printed event and then we set the cursor to second row first col and print the date.

Save the file and run

sudo node printTwoRows.js

And the output would look like

Print IP Address

Till now we have been printing static content. Now, we will query the OS and get the IP address of PI. And we will print the IP Address on to the LCD.

First create a new file

nano ipAddress.js

And paste the below code in the nano editor

The above code is almost same as the scrolling text one, except for line 13. Here we query the OS and get the IP address. This IP address is then used for printing.

Save the above file and run

sudo node ipAddress.js

Sweet right!

Stock Ticker

Now, we will Fire a request to the Google Finance API and then print the same on the LCD. Create a new file

nano printStockData.js

 Paste the below code into the nano editor

The above program is again derived from the scrolling text program. Except for the data that will be printed. In the above program, we init the LCD and at the very end (line 77), we trigger the  getQuote(). This function will contact the Google Finance API and get the ticker for the provided stock. We then parse this response and print the same to the console (line 43) as well as the LCD (line 48).

Save the file and run

sudo node printStockData.js

And you should see

Sweet Right!!


For the final example, we will host a web application. This will contain a text box, where a user can enter text. Upon submission of this text, we will display it on the LCD.

For this program, we will use another node module named tiny-router. This is a simple lightweight alternative of Express.js targeted at devices like PI. Let us first install it locally. Run

npm install tiny-router --save

And now, create a new file

nano server.js

And paste the below code into the nano editor

Things to notice

Line 1,2,4 :  We require the dependent modules

Line 5 : We init the code

Line 13 : We show a blinking cursor (for effect)

Line 15 : We listen to the /  route and dispatch the home page (which we will create in a moment).

Line 24 : This is the method that gets invoked when a user submits a text. Here we capture the text and print it on line 30. The logic is similar to the scrolling text program.

Save and close the server.js.

Now, we will create the home page. Create a new file

nano index.html

And paste the below code in the nano editor

A simple Bootstrap, jQuery page that will read a text from the textbox and post it to the server.

Save the file and run

sudo node server.js

And you should see

Simple and easy!

Hope this post gave you an idea how to work with a 16 x 2 LCD and Raspberry PI B+

Thanks for reading! Do comment.