// UH 5COM2004
// Practical 2 - Serial Monitor
// 2.3 - Photocell
//
// Connect a photoresistor
// LDR - light dependent resistor
// https://learn.adafruit.com/photocells
// simulator LDR behaves in the opposite way
#include <LiquidCrystal_I2C.h>
#define LDR_PIN 4 // A0 on ESP32 DevKit
int ldrReading;
float voltage;
// LDR Characteristics
const float GAMMA = 0.7;
const float RL10 = 50;
LiquidCrystal_I2C lcd(0x27, 20, 4);
void setup() {
// put your setup code here, to run once:
// set the data rate for serial communication
Serial.begin(115200);
Serial.println("Hello, ESP32!");
// configure pinMode
pinMode(LDR_PIN, INPUT);
lcd.init();
lcd.backlight();
}
void loop() {
// put your main code here, to run repeatedly:
// read the voltage on the LDR pin
ldrReading = analogRead(LDR_PIN);
voltage = ldrReading * (3.3/4095.0); // 12 bit precision
// display the reading on the serial monitor
Serial.print("A0: ");
Serial.print(ldrReading);
Serial.print(" V: ");
Serial.println(voltage);
lcd.setCursor(2, 0);
lcd.print("A0: ");
lcd.println(ldrReading);
lcd.setCursor(2, 1);
lcd.print("V :");
lcd.print(voltage);
// wait 1 second
delay(1000);
}