Skip to content

Instantly share code, notes, and snippets.

@suadanwar suadanwar/OLEDdisplay.ino
Last active Apr 4, 2019

Embed
What would you like to do?
This is a sample code for "How to Display on I2C OLED Using Arduino" tutorial. The tutorial link : http://tutorial.cytron.io/2019/04/04/how-to-display-on-i2c-oled-with-arduino/
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define LM35 A0
float temperature = 0;
float value = 0;
float voltage = 0;
const unsigned char myBitmapMaker [] PROGMEM = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff,
0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xfe, 0x00, 0x10, 0x07, 0xc1, 0x83, 0xc0, 0x78, 0x00, 0xfc, 0x06, 0x03, 0x80, 0x7f, 0xff,
0xff, 0xfe, 0x00, 0x10, 0x01, 0xc0, 0x83, 0x80, 0x30, 0x00, 0x7c, 0x06, 0x01, 0x00, 0x3f, 0xff,
0xff, 0xfe, 0x00, 0x10, 0x00, 0xc0, 0x83, 0x00, 0x10, 0x00, 0x7c, 0x06, 0x01, 0x00, 0x3f, 0xff,
0xff, 0xfe, 0x00, 0x10, 0x00, 0xc0, 0x83, 0x00, 0x00, 0x00, 0x7c, 0x02, 0x01, 0x00, 0x3f, 0xff,
0xff, 0xff, 0x00, 0x10, 0x20, 0xc0, 0x83, 0x04, 0x08, 0x00, 0xfc, 0x02, 0x01, 0x00, 0x7f, 0xff,
0xff, 0xff, 0xc0, 0xf0, 0x20, 0xc0, 0x83, 0x04, 0x0f, 0x07, 0xfc, 0x02, 0x01, 0x03, 0xff, 0xff,
0xff, 0xff, 0xc0, 0xf0, 0x20, 0xc0, 0x83, 0x06, 0x1f, 0x07, 0xfc, 0x00, 0x01, 0x03, 0xff, 0xff,
0xff, 0xff, 0xc0, 0xf0, 0x20, 0xc0, 0x83, 0x01, 0xff, 0x07, 0xfc, 0x00, 0x01, 0x03, 0xff, 0xff,
0xff, 0xff, 0xc0, 0xf0, 0x00, 0xc0, 0x83, 0x00, 0xff, 0x07, 0xfc, 0x00, 0x01, 0x00, 0x7f, 0xff,
0xff, 0xff, 0xc0, 0xf0, 0x01, 0xc0, 0x83, 0x80, 0x3f, 0x07, 0xfc, 0x00, 0x01, 0x00, 0x7f, 0xff,
0xff, 0xff, 0xc0, 0xf0, 0x01, 0xc0, 0x83, 0xc0, 0x1f, 0x07, 0xfc, 0x00, 0x01, 0x00, 0x7f, 0xff,
0xff, 0xff, 0xc0, 0xf0, 0x00, 0xc0, 0x83, 0xe0, 0x1f, 0x07, 0xfc, 0x00, 0x01, 0x00, 0x7f, 0xff,
0xff, 0xff, 0xc0, 0xf0, 0x20, 0xc0, 0x83, 0xf8, 0x0f, 0x07, 0xfc, 0x00, 0x01, 0x03, 0xff, 0xff,
0xff, 0xff, 0xc0, 0xf0, 0x20, 0xc0, 0x83, 0x04, 0x0f, 0x07, 0xfc, 0x00, 0x01, 0x03, 0xff, 0xff,
0xff, 0xff, 0xc0, 0xf0, 0x20, 0xc0, 0x83, 0x06, 0x0f, 0x07, 0xfc, 0x10, 0x01, 0x03, 0xff, 0xff,
0xff, 0xff, 0xc0, 0xf0, 0x20, 0xc0, 0x83, 0x06, 0x0f, 0x07, 0xfc, 0x10, 0x01, 0x03, 0xff, 0xff,
0xff, 0xff, 0xc0, 0xf0, 0x20, 0xc0, 0x83, 0x04, 0x0f, 0x07, 0xfc, 0x10, 0x01, 0x00, 0x3f, 0xff,
0xff, 0xff, 0xc0, 0xf0, 0x20, 0xc0, 0x03, 0x00, 0x0f, 0x07, 0xfc, 0x10, 0x81, 0x00, 0x3f, 0xff,
0xff, 0xff, 0xc0, 0xf0, 0x20, 0xe0, 0x07, 0x00, 0x1f, 0x07, 0xfc, 0x10, 0x81, 0x00, 0x3f, 0xff,
0xff, 0xff, 0xc0, 0xf0, 0x20, 0xf0, 0x0f, 0x80, 0x3f, 0x07, 0xfc, 0x10, 0x81, 0x00, 0x3f, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x1f, 0xe0, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0x04, 0x41, 0x81, 0xfc, 0x0f, 0xe0, 0xc0, 0xe0, 0x70, 0x42, 0x01, 0x00, 0xff, 0xff,
0xff, 0xff, 0x04, 0x40, 0x81, 0xf8, 0x0f, 0xc0, 0x40, 0xc0, 0x70, 0x42, 0x01, 0x00, 0x7f, 0xff,
0xff, 0xff, 0x04, 0x40, 0x81, 0xf8, 0x0f, 0xc0, 0x40, 0xc0, 0x30, 0x42, 0x01, 0x00, 0x7f, 0xff,
0xff, 0xff, 0x04, 0x40, 0x81, 0xf8, 0x07, 0xc0, 0x40, 0xc0, 0x30, 0x02, 0x01, 0x00, 0x3f, 0xff,
0xff, 0xff, 0x04, 0xc0, 0x81, 0xf8, 0x07, 0xc0, 0x40, 0xc0, 0x30, 0x06, 0x0f, 0x00, 0x3f, 0xff,
0xff, 0xff, 0x05, 0xc0, 0x01, 0xf8, 0x07, 0xc0, 0x00, 0xc0, 0x30, 0x06, 0x0f, 0x00, 0x3f, 0xff,
0xff, 0xff, 0x07, 0xc0, 0x01, 0xf8, 0x07, 0xc0, 0x00, 0xc0, 0x30, 0x06, 0x03, 0x00, 0x3f, 0xff,
0xff, 0xff, 0x07, 0xc0, 0x01, 0xf0, 0x07, 0xc0, 0x00, 0xc0, 0x30, 0x0e, 0x01, 0x00, 0x7f, 0xff,
0xff, 0xff, 0x07, 0xc0, 0x01, 0xf0, 0x07, 0xc0, 0x00, 0xc0, 0x30, 0x0e, 0x01, 0x00, 0xff, 0xff,
0xff, 0xff, 0x07, 0xc0, 0x01, 0xf0, 0x87, 0xc0, 0x00, 0x80, 0x10, 0x06, 0x01, 0x00, 0x3f, 0xff,
0xff, 0xff, 0x07, 0xc0, 0x01, 0xf0, 0x83, 0xc0, 0x00, 0x80, 0x10, 0x06, 0x0f, 0x00, 0x3f, 0xff,
0xff, 0xff, 0x07, 0xc0, 0x01, 0xf0, 0x03, 0xc0, 0x00, 0x80, 0x10, 0x06, 0x0f, 0x00, 0x3f, 0xff,
0xff, 0xff, 0x07, 0xc0, 0x01, 0xf0, 0x03, 0xc0, 0x10, 0x80, 0x10, 0x02, 0x0f, 0x00, 0x3f, 0xff,
0xff, 0xff, 0x07, 0xc0, 0x21, 0xf0, 0x03, 0xc0, 0x10, 0x80, 0x10, 0x02, 0x01, 0x00, 0x3f, 0xff,
0xff, 0xff, 0x07, 0xc2, 0x21, 0xf0, 0xc3, 0xc0, 0x10, 0x82, 0x10, 0x42, 0x01, 0x00, 0x3f, 0xff,
0xff, 0xff, 0x07, 0xc2, 0x21, 0xe0, 0xc3, 0xc1, 0x10, 0x86, 0x00, 0x42, 0x01, 0x00, 0x3f, 0xff,
0xff, 0xff, 0x07, 0xc2, 0x21, 0xf0, 0xc3, 0xe3, 0x10, 0x86, 0x10, 0x40, 0x01, 0x0c, 0x3f, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff,
0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
};
void setup() {
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Don't proceed, loop forever
}
display.clearDisplay();
// Draw the bitmap:
// drawBitmap(x position, y position, bitmap data, bitmap width, bitmap height, color)
display.drawBitmap(0, 0, myBitmapMaker, 128, 64, WHITE);
display.display();
delay(2000); // Pause for 2 seconds
// Clear the buffer
display.clearDisplay();
pinMode(LM35, INPUT);
}
void loop() {
displayTemp();
}
void displayTemp() {
value = analogRead(A0); // sensor output to arduino analog A0 pin
voltage = value * 0.00488;
temperature = voltage * 100;
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(20, 10);
display.print("Room Temperature");
display.setCursor(15, 30);
display.setTextSize(3);
display.print(temperature);
display.print("C");
display.display();
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.