Skip to content

9. Input devices

Group assignment:

  • Probe an input device(s)'s analog and digital signals

  • Document your work on the group work page and reflect on your individual page what you learned

To see our group assignment click here

Individual assignment:

  • add a sensor to a microcontroller board that you have designed and read it.

Threshold

A threshold value is defined in the code to distinguish between "light ON" and "light OFF" states.


My Custom PCB

The sensor is connected to the custom PCB I designed during Week 06 — Electronics Design, based on an ESP32-WROOM microcontroller.

PCB with LDR

Final Project Development: Smart Mode Management via LDR Sensor

This section details the development and implementation of the LDR (Light Dependent Resistor) sensor within the final project architecture.

System Dynamic Adaptation

The integration of this sensor enables the system to dynamically adapt its behavior based on ambient light level variations. By continuously monitoring the environment, the system automatically triggers seamless transitions between distinct operating modes.

Operational Modes

  • Trigger: Activated during optimal ambient light conditions.
  • Objective: Ensures full system responsiveness, maximum performance, and fluid user interaction.
  • Trigger: Activated automatically when darkness is detected.
  • Objective: Optimizes energy consumption by transitioning the system into a low-power state.

Wiring Diagram


Code

The following code reads the LDR analog value and switches between the two modes:

#define LDR_PIN 34        // Analog pin connected to LDR
#define THRESHOLD 2000    // Adjust based on your environment

void setup() {
  Serial.begin(115200);
  pinMode(LDR_PIN, INPUT);
}

void loop() {
  int ldrValue = analogRead(LDR_PIN);
  Serial.print("LDR Value: ");
  Serial.println(ldrValue);

  if (ldrValue < THRESHOLD) {
    // Room light is OFF → Night light mode
    Serial.println("Mode:  Night Light");
    // Add your night light logic here
  } else {
    // Room light is ON → Visitor mode
    Serial.println("Mode:  Visitor");
    // Add your visitor mode logic here
  }

  delay(500);
}

Calibration

Run the Serial Monitor first with the room light ON then OFF to read the actual LDR values and adjust THRESHOLD accordingly.


Serial Monitor — Demo

The video below shows the Serial Monitor output as the light condition changes in real time:


Wiring Diagram

LDR Pin ESP32 Pin
One leg 3.3V
Other leg GPIO 34 (analog) + 10kΩ to GND

Wiring Diagram

Wiring Diagram

Wiring Diagram


Results & Observations

  • The LDR successfully detects the light state of the room.
  • The ESP32 switches between Night Light and Visitor modes in real time.
  • The Serial Monitor confirms the readings and mode transitions.

Serial Monitor Screenshot


Files

File Description
LDR Code Arduino sketch for the LDR sensor