[Arduino Nano/나노] LCD 20*4 디스플레이 출력
아두이노 연결
LCD 20x4 |
Arduino Nano |
GND |
GND |
VCC |
5V |
SDA |
A4 |
SCL |
A5 |
사용 라이브러리
LiquidCrystal_I2C-master.zip
0.01MB
디스플레이에 출력하기전에 본인이 갖고있는 LCD의
I2C 주소를 먼저 알아야 한다
LCD 디스플레이 I2C 코드
// https://kocoafab.cc/tutorial/view/727
#include <Wire.h>
void setup()
{
Wire.begin();
Serial.begin(9600);
while (!Serial);
Serial.println("\nI2C Scanner");
}
void loop()
{
byte error, address;
int nDevices;
Serial.println("Scanning...");
nDevices = 0;
for(address = 1; address < 127; address++ )
{
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0)
{
Serial.print("I2C device found at address 0x");
if (address<16)
Serial.print("0");
Serial.print(address,HEX);
Serial.println(" !");
nDevices++;
}
else if (error==4)
{
Serial.print("Unknow error at address 0x");
if (address<16)
Serial.print("0");
Serial.println(address,HEX);
}
}
if (nDevices == 0)
Serial.println("No I2C devices found\n");
else
Serial.println("done\n");
delay(5000);
}
확인했으면 아래의 내용을 따라해보자
아두이노 코드
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4); // 본인의 I2C 주소 입력
void setup()
{
Serial.begin(9600);
lcd.init();
lcd.backlight();
Serial.println("lcd test start!"); // 시작했음을 시리얼 모니터에 표시
}
void loop()
{
// lcd 출력
lcd.setCursor(0,0); // 문자가 위치할 자리를 행,열의 순서로 지정 (1부터가 아닌 0부터 시작)
lcd.print("line 1");
lcd.setCursor(0,1);
lcd.print("line 2");
lcd.setCursor(0,2);
lcd.print("line 3");
lcd.setCursor(0,3);
lcd.print("line 4");
}
UV를 추가한 아두이노 코드
// UV 연결 방법은 https://dianamin329.tistory.com/11
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
void setup()
{
Serial.begin(9600);
lcd.init();
lcd.backlight();
lcd.clear();
Serial.println("lcd test start!"); Serial.println();
}
void loop()
{
uint16_t rawVal; // 정수변수 선언 (범위: 0~65535)
rawVal = analogRead(1); // A1 포트값을 0~1023사이의 값으로 데이터 수집
float vVal = rawVal * 5 / 1023.0; // 전압값으로 변환
//Serial.println("UV detecting...");
Serial.print("Raw Value(0-1023) : ");
Serial.print(rawVal); // 측정값 시리얼 모니터로 출력
Serial.print(" = ");
Serial.print(vVal,3); // 변환된 전압값을 소수점 3자리까지 출력
Serial.print("[V]");
Serial.print("\tUV Index(0 - 10) : ");
Serial.println(vVal * 10,1); // 전압값 x10으로 UVI 변환값 출력
// lcd 출력
lcd.setCursor(0,0);
lcd.print("This is UV");
lcd.setCursor(0,1);
lcd.print("Raw Value : ");
lcd.setCursor(12,1);
lcd.print(rawVal);
lcd.setCursor(0,2);
lcd.print("Voltage : ");
lcd.setCursor(10,2);
lcd.print(vVal,3);
lcd.setCursor(0,3);
lcd.print("UV Index : ");
lcd.setCursor(11,3);
lcd.print(vVal * 10, 1);
delay(1000);
}
'Arduino' 카테고리의 다른 글
[Arduino Nano/나노] UV 센서 (0) | 2021.02.01 |
---|---|
[Arduino Nano/나노] CDS 조도 센서 (0) | 2021.01.29 |
[Arduino Nano/나노] MQ-7 CO 센서 (0) | 2021.01.29 |
[Arduino Nano/나노] AM2302(DHT22) 온습도 센서 (0) | 2021.01.28 |
[Arduino Nano/나노] MH-Z19B CO2 센서 (0) | 2021.01.25 |
댓글