아두이노 시리얼 LCD 출력 - adu-ino silieol LCD chullyeog

목차
1) LCD 란?
2) LCD 문자 출력 준비(라이브러리 설치)
3) LCD로 문자 출력하기 
4) LCD 주소가 잘못 입력되어있을 때

연결과정 영상을 보시면 LCD(I2C) 연결과정을 파악하는데 도움이 됩니다.

아두이노 시리얼 LCD 출력 - adu-ino silieol LCD chullyeog

1) LCD 란?

문가와 기호를 표시할 수 있는 LCD로 보통은 16글자 2줄을 표시할 수 있는 LCD를 많이 사용합니다.

아두이노 시리얼 LCD 출력 - adu-ino silieol LCD chullyeog

하지만 LCD는 치명적인 단점이 있습니다.

총 16개의 핀이 있다는 것 입니다.

이 16개의 핀이 모두 데이터 핀에 연결되지는 않지만, 상당히 많은 데이터 핀을 소모한다는 것이 단점입니다.

아두이노 시리얼 LCD 출력 - adu-ino silieol LCD chullyeog
대충봐도 복잡하죠?

그래서 나온 것이 기존의 lcd를 바로 연결하는 것이 아닌 하나의 장치를 통해서 연결하는 방식입니다.

아두이노 시리얼 LCD 출력 - adu-ino silieol LCD chullyeog
아두이노 시리얼 LCD 출력 - adu-ino silieol LCD chullyeog

LCD용 12C 통신 모듈입니다

2)-1 LCD 문자 출력 준비(라이브러리 설치)

LCD를 이용하려면 특정 라이브러리를 추가해 주어야 합니다.

LCD와 여러 모듈을 이용하기 위해서는 "liquidcrystal I2C"라는 라이브러리가 필요하다, 그래서 특정 LCD에 맞는 라이브러리를 설치해 줄 필요가 있습니다.

아두이노를 실행하고 스케치 탭 - 라이브러리 포함하기 - 라이브러리 관리를 들어갑니다.

아두이노 시리얼 LCD 출력 - adu-ino silieol LCD chullyeog
아두이노 시리얼 LCD 출력 - adu-ino silieol LCD chullyeog
아두이노 시리얼 LCD 출력 - adu-ino silieol LCD chullyeog

2)-2 LCD 문자 출력 준비(라이브러리 다운받아 설치)

[1] 파일을 다운받는다

LiquidCrystal_I2C-1.1.2.zip

0.02MB

[2] 다운받은 파일을 압축해제하여 저장한다

아두이노 시리얼 LCD 출력 - adu-ino silieol LCD chullyeog

[3] 압축을 푼 파일을 아두이노 라이브러리에 저장한다 (위치 중요!!)

[C:\] -> [Program Files (x86)] -> [Arduino] -> [libraries]로 들어가서 저장합니다.

아두이노 시리얼 LCD 출력 - adu-ino silieol LCD chullyeog

3) LCD로 문자 출력하기

아두이노 시리얼 LCD 출력 - adu-ino silieol LCD chullyeog

GND : 빨간색
VCC (5V) : 검은색
SDA-데이터핀(A4) : 초록색
SCL-클럭핀(A5) : 노란색

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,20,4);    

//lcd(0x27,16열,2행)

// 0x27 이 lcd의 고유주소라고 생각하면 됩니다. 보통 0x27이지만 다른 lcd도 있습니다.
// 혹시 다른 고유주소라면, 예제프로그램에서 확인한후 똑같이 입력하여 코딩하시면 됩니다.

// lcd(0x27,16열,2행)  ->  고유주소=0x27 이고 총 16열 2행으로 구성되어 있다는 뜻 입니다.

void setup()
{
  lcd.init();  // LCD초기 설정
  
  lcd.backlight(); // LCD초기 설정  
  
  lcd.setCursor(0,0); 
  
  //처음 텍스트가 LCD에 나타날 위치  lcd.setCursor(열, 행)
  //밑에 사진을 통해 출력시작 위치를 확인할 수 있습니다.
  
  lcd.print("Hello, world!"); 
  
  lcd.setCursor(3,1); 
  
  lcd.print("How are you?"); 
  
}

void loop()
{
  
}
아두이노 시리얼 LCD 출력 - adu-ino silieol LCD chullyeog

4) LCD 주소가 잘못 입력되어있을 때

lcd가 연결되어있는 상태에서 밑에 코드를 입력한후 업로드합니다

그리고 시리얼 출력을 확인하면 LCD 주소를 알아낼 수 있습니다.

https://playground.arduino.cc/Main/I2cScanner/

 // --------------------------------------
// i2c_scanner
//
// Version 1
//    This program (or code that looks like it)
//    can be found in many places.
//    For example on the Arduino.cc forum.
//    The original author is not know.
// Version 2, Juni 2012, Using Arduino 1.0.1
//     Adapted to be as simple as possible by Arduino.cc user Krodal
// Version 3, Feb 26  2013
//    V3 by louarnold
// Version 4, March 3, 2013, Using Arduino 1.0.3
//    by Arduino.cc user Krodal.
//    Changes by louarnold removed.
//    Scanning addresses changed from 0...127 to 1...119,
//    according to the i2c scanner by Nick Gammon
//    https://www.gammon.com.au/forum/?id=10896
// Version 5, March 28, 2013
//    As version 4, but address scans now to 127.
//    A sensor seems to use address 120.
// Version 6, November 27, 2015.
//    Added waiting for the Leonardo serial communication.
// 
//
// This sketch tests the standard 7-bit addresses
// Devices with higher bit address might not be seen properly.
//

#include <Wire.h>


void setup()
{
  Wire.begin();

  Serial.begin(9600);
  while (!Serial);             // Leonardo: wait for serial monitor
  Serial.println("\nI2C Scanner");
}


void loop()
{
  byte error, address;
  int nDevices;

  Serial.println("Scanning...");

  nDevices = 0;
  for(address = 1; address < 127; address++ ) 
  {
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the 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("Unknown 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);           // wait 5 seconds for next scan
}
참조: I2C LCD로 문자 출력하기 (kocoafab.cc) 
[Arduino] 아두이노 LCD 16x2 다양한 예제 응용하기 (tistory.com) , [Arduino] 
[아두이노 중급] 30. I2C LCD 디스플레이(여러 개 사용하기) : 네이버 블로그 (naver.com)