Skip to content

Instantly share code, notes, and snippets.

@leon-anavi
Last active November 4, 2023 03:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save leon-anavi/7243430ede1b038d51862e3612224414 to your computer and use it in GitHub Desktop.
Save leon-anavi/7243430ede1b038d51862e3612224414 to your computer and use it in GitHub Desktop.
Arduino sketch for I2C mini OLED display
#include <FS.h> //this needs to be first, or it all crashes and burns...
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
// For OLED display
#include <U8g2lib.h>
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
const int pinButton = 0;
void drawDisplay(const char *line1, const char *line2 = "", const char *line3 = "")
{
// Write on OLED display
// Clear the internal memory
u8g2.clearBuffer();
// Set appropriate font
u8g2.setFont(u8g2_font_ncenR14_tr);
u8g2.drawStr(0,14, line1);
u8g2.drawStr(0,39, line2);
u8g2.drawStr(0,64, line3);
// Transfer internal memory to the display
u8g2.sendBuffer();
}
void setup()
{
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println();
u8g2.begin();
delay(10);
//Button
pinMode(pinButton, INPUT);
Serial.println("Testing the OLED I2C display...");
drawDisplay("TEST", "Hello", "World");
}
void loop()
{
if (false == digitalRead(pinButton))
{
Serial.println("Rebooting...");
// Restart the board
ESP.restart();
}
}
#include <FS.h> //this needs to be first, or it all crashes and burns...
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
// For OLED display
#include <U8g2lib.h>
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
const int pinButton = 0;
void drawDisplay(const char *line1, const char *line2 = "", const char *line3 = "")
{
// Write on OLED display
// Clear the internal memory
u8g2.clearBuffer();
// Set appropriate font
u8g2.setFont(u8g2_font_fub20_tr);
u8g2.drawStr(0,20, line1);
u8g2.setFont(u8g2_font_fub11_tr);
u8g2.drawStr(0,42, line2);
u8g2.drawStr(0,64, line3);
// Transfer internal memory to the display
u8g2.sendBuffer();
}
void setup()
{
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println();
u8g2.begin();
delay(10);
//Button
pinMode(pinButton, INPUT);
Serial.println("Testing the OLED I2C display...");
drawDisplay("YouTube", "Do you like", "this video?");
}
void loop()
{
}
#include <FS.h> //this needs to be first, or it all crashes and burns...
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
// For OLED display
#include <U8g2lib.h>
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
const int pinButton = 0;
void drawDisplay(const char *line1, const char *line2 = "", const char *line3 = "")
{
// Write on OLED display
// Clear the internal memory
u8g2.clearBuffer();
// Set appropriate font
u8g2.setFont(u8g2_font_fub20_tr);
u8g2.drawStr(0,20, line1);
u8g2.setFont(u8g2_font_fub11_tr);
u8g2.drawStr(0,42, line2);
u8g2.drawStr(0,64, line3);
// Transfer internal memory to the display
u8g2.sendBuffer();
}
void setup()
{
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println();
u8g2.begin();
delay(10);
//Button
pinMode(pinButton, INPUT);
Serial.println("Testing the OLED I2C display...");
drawDisplay("YouTube", "Do you like", "this video?");
}
int counter = 0;
void loop()
{
delay(3000);
if (false == digitalRead(pinButton))
{
Serial.println("Resetting the counter...");
drawDisplay("BUTTON", "Resetting...", "Counter: 0");
counter = 0;
}
else if (10 == counter)
{
Serial.println("You have reached the limit.");
drawDisplay("LIMIT", "Resetting...", "Counter: 0");
counter = 0;
}
else
{
counter++;
String text = "Counter: " + String(counter);
drawDisplay("DEMO", text.c_str(), "");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment