아두이노
아두이노 LCD와 시계
ADELA_J
2023. 7. 19. 16:42
** 컴파일을 먼저하고 업로드하는걸 추천함 **
<LCD에 글씨 나오게하기>
-LiquidCrystal_l2C 라이브러리 설치 필요(zip을 이용해서 했음)
>>LiquidCrystal 은 16개 핀 조정하는 라이브러리.
더보기
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 16, 2); //lcd 객체 선언
void setup()
{
lcd.begin(); //LCD 사용시작
}
void loop()
{
lcd.setCursor(5,0); //커서를 5열 0행에 가져다놓기
lcd.print("Hi, ^^");
delay(1000);
lcd.setCursor(3,1);
lcd.print("Codingrun");
delay(1000);
lcd.clear();
delay(1000);
}
만약 LCD가 안나오면 주소값이 다를 수 있어서 주소값을 확인해보면 됨
https://playground.arduino.cc/Main/I2cScanner/
- 적은 후에 시리얼모니터에 주소값이 나온다. 그러면 그걸로 lcd 주소값을 변경해주면 된다.
<LCD에 Hello, World>
더보기
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); //lcd 객체 선언
void setup()
{
lcd.begin(); //LCD초기 설정
lcd.backlight(); //LCD초기 설정
lcd.setCursor(0,0); //텍스트가 LCD에 나타날 위치
lcd.print("Hello, world");
lcd.setCursor(3,1);
lcd.print("How are you?");
}
void loop()
{}
<LCD에 움직이는 글자 넣기>
더보기
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup()
{
lcd.begin();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Hello, World!");
}
void loop()
{
for (int position=0; position<16; position++){
lcd.scrollDisplayLeft();
delay(150);
}
for (int position=0; position<35; position++){
lcd.scrollDisplayLeft();
delay(150);
}
}
<가변저항을 돌려가며 LED불이 켜지고 그 값이 LCD에 나오게 하기>
더보기
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
int val=0;
void setup()
{
lcd.begin();
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
lcd.backlight();
}
void loop()
{
val = analogRead(A0);
if (val <= 250) {
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
lcd.setCursor(5,0);
lcd.print("Color");
lcd.setCursor(5,1);
lcd.print("<B>");
}
else if(val > 250 && val <= 500){
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
lcd.setCursor(5,0);
lcd.print("Color");
lcd.setCursor(5,1);
lcd.print("<G>");
}
else{
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
lcd.setCursor(5,0);
lcd.print("Color");
lcd.setCursor(5,1);
lcd.print("<R>");
}
}
<온도+습도 LCD에 표시>
더보기
#include <DHT.h>
#include <DHT_U.h>
#include <LiquidCrystal_I2C.h>
#define DHTPIN 11
#define DHTTYPE DHT11
LiquidCrystal_I2C lcd(0x27, 16, 2);
DHT dht(DHTPIN, DHTTYPE);
int count=0;
void setup(){
Serial.begin(9600);
dht.begin(); //DHT초기화
lcd.begin();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print('210817_DHT_LCD2');
lcd.setCursor(0,1);
lcd.print("by B.H Chung");
delay(2000);
lcd.clear();
}
void loop(){
//DHT
delay(2000);
int h = dht.readHumidity();
int t = dht.readTemperature();
Serial.print("Humidity:" );
Serial.print(h);
Serial.print("%\t");
Serial.print("Temperature:");
Serial.print(t);
Serial.print(" C ");
Serial.print(count);
lcd.setCursor(0,0);
lcd.print("Temp:");
lcd.print(t);
lcd.print(" C");
lcd.setCursor(0,1);
lcd.print("Humi: ");
lcd.print(h);
lcd.print(" %");
lcd.setCursor(12,1);
lcd.print(count);
count += 1; //count = count +1;
if (count > 9999){
count = 0;
lcd.clear();
}
}
<시계>
- ds1032 검색 후 나오는 Rtc by Makuna 라이브러리를 설치해야한다.
더보기
// CONNECTIONS:
// DS1302 CLK/SCLK --> 5
// DS1302 DAT/IO --> 4
// DS1302 RST/CE --> 2
// DS1302 VCC --> 3.3v - 5v
// DS1302 GND --> GND
#include <RtcDS1302.h>
ThreeWire myWire(4,5,2); // IO, SCLK, CE
RtcDS1302<ThreeWire> Rtc(myWire); //RTC 라이브러리 생성
void setup ()
{
Serial.begin(57600);
//컴파일 시점의 날짜(__DATE__)와 시간(__TIME__)을 시리얼 모니터에 표시
Serial.print("compiled: ");
Serial.print(__DATE__);
Serial.println(__TIME__);
//RTC 모듈 라이브러리 시작
Rtc.Begin();
//RTCDateTime 클래스 생성(컴파일된 시간으로 설정)
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
printDateTime(compiled);
Serial.println();
//RTC모듈에 쓰기 금지 모드인지 확인
if (!Rtc.IsDateTimeValid())
{
// Common Causes:
// 1) first time you ran and the device wasn't running yet
// 2) the battery on the device is low or even missing
Serial.println("RTC lost confidence in the DateTime!"); //쓰기 금지모드이면 해제
Rtc.SetDateTime(compiled);
}
//RTC모듈이 동작중이 아니라면
if (Rtc.GetIsWriteProtected())
{
Serial.println("RTC was write protected, enabling writing now"); //시작
Rtc.SetIsWriteProtected(false);
}
if (!Rtc.GetIsRunning())
{
Serial.println("RTC was not actively running, starting now");
Rtc.SetIsRunning(true);
}
RtcDateTime now = Rtc.GetDateTime();
if (now < compiled)
{
Serial.println("RTC is older than compile time! (Updating DateTime)");
Rtc.SetDateTime(compiled);
}
else if (now > compiled)
{
Serial.println("RTC is newer than compile time. (this is expected)");
}
else if (now == compiled)
{
Serial.println("RTC is the same as compile time! (not expected but all is fine)");
}
}
void loop ()
{
RtcDateTime now = Rtc.GetDateTime();
printDateTime(now);
Serial.println();
if (!now.IsValid())
{
// Common Causes:
// 1) the battery on the device is low or even missing and the power line was disconnected
Serial.println("RTC lost confidence in the DateTime!");
}
delay(10000); // ten seconds
}
#define countof(a) (sizeof(a) / sizeof(a[0]))
void printDateTime(const RtcDateTime& dt)
{
char datestring[20];
snprintf_P(datestring,
countof(datestring),
PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
dt.Month(),
dt.Day(),
dt.Year(),
dt.Hour(),
dt.Minute(),
dt.Second() );
Serial.print(datestring);
}
<시간이 LCD에 나오게>
더보기
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <virtuabotixRTC.h>
virtuabotixRTC myRTC(6,7,8);
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup(){
Serial.begin(9600);
lcd.begin();
lcd.backlight();
myRTC.setDS1302Time(00,22,5,4,19,7,2023);
}
void loop(){
myRTC.updateTime();
lcd.setCursor(1,0);
lcd.print(myRTC.year);
lcd.print("/");
lcd.print(myRTC.month);
lcd.print("/");
lcd.print(myRTC.dayofmonth);
lcd.print("/");
switch (myRTC.dayofweek){
case 1:
lcd.print("Sun");
break;
case 2:
Serial.print("MON");
break;
case 3:
lcd.print("TUE");
break;
case 4:
lcd.print("WED");
break;
case 5:
lcd.print("THU");
break;
case 6:
lcd.print("FRI");
break;
case 7:
lcd.print("SAT");
break;
}
lcd.setCursor(5,1);
lcd.print(myRTC.hours);
lcd.print(":");
lcd.print(myRTC.minutes);
lcd.print(":");
lcd.print(myRTC.seconds);
delay(1000);
lcd.clear();
}