#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// OLED display TWI/I2C address (usually 0x3C or 0x3D)
#define OLED_ADDR 0x3C
// OLED display width and height, in pixels
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
// LED OUTPUT SHIFT REGISTER SETUP
// Define the connections to the shift register
const int dataPin = 23; // SER (Serial Input)
const int latchPin = 19; // RCLK (Register Clock)
const int clockPin = 18; // SRCLK (Shift Register Clock)
// Declaration for an SSD1306 display connected to I2C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire);
void setup() {
// initialize with the I2C addr 0x3C (for the 128x64)
if (!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Don't proceed, loop forever
}
// Clear the buffer
display.clearDisplay();
// Set text size, color, and position
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
// Display static text
display.println(F("Hello, ESP32!"));
display.println(F("SSD1306 OLED Test"));
// Update the display with all of the above graphics
display.display();
// LED SHIFT REGISTER
// Set pins to output
pinMode(dataPin, OUTPUT);
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
}
void updateShiftRegister(byte data) {
// Take the latchPin low so the LEDs don't change while sending in bits
digitalWrite(latchPin, LOW);
// Shift out the bits
shiftOut(dataPin, clockPin, MSBFIRST, data);
// Take the latchPin high so the LEDs will light up
digitalWrite(latchPin, HIGH);
}
void loop() {
// LED SHIFT REGISTER
// Turn all LEDs on
updateShiftRegister(B11111111);
delay(1000); // Wait for 1 second
// Turn all LEDs off
updateShiftRegister(B00000000);
delay(1000); // Wait for 1 second
}