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.
VIN
connected to the power supply through a rocker switch.GND
connected to the common ground.D15
connected to the piezo speaker.D25
and D26
connected to the PAM8403 audio amplifier inputs.D4
and D5
connected to the DotStar LED strip control inputs.D18
and D19
connected to the OLED display for I2C communication.D21
and D22
connected to the PCA9685 PWM Servo Breakout for I2C communication.VCC
and PWRIN
connected to the power supply.GND
connected to the common ground.SDA
and SCL
connected to the ESP32 for I2C communication.PWM0
connected to the servo motor for PWM control.5.0V
connected to the servo motor power supply.PWM
connected to the PCA9685 PWM Servo Breakout.VCC
and GND
connected to the power supply and common ground, respectively.VDD
connected to the power supply.GND
connected to the common ground.SCK
and SDA
connected to the ESP32 for I2C communication.+5V
connected to the power supply.GND
connected to the common ground.DI
and CI
connected to the ESP32 for data and clock signals.+
and -
connected to the TP4056 charging module.IN+
and IN-
connected to the Li-ion batteries.OUT+
connected to the rocker switch for power distribution.5vIN+
and GND/IN-
connected to the power supply and common ground, respectively.R-INPUT
and L-INPUT
connected to the ESP32 for audio signals.R+
and R-
connected to the speaker for audio output.+
and -
connected to the PAM8403 audio amplifier.pin1
and pin2
connected to the ESP32 and common ground, respectively.1
and 2
used to control the power supply from the TP4056 to the circuit.#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.