코드 검색

아두이노 코드 및 I2C를 사용한 SSD1306 OLED 128 x 32 디스플레이용 비디오

아두이노 코드 및 I2C를 사용한 SSD1306 OLED 128 x 32 디스플레이용 비디오

이 튜토리얼에서는 Arduino에서 I2C 통신을 사용하는 SSD1306 OLED 128 x 32 디스플레이를 활용하는 방법을 살펴보겠습니다. 목표는 텍스트, 사각형, 전압 및 온도와 같은 동적 값을 화면에 표시하는 것입니다. 이 설정은 컴팩트하고 효율적인 디스플레이 솔루션이 필요한 프로젝트에 이상적입니다.

SSD1306 OLED 128x32

필요한 하드웨어 구성 요소, 배선 지침 및 모든 것을 실행하기 위한 Arduino 코드를 단계별로 살펴보겠습니다. 시각적 가이드는 이 튜토리얼과 함께 제공되는 비디오(비디오 00:00)를 확인하세요.

하드웨어 설명

이 프로젝트의 주요 구성 요소는 SSD1306 OLED 디스플레이로, I2C 통신을 사용하여 쉽게 인터페이스할 수 있는 저전력 디스플레이입니다. 이 디스플레이는 128 x 32 픽셀 해상도를 제공하여 텍스트와 간단한 그래픽을 표시하는 데 적합합니다.

디스플레이를 제어하기 위해 Arduino 보드를 사용하며, I2C를 통해 명령과 데이터를 전송합니다. Arduino는 Adafruit_GFXAdafruit_SSD1306과 같은 라이브러리를 사용하여 정보 표시 로직을 처리합니다. 이러한 라이브러리는 통신 프로세스를 단순화하고 도형과 텍스트를 쉽게 그릴 수 있게 해줍니다.

데이터시트 세부 정보

제조업체Adafruit
부품 번호SSD1306
로직/IO 전압3.3 - 5 V
공급 전압3.3 - 5 V
전류 소비~20 mA (일반)
디스플레이 해상도128 x 32 픽셀
인터페이스I2C
크기128 x 32 mm
참고 사항 / 변형다양한 크기와 인터페이스로 제공됨
  • I2C 주소를 확인하세요. 일반적으로 OLED의 경우 0x3C입니다.
  • 손상을 방지하려면 적절한 전원 공급을 확인하세요.
  • 필요한 경우 SDA 및 SCL 라인에 풀업 저항을 사용하세요.
  • I2C 신호의 노이즈를 줄이려면 배선을 짧게 유지하세요.
  • 여러 모듈에 전원을 공급하는 경우 방열을 고려하세요.

배선 지침

Arduino wiring for SSD1306 OLED 128x32
Arduino wiring for SSD1306 OLED 128x32

SSD1306 OLED 디스플레이를 배선하려면 먼저 디스플레이의 VCC 핀을 Arduino의 5V 출력에 연결하세요. 다음으로 GND 핀을 Arduino의 접지 핀 중 하나에 연결합니다. I2C 통신을 위해 디스플레이의 SDA 핀을 Arduino의 SDA 핀(UNO의 경우 A4)에 연결하세요. 그런 다음 디스플레이의 SCL 핀을 Arduino의 SCL 핀(UNO의 경우 A5)에 연결합니다. 다른 Arduino 모델을 사용하는 경우 SDA 및 SCL의 특정 핀 매핑을 참조하세요.

추가 구성 요소로 제어하려는 LED가 있는 경우 330옴 저항을 통해 핀 9에 연결하세요. 저항의 다른 쪽 끝을 LED의 양극에 연결하고 음극은 접지에 연결해야 합니다. 이 설정을 통해 핀 9가 활성화되면 LED가 켜집니다.

코드 예제 및 설명

Arduino 코드에서는 OLED 디스플레이에 필요한 라이브러리를 포함하는 것으로 시작합니다. 주요 식별자는 Adafruit_SSD1306 클래스의 인스턴스인 display입니다. 디스플레이는 setup() 함수에서 초기화되며, 그리기를 준비하기 위해 디스플레이를 지웁니다.

SSD1306 OLED 128x32
void setup() {
  Serial.begin(9600);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.display();
  delay(2000);
  display.clearDisplay(); 
}

loop() 함수에서는 전압 값을 저장하는 문자열 변수 vString을 정의하며, 이 값은 동적으로 업데이트됩니다. robojaxText() 함수는 지정된 좌표에 텍스트를 화면에 표시하는 데 사용됩니다.

void loop() {
  String vString = String(count, 3);
  display.clearDisplay();
  robojaxText("Voltage:          ", 4, 3, 1, false);
  robojaxText(vString, 72, 3, 1, false);
  display.display();
  count += 0.173;
  delay(2000); 
}

robojaxText() 함수는 텍스트 위치 지정 및 표시에 중요합니다. 텍스트 문자열, x 및 y 좌표, 텍스트 크기, 디스플레이를 즉시 업데이트할지 여부를 결정하는 부울 값을 매개변수로 받습니다.

void robojaxText(String text, int x, int y, int size, boolean d) {
  display.setTextSize(size);
  display.setTextColor(WHITE);
  display.setCursor(x, y);
  display.println(text);
  if(d) {
    display.display();
  }
}

이 함수는 텍스트를 인쇄하기 전에 텍스트 크기, 색상 및 커서 위치를 설정합니다. 전체 코드 설명은 비디오(비디오 10:00)를 시청하세요.

데모 / 예상 결과

