AdSense

Sunday 14 December 2014

I2C OLED display

(Deutsche Version) If you look for "Arduino OLED Display" on Ebay, you will find a display with a size of 1 inch and a resolution of 128x84 pixels which is controlled via I2C. It costs less than 4 Euros. Of course, 1 inch isn't much, but I was curious and so I ordered it. To show you how small it really is, I placed a coin next to it:


Connecting the display is very easy (thanks to I2C): you only need the voltage supply and two wire for I2C. But how can you control the display? After some searching on Google I found out that the display is a clonde of the Adafruit OLED display called SSD1306. Adafruit offers an Arduino library for this display which can be downloaded HERE. Additionally you need the Adafruit-GFX-Library. After installing the two libraries you will find the example "ssd1306_128x64_i2c" under "Adafruit_SSD1306" in the Arduino IDE. But if you transmit this example to the Arduino nothing will happen. The china-clone-display has a different I2C adress than the original Adafruit display. You will have to change something in the setup() function. Change
  display.begin(SSD1306_SWITCHCAPVCC, 0x3D);  // initialize with the I2C addr 0x3D (for the 128x64)
to
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3C (for the 128x64).

After that you can transmit the example to your Arduino and you should see a small demonstration of the different functions of the GFX library on the display. Since there are a lot of functions and the example has a lot of code I will not explain it in detail. I think all of the functions in the example are self-explanatory. I you have problems understanding the code, just have a look in the header file of the GFX library. There you find further explanations like parameters for the functions. You can print text, draw squares, triangles, etc. Even inverting or scrolling the screen is possible.

You also have the possibility to print a user-defined bitmap. I'm going to explain this:

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

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

#define XPOS 50
#define YPOS 25
#define BITMAP_HEIGHT 16 
#define BITMAP_WIDTH  16 
static const unsigned char PROGMEM bitmap[] =
{ B00000001, B10000000,
  B00000010, B01000000,
  B00000100, B00100000,
  B00001000, B00010000,
  B00010000, B00001000,
  B00111111, B11111100,
  B00101000, B00010100,
  B00100100, B00100100,
  B00100010, B01000100,
  B00100001, B10000100,
  B00100010, B01000100,
  B00100100, B00100100,
  B00101000, B00010100,
  B00110000, B00001100,
  B00111111, B11111100,
  B00000000, B00000000 };

void setup()   {                

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3C (for the 128x64)
  
  //clear display buffer
  display.clearDisplay();
  //draw a bitmap stored in variable bitmap with given size at XPOS, YPOS in color white
  display.drawBitmap(XPOS, YPOS, bitmap, BITMAP_WIDTH, BITMAP_HEIGHT, WHITE);
  display.display();
 
}


void loop() { 
}


In this example we are going to draw a bitmap with the size of 16x16 pixels. Of course you can choose another size. The bitmap has to be stored in a character array. It is very important to use the keyword "PROGMEM" when declaring the variable. This will make the Arduino store the variable in the flash memory. The library only accepts bitmaps stored in flash. In the example, two characters store the 16 bit data of a line of the bitmap. If a bit is set, a pixel is set when the bitmap is drawn. To increase readability, line breaks were inserted. On line in the code is on line of the bitmap. To draw the bitmap, you have to call the function drawBitmap(). Parameters are: x- and y-position of the bitmap, the variable in which the bitmap is stored, height und width of the bitmap and its color (BLACK or WHITE) of a set pixel.

The result:

No comments:

Post a Comment