This circuit is designed to read the battery voltage connected to pin D34 of an ESP32 microcontroller and display the value on a 0.96" OLED display connected via I2C. The circuit also includes a battery indicator. The power is supplied by a 2000mAh battery, and the voltage is regulated using a TP4056 and MT3608 modules. A touch sensor is also included in the circuit.
Resistor
2000mAh Battery
TP4056
MT3608
Touch Sensor TTP233
ESP 32 DEVKIT V1 (30 pins)
0.96" OLED
/*
* This Arduino Sketch reads the battery voltage connected to pin D34 of the ESP32
* and displays the value on a 0.96" OLED display connected via I2C (SCK: D22, SDA: D21).
* It also includes a battery indicator.
*/
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup() {
// Initialize serial communication at 115200 baud rate
Serial.begin(115200);
// Initialize the OLED display
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.display();
delay(2000);
display.clearDisplay();
}
void loop() {
// Read the analog value from pin D34
int analogValue = analogRead(34);
// Convert the analog value to voltage (assuming 3.3V reference)
float voltage = analogValue * (3.3 / 4095.0);
// Print the voltage to the serial monitor
Serial.print("Battery Voltage: ");
Serial.print(voltage);
Serial.println(" V");
// Display the voltage on the OLED
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 10);
display.print("Battery Voltage: ");
display.print(voltage);
display.println(" V");
// Draw battery indicator
int batteryLevel = map(voltage, 3.0, 4.2, 0, 100);
display.drawRect(0, 30, 100, 10, SSD1306_WHITE);
display.fillRect(0, 30, batteryLevel, 10, SSD1306_WHITE);
display.display();
// Wait for a second before the next reading
delay(1000);
}
This code initializes the OLED display and reads the battery voltage from pin D34 of the ESP32. The voltage is then displayed on the OLED along with a battery level indicator. The voltage is also printed to the serial monitor for debugging purposes.