코드가 Arduino에 업로드되면 OLED 디스플레이에 전압, 온도 및 용량 값이 2초마다 동적으로 업데이트되어 표시됩니다. 또한 텍스트 요소 주위에 사각형이 그려진 것을 볼 수 있습니다. 디스플레이에 아무것도 표시되지 않으면 연결이 올바른지, I2C 주소가 코드에 지정된 주소와 일치하는지 확인하세요.

비디오 타임스탬프

  • 00:00 - 프로젝트 소개
  • 02:00 - 배선 지침
  • 05:00 - 코드 설명
  • 10:00 - 출력 데모

이미지

SSD1306 OLED 128x32
SSD1306 OLED 128x32
SSD1306 OLED 128x32
SSD1306 OLED 128x32
SSD1306 OLED 128x32
SSD1306 OLED 128x32
SSD1306 OLED 128x32
SSD1306 OLED 128x32
Arduino wiring for SSD1306 OLED 128x32
Arduino wiring for SSD1306 OLED 128x32
83-Arduino code and video for SSD1306 OLED 128 x 32 display with I2C
언어: C++
/*********************************************************************
This is an example for our monochrome OLEDs based on SSD1306 drivers.

This example is for a 128x32 size display using I2C.

If you get the error: Adafruit_GFX.h not found, download the Adafruit-GFX Library
https://github.com/adafruit/Adafruit-GFX-Library


Purchase this OLED module from Amazon: https://amzn.to/36zFvTb

Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, check license.txt for more information.
All text above, and the splash screen must be included in any redistribution.

 * Watch the video for this code to learn it fully.
  * Watch the video here: https://youtu.be/RjyulqVsz2o
 * This code is offered "as is" without any warranty.
 * Updated by Ahmad Shamshiri for Robojax.com video tutorial
 * on March 18, 2018 at 10:21 in Ajax, Ontario, Canada.
 * Please view other Robojax codes and videos at http://robojax.com/learn/arduino
 * If you are sharing this code, you must keep this copyright note.
 * 
*********************************************************************/

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2
double count=0;

/*
 * PIN connection:
 * Pin connection see: https://www.arduino.cc/en/Reference/Wire
 * for UNO: SDA to A4, SCL to A5
 * for Mega2560: SDA to 20, SCL to 21
 * for Leonardo: SDA to 2, SCL to 3
 * for Due: SDA to 20, SCL to 21
 * VCC to 5V
 * GND to GND :-)
 */


// this is the Width and Height of Display which is 128 x 32
#define LOGO16_GLCD_HEIGHT 32
#define LOGO16_GLCD_WIDTH  128 


#if (SSD1306_LCDHEIGHT != 32)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif

void setup()   {                
  Serial.begin(9600);

  // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3C (for the 128x32)
  display.display();
  delay(2000);
   // Clear the buffer.
  display.clearDisplay(); 
}


void loop() {
  String vString =  String(count, 3);// using a float and the 
  display.clearDisplay();
  robojaxText("Voltage:          ", 4, 3, 1, false);
  robojaxText(vString, 72, 3, 1, false);
  robojaxText("V", 110, 3, 1, false);
  robojaxText("Temperature: 32C", 4, 11, 1, false);
  robojaxText("Capacity:   92.86L", 4, 21, 1, false);
  //display.drawLine(1, 37, 100, 37, WHITE);
 display.drawRect(1, 1, 126,31, WHITE);
  //display.drawCircle(63,31, 31, WHITE);
   //display.startscrollright(0x00, 0x0F);
  display.display();
  count +=0.173;
   delay(2000); 
}


/*
 * robojaxText(String text, int x, int y,int size, boolean d)
 * text is the text string to be printed
 * x is the integer x position of text
 * y is the integer y position of text
 * size is the text size, 1, 2, 3 etc
 * d is either true or false.  If true, the display is updated.
 */
void robojaxText(String text, int x, int y,int size, boolean d) {

  display.setTextSize(size);
  display.setTextColor(WHITE);
  display.setCursor(x,y);
  display.println(text);
  if(d){
    display.display();
  }
  
  //delay(100);
}

필요할 수 있는 것들

자원 및 참고자료

파일📁

데이터시트 (pdf)

  • SSD1306 display datasheet
    SSD1306 is a single-chip CMOS OLED/PLED driver with controller for organic / polymer light emitting diode dot-matrix graphic display system. It consists of 128 segments and 64commons. This IC is designed for Common Cathode type OLED panel. The SSD1306 embeds with contrast control, display RAM and oscillator, which reduces the number of external components and power consumption. It has 256-step brightness control. Data/Commands are sent from general MCU through the hardware selectable 6800/8000 series compatible Parallel Interface, I2C interface or Serial Peripheral Interface. It is suitable for many compact portable applications, such as mobile phone sub-display, MP3 player and calculator, etc.
    SSD1306_display_datasheet.pdf 1.79 MB

프리징 파일

  • SSD1306 128x32 OLED Wiring
    Fritzing wiring diagram for an SSD1306 128x32 OLED display, showing connections to a microcontroller (likely Arduino). Useful for breadboard prototyping and verifying I2C or SPI wiring.
    SSD1306_128x32_wiring.fzz

Wiring diagram

  • SSD1306 128x32 OLED Wiring
    Fritzing wiring diagram for an SSD1306 128x32 OLED display, showing connections to a microcontroller (likely Arduino). Useful for breadboard prototyping and verifying I2C or SPI wiring.
    SSD1306_128x32_wiring.fzz