Anda di halaman 1dari 12

Part 3: Raspberry Pi and LCD

3.1 Introduction

An LCD, or liquid crystal display, is a simple screen that can display commands, bits of

information, or readings from your sensor - all depending on how you program your board.

Moreover, a Liquid Crystal Display (LCD) is a sophisticated module that can be used to display

text or numeric data.

Letters and numbers can easily be created using seven segment LED displays using a

simple wiring scheme obtained from the optoelectronic component's datasheet. Although seven

segment LED displays are energy efficient and easy to use, a major problem for the optoelectronic

component in sunlight is washout. The liquid crystal display (LCD) was developed to remove heat

and washout.

LCD 16x2 is a character type liquid crystal display, which can display 32 (16*2) characters

at the same time. It has 16 pins, of which at least 7 would be used each time.

In this paper, you’ll learn the basics of incorporating an LCD into your project. The

Raspberry Pi will allow simple text messages to be displayed on an LCD using the Python

programming language.

3.2 Objective

The aim of this paper is to make necessary codes to perform the required tasks using

Raspberry Pi.

Specifically, this includes:


1. Connect the LCD to GPIO (General Purpose Input Output) pins of PI to display characters

on it.

2. Write a program in Python to send the appropriate commands to the LCD through GPIO

and display the needed characters on its screen.

3. Make a program where the Raspberry Pi is connected to a LCD character display and

shows the status of the LED with switch.

4. Make a program where the Raspberry Pi is connected to a LCD character display and

shows the status if the sensor (LDR) detects.

3.3 MATERIALS

 Raspberry Pi  LDR

 10k potentiometer  10k resistor

 LCD 16x 2 Character display  Several jumper wires (Male to Female)

 Header  Laptop/PC

 LED  LAN connector

 Push Button/ Module  5-Volt supply to power Raspberry Pi

3.4 Procedures

The following shows the procedures used on the experiment.

3.4.1 LED with switch (STATUS)

The LCD has a 16-pin male header attached to it along the top edge. Pin 1 is the pin

closest to the corner of the LCD.

1. Create a schematic diagram of the LCD (Figure 2) to be connected in the Raspberry Pi

using GPIO ports.


2. Plug the 10K potentiometer. Connect one side of the potentiometer to 5V (pin number 2),

and the other side to GND (pin number 6). The potentiometer that’s in the circuit is

important for controlling the screen brightness.

Note : If you do not have one, then you can try swapping this out for a resistor. If you do use a

normal resistor, try using anything between 5k and 10k ohms. You may need to try out a few different

values before getting the perfect resistance.

3. Then, simply refer to the schematic diagram (Figure 2) below.

(VSS) goes to the GND.

(VDD) goes to the 5V.

(V0) goes to the middle pin of potentiometer.

(RS) goes to GPIO 22.

(RW) goes to GND.

(E) goes to GPIO 18.

(D4) goes to GPIO 16.

(D5) goes to GPIO 11.

(D6) goes to GPIO 12.

(D7) goes to GPIO 15.

(A ) goes to 5V.

(K) goes to GND.

Note: You are going to power the 16x2 LCD through the +5V rail. You can send control

signal of +3.3v to LCD but for working of LCD you need to power it by +5V. The LCD will not work

with +3.3V.
5. You may use your own module (Refer to Experiment 2) for the LED and switch. For the

LED, connect the anode to GPIO 20 (Pin 38) and cathode to the GND. Connect one side

of the switch to GPIO 21 (Pin 40) and the other side to 3.3V (Pin1).

6. Once everything is connected as per the schematic diagram (Figure 2), turn on the

Raspberry Pi to write the program in python.

7. Open the LXTerminal because you need to install/add the library from Adafruit.

Figure1. Running the Adafruit_CharLCD.py on the RPi using the LXTerminal.

 Adafruit_CharLCD.py – contains python class for LCD control.

7.1 Steps for adding the library from Adafruit.

1. Download the software files in a specific order. To obtain the files in the correct

