Cirkit Designer Logo
Cirkit Designer
Your all-in-one circuit design IDE
Home / 
Project Documentation

ESP32-Based Ambient Light Monitoring System with I2C LCD Display and Keypad Interface

Image of ESP32-Based Ambient Light Monitoring System with I2C LCD Display and Keypad Interface

Circuit Documentation

Summary

This circuit is designed to interface an ESP32 microcontroller with several peripherals including an ambient light sensor, a 4x4 membrane matrix keypad, an I2C LCD 16x2 screen, an I2C module, and a KY-008 laser emitter. The ESP32 reads ambient light intensity and displays the value on the LCD screen. The keypad allows user interaction to retrieve stored light intensity values from memory. The circuit is powered through the ESP32's Vin pin, and common ground connections are established across the components.

Component List

ESP32 (30 pin)

  • Description: A microcontroller with WiFi and Bluetooth capabilities, featuring a wide range of GPIO pins.
  • Pins: EN, VP, VN, D34, D35, D32, D33, D25, D26, D27, D14, D12, D13, GND, Vin, D23, D22, TX0, RX0, D21, D19, D18, D5, TX2, RX2, D4, D2, D15, 3V3

4X4 Membrane Matrix Keypad

  • Description: A simple interface for user input, featuring 16 buttons arranged in a 4x4 grid.
  • Pins: R1, R2, R3, R4, C1, C2, C3, C4

I2C LCD 16x2 Screen

  • Description: A liquid crystal display capable of showing 2 lines of 16 characters each, with an I2C interface for communication.
  • Pins: SCL, SDA, VCC (5V), GND, VDD, VO, RS, RW, E, D0, D1, D2, D3, D4, D5, D6, D7, BLA, BLK

I2C Module

  • Description: An interface module that allows the LCD screen to communicate over the I2C bus.
  • Pins: VSS, VO, RS, RW, E, DB0, DB1, DB2, DB3, DB4, DB5, DB6, DB7, LEDA, LEDK, GND, VCC, SDA, SCL, VDD

KY-008 Laser Emitter

  • Description: A module that emits a laser beam when powered.
  • Pins: SIG, 5v, GND

Ambient Light Sensor

  • Description: A sensor that detects ambient light intensity.
  • Pins: GND, OUT, VCC

Wiring Details

ESP32 (30 pin)

  • VP connected to Ambient Light Sensor OUT
  • D32 connected to 4X4 Keypad C4
  • D33 connected to 4X4 Keypad C3
  • D25 connected to 4X4 Keypad C2
  • D26 connected to 4X4 Keypad C1
  • D27 connected to 4X4 Keypad R4
  • D14 connected to 4X4 Keypad R3
  • D12 connected to 4X4 Keypad R2
  • D13 connected to 4X4 Keypad R1
  • GND connected to I2C Module GND, KY-008 Laser Emitter GND, Ambient Light Sensor GND
  • Vin connected to KY-008 Laser Emitter 5v, I2C Module VCC, Ambient Light Sensor VCC
  • D22 connected to I2C Module SCL
  • D21 connected to I2C Module SDA

4X4 Membrane Matrix Keypad

  • R1 connected to ESP32 D13
  • R2 connected to ESP32 D12
  • R3 connected to ESP32 D14
  • R4 connected to ESP32 D27
  • C1 connected to ESP32 D26
  • C2 connected to ESP32 D25
  • C3 connected to ESP32 D33
  • C4 connected to ESP32 D32

I2C LCD 16x2 Screen

  • GND connected to I2C Module VSS
  • VDD connected to I2C Module VDD
  • VO connected to I2C Module VO
  • RS connected to I2C Module RS
  • RW connected to I2C Module RW
  • E connected to I2C Module E
  • D0 connected to I2C Module DB0
  • D1 connected to I2C Module DB1
  • D2 connected to I2C Module DB2
  • D3 connected to I2C Module DB3
  • D4 connected to I2C Module DB4
  • D5 connected to I2C Module DB5
  • D6 connected to I2C Module DB6
  • D7 connected to I2C Module DB7
  • BLA connected to I2C Module LEDA
  • BLK connected to I2C Module LEDK

I2C Module

  • GND connected to ESP32 GND
  • VCC connected to ESP32 Vin
  • SDA connected to ESP32 D21
  • SCL connected to ESP32 D22
  • VSS connected to I2C LCD 16x2 Screen GND
  • VDD connected to I2C LCD 16x2 Screen VDD
  • VO connected to I2C LCD 16x2 Screen VO
  • RS connected to I2C LCD 16x2 Screen RS
  • RW connected to I2C LCD 16x2 Screen RW
  • E connected to I2C LCD 16x2 Screen E
  • DB0 connected to I2C LCD 16x2 Screen D0
  • DB1 connected to I2C LCD 16x2 Screen D1
  • DB2 connected to I2C LCD 16x2 Screen D2
  • DB3 connected to I2C LCD 16x2 Screen D3
  • DB4 connected to I2C LCD 16x2 Screen D4
  • DB5 connected to I2C LCD 16x2 Screen D5
  • DB6 connected to I2C LCD 16x2 Screen D6
  • DB7 connected to I2C LCD 16x2 Screen D7
  • LEDA connected to I2C LCD 16x2 Screen BLA
  • LEDK connected to I2C LCD 16x2 Screen BLK

KY-008 Laser Emitter

  • 5v connected to ESP32 Vin
  • GND connected to ESP32 GND

Ambient Light Sensor

  • OUT connected to ESP32 VP
  • VCC connected to ESP32 Vin
  • GND connected to ESP32 GND

Documented Code

/*
 * This Arduino Sketch is designed for an ESP32 microcontroller. It reads the 
 * ambient light intensity using an ambient light sensor and displays the 
 * value in lux on an I2C LCD screen. The values are also saved in memory. 
 * A 4x4 membrane keypad is used to read the saved values from memory.
 */

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <EEPROM.h>

// Pin definitions
#define LIGHT_SENSOR_PIN 36 // VP pin
#define KEYPAD_ROWS 4
#define KEYPAD_COLS 4

// I2C LCD setup
LiquidCrystal_I2C lcd(0x27, 16, 2);

// Keypad setup
char keys[KEYPAD_ROWS][KEYPAD_COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPins[KEYPAD_ROWS] = {13, 12, 14, 27}; // R1, R2, R3, R4
byte colPins[KEYPAD_COLS] = {26, 25, 33, 32}; // C1, C2, C3, C4
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, KEYPAD_ROWS, KEYPAD_COLS);

// Variables
int addr = 0;

void setup() {
  Serial.begin(115200);
  lcd.init();
  lcd.backlight();
  pinMode(LIGHT_SENSOR_PIN, INPUT);
}

void loop() {
  // Read light intensity
  int sensorValue = analogRead(LIGHT_SENSOR_PIN);
  float voltage = sensorValue * (3.3 / 4095.0);
  float lux = voltage * 1000; // Assuming a linear relationship for simplicity

  // Display on LCD
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Light Intensity:");
  lcd.setCursor(0, 1);
  lcd.print(lux);
  lcd.print(" lux");

  // Save to EEPROM
  EEPROM.put(addr, lux);
  addr += sizeof(float);
  if (addr >= EEPROM.length()) addr = 0;

  // Check keypad input
  char key = keypad.getKey();
  if (key) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Key Pressed:");
    lcd.setCursor(0, 1);
    lcd.print(key);
    delay(1000);
    if (key == 'A') {
      // Read from EEPROM
      float storedLux;
      EEPROM.get(addr, storedLux);
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Stored