#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
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
char temp[50];
void setup() {
Serial.begin(115200);
Serial.println("");
Serial.println("Hello, XIAO ESP32-C6!");
Serial.println("Welcome to Wokwi :-)");
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
// Show initial display buffer contents on the screen --
// the library initializes this with an Adafruit splash screen.
display.display();
delay(2000); // Pause for 2 seconds
// Clear the buffer
display.clearDisplay();
// Draw a single pixel in white
display.drawPixel(10, 10, SSD1306_WHITE);
// Show the display buffer on the screen. You MUST call display() after
// drawing commands to make them visible on screen!
display.display();
}
void loop() {
int moisture, lux;
moisture = analogRead((0));
lux = analogRead(2);
Serial.print("Soil Moisture reading: ");
Serial.println(moisture);
Serial.print("Lux reading: ");
Serial.println(lux);
sprintf(temp, "Soil Moisture: %d\nLux: %d", moisture, lux);
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.clearDisplay();
display.println(temp);
display.display();
delay(1000);
}
Loading
xiao-esp32-s3
xiao-esp32-s3
Loading
ssd1306
ssd1306