For this assignment I worked with an OLED Module 12864 (128x64) display with I2C interface for my final project. I originally wanted to use a larger OLED screen but after accidentally dropping it (oops!), I switched to this smaller version. The goal was to display the temperature readings from my DS18B20 sensor. Pictures in final project of the whole setup.
Connecting the OLED to my PCB was trickier than expected. The main confusion came from matching the correct pins:
I kept mixing up which pins on the XIAO corresponded to which connections on the OLED, but eventually got it working!
Here's the code I used to display temperature readings:
#include
#include
#include
#include
#include
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define SCREEN_ADDRESS 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
OneWire oneWire(2);
DallasTemperature sensors(&oneWire);
void setup() {
Serial.begin(9600);
sensors.begin();
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
}
void loop() {
sensors.requestTemperatures();
float tempC = sensors.getTempCByIndex(0);
display.clearDisplay();
display.setCursor(0,0);
display.print("Temperature:");
display.setCursor(0,20);
display.print(tempC);
display.print(" C");
display.display();
delay(1000);
}
Despite the initial setbacks (RIP larger OLED), this turned out to be a successful implementation. Key learnings: