Use OLED display + Arduino with examples
Hello everybody, in this tutorial I’m using 128*32 i2c OLED display that I bought for few bucks, it’s very tiny you can use it in some small DIY projects also it gives you lot of possibilities to do (Different font and sizes of texts, different shapes, scrolling, bmp pictures…) and in this tutorial I just went through some simple functions to make it clear and simple for beginners like me… Please note that this kind of display isn’t a color display, mine just print in blue (Left), some are in white, some can have yellow + one of the previous colors (Right).
While making this little tutorial I went to these links they’re helpful, if you want to check them:
https://learn.adafruit.com/adafruit-gfx-graphics-library/graphics-primitives
Wiring:
This is my wiring, it depends on your version, the one I’m using has 4 pins and I used them in the schematic, you may add RST pin…

Libraries:
Donwload Adafruit SSD1306 library
Or you can visit the link above to download them directly from Adafruit
Codes:
Download the code I used .ino or check below, I added some comments to make it easy to understand.
Code 1:
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 |
//This code is to use with 128*32 OLED i2c display, it's modified and adapted from the Adafruit example //It explains and shows some basic functions of the library //Refer to surtrtech.com for more information #include <SPI.h> //i2c and the display libraries #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 32 // OLED display height, in pixels #define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin) Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); //Declaring the display name (display) //The function below clears the whole screen and then it print one pixel in a specified position void draw_pixel(){ display.clearDisplay(); //Clear the display from previous things display.drawPixel(64, 15, WHITE); //Draw one pixel (x position, y position, color) display.display(); //Function to apply effects otherwise nothing will show in the screen } //The function below clears the whole screen and draw one line void draw_line(){ display.clearDisplay(); display.drawLine(0, 0, 50, 10, WHITE); //(Start x position, Start y position, Finish x position, Finish y position, color) display.display(); } //The function below clears the whole screen and draw a circle void draw_circle(){ display.clearDisplay(); display.drawCircle(display.width()/2, display.height()/2, 8, WHITE); //(Center x position, Center y position, radius, color) display.display(); } //The function below clears the whole screen and draw a rectangle void draw_rectangle(){ display.clearDisplay(); display.drawRect(0, 0, display.width()/2, display.height()/2, WHITE); //(Rectangle top left point x position, y position, Rectangle bottom right x position, y position, color) display.display(); } //The function below clears the whole screen and draw a triangle void draw_triangle(){ display.clearDisplay(); display.drawTriangle(10, 28, 64, 5, 110, 28, WHITE); //(x1,y1,x2,y2,x3,y3, color) three points of the triangle display.display(); } //The function below clears the whole screen and shows a text void print_text(){ display.clearDisplay(); display.setTextSize(2); //size of the text that will follow display.setTextColor(WHITE); //its color display.setCursor(17,8); //position from where you want to start writing display.println("SurtrTech"); //text todisplay display.display(); } //The function below clears the whole screen and shows 2 texts with different parameters void print_text_2(){ display.clearDisplay(); display.setTextSize(2); //size of the text that will follow display.setTextColor(WHITE); //its color display.setCursor(17,5); //position from where you want to start writing display.println("SurtrTech"); //text todisplay display.setTextSize(1); //size of the text that will follow, it's separate from the previous one display.setCursor(17,20); //its own position display.println("Youtube"); //the text it self will show with the new parameteres we changed display.display(); } //The function below clears the whole screen and shows a text then start scrolling on the left void scroll_1(){ display.clearDisplay(); display.setTextSize(2); display.setTextColor(WHITE); display.setCursor(17,5); display.println("Subscribe"); display.setTextSize(1); display.setCursor(17,20); display.println("to SurtrTech"); display.display(); display.startscrollleft(0x00, 0x0F); //you can use the different scrolling functions delay(4200); display.stopscroll();//stop the scrolling after a delay delay(2000); } void setup() { display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //Start the OLED display display.display(); delay(3000); display.clearDisplay(); display.display(); } void loop() { // draw_pixel(); //uncomment a function to test it // draw_line(); // draw_circle(); // draw_rectangle(); // draw_triangle(); // print_text(); // print_text_2(); // scroll_1(); } |
Other codes:
The other codes shown are examples from the library.
If you have any question, suggestion or problem… don’t hesitate.
Categories
Yassine View All
Automation and Electrical Engineer, Electronics amateur trying to share my little projects.
9 thoughts on “Use OLED display + Arduino with examples” Leave a comment ›