본문 바로가기
IoT 디바이스 활용/Arduino

Arduino - 16x2 LCD

by cooluk 2020. 10. 15.

16x2 LCD


16x2 LCD

1602 Character LCD (16×2, LiquidCrystalDisplay 1602 V1)

  • 1줄에 16개의 문자씩 2줄을 보여주는 LCD 모듈

  • 백라이트는 5V, 가변 저항으로 폰트의 명암을 조절

    image-20200911171134410

LCD Interface Converter

  • I2C 인터페이스를 사용해서 LCD를 제어

    • 단 4개 선으로 LCD 조작

    • 공급전압 : DC 2.5-6V

    • 점퍼 스위치 : LCD 백라이트 ON/OFF 제어

    • 가변저항 : LCD 명암 조절

    • I2C 주소 초기값 : 0x27 또는 0x3F

      image-20200911171241089 image-20200914092529684

image-20200911171303653

실수값을 문자열로 변경하기

  • char * dtostrf(double __val, signed char __width,

                         unsigned char \__prec, char * __s);
    • __val : 변환할 실수 값
    • __width : 전체 자리 수(소수점은 제외)
    • __prec: 소수점 이하 유효숫자 수
    • __s : 변한된 문자열을 저장할 버퍼
    float fltValue = 123.456;
    char pChrBuffer[50];
    
    dtostrf(fltValue , 5, 2, pChrBuffer);// 5 : width, 2 : precision


    라이브러리 준비

  • https://github.com/johnrickman/LiquidCrystal_I2C

  • 코드 다운로드 및 압축 해제

    • <문서>\Arduino\libraries\LiquidCrystal_I2C

주요 메소드

lcd.init(); // LCD 초기화
lcd.backlight(); // LCD 백라이트를 켠다
lcd.noBacklight(); // LCD 백라이트를 끈다
lcd.noDisplay(); // LCD 표시된 내용을 숨긴다
lcd.display(); // LCD 표시내용을 보여준다
lcd.cursor(); // 커서를 표시한다
lcd.noCursor(); // 커서를 없앤다.
lcd.setCursor(0,0); // 해당 LCD 좌표로 커서 이동
lcd.home(); // 커서를 0,0 좌표로 이동
lcd.blink(); // 커서를 깜빡임
lcd.noBlink(); // 커서를 깜빡이지 않음
lcd.write(36); // LCD 화면에 값 출력, 아스키코드 입력 시 해당문자 출력
lcd.print("TEST"); // LCD 화면에 값을 출력
lcd.clear(); // LCD 모든 내용 지움
lcd.scrollDisplayRight(); // lcd 내용을 우측으로 1칸 스크롤
lcd.scrollDisplayLeft(); // lcd 내용을 좌측으로 1칸 스크롤
lcd.autoscroll(); // 출력내용을 자동으로 우에서 좌로 스크롤

I2C 주소 스캐닝

#include <Wire.h>
void setup()
{
    Wire.begin();
    Serial.begin(9600);
}

void loop()
{
    byte error, address;
    int nDevices = 0;
    Serial.println("Start Scanning...");
    for (address = 1; address < 127; address++)
    {
        // 1바이트의 의미 없는 데이터를 전송
        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++;
        }
    }
    if (nDevices == 0)
        Serial.println("No I2C devices found\n");
    delay(5000); // 5초 후 다시 스캐닝
}

0x27


LCD 기본 출력

#include <LiquidCrystal_I2C.h>

// I2C 주소, 칸 수(X), 줄 수(Y)
LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup()
{
    lcd.init(); // LCD 초기화
    lcd.backlight(); // 백라이트 켜기
    lcd.setCursor(3,0); // 커서 위치 설정 (x,y)

    // 문자열 출력
    lcd.print("Hello, world!");
}

void loop() {
    lcd.backlight();
    delay(1000);
    lcd.noBacklight();
    delay(1000);
}

