Above: Pressure-sensitive and flex resistors
Add I2C OLED Display:
Prompt for Code (Google Gemini):
Write Arduino code for a system using an Arduino Nano, which measures and displays light levels. It uses a resistive photocell with a voltage divider, with the input signal connected to pin A0. The voltage level is displayed both on the serial monitor and on a standard 0.96" SSD1306 I2C OLED Display.
I2C (Inter-Integrated Circuit) is a short-distance, two-wire serial communication protocol used to connect microcontrollers to low-speed peripheral devices like sensors and displays. [1, 2, 3]
How I2C Works
I2C uses just two shared wires for communication between a controller (master) and target devices (slaves): [1, 2]
SDA (Serial Data Line): The line used to send and receive actual data.
SCL (Serial Clock Line): The line that carries the clock signal to keep data transfer in sync. [1, 2]
Every device on the I2C bus has a unique numerical address. This allows a controller to talk to one specific chip while ignoring the others on the same two wires. [1, 2, 3]
Key Features
Two-Wire Design: Saves physical space and wiring complexity compared to other methods.
Multi-Master and Multi-Slave: Supports multiple controllers and targets on the same bus.
Synchronous: Data is tightly paced by the clock signal.
Built-in Acknowledgement: Each transferred byte is confirmed with an ACK bit to verify successful delivery. [1, 2, 3, 4, 5]
Common Uses
Temperature and humidity sensors
OLED and LCD screens
Real-time clocks
Here is the complete Arduino code for your light measurement system.
To make this work, you will need to install the Adafruit GFX and Adafruit SSD1306 libraries, as well as the Wire library(for I2C) via the Arduino IDE Library Manager.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// OLED display dimensions
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
// I2C address for the 0.96" OLED (usually 0x3C, rarely 0x3D)
#define SCREEN_ADDRESS 0x3C
#define OLED_RESET -1 // Reset pin not used on most I2C modules
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Photocell (LDR) connected to analog pin A0
const int ldrPin = A0;
const float referenceVoltage = 5.0; // Assuming Nano is running at 5V logic
void setup() {
// Initialize Serial Monitor
Serial.begin(9600);
// Initialize the OLED display
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed. Check wiring!"));
for(;;); // Halt execution if display fails
}
// Clear the buffer
display.clearDisplay();
display.setTextColor(SSD1306_WHITE); // Draw white text
display.display();
delay(1000);
}
void loop() {
// Read the raw analog value (0-1023)
int rawValue = analogRead(ldrPin);
// Convert raw reading to voltage
float voltage = rawValue * (referenceVoltage / 1023.0);
// Output to Serial Monitor
Serial.print("Raw Sensor Value: ");
Serial.print(rawValue);
Serial.print(" | Voltage: ");
Serial.print(voltage);
Serial.println(" V");
// Output to OLED Display
display.clearDisplay();
// Draw Title
display.setTextSize(1); // Normal 1:1 pixel scale
display.setCursor(0, 0); // Start at top-left corner
display.print("Light Level Monitor");
// Draw Voltage (Large text)
display.setTextSize(2); // 2:2 pixel scale
display.setCursor(0, 25);
display.print(voltage);
display.print(" V");
// Draw Raw Value (Small text)
display.setTextSize(1);
display.setCursor(0, 50);
display.print("Raw analog: ");
display.print(rawValue);
// Push buffer to the screen
display.display();
// Wait 500ms before next reading
delay(500);
}
For the code to work correctly, double-check your hardware connections:
1. The 0.96" SSD1306 OLED (I2C):
VCC: to Nano 5V (or 3.3V, depending on your specific OLED module's rating)
GND: to Nano GND
SCL (Clock): to Nano A5
SDA (Data): to Nano A4
2. The Photocell (LDR) Voltage Divider:
Connect one leg of the LDR to 5V.
Connect the other leg of the LDR to both Pin A0 and one leg of a 10kΩ resistor.
Connect the other leg of the 10kΩ resistor to GND.
(Note: This configuration means voltage goes UP when light goes UP. If you swap the resistor and LDR, the voltage will go down as light goes up).