Raspberry Pi with 20x4 LCD - D.I.Y. Series (3) | 樹莓派 加上 20x4 液晶顯示 - 自己動手系列(3)

in cn •  7 years ago  (edited)

Last time I bought couple of accessories for my Raspberry Pi3 from the bookstore in Shamshuipo.

上次在深水埗那書店買了幾件配件給我的樹莓派。

B8BBE34A-6C81-45F0-AC44-21E16C5EFC8A.jpeg

Let see what they are: First came a Lego-style box for Raspberry Pi3, look at the openings, they were customized for Pi 3.

讓我們仔細看看:先來是樂高積木款式的盒子,看看那些口子就知道這是pi 3 專用的。

4BA2D1CE-484F-4D94-99AF-E12A27268BB5.jpeg

Just need to align the holes on circuit board with the poles on the case, it was fairly easy to put Pi 3 in the case. This was how it looks like afterward.

只需要將電路板上的小孔對準盒中的小棒子,就可以把樹莓派安裝到盒子中。完成後就是這個樣子。

66CEDE09-A772-4AE9-860D-2F2A20C399C6.jpeg

D57BD1E1-777A-4B8E-B76D-9E13C1D8AE45.jpeg

20BD6175-3CF7-4FE4-9D45-A4E3CF4C6EC9.jpeg

BAD8EFB9-1678-4C84-9DC7-49B6BB84AF92.jpeg

The other 2 accessories were one integrated 20x4 LCD display and some female-to-female breadboard cables.

另外兩件配件是一塊經已整合好的20x4液晶顯示板和一些麵包板連接線。

3FDD41AA-8EC8-4B6E-9807-B37BF090E151.jpeg

50624029-F748-4DF5-B1D6-887B8D76C9EE.jpeg

CA89D61B-55EF-44AE-87A4-72FFCB8463E3.jpeg

DC7B6DC6-13CB-447E-B9A2-C4779C2EC0D8.jpeg

I was going to connect this LCD to Pi3 and use it for displaying of text. First of all I had to connect it to Pi3. The LCD already has a I2C LCD Controller integrated, I only need to connect the 4 pins of the controller to the GPIO pins of Pi3 using following mapping :

LCD Pin #Pi3 GPIO Pin #
GNDPIN 6
VCCPIN 2
SDAPIN 3
SCLPIN 5

我想把液晶顯示板接到樹莓派上作為顯示之用。所以我因為液晶顯示板已經整合了控制器,所以我只需根據上面的針腳配置表將它接到樹莓派的GPIO針腳上。

CC78023A-868C-4363-A416-E4BA1D038705.jpeg

25777C33-04E8-45BD-B11E-512B8ABA0C5E.jpeg

Then I had to enable i2c on Pi3. From Command Line type : sudo raspi-config

先要開啟在樹莓派上的i2c 支援,在命令行鍵入上面的命令

79D813BF-AED2-4EB2-94A7-BB1441AB7A03.jpeg

Complete following steps:

  • Select "Interfacing Options"
  • Select "I2C"
  • Select "Yes"
  • Select "Ok"
    Then select "Finish" to quit raspi-config.

完成上面的步驟之後就選"Finish"去離開。

6171BDC7-9D04-4101-A717-B02734B63F25.jpeg

87B8694E-2600-4493-8E4E-1EA890F9219E.jpeg

C3AF0291-A8C1-4345-8BFB-FC8165A85282.jpeg

4920F615-224B-40D0-9985-1AD7DD8578AF.jpeg

Then I have to install python i2c utilities "python-smbus" & "i2c-tools" by typing following commands in Command Line:

  • sudo apt-get update
  • sudo apt-get install -y python-smbus i2c-tools

在命令行打入上面的命令去安裝 python 應用 "python-smbus" 和 "i2c-tools"。

I tested the LCD hardware by

  • sudo i2cdetect -y 1

利用上面的命令去測驗液晶顯示板

372C6B75-FF66-4B45-97A5-0B6AE29DC203.jpeg

This showed the LCD was connected to address 0x3f.

可以見到液晶顯示板已經連接到記憶地址0x3f。

In order to talk to the LCD, python libraries were needed. Using following to steps to create the libraries:

要用上液晶顯示板,先要有相對應的python代碼庫。利用下面的命令去建立兩個檔案:

  • sudo nano i2c_lib.py

