樹莓派與傳感器
  • 前言
  • 樹莓派基礎
  • 樹莓派與Arduino
  • 樹莓派與Microbit
  • 用Python點亮LED
  • 把玩三色LED燈與PWM呼吸燈
  • 蜂鳴器
  • 按鈕開關
  • 人體移動感應器(PIR)
  • 尋線、避障與測距
  • 樹莓派:類比轉數位處理
  • 溫溼度感測器DHT11
  • 簡易的心跳偵測模組零件
  • 聲音感測器
  • 與火災相關的警報零組件
  • DS18B20溫度感測器
  • TM1638七節LED顯示器
  • MAX7219與矩陣式LED
  • 液晶顯示LCD1602
  • Django與物聯網
  • 使用VS Code遠端開發Django
  • 樹莓派與自走車
    • 控制馬達正反轉基礎
      • 自走車方向控制
    • 遠端鍵盤控制自走車+WebCam
    • 使用網路與搖桿、手機控制
  • 樹莓派與紅外線遙控器
  • 遠端GPIO:pigpio + piscope
Powered by GitBook
On this page

Was this helpful?

液晶顯示LCD1602

PreviousMAX7219與矩陣式LEDNextDjango與物聯網

Last updated 6 years ago

Was this helpful?

一、直接使用GPIO模式

要把樹莓派感測的資料顯示出來,比如溫度溼度等等,除了先前介紹的七節LED或是矩陣LED外,LCD1602是最常用的顯示模組,因為物美價廉且行之多年,要使用它有二種方法,先介紹直接使用GPIO的模式。

首先看一下它的接腳說明:其實真正使用的是上圖黃色的腳位,不要被16個腳位嚇到了

from RPLCD.gpio import CharLCD
import RPi.GPIO as GPIO
import time

lcd = CharLCD(cols=16, rows=2, pin_rs=37, pin_e=35, pins_data=[33, 31, 29, 23], numbering_mode=GPIO.BOARD)

lcd.clear()
lcd.home()
lcd.write_string('Hello World\r\nMy Name is Wooss')
time.sleep(3)
lcd.clear()

for i in range(10):
    lcd.cursor_pos = (0, 0)
    lcd.write_string("Time: %s" % time.strftime("%H:%M:%S"))
    print("Time: %s" % time.strftime("%H:%M:%S"))
    print("Date: %s" % time.strftime("%m/%d/%Y"))
    lcd.cursor_pos = (1, 0)
    lcd.write_string("Date: %s" % time.strftime("%m/%d/%Y"))
    time.sleep(1)

framebuffer = [
    '',
    '',
]


def write_to_lcd(lcd, framebuffer, num_cols):
    lcd.home()
    for row in framebuffer:
        lcd.write_string(row.ljust(num_cols)[:num_cols])
        lcd.write_string('\r\n')

lcd.clear()
lcd.home()
write_to_lcd(lcd, framebuffer, 16)

long_string = 'This string is too long to fit'


def loop_string(string, lcd, framebuffer, row, num_cols, delay=0.3):
    padding = ' ' * num_cols
    s = padding + string + padding
    for i in range(len(s) - num_cols + 1):
        framebuffer[row] = s[i:i + num_cols]
        write_to_lcd(lcd, framebuffer, num_cols)
        time.sleep(delay)


while True:
    loop_string(long_string, lcd, framebuffer, 0, 16)

# GPIO.cleanup()

使用的Python Package 為

參考網站一:

參考網站二:

https://github.com/dbrgn/RPLCD
http://www.circuitbasics.com/raspberry-pi-lcd-set-up-and-programming-in-python/
https://www.raspberrypi-spy.co.uk/2012/07/16x2-lcd-module-control-using-python/