#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// Define the screen dimensions
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
// Define the I2C address for the display
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Define the analog input pin
const int analogPin = 34; // Change to the pin you are using
void setup() {
// Start the display
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Use 0x3D if 0x3C doesn't work
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.display();
delay(2000); // Pause for 2 seconds
// Clear the buffer
display.clearDisplay();
// Initialize serial communication
Serial.begin(115200);
}
void loop() {
// Read the analog input
int sensorValue = analogRead(analogPin);
// Convert the analog reading to voltage (assuming 3.3V reference)
float voltage = sensorValue * (3.3 / 4095.0);
// Convert voltage to millivolts
int millivolts = voltage * 1000;
// Display the voltage on the OLED
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.print("Eng. Odeh Alamrow Voltage (mV): ");
display.print(millivolts);
display.display();
// Print the voltage to the Serial Monitor
Serial.print("Voltage: ");
Serial.print(millivolts);
Serial.println(" mV");
// Wait for a bit before the next loop
delay(1000);
}