order, open the LXTerminal and type the Linux command ~sudo apt-get install

git.

2. Then, download the files from Github using the following Linux command:

$ git clone https://github.com/adafruit/Adafruit Raspberry-Pi-Python-Code.git.


3. With the folders properly installed onto the RPi, you can change

the code directory using the linux command:

$cd Adafruit-Raspberry-Pi-Python-code

4. Inside the directory, install the Adafruit_CharLCD using the Linux command:

sudo python setup.py install.

8. Create a Python program.

9. Use the BCM instead of using the BOARD numbering.

10. After writing the program and executing it, the Raspberry Pi sends characters to LCD one

by one and the LCD displays the characters on the screen.

11. “BUTTON PRESSED” should pop up on your LCD as you press the button. Furthermore,

you may adjust the contrast using the potentiometer if you can’t make out the words

clearly.

12. Make sure the code is correct and double-check the connections.

3.4.3 Sensor On/Off (STATUS)

1. Create a schematic diagram of the LCD (Figure 3) to be connected in the Raspberry

Pi using GPIO ports.

2. Plug your 10K potentiometer. Connect one side of the potentiometer to 5V (pin number

2), and the other side to GND (pin number 6). The potentiometer that’s in the circuit is

important for controlling the screen brightness.

Note : If you do not have one, then you can try swapping this out for a resistor. If you do use a

resistor, try using anything between 5k and 10k ohms. You may need to try out a few different

values before getting the perfect resistance.

3. Then, simply refer to the schematic diagram below.


(VSS) goes to the GND.

(VDD) goes to the 5V.

(V0) goes to the middle pin of potentiometer.

(RS) goes to GPIO 22.

(RW) goes to GND.

(E) goes to GPIO 18.

(D4) goes to GPIO 16.

(D5) goes to GPIO 11.

(D6) goes to GPIO 12.

(D7) goes to GPIO 15.

(A ) goes to 5V.

(K) goes to GND.

Note: You are going to power the 16x2 LCD through the +5V rail. You can send control

signal of +3.3v to LCD but for working of LCD you need to power it by +5V. The LCD will not work

with +3.3V.

4. You may use the Arduino as ADC Converter. Connect GND (Pin 39) of Raspberry Pi

to the GND of Arduino. Also, connect GPIO21 to pin ~9 of Arduino.

5. Since LDR changes only the resistance, so you must connect it to 5V of the Arduino.

Then, connect the LDR to analog. Moreover, connect the output of the LDR to digital

which is the Arduino. Lastly, connect the Arduino to Raspberry Pi. Just simply refer to

the diagram again.

6. Once everything is connected as per the circuit diagram (Figure 3), turn on the

Raspberry Pi to write the program in python.

7. Create a Python program.

8. Since you’ve already added the library from Adafruit, proceed to the next step
9. Use the BCM instead of using the BOARD numbering.

10. After writing the program and executing it, the Raspberry Pi sends characters to

LCD one by one and the LCD displays the characters on the screen.

11. “LIGHT DETECTED” should pop up on the LCD. Furthermore, you may adjust the

contrast using the potentiometer if you can’t make out the words clearly.

12. Make sure the code is correct and double-check the connections.

3.5 Schematic Diagram

3.5.1 LED with switch (STATUS)

Figure 2. Schematic Diagram of LED with switch


3.5.2 Sensor On/Off (STATUS)

Figure 3. Schematic Diagram of Sensor (On/Off)


3.6 Codes

3.6.1 LED with switch (STATUS)

# Raspberry Pi pin assignment:


lcd_rs = 25 #pin22
lcd_en = 24 #pin18
lcd_d4 = 23 #pin16
lcd_d5 = 17 #pin11
lcd_d6 = 18 #pin12
lcd_d7 = 22 #pin15
lcd_backlight = 4

# Define LCD column and row size for 16x2 LCD.


lcd_columns = 16
lcd_rows = 2

# Initialize the LCD using the pins above.


