// This code mostly generated from ChatGPT.
// "Build a Arduino Uno project that can read a AC and DC voltage
// and display it on a ssd1306 oled display module.
// Can you help me with arduino script and hardware connections."
//================================================================
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for SSD1306 display connected using software I2C (default case)
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Analog pins for voltage sensors
const int pinDCVoltage = A0; // DC voltage divider output connected here
const int pinACVoltage = A1; // ZMPT101B module output connected here
void setup() {
Serial.begin(115200);
// Initialize OLED display
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Check your display's I2C address
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.display();
delay(1000); // Pause
display.clearDisplay();
// Set the analog reference to 5V
analogReference(DEFAULT);
}
void loop() {
// Read DC Voltage
int sensorValueDC = analogRead(pinDCVoltage);
float voltageDC = sensorValueDC * (5.0 / 1023.0) * ((1e6 + 200e3) / 200e3); // Example for a 1MΩ:200kΩ divider
// Read AC Voltage (needs proper calibration and conversion)
int sensorValueAC = analogRead(pinACVoltage);
float voltageAC = sensorValueAC * (5.0 / 1023.0); // This will need to be scaled properly
// Display Voltages on OLED
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.print("DC Voltage: ");
display.print(voltageDC);
display.println(" vDC");
display.setCursor(0,10);
display.print("AC Voltage: ");
display.print(voltageAC);
display.println(" vAC");
display.display();
// also send to serial port
Serial.print("DC Voltage: ");
Serial.print(voltageDC);
Serial.println(" vDC");
Serial.print("AC Voltage: ");
Serial.print(voltageAC);
Serial.println(" vAC");
delay(500); // Update every half second
}