반응형

라즈베리파이 4에 캐릭터 LCD를 연결하여 사용하는 방법을 설명합니다. 



2017. 02. 19  최초작성

2021. 02. 05  라즈베리파이 4에서 진행 




필요한 부품은 다음과 같습니다. 

 

  • 캐릭터 LCD 1602

 

 

 

 

  • 핀헤더 1 x 40p, 2.54mm

 

 

 

 

  • 가변저항 10K ohm

 

 

 

 

  • 저항 220 ohm, ¼ W

잘못 주문해서 220K ohm을 받았지만 다행히 캐릭터 LCD가 동작 하네요.

 

 

 

 

  • 점퍼선 Male - Female

 

 

 

캐릭터 LCD에 핀헤더를 납땜해줘야 합니다. 핀헤더 16개핀을 잘라서 캐릭터 LCD에 연결합니다.

 

 

 

 

 

인두기로 납땜해서 고정시켜 줍니다.

 

 

 

 

 

 

다음처럼 부품들을 브레드보드에 연결해줍니다. 

나중에 검은색 선이 있는 라인은 ground, 빨간색 선이 있는 라인은 5V에 연결되게 됩니다. 

 

 

 



라즈베리파이4와 브레드보드에 연결된 캐릭터 LCD를 다음과 같이 연결합니다.

아래 있는 Raspberry Pi 4의 pinout을 참고하세요. LCD는 위 그림에 왼쪽이 1번, 오른쪽 끝이 16번이라고 적어놓았습니다.


브레드보드의 ground  --------------  6번핀(ground)

브레드보드의 5V          -------------- 2번핀(+5V)

 

LCD  4번핀(RS)    --------------  26번핀(GPIO 7)

LCD  6번핀(EN,E) --------------  24번핀(GPIO 8)

 

LCD 11번핀(D4) --------------  11번핀(GPIO 17)

LCD 12번핀(D5) --------------  12번핀(GPIO 18)

LCD 13번핀(D6) --------------  13번핀(GPIO 27)

LCD 14번핀(D7) --------------  15번핀(GPIO 22)



Raspberry Pi 4의 pinout입니다. 

 

 

 




다음처럼 예제 코드를 컴파일하여 실행합니다.

 

pi@raspberrypi:~ $ gcc lcd.c -o lcd -lwiringPi -lwiringPiDev

pi@raspberrypi:~ $ ./lcd



첫번째 줄에 문자열이 스크롤 되고 두번째 줄에 현재 시간이 표시됩니다. 




본 포스트에서 사용한 코드입니다.

 

#include <stdio.h>

#include <stdlib.h>

#include <stdint.h>

 

#include <unistd.h>

#include <string.h>

#include <time.h>

 

#include <wiringPi.h>

#include <lcd.h>

 

#ifndef TRUE

#  define       TRUE    (1==1)

#  define       FALSE   (1==2)

#endif

 

 

// Global lcd handle:

 

static int lcdHandle ;

 

 

/*

 * scrollMessage:

 *********************************************************************************

 */

 

static const char *message =

  "                    "

  "Wiring Pi by Gordon Henderson. HTTP://WIRINGPI.COM/"

  "                    " ;

 

void scrollMessage (int line, int width)

{

  char buf [32] ;

  static int position = 0 ;

  static int timer = 0 ;

 

  if (millis () < timer)

    return ;

 

  timer = millis () + 200 ;

 

  strncpy (buf, &message [position], width) ;

  buf [width] = 0 ;

  lcdPosition (lcdHandle, 0, line) ;

  lcdPuts     (lcdHandle, buf) ;

 

  if (++position == (strlen (message) - width))

    position = 0 ;

}

 

 

/*

 * waitForEnter:

 *********************************************************************************

 */

 

static void waitForEnter (void)

{

  printf ("Press ENTER to continue: ") ;

  (void)fgetc (stdin) ;

}

 

 

/*

 * The works

 *********************************************************************************

 */

 

int main (int argc, char *argv[])

{

  int i ;

  int lcd ;

  int bits, rows, cols ;

 

  struct tm *t ;

  time_t tim ;

 

  char buf [32] ;

 

  bits = 4;//GPIO 4개 핀 이용

  cols = 16;//1602

  rows = 2;

 

 

  wiringPiSetup () ;

 

  //RPI4 GPIO 7 --- LCD RS  pin --- wiringPi Pin 11

  //RPI4 GPIO 8 --- LCD  E  pin --- wiringPi Pin 10

  //RPI4 GPIO 17 --- LCD D4  pin --- wiringPi Pin 0

  //RPI4 GPIO 18 --- LCD D5  pin --- wiringPi Pin 1

  //RPI4 GPIO 27 --- LCD D6  pin --- wiringPi Pin 2

  //RPI4 GPIO 22 --- LCD D7  pin --- wiringPi Pin 3

  lcdHandle = lcdInit (rows, cols, 4, 11, 10, 0,1,2,3,0,0,0,0) ;

 

 

  if (lcdHandle < 0)

  {

    fprintf (stderr, "%s: lcdInit failed\n", argv [0]) ;

    return -1 ;

  }

 

 

  //간단히 2줄 출력하기

  //lcdPosition (lcdHandle, 0, 0) ; lcdPuts (lcdHandle, "Gordon Henderson") ;

  //lcdPosition (lcdHandle, 0, 1) ; lcdPuts (lcdHandle, "  wiringpi.com  ") ;

 

  //waitForEnter () ;//사용자 입력대기

 

  lcdCursor      (lcdHandle, FALSE) ;

  lcdCursorBlink (lcdHandle, FALSE) ;

  lcdClear       (lcdHandle) ;

 

 

  //스크롤하는 메시지와 시계출력하기

  for (;;)

  {

    scrollMessage (0, cols) ;

 

    if (rows == 1)

      continue ;

 

    tim = time (NULL) ;

    t = localtime (&tim) ;

 

    sprintf (buf, "%02d:%02d:%02d", t->tm_hour, t->tm_min, t->tm_sec) ;

 

    lcdPosition (lcdHandle, (cols - 8) / 2, 1) ;

    lcdPuts     (lcdHandle, buf) ;

 

    if (rows == 2)

      continue ;

 

    sprintf (buf, "%02d/%02d/%04d", t->tm_mday, t->tm_mon + 1, t->tm_year+1900) ;

 

    lcdPosition (lcdHandle, (cols - 10) / 2, 2) ;

    lcdPuts     (lcdHandle, buf) ;

 

  }

 

  return 0 ;

}


참고

http://wiringpi.com/dev-lib/lcd-library/


반응형

문제 발생시 지나치지 마시고 댓글 남겨주시면 가능한 빨리 답장드립니다.

도움이 되셨다면 토스아이디로 후원해주세요.
https://toss.me/momo2024


제가 쓴 책도 한번 검토해보세요 ^^

+ Recent posts