ESP32-Controlled OLED Display and Servo with DotStar LED Strip and Audio Output
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary of the Circuit
This circuit is designed to incorporate a variety of components including microcontrollers, power management modules, audio output devices, a display, and LED lighting. The primary controller is an ESP32 DEVKIT V1 microcontroller, which interfaces with an OLED display, a DotStar LED strip, a servo motor via a PCA9685 PWM Servo Breakout, and audio output through a PAM8403 amplifier module. The circuit is powered by a pair of 18650 Li-ion batteries, managed by a TP4056 charging module, and controlled by a rocker switch. The ESP32 also drives a piezo speaker directly for additional audio signaling.
Component List
Microcontrollers
- ESP 32 DEVKIT V1 (30 pins): A microcontroller with WiFi and Bluetooth capabilities, featuring a variety of digital and analog I/O pins.
Power Management
- 18650 Li-ion Battery x 2: Provides power to the circuit.
- TP4056: A charging module for the Li-ion batteries.
- Rocker Switch (SPST): Controls the power supply to the circuit.
Display
- 0.96" OLED: A small display for visual output.
LED Lighting
- Adafruit DotStar Strip 60M_B: An addressable RGB LED strip.
Motor Control
- Adafruit PCA9685 PWM Servo Breakout: Used to control servo motors via PWM signals.
Audio Output
- PAM8403: An audio amplifier module for driving speakers.
- Speaker: Outputs audio from the amplifier.
- Piezo Speaker: Provides simple audio alerts or signals.
Servo Motor
- Servo: A motor that can be positioned to specific angles using PWM signals.
Wiring Details
ESP 32 DEVKIT V1 (30 pins)
VINconnected to the power supply through a rocker switch.GNDconnected to the common ground.D15connected to the piezo speaker.D25andD26connected to the PAM8403 audio amplifier inputs.D4andD5connected to the DotStar LED strip control inputs.D18andD19connected to the OLED display for I2C communication.D21andD22connected to the PCA9685 PWM Servo Breakout for I2C communication.
Adafruit PCA9685 PWM Servo Breakout
VCCandPWRINconnected to the power supply.GNDconnected to the common ground.SDAandSCLconnected to the ESP32 for I2C communication.PWM0connected to the servo motor for PWM control.5.0Vconnected to the servo motor power supply.
Servo
PWMconnected to the PCA9685 PWM Servo Breakout.VCCandGNDconnected to the power supply and common ground, respectively.
0.96" OLED
VDDconnected to the power supply.GNDconnected to the common ground.SCKandSDAconnected to the ESP32 for I2C communication.
Adafruit DotStar Strip 60M_B
+5Vconnected to the power supply.GNDconnected to the common ground.DIandCIconnected to the ESP32 for data and clock signals.
18650 Li-ion Battery x 2
+and-connected to the TP4056 charging module.
TP4056
IN+andIN-connected to the Li-ion batteries.OUT+connected to the rocker switch for power distribution.
PAM8403
5vIN+andGND/IN-connected to the power supply and common ground, respectively.R-INPUTandL-INPUTconnected to the ESP32 for audio signals.R+andR-connected to the speaker for audio output.
Speaker
+and-connected to the PAM8403 audio amplifier.
Piezo Speaker
pin1andpin2connected to the ESP32 and common ground, respectively.
Rocker Switch (SPST)
1and2used to control the power supply from the TP4056 to the circuit.
Documented Code
ESP 32 DEVKIT V1 (30 pins) - sketch.ino
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// Define screen dimensions
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
// Define I2C pins for OLED display (adjust as needed)
#define OLED_SDA 19
#define OLED_SCL 18
// Create an instance of the display
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// Ball properties
int ball_x = SCREEN_WIDTH / 2;
int ball_y = SCREEN_HEIGHT / 2;
int ball_radius = 5;
int ball_x_speed = 2;
int ball_y_speed = 2;
void setup() {
// Start serial communication for debugging
Serial.begin(115200);
// Initialize I2C for the OLED display with custom SDA and SCL pins
Wire.begin(OLED_SDA, OLED_SCL);
// Initialize the display
if (!display.begin(SSD1306_I2C_ADDRESS, 0x3C)) { // 0x3C is the common I2C address
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
display.clearDisplay();
display.display();
}
void loop() {
// Clear the display
display.clearDisplay();
// Draw the ball
display.fillCircle(ball_x, ball_y, ball_radius, SSD1306_WHITE);
// Update the ball's position
ball_x += ball_x_speed;
ball_y += ball_y_speed;
// Check for collision with the screen edges (bounce effect)
if (ball_x - ball_radius <= 0 || ball_x + ball_radius >= SCREEN_WIDTH) {
ball_x_speed = -ball_x_speed; // Reverse direction on X axis
}
if (ball_y - ball_radius <= 0 || ball_y + ball_radius >= SCREEN_HEIGHT) {
ball_y_speed = -ball_y_speed; // Reverse direction on Y axis
}
// Update the display with the new frame
display.display();
// Delay to control the speed of animation
delay(30);
}
This code is designed to run on the ESP32 microcontroller and controls an OLED display to animate a bouncing ball. It initializes the display, sets up the ball's properties, and contains a loop that updates the ball's position, checks for collisions, and redraws the ball on the display. The I2C communication with the display is set up with custom SDA and SCL pins defined in the code.