Lesson 18 – 1602 Display


Tutorial

1. Introduction
In this lesson, we will use raspberry pi to control a 1602 LCD to display number 0. This time we use 11 GPIO ports and 8-bit connection method.

2. Hardware Required

  • 1602 LCD Display *1
  • 10KΩ Potentiometer*1
  • RPI GPIO Shield*1
  • 40Pin Colorful Ribbon Cable *1
  • Breadboard*1
  • USB Cable*1
  • Raspberry Motherboard*1
  • Jumper Wire*Several

3. Connection Diagram

4. Programming

  • a. Use winSCP to put the lesson17_1602_LCD into the lesson folder of raspberry system.
    b. In the terminal input cd lesson17_1602_LCD to enter the folder.
    c. In the terminal execute make to generate a 1602_LCD executable file inside the folder.
    d. Wiring as the above diagram, then enter the lesson17_1602_LCD folder, execute sudo ./1602_LCD at the terminal.
    1602 LED will display the number.
    e. Ctrl + c can exit the processing program.

5. Sample Code

 

The first four lines below are –

  • #include <stdio.h>
  • #include <stdlib.h>
  • #include <stdint.h>
  • #include <wiringPi.h>
 

#include 
#include 
#include 
#include 

int main()
{

//int RS=21,RW=22,EN=23;
//int DB0=3,DB1=4,DB2=5,DB3=6,DB4=7,DB5=8,DB6=9,DB7=10;
int DB0=0,DB1=1,DB2=2,DB3=3,DB4=4,DB5=5,DB6=6,DB7=7;
int RS=21,RW=22,EN=23;
//int i;
int i;

wiringPiSetup();


   
  //Serial.begin(9600);
      pinMode(RS,OUTPUT);
      pinMode(RW,OUTPUT);
      pinMode(EN,OUTPUT);
      pinMode(DB0,OUTPUT);
      pinMode(DB1,OUTPUT);
      pinMode(DB2,OUTPUT);
      pinMode(DB3,OUTPUT);
      pinMode(DB4,OUTPUT);
      pinMode(DB5,OUTPUT);
      pinMode(DB6,OUTPUT);
      pinMode(DB7,OUTPUT);       
     
  
  digitalWrite(RS,HIGH);
  digitalWrite(RW,LOW);
  digitalWrite(RS,LOW);
  digitalWrite(EN,LOW);
  delay(1);
   //digitalWrite(DB0,0);
   //digitalWrite(DB1,0);
   digitalWrite(DB2,0);
   digitalWrite(DB3,1);
   digitalWrite(DB4,1);
   digitalWrite(DB5,1);
   digitalWrite(DB6,0);
   digitalWrite(DB7,0);
  digitalWrite(EN,HIGH);
  
  //Serial.println("zzl");
   
   delay(1);
   digitalWrite(EN,LOW);
   digitalWrite(RS,HIGH);
   
   delay(5);
  /************************************/
  digitalWrite(RS,HIGH);
  digitalWrite(RW,LOW);
  digitalWrite(RS,LOW);
  digitalWrite(EN,LOW);
  delay(1);
   digitalWrite(DB0,LOW);
   digitalWrite(DB1,LOW);
   digitalWrite(DB2,HIGH);
   digitalWrite(DB3,HIGH);
   for(i=4;i<8;i++)
   { digitalWrite(i,LOW); }  
  digitalWrite(EN,HIGH);
  

   
   delay(1);
   digitalWrite(EN,LOW);
   digitalWrite(RS,HIGH);
   
   delay(5);
  
  
  
  /**************************************/
  digitalWrite(RS,HIGH);
  digitalWrite(RW,LOW);
  digitalWrite(RS,LOW);
  digitalWrite(EN,LOW);
  delay(1);
   digitalWrite(DB0,LOW);
   digitalWrite(DB1,HIGH);
   digitalWrite(DB2,HIGH);
   digitalWrite(DB3,LOW);
   for(i=4;i<8;i++)
   { digitalWrite(i,LOW); }  
  digitalWrite(EN,HIGH);
  

   
   delay(1);
   digitalWrite(EN,LOW);
   digitalWrite(RS,HIGH);
   
   delay(5);
   /*******************************************/
   
  digitalWrite(RS,HIGH);
  digitalWrite(RW,LOW);
  digitalWrite(RS,LOW);
  digitalWrite(EN,LOW);
  delay(1);
   digitalWrite(DB0,HIGH);
   for(i=1;i<8;i++)
   { digitalWrite(i,LOW); }  
   digitalWrite(EN,HIGH);
    
   delay(1);
   digitalWrite(EN,LOW);
   digitalWrite(RS,HIGH);
   
   delay(5);
   /*************************************************/
 // put your setup code here, to run once:

for (;;) 
{
  digitalWrite(RS,LOW);
  digitalWrite(RW,LOW);
  digitalWrite(RS,HIGH);
  digitalWrite(EN,LOW);
  delay(1);
     for(i=0;i<4;i++)
    {digitalWrite(i,LOW);}
    digitalWrite(DB4,HIGH);
    digitalWrite(DB5,HIGH);
    digitalWrite(DB6,LOW);
    digitalWrite(DB7,LOW);
  digitalWrite(EN,HIGH);
  

  
   
   delay(1);
   digitalWrite(EN,LOW);
   digitalWrite(RS,LOW);
   delay(100);
   //while(1);
   //delay(5000);
  // put your main code here, to run repeatedly:

}
return 0;
}

 

