본문 바로가기
IOT 로봇 아두이노/아두이노 기본센서 연결하기

[아두이노 기초] LCD 글자 출력하기

by Jango_infoinno 2021. 4. 26.

 

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

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


1) LCD 란?

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

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

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

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

대충봐도 복잡하죠?

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

LCD용 12C 통신 모듈입니다


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

 

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

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

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

 


반응형

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

[1] 파일을 다운받는다

LiquidCrystal_I2C-1.1.2.zip
0.02MB

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

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

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

 

 


3) LCD로 문자 출력하기

 

 

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()
{
  
}

 


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
}

 


LCD 관련 추천 글

[아두이노/기본센서 연결하기] - [아두이노 응용] 온도 습도 LCD로 출력하기

 

[아두이노 응용] 온도 습도 LCD로 출력하기

24. 온습도 측정하기 (tistory.com) 24. 온습도 측정하기 온습도센서(DHT11)란? 온습도 센서 이용하여 측정한 온습도 값을 시리얼에 뿌리기 1) 온습도센서(DHT11)란? - DHT11을 쉽게 사용하는 모듈 온습도 센

infoinno.info

[아두이노/기본센서 연결하기] - [아두이노 응용] 초음파센서 거리 측정 후 LCD 출력 하기

 

[아두이노 응용] 초음파센서 거리 측정 후 LCD 출력 하기

이전에 배웠던 초음파센서를 이용한 거리측정값을 LCD로 출력해 보는것을 배워볼 것 입니다! [기본센서 연결하기] - 9. 초음파센서 연결하기 9. 초음파센서 연결하기 //초음파 센서의 핀번호를 설

infoinno.info

[아두이노/센서 및 엑츄에이터] - LCD

 

LCD

16x2 LCD 모듈 16x2 LCD 모듈 16x2 LCD 모듈 핀아웃 핀 구성 데이터시트 components101.com/displays/16x2-lcd-pinout-datasheet 16x2 LCD Module: Pinout, Diagrams, Description & Datasheet 16×2 LCD is named so because it has 16 Columns and 2 Rows

infoinno.info

LCD 명령어 정리

 

UNO R3 RFID 스타터 키트 업그레이드 코딩 아두이노 : 인포이노infoinno

[인포이노infoinno] 생활에 유용한 제품 판매 온라인 스토어 인포이노

smartstore.naver.com

 

반응형

댓글