type following in the file:

把下面的代碼輸入到檔案中:

import smbus
from time import *

class i2c_device:
   def __init__(self, addr, port=1):
      self.addr = addr
      self.bus = smbus.SMBus(port)

# Write a single command
   def write_cmd(self, cmd):
      self.bus.write_byte(self.addr, cmd)
      sleep(0.0001)

# Write a command and argument
   def write_cmd_arg(self, cmd, data):
      self.bus.write_byte_data(self.addr, cmd, data)
      sleep(0.0001)

# Write a block of data
   def write_block_data(self, cmd, data):
      self.bus.write_block_data(self.addr, cmd, data)
      sleep(0.0001)

# Read a single byte
   def read(self):
      return self.bus.read_byte(self.addr)

# Read
   def read_data(self, cmd):
      return self.bus.read_byte_data(self.addr, cmd)

# Read a block of data
   def read_block_data(self, cmd):
      return self.bus.read_block_data(self.addr, cmd)

And other file | 第二個檔案:

  • sudo nano lcddriver.py

Type following in the file:

把下面的代碼輸入到檔案中:

import i2c_lib
from time import *

# LCD Address
ADDRESS = 0x3f

# commands
LCD_CLEARDISPLAY = 0x01
LCD_RETURNHOME = 0x02
LCD_ENTRYMODESET = 0x04
LCD_DISPLAYCONTROL = 0x08
LCD_CURSORSHIFT = 0x10
LCD_FUNCTIONSET = 0x20
LCD_SETCGRAMADDR = 0x40
LCD_SETDDRAMADDR = 0x80

# flags for display entry mode
LCD_ENTRYRIGHT = 0x00
LCD_ENTRYLEFT = 0x02
LCD_ENTRYSHIFTINCREMENT = 0x01
LCD_ENTRYSHIFTDECREMENT = 0x00

# flags for display on/off control
LCD_DISPLAYON = 0x04
LCD_DISPLAYOFF = 0x00
LCD_CURSORON = 0x02
LCD_CURSOROFF = 0x00
LCD_BLINKON = 0x01
LCD_BLINKOFF = 0x00

# flags for display/cursor shift
LCD_DISPLAYMOVE = 0x08
LCD_CURSORMOVE = 0x00
LCD_MOVERIGHT = 0x04
LCD_MOVELEFT = 0x00

# flags for function set
LCD_8BITMODE = 0x10
LCD_4BITMODE = 0x00
LCD_2LINE = 0x08
LCD_1LINE = 0x00
LCD_5x10DOTS = 0x04
LCD_5x8DOTS = 0x00

# flags for backlight control
LCD_BACKLIGHT = 0x08
LCD_NOBACKLIGHT = 0x00

En = 0b00000100 # Enable bit
Rw = 0b00000010 # Read/Write bit
Rs = 0b00000001 # Register select bit

class lcd:
   #initializes objects and lcd
   def __init__(self):
      self.lcd_device = i2c_lib.i2c_device(ADDRESS)

      self.lcd_write(0x03)
      self.lcd_write(0x03)
      self.lcd_write(0x03)
      self.lcd_write(0x02)

      self.lcd_write(LCD_FUNCTIONSET | LCD_2LINE | LCD_5x8DOTS | LCD_4BITMODE)
      self.lcd_write(LCD_DISPLAYCONTROL | LCD_DISPLAYON)
      self.lcd_write(LCD_CLEARDISPLAY)
      self.lcd_write(LCD_ENTRYMODESET | LCD_ENTRYLEFT)
      sleep(0.2)

   # clocks EN to latch command
   def lcd_strobe(self, data):
      self.lcd_device.write_cmd(data | En | LCD_BACKLIGHT)
      sleep(.0005)
      self.lcd_device.write_cmd(((data & ~En) | LCD_BACKLIGHT))
      sleep(.0001)

   def lcd_write_four_bits(self, data):
      self.lcd_device.write_cmd(data | LCD_BACKLIGHT)
      self.lcd_strobe(data)

   # write a command to lcd
   def lcd_write(self, cmd, mode=0):
      self.lcd_write_four_bits(mode | (cmd & 0xF0))
      self.lcd_write_four_bits(mode | ((cmd << 4) & 0xF0))

   # put string function
   def lcd_display_string(self, string, line):
      if line == 1:
         self.lcd_write(0x80)
      if line == 2:
         self.lcd_write(0xC0)
      if line == 3:
         self.lcd_write(0x94)
      if line == 4:
         self.lcd_write(0xD4)

      for char in string:
         self.lcd_write(ord(char), Rs)

   # clear lcd and set to home
   def lcd_clear(self):
      self.lcd_write(LCD_CLEARDISPLAY)
      self.lcd_write(LCD_RETURNHOME)

