본문 바로가기

아두이노-스케치

캐릭터 LCD 1602A 다루기 - 글자표시, 온도와 빛 센서 표시



#include <LiquidCrystal.h>

#include <DHT11.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int pin=8;

DHT11 dht11(pin);

 

void setup()

{

lcd.begin(16,2); //16 by 2 character display

}

 

int err;

float temp, humi;

void loop()

{

delay(1000);

if((err=dht11.read(humi, temp))==0)

{

lcd.clear();

lcd.setCursor(0,0);

lcd.print("Humidity= ");

lcd.print(humi,1);

lcd.print("%");

lcd.setCursor(0,1);

lcd.print("Temp= ");

lcd.print(temp,1);

lcd.print(" C");

}

else //에러일 경우 처리

{

lcd.setCursor(0,0);

lcd.print("error");

}

 

}







여기서는 윗부분의 기본 코드만을 테스트 하고 온습도 센서 표시하는 프로그램은 다른 블로그 글을 이용하기로 한다.


1602A Character LCD는 1줄에 16개의 문자씩 2줄을 보여주는 LCD 모듈입니다. 

백라이트는 5V, 가변 저항을 사용하면 폰트의 명암을 조절할 수 있습니다. 가격은 3천원~5천원 정도.


 

LCD 출력화면


아래 이미지와 소스코드 출처 : https://www.arduino.cc/en/Tutorial/HelloWorld


연결도


중요한점은 실제 연결할때는 가변저항이나 저항도 연결하지 않았다. 백라이트 전원 연결도 직접 5V 를 연결하였고, 가변저항의 출력이 LCD 3번 VO로 들어가는데 이것은 GND 로 연결하니 잘 나온다. 




image developed using Fritzing. For more circuit examples, see the Fritzing project page



아래는 회로도를 나타낸다.







캐릭터 LCD 1602A 기본코드 윗줄에 Hello World! 라고 표시, 아래줄에 1초 마다 시간증가

 

/*

  LiquidCrystal Library - Hello World


*/


// include the library code:

#include <LiquidCrystal.h>


// initialize the library with the numbers of the interface pins

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);     //RS E D4 D5 D6 D7


void setup() {

  // set up the LCD's number of columns and rows:

  lcd.begin(16, 2);

  // Print a message to the LCD.

  lcd.print("hello, world!");

}


void loop() {

  // set the cursor to column 0, line 1

  // (note: line 1 is the second row, since counting begins with 0):

  lcd.setCursor(0, 1);

  // print the number of seconds since reset:

  lcd.print(millis() / 1000);

}





아래는 온도센서와 빛 센서를 이용하여 LCD 에 출력하는 코드 - 출처 : https://learn.adafruit.com/adafruit-arduino-lesson-12-lcd-displays-part-2/arduino-code


연결도는 아래와 같다.




아두이노 스케치 소스코드


/*

Adafruit Arduino - Lesson 12. Light and Temperature

*/

 

#include <LiquidCrystal.h>

 

int tempPin = 0;

int lightPin = 1;

 

//                BS  E  D4 D5  D6 D7

LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

 

void setup() 

{

  lcd.begin(16, 2);

}

 

void loop()

{

  // Display Temperature in C

  int tempReading = analogRead(tempPin);

  float tempVolts = tempReading * 5.0 / 1024.0;

  float tempC = (tempVolts - 0.5) * 100.0;

  float tempF = tempC * 9.0 / 5.0 + 32.0;

  //         ----------------

  lcd.print("Temp         F  ");

  lcd.setCursor(6, 0);

  lcd.print(tempF);

  

  // Display Light on second row

  int lightReading = analogRead(lightPin);

  lcd.setCursor(0, 1);

  //         ----------------

  lcd.print("Light           ");  

  lcd.setCursor(6, 1);

  lcd.print(lightReading);

  delay(500);

}