Full source code for project.
Include File: "OLED.h" ++
Constructor OLED(sda, scl, rst, address=0x3c, offset=0); **
public methods:
void begin(void);
void on(void);
void off(void);
void clear(void);
void print(char *s, uint8_t r=0, uint8_t c=0);
void print(unsigned char ch);
void print(int ii);
++ OLED will include "Wire.h" that is part of the Arduino IDE
** With the OLED used in this project the default address 0x3C matches the actual address.
---------------------------------
Abstract: Basically a brief summary on how to use the TTGO ESP8266 OLED
Keywords: OLED, TTGO ESP8266
Possible Audience: Developers wishing to use the TTGO ESP8266 OLED. Minimal programming skills required.
Hardware Required: TTGO ESP8266 OLED (and Arduino IDE) only**.
Libraries Required: Wire.h part of the Arduino IDE. Project will download/modify existing OLED library
** The prototype uses the board "LOLIN(WEMOS) D1 mini Lite. Select as shown
I have a TTGO ESP8266 OLED that I wish to use for a WiFi project. Photo shows the device in a WiFi model train points controller.
The OLED uses an I2C interface at address 0x3C and is wired to the ESP8266 as follows:
#define SDA D4
#define SCL D5
#define RST D2
This page will summarise putting the device to work.
---------------------
The OLED display is interfaced to the ESP8266 via the I2C bus.
The seller provided some sample code. A simplified version OLED.ino will be used to test the LED.:
The above program included the file "OLED.h". Where the OLED.h (and OLED.cpp) code came from was not specified.
A search of the web (2019) with the keyword "OLED.h" gave the following page:
github.com/klarsys/esp8266-OLED
This page contained two files OLED.cpp and OLED.h.
A new project was set up that included three files:
(i) OLED the test as given above. The Adruino IDE will give it the extension *.ino.
(ii) OLED.cpp (In Arduino IDE use inverted triangle and create New_Tab - give it the label OLED.cpp
(iii) OLED.h
Populate OLED.cpp and OLED.h from github.com/klarsys/esp8266-OLED.
---------------------------
Running the code as given gave a rubbish message on the display.
Closer examination revealed every alternate row was missing.
Displaying 0AAH should give alternate lines but gave a solid block 4 rows high
Displaying 055H (the complement of 0AAH) should also give alternate lines but gave a blank display
Note the comments in the OLED.cpp page indicate the code is for a 128x64 device- the device in this HTML page is 128X32 so changes should be expected.
The solution was to modify one line of the initialisation code (in OLED.cpp)
The critical command was the DA command towards the end which controls the H/W configuration.
The command was changed from 0xDA, 0x12 to 0xDA,0x 22 resulting in the OLED displaying correctly.
See snippet below from the display driver chip (SSD1306) data manual.
--------------------------
As given the program will only print character strings. As part of the implementation the program prints a single character. This code may be trapped to allow a print(unsigned char ch) method.
1. In the header file add the public method void print(char ch); and add the private method void SendChar(unsigned char ch)** to the OLED class.
2. Implement the code for print(ch) in the *.cpp file. ie
** SendChar actually sends the information for the first column of a character. To obtain the full character 8 columns must be send/transferred.
--------------------------------------------------------
To upgrade the code to handle integers:
1. Add the following to the header file
void print(int ii);
2. Add the following to the implementation code**
With this code the only complication is that the order must be reversed which gives a second while( ) loop.
** This code only applies to positive integers
----------------------------------
To simplify the library for the end user the OLED setup can be implemented/transferred to the library.
1. In the header and implementation files add the reset pin to the constructor. ie OLED(uint8_t sda, uint8_t scl, uint8_t rst,uint8_t address=0x3c, uint8_t offset=0). Note the reset pin is the third parameter. To match the instance/object in the application code should include RST. ie OLED display(SDA, SCL, RST) where RST has been previously defined as pin D2.
2. Create a private variable _rst in the header file private: uint8_t _sda, _scl, _rst, _address, _offset; and in the implementation file assign it to the third parameter of the constructor. ie
OLED::OLED(uint8_t sda, uint8_t scl, uint8_t rst, uint8_t address, uint8_t offset) {_sda = sda;_scl = scl;_rst = rst;_address = address;_offset = offset;}3. Move the initialisations from the *.ino file to the begin( ) code in the implementation replacing RST with _rst
void OLED::begin(void) {// set up i2cpinMode(_rst, OUTPUT);digitalWrite(_rst, LOW); // turn D2 low to reset OLEDdelay(50);digitalWrite(_rst, HIGH); // while OLED is running, must set D2 in highdelay(100);Wire.begin(_sda, _scl);init_OLED();reset_display();}The basic starting code is reduced to:
#include "OLED.h"OLED display(D4,D5,D2);void setup(){display.begin();}void loop() {}The code can then be enhanced with any messages etc
----------------------------------------------
To save the library for future projects:
(i) Make a *.zip file of the OLED.cpp and OLED.h files. By default it will have the label OLED.zip
(ii) In the Arduino IDE select sketch--> Include Library --> Add *.ZIP Library.
(iii) Select OLED.zip. It will now be added to your libraries. See Files -->Preferences --> Sketch book location.
(iv) To use the library in future projects manually use "#include <OLED.h" or use Sketch-->Include Library and select from contributed list.
In all embedded systems time is a consideration. If a product will not meet its time requirements then the solutions are to consider a different algorithm or implement the design with a fast more expensive micro-controller. By the same token if the design more that meets its time requirements it might be possible to select a less expensive micro-controller or to slow the clock speed and reduce the potential for electrical interference.
The following trace shows the time response of the OLED at start up. The 50mSec pulse and 100mSec delay are part of the code. Then follows a 324mSec burst of activity on the I2C lines (clock and data). This is the println( ) implemented with a library routine. The developer will now know writing a one line message will take 300mSec and can use this as a starting point in any future performance calculations.
By zooming in on the trace the two signals may be seen in greater detail. The display was at 100mSec/div and the expanded trace 20uSec/div. It may be deduced that SCLK is 10uSec and one byte of data takes 95uSec..