Make sure the i2C address in this file is set to #3f.

記得要把代碼中的i2c地址改成 #3f.

# LCD Address
ADDRESS = 0x3f

Finally I could use the LCD dispaly by creating a python script file:

最後就是建立python檔案來顯示文字到液晶顯示板:

  • sudo nano display.py
# loading the class
import lcddriver

from time import *

import time

# lcd start
lcd = lcddriver.lcd()

# this command clears the display (captain obvious)
lcd.lcd_clear()
# clock = time()

actions = ['Follow', ' Vote', ' Resteem']
action1 = ""
print ("Start : %s" % time.ctime())

# now we can display some characters (text, line)
lcd.lcd_display_string("Hello Steemit !", 1)
lcd.lcd_display_string("This is @guyverckw", 2)
lcd.lcd_display_string("   Please   ", 3)
for action in actions :
        action1 = action1 + action
        lcd.lcd_display_string(action1 , 4)
        time.sleep( 2 )

print ("End : %s" % time.ctime())

Then run the script | 執行代碼:

  • python display.py
    Then you should able to see this:

應該就能見到這樣:

0B4638DE-6672-4AB6-99F4-0CF216CE1B85.gif

I encountered a major problem when trying to make this works, there was nothing displayed on the LCD. After checking a lot of works from others, I found that it was the contrast of the LCD was not properly set. Contrast of it can be set at the switch on the controller:

我在這個過程中遇到最大的問題就是在液晶顯示板上看不見任何東西。經過大量翻查其他人的經驗,我發現原來是因為液晶顯示板的對比度設置得不好。只要在控制器上的這個開關上扭動一下,就可以改變對比度。

8731A3A3-3BAA-4900-97C6-1ED33982DCE7.jpeg

After contrast was adjusted correctly texts could be seen on LCD.

對比度設置妥當就能見到文字了。

FE267572-12B5-453C-8DB6-DFEF8290FE0A.jpeg

This conclude my test with this 20x4 LCD panel. I will use it on other projects later. I will share them after they are done. Stay tuned.

液晶顯示板的初試驗完滿結束。我將會把它用於其他的項目上面。有機會再為大家分享。


請關注!點讚!轉發!

0B4638DE-6672-4AB6-99F4-0CF216CE1B85.gif

Please Follow! Upvote! Resteem!

IMG_1080.JPG
兼賣樹莓派的書店 | Bookstore that sold Raspberry Pi parts

樹莓派 3 開盒及初建分享 | Raspberry Pi 3 Open box and Setup

!steemitworldmap 22.331800 lat 114.161748 long d3scr

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

wow this really requires a lot of skill I do not think I can build this on my own.

  ·  7 years ago 

Frankly, follow exactly the steps in my post, it is not difficult to build the same thing. Give it a try.

Upped!! Will you soon try SteemPi on it ? :)

  ·  7 years ago 

Actually, SteemPi is now on it already, just the LED part I not yet tested. After the LED part is done, I will post it.

Awesome :)
I just bought steempi.com (you can see it in my latest post very happy :) ! )

  ·  7 years ago 

This is a test for the LED notification of Steempi.

That's looks a great buy :D

  ·  7 years ago 

Yes, it is fun.

说难不难,说简单也不简单的小玩具啊。

  ·  7 years ago 

太容易就真的變成玩具了

好繽紛的線! 希望之後可以看見你們都玩些什麼~ :D

  ·  7 years ago 

Yes, madam!

Nice!!! :D

like your post....

雖然完全看不明白,但是用那 Lego 組裝很有趣!👍🏻👍🏻

  ·  7 years ago 

找不到其他Lego配件,否則應該更有趣

are you a engineer

  ·  7 years ago 

I used to be, not anymore

Wow nice post

Very good man

Thanks for sharing..

Nice buy.