// Wokwi Simulation - ESP32 with LDR and Exponential Smoothing
// α = 0.2 for noise reduction
const int ldrPin = 34; // LDR connected to GPIO34 (Analog Input)
const float alpha = 0.2; // Smoothing factor (0.0 to 1.0)
float smoothedValue = 0; // Stores the smoothed value
unsigned long lastRead = 0; // For timing
const int readInterval = 50; // Read every 50ms
void setup() {
Serial.begin(115200);
pinMode(ldrPin, INPUT);
// Initial reading to start the smoothing
int rawValue = analogRead(ldrPin);
smoothedValue = rawValue;
Serial.println("ESP32 LDR with Exponential Smoothing (α=0.2)");
Serial.println("Time(ms)\tRaw\tSmoothed");
}
void loop() {
unsigned long currentTime = millis();
if (currentTime - lastRead >= readInterval) {
lastRead = currentTime;
// Read raw value from LDR
int rawValue = analogRead(ldrPin);
// Apply exponential smoothing
smoothedValue = alpha * rawValue + (1 - alpha) * smoothedValue;
// Print results
Serial.print(currentTime);
Serial.print("\t");
Serial.print(rawValue);
Serial.print("\t");
Serial.println(smoothedValue);
}
// Small delay to prevent busy-waiting
delay(1);
}