液晶顯示器介紹<檔案位置>
圖片來源:
LCD 為液晶顯示器,分為文字形與圖形兩種模式。
LCD1602: 可以顯示2列,每列16個字元。
LCD2004: 可以顯示4列,每列20個字元。
輔助顯示部分可開啟背光,提高裝置的可讀與辨識性,也可以關閉背光等功功能。
使用I2C :esp32 sda= GPIO_21 scl= GPIO_22
電源使用:5V
I2C 掃描程式
#include <Wire.h>
void setup()
{
Serial.begin (115200);
Wire.begin (21, 22); // sda= GPIO_21 /scl= GPIO_22
}
void Scanner ()
{
Serial.println ();
Serial.println ("I2C scanner. Scanning ...");
byte count = 0;
Wire.begin();
for (byte i = 8; i < 120; i++)
{
Wire.beginTransmission (i); // Begin I2C transmission Address (i)
if (Wire.endTransmission () == 0) //0=success(ACK response)
{
Serial.print ("Found address: ");
Serial.print (i, DEC);
Serial.print (" (0x");
Serial.print (i, HEX); // PCF8574 7 bit address
Serial.println (")");
count++;
}
}
Serial.print ("Found ");
Serial.print (count, DEC); // numbers of devices
Serial.println (" device(s).");
}
void loop()
{
Scanner ();
delay (5000);
}
LCD顯示測試
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);//0x27為裝置位址
// esp32 sda= GPIO_21 /scl= GPIO_22
void setup()
{
// initialize the LCD
lcd.begin();
// Turn on the blacklight and print a message.
lcd.backlight();
lcd.print("Hello, world!");
lcd.setCursor(0,1);//設定游標
lcd.print("CPshs.hcc.edu.tw!"); //印出文字
}
void loop()
{
// Do nothing here...
}
LCD顯示測試 使用序列窗傳遞顯示
/**
* Displays text sent over the serial port (e.g. from the Serial Monitor) on
* an attached LCD.
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup()
{
lcd.begin();
lcd.backlight();
// Initialize the serial port at a speed of 9600 baud
Serial.begin(9600);
}
void loop()
{
// If characters arrived over the serial port...
if (Serial.available()) {
// Wait a bit for the entire message to arrive
delay(100);
// Clear the screen
lcd.clear();
// Write all characters received with the serial port to the LCD.
while (Serial.available() > 0) {
lcd.write(Serial.read());
}
}
}
溫溼度顯示在LCD上
// ===== LCD顯示溫濕度 =====
#include <Wire.h> //掛載Wire.h標頭檔
#include <LiquidCrystal_I2C.h> //掛載LiquiaCrystal_I2C.h標頭檔
const int Address=0x27; //宣告I2C位址
LiquidCrystal_I2C I2C_LCD(Address,16,2); //建構LiquiaCrystal_I2C.h物件
// sda= GPIO_21 /scl= GPIO_22
byte degree[]={
0b00110,
0b01001,
0b01001,
0b00110,
0b00000,
0b00000,
0b00000,
0b00000 };
#include <DHT.h>
const int dhtPin=04; //宣告04腳連接dhtPin信號
DHT myDHT(dhtPin,DHT11);
float tempC=0.0,tempF=0.0,RH=0.0;
//宣告攝氏溫度(tempC)、華氏溫度(tempF)與濕度(RH)之變數
const int dipPin=39; //宣告39腳連接dipPin信號
//初始設定
void setup() {
I2C_LCD.begin(); //啟用LCD
I2C_LCD.backlight(); //開啟LCD背光
I2C_LCD.createChar(0,degree); //載入自建字元
myDHT.begin(); //啟用DHT11
pinMode(dipPin, INPUT);//設定攝氏、華氏溫度切換
}
//主程式
void loop() {
int dip = digitalRead (dipPin); //讀取指撥開關
I2C_LCD.home(); //啟用LCD歸位
I2C_LCD.print("Temp. : "); //顯示Temp. :
if (dip) { //指撥開關切到OFF位置(攝氏溫度)
tempC = myDHT.readTemperature(0); //讀取攝氏溫度
I2C_LCD.setCursor(8,0); //游標移至(8,0)位置
I2C_LCD.print(tempC); //顯示攝氏溫度
I2C_LCD.setCursor(14,0); //游標移至(14,0)位置
I2C_LCD.write(0); //顯示度
I2C_LCD.print("C"); //顯示C
}
else { //指撥開關切到ON位置(華氏溫度)
tempF = myDHT.readTemperature(1); //讀取華氏溫度
I2C_LCD.setCursor(8,0); //游標移至(8,0)位置
I2C_LCD.print(tempF); //顯示華氏溫度
I2C_LCD.setCursor(14,0); //游標移至(14,0)位置
I2C_LCD.write(0); //顯示度
I2C_LCD.print("F"); //顯示F
}
RH = myDHT.readHumidity(); //讀取濕度
I2C_LCD.setCursor(0,1); //游標移至(0,1)位置
I2C_LCD.print("Humi. : "); //顯示Humi. :
I2C_LCD.print(RH); //顯示度
I2C_LCD.print("%RH"); //顯示%RH
delay(500); //暫停0.5秒
}
相關應用