LCD 출력 위치 설정

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup()
{
    Serial.begin(9600);
    lcd.init();
    lcd.setCursor(0, 0);
    lcd.print("Arduino LCD");
    delay(1000);
    lcd.setCursor(0, 1);
    lcd.print("Welcome");
    delay(250);

    // LCD 백라이트 두번 점멸
    lcd.noBacklight();
    delay(250);
    lcd.backlight();
    delay(250);
    lcd.noBacklight();
    delay(250);
    lcd.backlight();
    delay(3000);
    // Open Serial Monitor Type to display 표시
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Open Serial Mntr");
    lcd.setCursor(0, 1);
    lcd.print("Type to display");
}


// 파이썬의 input() 함수에 해당..
String readLine() {
    //
    String line = "";
    // serial로 부터 한줄 입력 받아... line 변수에 저장

    while (Serial.available() > 0)
        {
            char ch = Serial.read();  // 문자 1개 리드
            if(ch != '\r' && ch != '\n')
                line += ch;
        }

    return line;
}


// 16x2
// 라인별로 한줄 전체를 덮어쓰는 형태...
void loop() {

    if (Serial.available()) // 수신된 데이터 있는지
    {
        delay(100);
        // lcd.clear(); // 긴 문장 보내고 ---> 짧은 문장 전송.
        lcd.setCursor(0, 0);
        lcd.print("Message from PC");
        lcd.setCursor(0, 1);

        String line = readLine();
        if(line != "") {  //수신데이터 유무
            lcd.setCursor(0, 1);
            char buf[17];  // null 문자열이 자동적으로 맨 끝에 자동으로 생겨서 17개로 배열만든다.
            float d = 3.14159;
            char buf2[10];
            dtostrf(d, 5, 3, buf2);

            // lcd.print(line.c_str());  // const char * 타입
            sprintf(buf,  "%-8s %s   ", line.c_str(), buf2);  // 문자열 buf에 출력, %-16s : 16칸 왼쪽 정렬
            lcd.print(buf);
        }
    }
}


사용자 문자 정의 가능

  • 5x7 도트 문자 폰트 데이터

    image-20200914112454858

사용자 정의 문자 테이터

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);
uint8_t smile1[8] = {0x00, 0x11, 0x00, 0x00, 0x00, 0x11, 0x0E, 0x00}; 
uint8_t smile2[8] = {0x00, 0x11, 0x00, 0x00, 0x00, 0x0E, 0x11, 0x00}; 
void setup()
{
    lcd.init();

    lcd.backlight();

    lcd.createChar(0, smile1);
    lcd.createChar(1, smile2);

    lcd.home();
    lcd.write(0);
    lcd.write(1);
}
void loop()
{
}

시계 (실제 시간X reset 시간 기준)

#include <LiquidCrystal_I2C.h>
#include <SimpleTimer.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);
SimpleTimer timer;


void printTime() {
    char buf[17];
    unsigned long t = millis();

    //milisecond -> 시:분:초로 변환해서 출력
    int misec = t%1000/100; //100ms 단위
    t = t/1000; // 밀리초 -> 초
    int h = t / 3600; // 초 -> 시간
    int m = (t - (h*3600)) / 60; //초 -> 분
    int s = t - ( h*3600 + m*60); //초

    sprintf(buf, "%02d:%02d:%02d.%d", h, m, s, misec);

    lcd.setCursor(0,0);
    lcd.print(buf);
}

void setup()
{
    Serial.begin(9600);
    lcd.init();
    lcd.backlight();

    timer.setInterval(100, printTime);
}

void loop() {
    timer.run();
}

'IoT 디바이스 활용 > Arduino' 카테고리의 다른 글

Arduino - 버튼 (Led.h, Button.h)  (0) 2020.10.15
Arduino - 피에조 부저  (0) 2020.10.15
Arduino - 3색 LED  (0) 2020.10.15
Arduino - LED (TrafficLight.h)  (0) 2020.10.15
Arduino - 개발환경 구축  (0) 2020.10.15

댓글