├── ExampleSketch.ino ├── OLedI2C.cpp ├── OLedI2C.h └── README.md /ExampleSketch.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Example sketch to display Strings and float values on the OLED 3 | 1602 display from Wide.HK. This uses a Lbrary that I've 4 | put together containing some basic functions. 5 | The I2C Address is set to 0x3C in OLedI2C.cpp 6 | Phil Grant 2013 www.gadjet.co.uk 07/11/13 7 | */ 8 | 9 | #include "Wire.h" 10 | #include "OLedI2C.h" 11 | OLedI2C LCD; 12 | float digit; //Variable to hold sample temperature value 13 | 14 | void setup() 15 | { 16 | Wire.begin(); 17 | LCD.init(); 18 | digit = 21.6; //This would normally be the float value returned from a temp sensor or other sensor 19 | } 20 | void loop() 21 | { 22 | LCD.sendString("Temp",0,0); //Now includes the cursor position data (col, row) 23 | LCD.sendFloat(digit,5,2,7,0); //Send the float value to the display starting 24 | //at col 7 row 1 with 5 digits and 2 decimal places 25 | while(1); 26 | } 27 | -------------------------------------------------------------------------------- /OLedI2C.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This is my first Library to make writing to the OLED 1602 3 | Display much easier, the display is based on the SSD1311. 4 | The display has 16 characters and 2 lines. 5 | The library is far from complete and may be prone to errors. 6 | Feedback welcome, visit www.gadjet.co.uk 7 | Phil Grant 2013 8 | Scrolling contributed by Nathan Chantrell http://nathan.chantrell.net/ 9 | 10 | Updated 06/11/2013 to include the cursPos data in the sendString function 11 | sendString("string", col, row) 12 | 13 | */ 14 | #include "OLedI2C.h" 15 | #include "Wire.h" 16 | #define OLED_Address 0x3c 17 | #define OLED_Command_Mode 0x80 18 | #define OLED_Data_Mode 0x40 19 | 20 | OLedI2C::OLedI2C(){} 21 | OLedI2C::~OLedI2C(){} 22 | 23 | 24 | void OLedI2C::init() { 25 | // *** I2C initial *** // 26 | delay(100); 27 | sendCommand(0x2A); // **** Set "RE"=1 00101010B 28 | sendCommand(0x71); 29 | sendCommand(0x5C); 30 | sendCommand(0x28); 31 | 32 | sendCommand(0x08); // **** Set Sleep Mode On 33 | sendCommand(0x2A); // **** Set "RE"=1 00101010B 34 | sendCommand(0x79); // **** Set "SD"=1 01111001B 35 | 36 | sendCommand(0xD5); 37 | sendCommand(0x70); 38 | sendCommand(0x78); // **** Set "SD"=0 01111000B 39 | 40 | sendCommand(0x08); // **** Set 5-dot, 3 or 4 line(0x09), 1 or 2 line(0x08) 41 | 42 | sendCommand(0x06); // **** Set Com31-->Com0 Seg0-->Seg99 43 | 44 | // **** Set OLED Characterization *** // 45 | sendCommand(0x2A); // **** Set "RE"=1 46 | sendCommand(0x79); // **** Set "SD"=1 47 | 48 | // **** CGROM/CGRAM Management *** // 49 | sendCommand(0x72); // **** Set ROM 50 | sendCommand(0x00); // **** Set ROM A and 8 CGRAM 51 | 52 | 53 | sendCommand(0xDA); // **** Set Seg Pins HW Config 54 | sendCommand(0x10); 55 | 56 | sendCommand(0x81); // **** Set Contrast 57 | sendCommand(0xFF); 58 | 59 | sendCommand(0xDB); // **** Set VCOM deselect level 60 | sendCommand(0x30); // **** VCC x 0.83 61 | 62 | sendCommand(0xDC); // **** Set gpio - turn EN for 15V generator on. 63 | sendCommand(0x03); 64 | 65 | sendCommand(0x78); // **** Exiting Set OLED Characterization 66 | sendCommand(0x28); 67 | sendCommand(0x2A); 68 | //sendCommand(0x05); // **** Set Entry Mode 69 | sendCommand(0x06); // **** Set Entry Mode 70 | sendCommand(0x08); 71 | sendCommand(0x28); // **** Set "IS"=0 , "RE" =0 //28 72 | sendCommand(0x01); 73 | sendCommand(0x80); // **** Set DDRAM Address to 0x80 (line 1 start) 74 | 75 | delay(100); 76 | sendCommand(0x0C); // **** Turn on Display 77 | } 78 | 79 | void OLedI2C::cursPos(uint8_t col, uint8_t row) 80 | { 81 | int row_offsets[] = { 0x00, 0x40 }; 82 | sendCommand(0x80 | (col + row_offsets[row])); 83 | } 84 | 85 | void OLedI2C::clearLcd() 86 | { 87 | sendCommand(0x01); 88 | } 89 | 90 | void OLedI2C::lcdOff() 91 | { 92 | sendCommand(0x08); // **** Turn on Off 93 | } 94 | 95 | void OLedI2C::lcdOn() 96 | { 97 | sendCommand(0x0C); // **** Turn on On 98 | } 99 | 100 | void OLedI2C::sendCommand(unsigned char command) 101 | { 102 | Wire.beginTransmission(OLED_Address); // **** Start I2C 103 | Wire.write(OLED_Command_Mode); // **** Set OLED Command mode 104 | Wire.write(command); 105 | Wire.endTransmission(); // **** End I2C 106 | delay(10); 107 | } 108 | void OLedI2C::sendFloat(float digit, uint8_t dec, uint8_t nad, uint8_t col, uint8_t row) 109 | { 110 | char line[10];//Ten characters, I hope that's enough 111 | dtostrf(digit,dec,nad,line);//Convert the float value to a string 112 | sendString(line, col, row); 113 | 114 | } 115 | 116 | void OLedI2C::setContrast(unsigned char contrast) // contrast as 0x00 to 0xFF 117 | { 118 | //Set OLED Command set 119 | sendCommand(0x2A); 120 | sendCommand(0x79); 121 | 122 | sendCommand(0x81); // Set Contrast 123 | sendCommand(contrast); // send contrast value 124 | sendCommand(0x78); // Exiting Set OLED Command set 125 | sendCommand(0x28); 126 | 127 | } 128 | 129 | void OLedI2C::sendString(const char *String, uint8_t col, uint8_t row) 130 | { 131 | cursPos(col, row); 132 | unsigned char i=0; 133 | while(String[i]) 134 | { 135 | sendData(String[i]); // *** Show String to OLED 136 | i++; 137 | } 138 | } 139 | void OLedI2C::sendData(unsigned char data) 140 | { 141 | Wire.beginTransmission(OLED_Address); // **** Start I2C 142 | Wire.write(OLED_Data_Mode); // **** Set OLED Data mode 143 | Wire.write(data); 144 | Wire.endTransmission(); // **** End I2C 145 | } 146 | void OLedI2C::scrollString(char* message, byte row, unsigned int time)//written by Nathan Chantrell http://nathan.chantrell.net/ 147 | { 148 | char buffer[16]; 149 | for (byte i=0;istrlen(message)+15)) { // pad and trail with blank spaces 153 | buffer[j]=' '; 154 | } 155 | else buffer[j]=message[pos-16]; 156 | pos++; 157 | } 158 | //cursPos(0,row); removed by PG 159 | sendString(buffer, 0, row); //Edited by PG tho include the cursor pos within the sendString command 160 | delay(time); 161 | } 162 | } 163 | -------------------------------------------------------------------------------- /OLedI2C.h: -------------------------------------------------------------------------------- 1 | /* 2 | OLedI2C.h 3 | OLED 1602 Library for the I2C 4 | OLED display from WIDE.HK 5 | Also my first attempt at a Library 6 | Phil Grant Sept 2013 7 | www.gadjet.co.uk 8 | Scrolling contributed by Nathan Chantrell http://nathan.chantrell.net/ 9 | Updated 06/11/2013 to include the cursPos data within the sendString function. 10 | */ 11 | #ifndef OLedI2C_h 12 | #define OLedI2C_h 13 | #include "Arduino.h" 14 | #include "Wire.h" 15 | 16 | class OLedI2C { 17 | public: 18 | OLedI2C(); 19 | ~OLedI2C(); 20 | void init(); 21 | void sendCommand(unsigned char command); 22 | void sendString(const char *String, uint8_t col, uint8_t row); 23 | void sendFloat(float digit, uint8_t dec, uint8_t nad, uint8_t col, uint8_t row); 24 | void sendData(unsigned char data); 25 | void clearLcd(); 26 | void cursPos(uint8_t, uint8_t); // cloumn, row 27 | void scrollString(char* message, byte row, unsigned int time); //written by Nathan Chantrell http://nathan.chantrell.net/ 28 | void lcdOff(); 29 | void lcdOn(); 30 | void setContrast(unsigned char contrast); // contrast should be the hex value between 0x00 and 0xFF 31 | }; 32 | #endif 33 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 1602-OLED-Arduino-Library 2 | ========================= 3 | 4 | wide.HK 1602 OLED Library 5 | 6 | This Library is intended for the Wide.HK OLED 1602 display. 7 | 8 | KEYWORDS 9 | 10 | OLedI2C KEYWORD1 11 | 12 | init KEYWORD2 13 | 14 | sendCommand KEYWORD2 15 | 16 | sendString KEYWORD2 17 | 18 | sendData KEYWORD2 19 | 20 | clearLcd KEYWORD2 21 | 22 | cursPos KEYWORD2 23 | 24 | scrollString KEYWORD2 Writen by Nathan Chantrell http://nathan.chantrell.net 25 | 26 | Updated sendString to include cursPos data. sendString("String", col, row) 27 | Updated Library by adding sendFloat function, this allows a float value to be 28 | sent to the LCD, the float is converted to a string and sent using the sendString function 29 | --------------------------------------------------------------------------------