SP32-C3 Tiny OLED Tutorial: Pinout, Setup & First Programs
A tiny yet powerful ESP32-C3 board with a built-in OLED display.
Components and supplies
1
esp32-c3 with built in OLED
Apps and platforms
1
Arduino IDE
Project description
Code
Display EUR/USD currency exchange rates
cpp
JSON form api.frankfurter.app API
1#include <Arduino.h> 2#include <WiFi.h> 3#include <WiFiClientSecure.h> 4#include <U8g2lib.h> 5#include <Wire.h> 6#include <ArduinoJson.h> 7 8// ---- WiFi settings ---- 9const char* ssid = "Dziubym"; 10const char* password = "Mikigosia1"; 11 12// ---- OLED pins ---- 13#define SDA_PIN 5 14#define SCL_PIN 6 15U8G2_SSD1306_72X40_ER_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE); 16 17// ---- HTTPS client ---- 18WiFiClientSecure client; 19 20 21 22// ---- Display helper ---- 23void drawOLEDMessage(const char* msg) { 24 u8g2.clearBuffer(); 25 u8g2.setDrawColor(1); 26 u8g2.setFont(u8g2_font_5x8_tr); 27 u8g2.drawStr(0, 12, msg); 28 u8g2.sendBuffer(); 29} 30 31String getEURtoUSD() { 32 client.setInsecure(); 33 34 if (!client.connect("api.frankfurter.app", 443)) { 35 return "Connection failed"; 36 } 37 38 // Send HTTP GET request 39 client.print( 40 String("GET /latest?from=EUR&to=USD HTTP/1.1\r\n") + 41 "Host: api.frankfurter.app\r\n" + 42 "Connection: close\r\n\r\n" 43 ); 44 45 // Wait for response 46 unsigned long timeout = millis(); 47 while (!client.available()) { 48 if (millis() - timeout > 5000) { // 5s timeout 49 client.stop(); 50 return "Timeout"; 51 } 52 delay(10); 53 } 54 55 // Skip headers 56 while (client.available()) { 57 String line = client.readStringUntil('\n'); 58 if (line == "\r") break; // end of headers 59 } 60 61 // Read body 62 String payload = ""; 63 while (client.available()) { 64 payload += client.readString(); 65 } 66 67 client.stop(); 68 69 // Parse JSON 70 DynamicJsonDocument doc(512); 71 DeserializationError error = deserializeJson(doc, payload); 72 if (error) { 73 return "JSON parse error"; 74 } 75 76 float rate = doc["rates"]["USD"]; 77 return "1EUR=" + String(rate, 4) + "USD"; 78} 79 80 81void setup() { 82 Wire.begin(SDA_PIN, SCL_PIN); 83 u8g2.begin(); 84 85 drawOLEDMessage("Connecting WiFi..."); 86 WiFi.begin(ssid, password); 87 while (WiFi.status() != WL_CONNECTED) { 88 delay(300); 89 } 90} 91 92void loop() { 93 drawOLEDMessage("Fetching rate..."); 94 String rate = getEURtoUSD(); 95 96 // Display result 97 u8g2.clearBuffer(); 98 u8g2.setDrawColor(1); 99 u8g2.setFont(u8g2_font_5x8_tr); 100 u8g2.drawStr(0, 10, "EUR->USD Rate:"); 101 u8g2.drawStr(0, 30, rate.c_str()); 102 u8g2.sendBuffer(); 103 104 delay(3600000); // show result for 1h seconds before sleep 105}
Hello World sketch
cpp
Sketch using U8g2lib.h library
1#include <Arduino.h> 2#include <U8g2lib.h> 3 4 5#include <Wire.h> 6 7#define SDA_PIN 5 8#define SCL_PIN 6 9 10 11U8G2_SSD1306_72X40_ER_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE); // EastRising 0.42" OLED 12 13void setup(void) { 14 Wire.begin(SDA_PIN, SCL_PIN); 15 u8g2.begin(); 16} 17 18void loop(void) { 19 u8g2.clearBuffer(); // clear the internal memory 20 u8g2.setFont(u8g2_font_ncenB08_tr); // choose a suitable font 21 u8g2.drawStr(0,10,"Hello World!"); // write something to the internal memory 22 u8g2.sendBuffer(); // transfer internal memory to the display 23 delay(1000); 24}
Hello world sketch using wrong libraries
cpp
Sketch using Adafruit libraries
1#include <Arduino.h> 2#include <Wire.h> 3#include <Adafruit_GFX.h> 4#include <Adafruit_SSD1306.h> 5 6#define SCREEN_WIDTH 72 7#define SCREEN_HEIGHT 40 8 9#define SDA_PIN 5 10#define SCL_PIN 6 11 12// If your display has no reset pin, use -1 13Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); 14 15void setup() { 16 Wire.begin(SDA_PIN, SCL_PIN); 17 display.begin(SSD1306_SWITCHCAPVCC, 0x3C); 18} 19 20void loop() { 21 display.clearDisplay(); 22 display.setTextSize(1); 23 display.setTextColor(SSD1306_WHITE); 24 display.setCursor(0, 10); 25 display.println("Hello World!"); 26 display.display(); // Send buffer to display 27 28 delay(1000); 29}
Displaying Bitmap
cpp
Both correct band incorrect bit order
1#include <Arduino.h> 2#include <U8g2lib.h> 3#include <Wire.h> 4 5#define SDA_PIN 5 6#define SCL_PIN 6 7 8U8G2_SSD1306_72X40_ER_F_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE); 9 10extern const unsigned char epd_bitmap_esp32_c3_graffiti_70x30[]; 11// Incorrect bits order 12/*// 'esp32_c3_graffiti_70x30', 70x30px 13const unsigned char epd_bitmap_esp32_c3_graffiti_70x30 [] PROGMEM = { 14 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 15 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 16 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x18, 0x00, 0x00, 0x03, 0x00, 0x00, 0x07, 0xcf, 0xf7, 17 0x1f, 0xe0, 0x00, 0x1f, 0x00, 0x00, 0x1f, 0x9f, 0xff, 0x8f, 0xe7, 0xe0, 0x7f, 0x3f, 0xe0, 0x3f, 18 0x9f, 0xef, 0xe7, 0xf7, 0xf0, 0x7f, 0xbf, 0xe0, 0x3f, 0x3d, 0xef, 0xfa, 0x77, 0xf8, 0x77, 0xbf, 19 0xe0, 0x38, 0x39, 0xc7, 0xfa, 0x77, 0x78, 0xf7, 0xa3, 0x80, 0x38, 0x79, 0x8e, 0x78, 0xf6, 0x78, 20 0xe3, 0x87, 0x00, 0x7f, 0x7e, 0x0e, 0x71, 0xe0, 0x70, 0xe3, 0x8f, 0x00, 0x7f, 0x1f, 0x8f, 0xe3, 21 0xf0, 0xe6, 0xe0, 0x1f, 0xe0, 0x7e, 0x07, 0xef, 0xc3, 0xf9, 0xce, 0xe0, 0x03, 0xe0, 0x70, 0x01, 22 0xef, 0x88, 0x7b, 0xc1, 0xe1, 0x80, 0xf0, 0xf0, 0x30, 0xfe, 0x38, 0x77, 0x81, 0xe3, 0xb0, 0xf0, 23 0xf7, 0xfb, 0xee, 0x3d, 0xef, 0x39, 0xe7, 0x71, 0xe0, 0xff, 0x7f, 0xce, 0x1f, 0xcf, 0xf8, 0xff, 24 0x7f, 0xc0, 0xff, 0x7f, 0x8e, 0x1f, 0xdf, 0xf8, 0xfe, 0x7f, 0x80, 0xfc, 0x3e, 0x0e, 0x1f, 0x3f, 25 0xf8, 0xfc, 0x7f, 0x00, 0xe0, 0x1c, 0x08, 0x0c, 0x3e, 0x00, 0x78, 0x3c, 0x00, 0x00, 0x00, 0x00, 26 0x00, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 27 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 28 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 29 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 30 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 31}; 32*/ 33// Correct bits order 34const unsigned char epd_bitmap_esp32_c3_graffiti_70x30 [] PROGMEM = { 35 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 36 0x00, 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x1C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 48 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 49 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 50 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 51 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 52}; 53 54 55void setup() { 56 Wire.begin(SDA_PIN, SCL_PIN); 57 u8g2.begin(); 58} 59 60void loop() { 61 u8g2.clearBuffer(); 62 // Centered: (72 - 70) / 2 = 1 pixel offset 63 u8g2.drawXBM(1, 5, 70, 30, epd_bitmap_esp32_c3_graffiti_70x30); 64 u8g2.sendBuffer(); 65 delay(500); 66}
Comments
Only logged in users can leave comments