본문 바로가기

상상 Maker Space and US

아두이노 따라하기_02_128x64 I2C OLED 구동하기

128x64 I2C OLED를 아두이노 우노 보드에서 구동시키는 방법입니다. 

 

●준비물

 -. 아두이노 우노 

 -. 128x64 I2C OLED 

 -. Wire

 -. bread board(wire만으로도 연결가능)

 

●제품 사양 

 -. interface : I2C(동일 제품에 SPI 통신으로 interface하는 제품도 있음)

 -. 해상도 : 128x64 

 -. 사이즈 : 0.96" 

 -. 구동전압 : 3~5V( 구동전압은 3.3V/5V 둘다 잘 동작하는것을 확인 하였습니다.)

 -. 드라이버 IC : SSD1306 

 

 

●결선도 

점퍼핀이 수직으로 나와 있어 bread board에 연결함. 

전원을 3.3V, GDN, I2C(SCL, SDA) 연결 

 

●라이브러리 

-. u8g2 

라이브러리 매지저를 이용한 설치 방법입니다. 

툴>라이브러리 관리 실행하여 설치하고자하는 라이브러리를 검색하여 설치하는 방법입니다. 

설치하고 나니 'INSTALLED'로 나옵니다. 

(지난번 2004 LiquicCrystal_I2C 설치할때와 똑같이 해도됩니다.)

-. 다른 라이브러리 이용하기 

   Adafruit SD1306

   Adafruit GFX Library 

   라이브러리 추가하는 방법은 이제 생략하겠습니다. 

 

●code 

-. u8g2 이용하기 

어떤 라이브러리를 추가하든 예제가 주어집니다. 예제를 실행해 Hellow world를 추가해 보겠습니다. 

파일>예제>u8g2>u8x8>HellowWorld

SSD1306 driver를 IC를 사용하고 128X64 OLED는 동일하지만 만드는 회사마다, Interface가 조금씩 다르기때문에 이부분을 잘 선택해 줍니다. 주석처리된 부분을 해제시켜 줍니다. 저같은 경우는 아래 녹색 글씨 부분입니다. 

 

#include <Arduino.h>

#include <U8x8lib.h>

#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>

#endif

 

U8X8_SSD1306_128X64_NONAME_SW_I2C u8x8(/* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);   // OLEDs without Reset of the Display

 

void setup(void)
{
  u8x8.begin();
  u8x8.setPowerSave(0);      
}

void loop(void)
{
  u8x8.setFont(u8x8_font_chroma48medium8_r);
  u8x8.drawString(0,0,"Helloo World!");
  delay(2000);
}

-. Adafruit SD1306이용하기 

파일>예제>adafruit SSD1306>ssd1306_128x64_i2c 

 

code가 길어서 적지 않았습니다 바로 실행해 보세요. 

화면이 아무것도 출력이 되지 않으면, 아래의 코드레서 Address를 check해주세요.

0x3D > 0x3C로 변경해보고 실행 

  if(!display.begin(SSD1306_SWITCHAPVCC,0x3D)) { // Address 0x3D for 128x64

 

0x3D, 0x3C로도 안되면 Address를 아래의 방법으로 찾아서 변경해 주시면 됩니다.

https://www.youtube.com/watch?v=dVqv3LvJAJY

https://garagegeekguy.blogspot.com/2018/10/how-to-find-out-address-of-your-i2c.html

 

간단히 설명하면 

아래의 code를 복사해 넣고 실행한후에 툴>시리얼모니터를 실행하면 I2C에 붙어있는 Device의 address를 알수 있습니다. 



#include <Wire.h>

 

void setup()

{

Wire.begin();

Serial.begin(9600);

while (!Serial);

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("Unknow 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

}


●결과물 

좌(u8g2 라이브러리 이용), 중,우(Adafruit SSD1306, Adafruit GFX 라이브러리 이용)

 

●마치며 

단순 text출력은 비교적 쉬운데, 그래픽 관련 API들을 사용하려면, 이제부터는 공부가 필요합니다. 

그리고 화면 구성을 어떻게 사용할지도 구상해야 합니다.

라이브러리마다 github에서 매뉴얼도 제공하기때문에, 참조하시어 본인의 제작물에 적용을 해보시면 되겠습니다. 

 

https://github.com/olikraus/u8g2/wiki

https://github.com/adafruit/Adafruit_SSD1306

https://github.com/adafruit/Adafruit-GFX-Library

 

 

나머지 참조할만한 내용들입니다. 

 

https://m.blog.naver.com/PostView.nhn?blogId=namgoocha&logNo=220670935526&proxyReferer=https%3A%2F%2Fwww.google.co.kr%2F

https://www.best-microcontroller-projects.com/ssd1306.html