lcd = LCD.Adafruit_CharLCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7,
lcd_columns, lcd_rows, lcd_backlight)

# Print a two line message


lcd.message('Hello User!')
# Wait 5 seconds
time.sleep(5.0) #time delay of 5.0 s
lcd.clear() #clear the message

try:
while True: #keeps the program running unless CTRL+C is press
if GPIO.input(21) == True:
GPIO.output(20, GPIO.HIGH) #set LED to turn on
lcd.message('Button Pressed!\nLED on')
time.sleep(1) #time delay of 1 s
lcd.clear()#clear the message
GPIO.output(20, GPIO.LOW) #set LED to turn off
except KeyboardInterrupt: #set GPIO alarm warning

GPIO.cleanup()#reset the program


3.6.2 Sensor On/Off (STATUS)

#Rasperry Pi Interface with LCD, LED and Sensor Status

import time #import from time library


import RPi.GPIO as GPIO #import from GPIO library
import Adafruit_CharLCD as LCD #import from Adafruit library
GPIO.setmode(GPIO.BCM) #set up BCM

GPIO.setup(21, GPIO.OUT) # set GPIO 21 as an output (LED)


GPIO.setup(20, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) #set GPIO 20 (button)
GPIO.output(21, GPIO.LOW) #set LED off
# Raspberry Pi pin configuration:
lcd_rs = 25 #pin22
lcd_en = 24 #pin18
lcd_d4 = 23 #pin16
lcd_d5 = 17 #pin11
lcd_d6 = 18 #pin12
lcd_d7 = 22 #pin15
lcd_backlight = 4

# Define LCD column and row size for 16x2 LCD.


lcd_columns = 16
lcd_rows = 2

# Initialize the LCD using the pins above.


lcd = LCD.Adafruit_CharLCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7,
lcd_columns, lcd_rows, lcd_backlight)

# Print a two line message


lcd.message('Hello User!')
time.sleep(5.0) #time delay of 5.0 s
lcd.clear() #clear the message

try:
while True: #keeps the program running unless CTRL+C is press

if GPIO.input(21) == True:
GPIO.output(20, GPIO.HIGH) #set LED to turn on
lcd.message('Sensor On, Detecting…!') #display message
time.sleep(1) #time delay of 1 s
lcd.clear()#clear the message
GPIO.output(20, GPIO.LOW) #set LED to turn off
except KeyboardInterrupt: #set GPIO alarm warning

GPIO.cleanup() #reset the program


3.6.3 Codes for Arduino

const int analogInPin = A0; # Analog input pin that the potentiometer is attached to
const int OutPin = 8; # Analog output pin that the LED is attached to

int sensorValue = 0; # value read from the pot


int outputValue = 0; # value output to the PWM (analog out)

# Your threshold value


int SensorThres = 400; # maximum amount

void setup() {
# initialize serial communications at 9600 bps:
Serial.begin(9600);
}

void loop() {
# read the analog in value:
sensorValue = analogRead(analogInPin);
# map it to the range of the analog out:
outputValue = map(sensorValue, 0, 1023, 0, 1000);
if (sensorValue > SensorThres)
{
digitalWrite(OutPin, HIGH);
}
else
{
digitalWrite(OutPin, LOW);
}

# print the results to the serial monitor:


Serial.print("sensor = ");
Serial.print(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);

# wait 2 milliseconds before the next loop


# for the analog-to-digital converter to settle
# after the last reading:
delay(200);
}
3.7 Conclusion

Communicating with the Raspberry Pi LCD 16×2 display is easy because of the library

provided by Adafruit. It is incredibly easy to write Python scripts to setup and alter the display.

Based on the experiment, it is easy to interface the LCD to Raspberry Pi although many

GPIO pin will be used in Raspberry Pi .When the LCD is added to any project it gives a good way

to monitor on what is happening on the system.

3.8 Exercise

1. Create a program using a temperature sensor. Display the temperature on the LCD.

Anda mungkin juga menyukai