Monday, February 15, 2016

Arduino: Connecting to the 0.96" OLED Display Module LED Display

I bought an OLED display module on Ebay couple weeks ago.  It is bigger (a lot) looking at the image but physically the board is about 1"x1".  The display is about 1" x 9/16".  There are quite a few examples and tutorials on the web you can just download and use.  I thought that I could put together this tutorial as a note for later reference and for anyone that does not know how and where to start.  There were some issues with I2C mode that I couldn't get it to run on this display.  Maybe it just my Mega 2560 board as I haven't successfully get the to I2C run on it.

Features:


1.Module Size :27.0MM*27.0MM*4.1MM
2. Resolution: 128 x 64
3. Visual Angle: >160°
4. Input Voltage: 3.3V ~ 6V
5. Compatible I/O Level: 3.3V, 5V
6.Viewing angle:>160
7. Only Need 2 I/O Port to Control
8. IC :SSD1306
9. Full Compatible Arduino, 51 Series,MSP430 series,STM32/2,CSR IC,etc

 

Display Pinout:


- GND - Connects to ground connection
- VDD - Connects to 3.3 volt supply
- SCK - Serial clock pin
- SDA - Serial data pin
- RES - Reset pin, this pin is high during operation.
- DC - Data command pin, when high data pins D0-D7 will be treated as data.  When low the data will be sent to the command register.
- CS - Chip select pin, active low

This display uses SSD1306 chip, see chip spec here for details.

Adafruit has a library called Adafruit_SSD1306 that is compatible with this display and can be downloaded here.

Only two files are required: 
- Adafruit_SSD1306.cpp
- Adafruit_SSD1306.h

Create Adafruit_SSD1306 folder in C:\Users\<UserName>\Documents\Arduino\libraries directory and copy the two files into it.  This library requires the Adafruit_GFX library which can be downloaded here.  Again, only two files are required:

- Adafruit-GFX.cpp
- Adafruit-GFX.h

Copy the two files into C:\Users\<UserName>\Documents\Arduino\libraries\Adafruit-GFX directory.

The Adafruit_SSD1306 class in Adafruit_SSD1306.cpp runs in three modes: Custom pin SPI (Software SPI), hardware SPI and I2C.

Custom Pin SPI Mode:


In this mode, any digital pins on Arduino or Mega can be used to connect to the display's pins (SCK, SDA, RES, DC and CS).  The Adafruit_SSD1306 also supports different resolutions 128x64, 128x32 and 128x16.  Make sure the line #define SSD1306_128_64 is uncommented in the Adafruit_SSD1306.h file and other resolution types are commented out.  The following example shows the usage of the library in custom pin mode.  In the example, I used the Mega 2560 and chose digital pins 22,24,26,28,30 for SCK, SDA, RESET, DC, CS respectively:

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

#define SCK 22
#define SDA 24
#define RESET 26
#define DC    28
#define CS    30
Adafruit_SSD1306 display(SDA, SCK, DC, RESET, CS);

void setup()
{
  //Setup internal VCC to supply voltage
  display.begin(SSD1306_SWITCHCAPVCC);
  // Display the Adafruit logo
  display.display();
 
}

void loop()
{
  delay(1000);
  display.clearDisplay();
  // Draw a pixel at location (10,10)
  display.drawPixel(10, 10, WHITE);
  display.display();
  delay(1000);
  // Draw vertical and horizontal line from (0,0) and 100 pixels long with color white
  display.drawFastVLine(0, 0, 100, WHITE);
  display.drawFastHLine(0, 0, 100, WHITE);
  delay(1000);
  // Setup text size, color and display numbers and characters.
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0,0);

  for (uint8_t i='0'; i <= '9'; i++) 
    display.write(i);
  display.println();
  for (uint8_t i='A'; i <= 'Z'; i++) 
    display.write(i);
  display.println();
  for (uint8_t i='a'; i <= 'z'; i++) 
    display.write(i);
  display.println();
  display.display();
}

Hardware SPI Mode:


Using the hardware SPI, the display's SDA pin connects to the Arduino's MOSI pin and the display's SCK connects to Arduino's SCK pin.  The DC, RESET and CS pins can connect to any digital pins.  On the Mega 2560, MOSI and SCK are 51 and 52 respectively.  On the Arduino UNO they are 11 and 13 respectively. In the example above, I just have to change:
#define SCK 22
#define SDA 24
#define RESET 26
#define DC    28
#define CS    30
Adafruit_SSD1306 display(SDA, SCK, DC, RESET, CS);
to

#define RESET 26
#define DC    28
#define CS    30
Adafruit_SSD1306 display(DC, RESET, CS); 

I2C Mode:

In this mode, supposedly all I need are the RESET pin and the I2C pins (SCK and SDA).  The reset pin can be connected to any digital pin.  The display's SCK is connected to Arduino's SCL pin and the display's SDA is connected to Arduino's SDA pin.  On the Mega 2560, the SCL and SDA pins are 22 and 21 respectively.  On the Arduino, the SCL is A5 and SDA is A4. So in the example above, I just have to remove:

#define SCK 22
#define SDA 24
#define RESET 26
#define DC    28
#define CS    30
Adafruit_SSD1306 display(SDA, SCK, DC, RESET, CS);

and add

#define RESET 26
Adafruit_SSD1306 display(RESET);

As mentioned, I have not successfully run I2C on my Mega.  I gotta try this on my Uno when it arrives.


No comments:

Post a Comment