一、如何使用 LCD_I2C:
1、下載 LCD_I2C 函式庫,網址如下:
https://www.arduino.cc/reference/en/libraries/lcd_i2c/
點選最近版本的程式庫,即可下載程式庫的安裝檔
2、(1)將程式庫解壓縮後,整個資料夾複製或剪下至 Arduino 資料夾裡--
>libraries 底下,即完成程式庫的安裝。
(2)或是從程式庫管理員裡搜尋並安裝:
(3)搜尋 LCD_I2C,進行安裝或確認已安裝
3、接線圖
4、程式碼:
(1)HELLO WORLD
#include <LCD_I2C.h>
LCD_I2C lcd(0x27, 16, 2); // Default address of most PCF8574 modules, change
according
void setup()
{
lcd.begin(); // If you are using more I2C devices using the Wire library use
lcd.begin(false)
// this stop the library(LCD_I2C) from calling Wire.begin()
lcd.backlight();
}
void loop()
{
lcd.print(" Hello"); // You can make spaces using well... spaces
lcd.setCursor(5, 1); // Or setting the cursor in the desired position.
lcd.print("World!");
delay(500);
// Flashing the backlight
for (int i = 0; i < 5; ++i)
{
lcd.backlight();
delay(50);
lcd.noBacklight();
delay(50);
}
lcd.backlight();
lcd.clear();
delay(500);
}
(2)Function
#include <LCD_I2C.h>
LCD_I2C lcd(0x27, 16, 2); // Default address of most PCF8574 modules, change
according
/*
* When using lcd.print() (and almost everywhere you use string literals),
* is a good idea to use the macro F(String literal).
* This tells the compiler to store the string array in the flash memory
* instead of the ram memory. Usually you have more spare flash than ram.
* More info:
https://www.arduino.cc/reference/en/language/variables/utilities/progmem/
*/
void setup()
{
lcd.begin();
lcd.backlight();
}
void loop()
{
//Autoscroll
lcd.setCursor(5, 0);
lcd.print(F("Autoscrolling"));
lcd.setCursor(10, 1);
lcd.autoscroll();
for (int i = 0; i < 10; i++)
{
lcd.print(i);
delay(200);
}
lcd.noAutoscroll();
lcd.clear();
// Scroll left and right
lcd.setCursor(10, 0);
lcd.print(F("To the left!"));
for (int i = 0; i < 10; i++)
{
lcd.scrollDisplayLeft();
delay(200);
}
lcd.clear();
lcd.print(F("To the right!"));
for (int i = 0; i < 10; i++)
{
lcd.scrollDisplayRight();
delay(200);
}
lcd.clear();
//Cursor
lcd.setCursor(0, 0);
lcd.cursor();
lcd.print(F("Cursor"));
delay(3000);
lcd.clear();
//Cursor blink
lcd.setCursor(0, 0);
lcd.blink();
lcd.print(F("Cursor blink"));
delay(3000);
lcd.clear();
//Blink without cursor
lcd.setCursor(0, 0);
lcd.noCursor();
lcd.print(F("Just blink"));
delay(3000);
lcd.noBlink();
lcd.clear();
}
(3)Custom_Chars
#include <LCD_I2C.h>
LCD_I2C lcd(0x27, 16, 2); // Default address of most PCF8574 modules, change
according
uint8_t happy[8] =
{
0b00000,
0b10001,
0b00000,
0b00000,
0b10001,
0b01110,
0b00000,
0b00000,
};
uint8_t wow[8] =
{
0b00000,
0b10001,
0b00000,
0b01110,
0b10001,
0b01110,
0b00000,
0b00000,
};
uint8_t anchor[8] =
{
0b01110,
0b01010,
0b01110,
0b00100,
0b10101,
0b10101,
0b01110,
0b00100
};
uint8_t snow[8] =
{
0b01000,
0b11101,
0b01011,
0b00001,
0b00100,
0b01110,
0b00100,
0b10000
};
void setup()
{
lcd.begin();
lcd.backlight();
lcd.createChar(0, happy);
lcd.createChar(1, wow);
lcd.createChar(2, anchor);
lcd.createChar(3, snow);
lcd.write(0);
lcd.write(1);
lcd.write(2);
lcd.write(3);
}
void loop()
{
}
二、DHT-11/DHT-22 溫度感測器
教學網址
https://randomnerdtutorials.com/esp32-dht11-dht22-temperature-humidity-sensor-
arduino-ide/