#include <math.h>
// Define Pins
const int potPin = 34; // Potentiometer
const int ldrPin = 35; // LDR Sensor
const int ntcPin = 32; // NTC Thermistor
const int ledPin = 5; // LED PWM output
// Constants for ADC
const int adcMax = 4095; // ESP32 ADC 12-bit max value
const float vRef = 3.3; // Reference voltage
// Constants for NTC Thermistor
const float BETA = 3950; // Beta coefficient of the thermistor
const float R0 = 10000; // 10kΩ resistor at 25°C
const float T_zero = 298.15; // Reference temperature in Kelvin (25°C)
// Constants for LDR
const float ldrResistor = 10000; // 10kΩ pull-down resistor
void setup() {
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
}
void loop() {
// Read ADC values
int potValue = analogRead(potPin);
int ldrValue = analogRead(ldrPin);
int ntcValue = analogRead(ntcPin);
// Convert Potentiometer ADC value to PWM (0-255)
int potBrightness = map(potValue, 0, adcMax, 0, 255);
analogWrite(ledPin, potBrightness);
// Convert LDR ADC to Resistance
float vLDR = (ldrValue / (float)adcMax) * vRef;
float rLDR = (vRef * ldrResistor / vLDR) - ldrResistor;
// Convert LDR Resistance to Lux (approximation formula)
float lux = pow(10, (log10(rLDR) - 1.5)); // Approximate Lux conversion
// Adjust LED brightness using LDR (higher light = dimmer LED)
int ldrBrightness = map(lux, 0, 1000, 255, 0); // 0 lux = max brightness
analogWrite(ledPin, ldrBrightness);
// Convert ADC value to Voltage for NTC
float vNTC = (ntcValue / (float)adcMax) * vRef;
// Convert Voltage to Resistance for NTC
float rNTC = (vRef * R0 / vNTC) - R0;
// Calculate Temperature using the Steinhart-Hart Equation
float temperature = 1 / ((log(rNTC / R0) / BETA) + (1 / T_zero)) - 273.15;
// Print values to Serial Monitor
Serial.print("Potentiometer: "); Serial.print(potValue);
Serial.print(" | LDR: "); Serial.print(ldrValue);
Serial.print(" | LDR Lux: "); Serial.print(lux);
Serial.print(" | NTC Temp: "); Serial.print(temperature); Serial.println(" °C");
delay(500);
}