This post shows how to interface PIC18F46K22 microcontroller with SSD1306 OLED (128×64 pixel).
The SSD1306 is a monochrome display which means it has only one color (white, blue, yellow …).
This display communicates with the master device over I2C mode, SPI mode or 8-bit parallel mode. In this project I’m going to use the I2C mode.
The compiler used in this project is mikroElektronika mikroC PRO for PIC.
In the I2C mode there are two lines between the microcontroller and the SSD1306 OLED display board: SDA (serial data) and SCL (serial clock). An additional pin for hardware reset is required if the display has a reset pin.
In this project the SSD1306 OLED is configured to work in I2C mode, make sure that your display is configured to work in I2C mode, some displays need jumper placing or some soldering.
Hardware Required:
- PIC18F46K22 microcontroller —-> datasheet
- SSD1306 OLED display
- 5V source
- Breadboard
- Jumper wires
Interfacing PIC18F46K22 with SSD1306 OLED display circuit:
The image below shows example circuit diagram.
(All grounded terminals are connected together)
The PIC18F46K22 microcontroller has 2 hardware I2C modules (MSSP1 and MSSP2 modules).
In this project I2C1 module is used with SDA1 on pin RC4 (#23) and SCL1 on pin RC3 (#18). The SDA1 pin of the MCU is connected to the SDA pin of the display and the SCL1 pin of the MCU is connected to the SCL pin of the display.
The reset pin of the display is connected to pin RD4 (#27) of the microcontroller.
The SSD1306 OLED display DC pin is connected to VDD which means I2C slave address of the display is 0x7A.
In this project the PIC18F46K22 microcontroller runs with its internal oscillator @ 16 MHz and MCLR pin is configured as an input pin.
Interfacing PIC18F46K22 with SSD1306 OLED display C code:
The following C code is for mikroC PRO for PIC compiler, it was tested with version 7.2.0.
To be able to compile the C code below with no error, a driver for the SSD1306 OLED display is required, it’s name is SSD1306OLED.C, for more information about this driver, visit the following post:
SSD1306 OLED display library for mikroC compiler | mikroC Projects
It also can be downloaded also from the link below:
SSD1306 OLED mikroC library
after the download, add the driver file to mikroC project folder.
The example code is a modified version of an Adafruit example for a 128×64 pixel display.
Full mikroC code:
Configuration words (for PIC18F46K22 MCU):
CONFIG1H = 0x0028
CONFIG2L = 0x0018
CONFIG2H = 0x003C
CONFIG3H = 0x0037
CONFIG4L = 0x0081
CONFIG5L = 0x000F
CONFIG5H = 0x00C0
CONFIG6L = 0x000F
CONFIG6H = 0x00E0
CONFIG7L = 0x000F
CONFIG7H = 0x0040
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 | /************************************************************************************** Interfacing PIC18F46K22 microcontroller with SSD1306 OLED (128x64 Pixel). C Code for mikroC PRO for PIC compiler. Internal oscillator used @ 16MHz Configuration words: CONFIG1H = 0x0028 CONFIG2L = 0x0018 CONFIG2H = 0x003C CONFIG3H = 0x0037 CONFIG4L = 0x0081 CONFIG5L = 0x000F CONFIG5H = 0x00C0 CONFIG6L = 0x000F CONFIG6H = 0x00E0 CONFIG7L = 0x000F CONFIG7H = 0x0040 This is a free software with NO WARRANTY. http://simple-circuit.com/ ***************************************************************************************/ // SSD1306 OLED reset pin definition (if available) #define SSD1306_RST RD4_bit #define SSD1306_RST_DIR TRISD4_bit #include <SSD1306OLED.c> // include SSD1306 OLED display driver source code char txt[11]; void testdrawcircle(void) { unsigned short i; for (i = 0; i < SSD1306_LCDHEIGHT; i += 2) { SSD1306_DrawCircle(SSD1306_LCDWIDTH/2, SSD1306_LCDHEIGHT/2, i); SSD1306_Display(); delay_ms(1); } } void testfillrect(void) { unsigned short i; SSD1306_Color = true; for (i = 0; i < SSD1306_LCDHEIGHT/2; i += 3) { // alternate SSD1306_Colors SSD1306_FillRect(i, i, SSD1306_LCDWIDTH - i*2, SSD1306_LCDHEIGHT - i*2); SSD1306_Display(); delay_ms(1); if(SSD1306_Color) SSD1306_Color = false; else SSD1306_Color = true; } SSD1306_Color = true; } void testdrawtriangle(void) { unsigned short i; for (i = 0; i < SSD1306_LCDHEIGHT/2; i += 5) { SSD1306_DrawTriangle(SSD1306_LCDWIDTH/2, SSD1306_LCDHEIGHT/2 - i, SSD1306_LCDWIDTH/2 - i, SSD1306_LCDHEIGHT/2 + i, SSD1306_LCDWIDTH/2 + i, SSD1306_LCDHEIGHT/2 + i); SSD1306_Display(); delay_ms(1); } } void testfilltriangle(void) { int i; SSD1306_Color = true; for (i = SSD1306_LCDHEIGHT/2; i > 0; i -= 5) { SSD1306_FillTriangle(SSD1306_LCDWIDTH/2, SSD1306_LCDHEIGHT/2 - i, SSD1306_LCDWIDTH/2 - i, SSD1306_LCDHEIGHT/2 + i, SSD1306_LCDWIDTH/2 + i, SSD1306_LCDHEIGHT/2 + i); if(SSD1306_Color) SSD1306_Color = false; else SSD1306_Color = true; SSD1306_Display(); delay_ms(1); } SSD1306_Color = true; } void testdrawroundrect(void) { unsigned short i; for (i = 0; i < SSD1306_LCDHEIGHT/2 - 2; i += 2) { SSD1306_DrawRoundRect(i, i, SSD1306_LCDWIDTH - 2*i, SSD1306_LCDHEIGHT - 2*i, SSD1306_LCDHEIGHT/4 - i/2); SSD1306_Display(); delay_ms(1); } } void testfillroundrect(void) { unsigned short i; SSD1306_Color = true; for (i = 0; i < SSD1306_LCDHEIGHT/2 - 2; i += 2) { SSD1306_FillRoundRect(i, i, SSD1306_LCDWIDTH - 2*i, SSD1306_LCDHEIGHT - 2*i, SSD1306_LCDHEIGHT/4 - i/2); if(SSD1306_Color) SSD1306_Color = false; else SSD1306_Color = true; SSD1306_Display(); delay_ms(1); } SSD1306_Color = true; } void testdrawrect(void) { unsigned short i; for (i = 0; i < SSD1306_LCDHEIGHT/2; i += 2) { SSD1306_DrawRect(i, i, SSD1306_LCDWIDTH - 2*i, SSD1306_LCDHEIGHT - 2*i); SSD1306_Display(); delay_ms(1); } } void testdrawline() { int i; for (i = 0; i < SSD1306_LCDWIDTH; i += 4) { SSD1306_DrawLine(0, 0, i, SSD1306_LCDHEIGHT - 1); SSD1306_Display(); delay_ms(1); } for (i = 0; i < SSD1306_LCDHEIGHT; i += 4) { SSD1306_DrawLine(0, 0, SSD1306_LCDWIDTH - 1, i); SSD1306_Display(); delay_ms(1); } delay_ms(250); SSD1306_ClearDisplay(); for (i = 0; i < SSD1306_LCDWIDTH; i += 4) { SSD1306_DrawLine(0, SSD1306_LCDHEIGHT - 1, i, 0); SSD1306_Display(); delay_ms(1); } for (i = SSD1306_LCDHEIGHT - 1; i >= 0; i -= 4) { SSD1306_DrawLine(0, SSD1306_LCDHEIGHT - 1, SSD1306_LCDWIDTH - 1, i); SSD1306_Display(); delay_ms(1); } delay_ms(250); SSD1306_ClearDisplay(); for (i = SSD1306_LCDWIDTH - 1; i >= 0; i -= 4) { SSD1306_DrawLine(SSD1306_LCDWIDTH - 1, SSD1306_LCDHEIGHT - 1, i, 0); SSD1306_Display(); delay_ms(1); } for (i = SSD1306_LCDHEIGHT - 1; i >= 0; i -= 4) { SSD1306_DrawLine(SSD1306_LCDWIDTH - 1, SSD1306_LCDHEIGHT - 1, 0, i); SSD1306_Display(); delay_ms(1); } delay_ms(250); SSD1306_ClearDisplay(); for (i = 0; i < SSD1306_LCDHEIGHT; i += 4) { SSD1306_DrawLine(SSD1306_LCDWIDTH - 1, 0, 0, i); SSD1306_Display(); delay_ms(1); } for (i = 0; i < SSD1306_LCDWIDTH; i += 4) { SSD1306_DrawLine(SSD1306_LCDWIDTH - 1, 0, i, SSD1306_LCDHEIGHT - 1); SSD1306_Display(); delay_ms(1); } delay_ms(250); } void testscrolltext(void) { SSD1306_ClearDisplay(); SSD1306_GotoXY(50, 8); SSD1306_TextSize(2); SSD1306_Print("scroll"); SSD1306_Display(); delay_ms(1); SSD1306_StartScrollRight(0x00, 0x0F); delay_ms(2000); SSD1306_StopScroll(); delay_ms(1000); SSD1306_StartScrollLeft(0x00, 0x0F); delay_ms(2000); SSD1306_StopScroll(); delay_ms(1000); SSD1306_StartScrollDiagRight(0x00, 0x07); delay_ms(2000); SSD1306_StartScrollDiagLeft(0x00, 0x07); delay_ms(2000); SSD1306_StopScroll(); } // main function void main() { OSCCON = 0x70; // set internal oscillator to 16MHz ANSELC = 0; // configure all PORTC pins as digital ANSELD = 0; // configure all PORTD pins as digital delay_ms(1000); // wait a second I2C1_Init(400000); // initialize I2C communication with clock frequency of 400kHz // initialize the SSD1306 OLED with an I2C addr = 0x7A (default address) SSD1306_Begin(SSD1306_SWITCHCAPVCC, SSD1306_I2C_ADDRESS); // print image buffer SSD1306_Display(); delay_ms(5000); SSD1306_ClearDisplay(); // clear the buffer SSD1306_Color = true; // set color to true (ON) // draw a single pixel SSD1306_DrawPixel(10, 10); // Show the display buffer on the hardware. // NOTE: You _must_ call SSD1306_Display() after making any drawing commands // to make them visible on the display hardware! SSD1306_Display(); delay_ms(2000); SSD1306_ClearDisplay(); // clear the buffer // draw many lines testdrawline(); SSD1306_Display(); delay_ms(2000); SSD1306_ClearDisplay(); // clear the buffer // draw rectangles testdrawrect(); SSD1306_Display(); delay_ms(2000); SSD1306_ClearDisplay(); // clear the buffer // draw multiple rectangles testfillrect(); SSD1306_Display(); delay_ms(2000); SSD1306_ClearDisplay(); // clear the buffer // draw mulitple circles testdrawcircle(); SSD1306_Display(); delay_ms(2000); SSD1306_ClearDisplay(); // clear the buffer // draw a white circle, 10 pixel radius SSD1306_FillCircle(SSD1306_LCDWIDTH/2, SSD1306_LCDHEIGHT/2, 10); SSD1306_Display(); delay_ms(2000); SSD1306_ClearDisplay(); // clear the buffer testdrawroundrect(); SSD1306_Display(); delay_ms(2000); SSD1306_ClearDisplay(); // clear the buffer testfillroundrect(); SSD1306_Display(); delay_ms(2000); SSD1306_ClearDisplay(); // clear the buffer testdrawtriangle(); SSD1306_Display(); delay_ms(2000); SSD1306_ClearDisplay(); // clear the buffer testfilltriangle(); SSD1306_Display(); delay_ms(2000); SSD1306_ClearDisplay(); // clear the buffer // draw scrolling text testscrolltext(); SSD1306_Display(); delay_ms(2000); SSD1306_ClearDisplay(); // clear the buffer // text display tests SSD1306_Color = 1; SSD1306_TextSize(1); SSD1306_GotoXY(2, 7); SSD1306_Print("hello, world!"); sprintf(txt, "%.6f", 3.141592); SSD1306_GotoXY(2, 16); SSD1306_Print(txt); SSD1306_TextSize(2); SSD1306_GotoXY(2, 26); sprintf(txt, "0x%LX", 0xDEADBEEF); SSD1306_Print(txt); SSD1306_Display(); delay_ms(2000); while(1) { // invert the display SSD1306_InvertDisplay(true); delay_ms(1000); SSD1306_InvertDisplay(false); delay_ms(1000); } } // end of code. |
The result of this project should be as shown in the following video (except the first image) where PIC18F4550 microcontroller is used:
Discover more from Simple Circuit
Subscribe to get the latest posts sent to your email.
Hi, thank you for sharing this program. I got the same PIC MCU and the oled display and have interconnected them according to the schematic given in this post. I have been trying to compile this program in Mikroc for sometime but I get stopped at one or another error. This time I got ‘undefined identifier I2C1_Start’. Can someone please hellp with error or kindly put the programs in a mikroc project so its easier to run it and learn from it? I would like to be able to compile and run it. Thank you.
Hi, I have PIC18f4520 and ssd1308. would this program work for me and what changes, hardware and software, I would need to make if you can plz elaborate? thx
Thanks for your help. The library works perfectly. Address was 0x78. Thank you and keep up the great work 😉
I’m interested in this project but i’m looking at the SSD1306 OLED display on ebay and some have 4 pins and others have 7 pins (like the one shown on this page). If i buy the 4 pin version, will this still work?
The 4-pin SSD1306 OLED module also should work.
If it didn’t work with the default I2C address which is 0x7A try with 0x78.
So the function:
SSD1306_Begin(SSD1306_SWITCHCAPVCC, SSD1306_I2C_ADDRESS);
becomes:
SSD1306_Begin(SSD1306_SWITCHCAPVCC, 0x78);