- Notifications
You must be signed in to change notification settings - Fork656
Driver for the SSD1306 and SH1106 based 128x64, 128x32, 64x48 pixel OLED display running on ESP8266/ESP32
License
ThingPulse/esp8266-oled-ssd1306
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
This is a driver for SSD1306 and SH1106 128x64, 128x32, 64x48 and 64x32 OLED displays running on the Arduino/ESP8266 & ESP32 and mbed-os platforms.Can be used with either the I2C or SPI version of the display.
This library drives the OLED display included in theThingPulse IoT starter kit aka classic kit aka weather station kit.
You can either download this library as a zip file and unpack it to your Arduino/libraries folder or find it in the Arduino library manager under "ESP8266 and ESP32 Oled Driver for SSD1306 display". For mbed-os a copy of the files are available as an mbed-os library.
It is also available as aPlatformIO library. Just execute the following command:
platformio lib install 2978![]() | This is a ThingPulseprime project. See ouropen-source commitment declaration for what this means. |
This library has initially been written byDaniel Eichhorn. Many thanks go toFabrice Weinberg for optimizing and refactoring many aspects of the library. Also many thanks to the many committers who helped to add new features and who fixed many bugs. Mbed-OS support and other improvements were contributed byHelmut Tschemernjak.
The init sequence for the SSD1306 was inspired by Adafruit's library for the same display.
This library has been adopted to support the ARM mbed-os environment. A copy of this library is available in mbed-os under the name OLED_SSD1306 by Helmut Tschemernjak. An alternate installation option is to copy the following files into your mbed-os project: OLEDDisplay.cpp OLEDDisplay.h OLEDDisplayFonts.h OLEDDisplayUi.cpp OLEDDisplayUi.h SSD1306I2C.h
Check out the examples folder for a few comprehensive demonstrations how to use the library. Also check out theESP8266 Weather Station library which uses the OLED library to display beautiful weather information.
The API changed a lot with the 3.0 release. If you were using this library with older versions please have a look at theUpgrade Guide.
Going from 3.x version to 4.0 a lot of internals changed and compatibility for more displays was added. Please read theUpgrade Guide.
- Draw pixels at given coordinates
- Draw lines from given coordinates to given coordinates
- Draw or fill a rectangle with given dimensions
- Draw Text at given coordinates:
- Define Alignment: Left, Right and Center
- Set the Fontface you want to use (see section Fonts below)
- Limit the width of the text by an amount of pixels. Before this widths will be reached, the renderer will wrap the text to a new line if possible
- Display content in automatically side scrolling carousel
- Define transition cycles
- Define how long one frame will be displayed
- Draw the different frames in callback methods
- One indicator per frame will be automatically displayed. The active frame will be displayed from inactive once
Fonts are defined in a proprietary but open format. You can create new font files by choosing from a given listof open sourced Fonts from this web app:http://oleddisplay.squix.chChoose the font family, style and size, check the preview image and if you like what you see click the "Create" button. This will create the font array in a text area form where you can copy and paste it into a new or existing header file.
The library supports different protocols to access the OLED display. Currently there is support for I2C using the built in Wire.h library, I2C by using the much fasterBRZO I2C library written in assembler and it also supports displays which come with the SPI interface.
#include<Wire.h>#include"SSD1306Wire.h"// for 128x64 displays:SSD1306Wiredisplay(0x3c, SDA, SCL);// ADDRESS, SDA, SCL// for 128x32 displays:// SSD1306Wire display(0x3c, SDA, SCL, GEOMETRY_128_32); // ADDRESS, SDA, SCL, GEOMETRY_128_32 (or 128_64)// for using 2nd Hardware I2C (if available)// SSD1306Wire(0x3c, SDA, SCL, GEOMETRY_128_64, I2C_TWO); //default value is I2C_ONE if not mentioned// By default SD1306Wire set I2C frequency to 700000, you can use set either another frequency or skip setting the frequency by providing -1 value// SSD1306Wire(0x3c, SDA, SCL, GEOMETRY_128_64, I2C_ONE, 400000); //set I2C frequency to 400kHz// SSD1306Wire(0x3c, SDA, SCL, GEOMETRY_128_64, I2C_ONE, -1); //skip setting the I2C bus frequency
for a SH1106:
#include<Wire.h>#include"SH1106Wire.h"SH1106Wiredisplay(0x3c, SDA, SCL);// ADDRESS, SDA, SCL// By default SH1106Wire set I2C frequency to 700000, you can use set either another frequency or skip setting the frequency by providing -1 value// SH1106Wire(0x3c, SDA, SCL, GEOMETRY_128_64, I2C_ONE, 400000); //set I2C frequency to 400kHz// SH1106Wire(0x3c, SDA, SCL, GEOMETRY_128_64, I2C_ONE, -1); //skip setting the I2C bus frequency
#include<brzo_i2c.h>#include"SSD1306Brzo.h"SSD1306Brzodisplay(0x3c, SDA, SCL);// ADDRESS, SDA, SCL
or for the SH1106:
#include<brzo_i2c.h>#include"SH1106Brzo.h"SH1106Brzodisplay(0x3c, SDA, SCL);// ADDRESS, SDA, SCL
#include<SPI.h>#include"SSD1306Spi.h"SSD1306Spidisplay(D0, D2, D8);// RES, DC, CS
or for the SH1106:
#include<SPI.h>#include"SH1106Spi.h"SH1106Spidisplay(D0, D2, CS);// RES, DC, CS
In case the CS pin is not used (hard wired to ground), pass CS as -1.
// Initialize the displayvoidinit();// Free the memory used by the displayvoidend();// Cycle through the initializationvoidresetDisplay(void);// Connect again to the display through I2Cvoidreconnect(void);// Turn the display onvoiddisplayOn(void);// Turn the display offsvoiddisplayOff(void);// Clear the local pixel buffervoidclear(void);// Write the buffer to the display memoryvoiddisplay(void);// Inverted display modevoidinvertDisplay(void);// Normal display modevoidnormalDisplay(void);// Set display contrast// really low brightness & contrast: contrast = 10, precharge = 5, comdetect = 0// normal brightness & contrast: contrast = 100voidsetContrast(uint8_t contrast,uint8_t precharge =241,uint8_t comdetect =64);// Convenience method to accessvoidsetBrightness(uint8_t);// Turn the display upside downvoidflipScreenVertically();// Draw the screen mirroredvoidmirrorScreen();
/* Drawing functions*/// Sets the color of all pixel operations// color : BLACK, WHITE, INVERSEvoidsetColor(OLEDDISPLAY_COLOR color);// Draw a pixel at given positionvoidsetPixel(int16_t x,int16_t y);// Draw a line from position 0 to position 1voiddrawLine(int16_t x0,int16_t y0,int16_t x1,int16_t y1);// Draw the border of a rectangle at the given locationvoiddrawRect(int16_t x,int16_t y,int16_t width,int16_t height);// Fill the rectanglevoidfillRect(int16_t x,int16_t y,int16_t width,int16_t height);// Draw the border of a circlevoiddrawCircle(int16_t x,int16_t y,int16_t radius);// Fill circlevoidfillCircle(int16_t x,int16_t y,int16_t radius);// Draw an empty triangle i.e. only the outlinevoiddrawTriangle(int16_t x0,int16_t y0,int16_t x1,int16_t y1,int16_t x2,int16_t y2);// Draw a solid triangle i.e. filledvoidfillTriangle(int16_t x0,int16_t y0,int16_t x1,int16_t y1,int16_t x2,int16_t y2);// Draw a line horizontallyvoiddrawHorizontalLine(int16_t x,int16_t y,int16_t length);// Draw a lin verticallyvoiddrawVerticalLine(int16_t x,int16_t y,int16_t length);// Draws a rounded progress bar with the outer dimensions given by width and height. Progress is// a unsigned byte value between 0 and 100voiddrawProgressBar(uint16_t x,uint16_t y,uint16_t width,uint16_t height,uint8_t progress);// Draw a bitmap in the internal image formatvoiddrawFastImage(int16_t x,int16_t y,int16_t width,int16_t height,constuint8_t *image);// Draw a XBMvoiddrawXbm(int16_t x,int16_t y,int16_t width,int16_t height,constuint8_t *xbm);
// Draws a string at the given location, returns how many chars have been writtenuint16_tdrawString(int16_t x,int16_t y,const String &text);// Draws a String with a maximum width at the given location.// If the given String is wider than the specified width// The text will be wrapped to the next line at a space or dash// returns 0 if everything fits on the screen or the numbers of characters in the// first line if notuint16_tdrawStringMaxWidth(int16_t x,int16_t y,uint16_t maxLineWidth,const String &text);// Returns the width of the const char* with the current// font settingsuint16_tgetStringWidth(constchar* text,uint16_t length,bool utf8 =false);// Convencience method for the const char versionuint16_tgetStringWidth(const String &text);// Specifies relative to which anchor point// the text is rendered. Available constants:// TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER, TEXT_ALIGN_RIGHT, TEXT_ALIGN_CENTER_BOTHvoidsetTextAlignment(OLEDDISPLAY_TEXT_ALIGNMENT textAlignment);// Sets the current font. Available default fonts// ArialMT_Plain_10, ArialMT_Plain_16, ArialMT_Plain_24// Or create one with the font tool at http://oleddisplay.squix.chvoidsetFont(constuint8_t* fontData);
Because this class has been "derived" from Arduino'sPrint class, you can use the functions it provides. In plain language, this means that you can useprint,println andprintf to the display. Internally, a buffer holds the text that was printed to the display previously (that would still fit on the display) and every time you print something, this buffer is put on the screen, using the functions from the previous section.
What that means is that printing usingprint and "manually" putting things on the display are somewhat mutually exclusive: as soon as you print, everything that was on the display already is gone and only what you put there before withprint,println orprintf remains. Still, usingprint is a very simple way to put something on the display quickly.
One extra function is provided:cls()
// cls() will clear the display immediately and empty the logBuffer, meaning// the next print statement will print at the top of the display again.// cls() should not be confused with clear(), which only clears the internal// graphics buffer, which can then be shown on the display with display().voidcls();> _Note that printing to the display, contrary to what you might expect, doesnot wrap your lines, so everything on a line that doesn't fit on the screen is cut off._
The Ui Library is used to provide a basic set of user interface elements calledFrames andOverlays. AFrame is used to provideinformation to the user. The default behaviour is to display aFrame for a defined time and than move to the nextFrame. The library alsoprovides anIndicator element that will be updated accordingly. AnOverlay on the other hand is a piece of information (e.g. a clock) thatis always displayed at the same position.
/** * Initialise the display*/voidinit();/** * Configure the internal used target FPS*/voidsetTargetFPS(uint8_t fps);/** * Enable automatic transition to next frame after the some time can be configured with * `setTimePerFrame` and `setTimePerTransition`.*/voidenableAutoTransition();/** * Disable automatic transition to next frame.*/voiddisableAutoTransition();/** * Set the direction if the automatic transitioning*/voidsetAutoTransitionForwards();voidsetAutoTransitionBackwards();/** * Set the approx. time a frame is displayed*/voidsetTimePerFrame(uint16_t time);/** * Set the approx. time a transition will take*/voidsetTimePerTransition(uint16_t time);/** * Draw the indicator. * This is the default state for all frames if * the indicator was hidden on the previous frame * it will be slided in.*/voidenableIndicator();/** * Don't draw the indicator. * This will slide out the indicator * when transitioning to the next frame.*/voiddisableIndicator();/** * Enable drawing of all indicators.*/voidenableAllIndicators();/** * Disable drawing of all indicators.*/voiddisableAllIndicators();/** * Set the position of the indicator bar.*/voidsetIndicatorPosition(IndicatorPosition pos);/** * Set the direction of the indicator bar. Defining the order of frames ASCENDING / DESCENDING*/voidsetIndicatorDirection(IndicatorDirection dir);/** * Set the symbol to indicate an active frame in the indicator bar.*/voidsetActiveSymbol(constuint8_t* symbol);/** * Set the symbol to indicate an inactive frame in the indicator bar.*/voidsetInactiveSymbol(constuint8_t* symbol);/** * Configure what animation is used to transition from one frame to another*/voidsetFrameAnimation(AnimationDirection dir);/** * Add frame drawing functions*/voidsetFrames(FrameCallback* frameFunctions,uint8_t frameCount);/** * Add overlays drawing functions that are draw independent of the Frames*/voidsetOverlays(OverlayCallback* overlayFunctions,uint8_t overlayCount);/** * Set the function that will draw each step * in the loading animation*/voidsetLoadingDrawFunction(LoadingDrawFunction loadingDrawFunction);/** * Run the loading process*/voidrunLoadingProcess(LoadingStage* stages,uint8_t stagesCount);// Manual controlvoidnextFrame();voidpreviousFrame();/** * Switch without transition to frame `frame`.*/voidswitchToFrame(uint8_t frame);/** * Transition to frame `frame`. When the `frame` number is bigger than the current * frame the forward animation will be used, otherwise the backwards animation is used.*/voidtransitionToFrame(uint8_t frame);// State InfoOLEDDisplayUiState*getUiState();// This needs to be called in the main loop// the returned value is the remaining time (in ms)// you have to draw after drawing to keep the frame budget.int8_tupdate();
If you want to display your own images with this library, the best way to do this is using a bitmap.
There are two options to convert an image to a compatible bitmap:
- Using Gimp.In this case exporting the bitmap in an 1-bit XBM format is sufficient.
- Using a converter website.You could also use online converter services like e.g.https://javl.github.io/image2cpp/. The uploaded image should have the same dimension as the screen (e.g. 128x64). The following output settings should be set:
- Draw Mode: Horizontal - 1 bit per pixel
- Swap bits in byte: swap checkbox should be checked.
The resulting bitmap can be put into a header file:
constunsignedchar epd_example [] PROGMEM = {0x00,0x00,0x00,0x00,0x00,0x00, ... ...};
Subsequently, it can be used like this:
display.clear();display.drawXbm(0,0,128,64, epd_example);// assuming your bitmap is 128x64display.display();
This frame shows three things:
- How to draw an XMB image
- How to draw static text which is not moved by the frame transition
- The active/inactive frame indicators
Currently there are one fontface with three sizes included in the library: Arial 10, 16 and 24. Once the converter is published you will be able to convert any ttf font into the used format.
This frame demonstrates the text alignment. The coordinates in the frame show relative to which position the texts have been rendered.
This shows how to use define a maximum width after which the driver automatically wraps a word to the next line. This comes in very handy if you have longer texts to display.
This shows the code working on the SPI version of the display. See demo code for ESP8266 pins used.
- QRCode ESP8266 (by @anunpanya)
- Scan I2C (by @hallard)
- ThingPulse Weather Station
- Meshtastic - an open source GPS communicator mesh radio
- OpenMQTTGateway - OpenMQTTGateway aims to unify various technologies and protocols into a single firmware. This reduces the need for multiple physical bridges and streamlines diverse technologies under the widely-used MQTT protocol.
- OpenAstroTracker - Open source hardware and software for Astrophotography. The firmware for the mounts supports displays and uses this library to drive them.
- Yours?
About
Driver for the SSD1306 and SH1106 based 128x64, 128x32, 64x48 pixel OLED display running on ESP8266/ESP32
Topics
Resources
License
Contributing
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.