Prerequisites

  1. This development track is based on the Rasbperry Pi and the Ultimate Starter Kit for the Raspberry Pi.
  2. You will need access to both the Raspberry Pi SBC and the electronics components part of the Ultimate Starter Kit for the Raspberry Pi kit to be able to work on these tutorials.
  3. If you haven’t purchased the Raspberry Pi 4 B yet please head over to our store and purchase one now. You can pick up the Ultimate Starter Kit for the Raspberry Pi from OztoyLib.
  4. Depending on where you live you might also be able to pick up the Raspberry Pi and Ultimate Starter Kit for the Raspberry Pi at your local electronics hobby store.

About the Ultimate Starter Kit for the Raspberry Pi

The Ultimate Starter Kit for the Raspberry Pi comes packed with ~37 different electronic bits (Sensors, LEDs, switches, LCD, servo, etc.) including tutorials to get started. You will learn how to connect up the ~37 different electronic bits (Sensors, LEDs, switches, LCD, servo, etc.), create circuits using the Raspberry Pi including learning to write code in C. As you progress through the different tutorials you will explore the different capabilities of the Raspberry Pi including the intricacies of integrating the Raspberry Pi with the different electronic bits i.e. sensors, LED’s, switches, servos, etc. included in the starter kit.

The Ultimate Starter Kit for the Raspberry Pi comes along with detailed tutorials including code samples. Ultimate Starter Kit for the Raspberry Pi walks you through the basics of using the Raspberry Pi in a hands-on way. You’ll learn the fundamentals of electronics, programming in C and gain hands on experience through working on the Raspberry Pi building creative projects. The kit includes a selection of the most common and useful electronic components with an ebook of 37 projects. Starting the basics of electronics, to more complex projects, the kit will get you interacting with the physical world using sensor and actuators. Along with the kit you get access to detailed tutorials and wiring diagrams.


About the Raspberry Pi

The Raspberry Pi is a series of small single-board computers developed in the United Kingdom by the Raspberry Pi Foundation to promote the teaching of basic computer science in schools and in developing countries. It is a capable little computer which can be used in electronics projects, and for many of the things that your desktop PC does, like spreadsheets, word processing, browsing the internet, and playing games. The original model became far more popular than anticipated, selling outside its target market for uses such as robotics.

The Raspberry Pi does not include peripherals (such as keyboards, mice and cases). However, some accessories have been included in several official and unofficial bundles. According to the Raspberry Pi Foundation, over 5 million Raspberry Pis were sold by February 2015, making it the best-selling British computer. By November 2016 they had sold 11 million units, and 12.5m by March 2017, making it the third best-selling “general purpose computer”. In July 2017, sales reached nearly 15 million.In March 2018, sales reached 19 million. Most Pis are made in a Sony factory in Pencoed, Wales; some are made in China or Japan.

You can read more about the Raspberry Pi here – RaspberryPi.org.

 

Questions