//Including the necessary libraries
// Include the Wire library for I2C communication which include functions like
//Wire.setClock(clockFrequency): Sets the I2C clock frequency.
//Wire.begin();Initializes the I2C communication ,etc.
#include <Wire.h>
// Include the Adafruit GFX library for graphics functions like
//display.setTextSize(size) , display.setTextColor(color) , display.print(text) , etc.
#include <Adafruit_GFX.h>
// Include the Adafruit SSD1306 library to control the OLED display. It include functions like:
//display.display(); Sends the content of the display buffer to the screen, making the changes visible.
//display.clearDisplay(): Clears the display buffer, etc.
#include <Adafruit_SSD1306.h>
// Define the width of the OLED display in pixels
#define SCREEN_WIDTH 128
// Define the height of the OLED display in pixels
#define SCREEN_HEIGHT 64
// Define the reset pin for the OLED (not used in this case, so set to -1)
//The Adafruit_SSD1306 library expects you to specify a pin for the reset function when you initialize the display object that's why we consider it even if we don't want to use it.
//The reset pin is used to ensure that the display starts up correctly, clears any errors, and provides a consistent initialization.
#define OLED_RESET -1
// Create an object 'display' to interface with the OLED
//Even though SCREEN_WIDTH, SCREEN_HEIGHT, and OLED_RESET are not used directly in the rest of your code, they are essential for the correct initialization and operation of the OLED display
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Define the GPIO pin (34) where the potentiometer is connected for analog input
#define POTENTIOMETER_PIN 34
void setup() {
// Initialize serial communication at a baud rate of 115200
Serial.begin(4800); //The baud rate is the speed at which data is transmitted over a serial communication channel.
// Initialize the OLED display
//This parameter tells the library how the display is powered. In this case, it's indicating that the display is powered by the on-board capacitor via the voltage regulator
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Start the OLED with a VCC switch capacitor and I2C address 0x3C ( 0x3c is the default attribute in this OLED display.click on the question mark to know more)
//The F() macro is used to store the string in flash memory instead of SRAM, saving RAM on the microcontroller.
Serial.println(F("SSD1306 allocation failed")); // If initialization fails, print an error message
//This is an infinite loop. If the initialization fails, the code enters this loop and effectively halts the program, preventing further execution.
for(;;);
}
display.clearDisplay(); // Clear the OLED display buffer, readying it for new content
}
void loop() {
int rawValue = analogRead(POTENTIOMETER_PIN); // Read the analog value from the potentiometer
float voltage = rawValue / 4095.0 * 3.3; // Convert the analog value (0-4095) to a voltage (assuming 3.3V reference)
// Print the measured voltage to the serial monitor
Serial.print("Voltage: ");
Serial.println(voltage); // print on next line
// Display the measured voltage on the OLED
display.clearDisplay(); // Clear the OLED display buffer
display.setTextSize(2); // Set the text size to 2
display.setTextColor(SSD1306_WHITE); // Set the text color to white (for the OLED)
//0: The x-coordinate (horizontal position) where the text will start. 0 means the beginning of the screen horizontally; 0: The y-coordinate (vertical position) where the text will start. 0 means the top of the screen vertically.
display.setCursor(0, 0); // Set the cursor to the top-left corner of the screen
display.print("Voltage:"); // Print the label "Voltage:" on the OLED
display.setCursor(0, 25); // Move the cursor down slightly for the voltage value
display.print(voltage, 2); // Print the voltage value with 2 decimal places
display.print(" V"); // Print the unit "V" (Volts) after the voltage value
display.display(); // Display the updated content on the OLED screen
delay(1000); // Delay for 1000 milliseconds (1sec) before the next loop iteration (stabilizes the display update rate)
}
//map(sensorValue, 0, 1023, 0, 255): This scales the sensor's reading from the range of 0-1023 down to 0-255.
//analogWrite(ledPin, outputValue); // Output this value to an LED
//pinMode(13, OUTPUT); // Set GPIO 13 as an output
//digitalWrite(13, HIGH); // Set GPIO 13 to HIGH (3.3V)
//ledcSetup(0, 5000, 8); // Setup PWM on channel 0, 5 kHz frequency, 8-bit resolution
//ledcAttachPin(13, 0); // Attach GPIO 13 to channel 0
//ledcWrite(0, 128); // Output a 50% duty cycle PWM on GPIO 13
Loading
ssd1306
ssd1306