• LDR (Light Dependent Resistor) Exercise | ACE-Lab

    LDR (Light Dependent Resistor)

    This exercise shows how to read an analogue light signal from an LDR, scale the 10-bit Arduino input range to an 8-bit PWM output range, and use the measured light level to control LED brightness. It then extends the model by adding a MATLAB Function block to create threshold-based logic.

    1. Learning Outcomes

    After completing this section, learners should be able to map an analogue input reading from an LDR to a suitable range for PWM-based LED brightness control, and use a MATLAB Function block to implement threshold-based on/off control logic from measured LDR values.

    Core Outcomes
    1
    Map analogue sensor data to PWM output

    Scale an LDR-derived ADC reading into the range required for PWM duty cycle control.

    2
    Use MATLAB Function logic in a Simulink model

    Implement a simple decision-making block that switches behaviour based on measured light level.

    2. Requirements

    The exercise has the following primary requirements:

    Specification
    1

    Implement a correct LDR voltage divider circuit and connect the analogue output to Arduino input A0.

    2

    Acquire the LDR voltage using the Arduino 10-bit ADC, producing values in the range 0–1023.

    3

    Scale the ADC output from 0–1023 to the 0–255 range required for PWM duty cycle control.

    4

    Generate a PWM signal on pin 9 to control LED brightness based on measured light intensity.

    3. Hardware Set-Up

    This exercise involves connecting an LED to a PWM Arduino output pin and controlling its brightness using the analogue reading from the LDR sensor. The LDR is used in a voltage divider arrangement so that changes in light level can be measured at Arduino input A0.

    Hardware

    Required hardware:

    • Arduino Uno board (supported by Simulink)
    • USB cable Type A to Type B
    • Breadboard
    • LED
    • 2 × 220 Ohm resistors
    • LDR sensor
    • 5 × male-to-male breadboard wires

    Assembly guidance

    One resistor is used for the LED output path and the second resistor is paired with the LDR to create a voltage divider. The junction between the LDR and resistor is connected to A0 so the Arduino can measure the light-dependent voltage.

    1

    Place the LED on the breadboard and add a 220 Ohm resistor to its anode.

    2

    Connect the LED cathode to GND and the free end of the resistor to pin 9.

    3

    Place the LDR on the breadboard and connect one of its legs to 5 V.

    4

    Add a 220 Ohm resistor from the second LDR leg to GND to form a voltage divider.

    5

    Connect the junction between the LDR and resistor to A0 on the Arduino.

    Fritzing circuit diagram showing Arduino Uno, LED on pin 9, and an LDR voltage divider connected to A0
    Hardware circuit diagram for the LDR exercise, showing the LED connected to PWM pin 9 and the LDR voltage-divider connection to analogue input A0.

    6. Additional Exercises

    After completing the main tasks, extend the exercise by improving the signal mapping and exploring the effect of threshold choice under different lighting conditions.

    Extension Work

    Exercise 1: Reverse the brightness behaviour

    Modify the Gain or MATLAB Function logic so that the LED becomes brighter as ambient light decreases.

    Exercise 2: Add a tunable threshold

    Replace the fixed midpoint, 255/2, with a tunable threshold parameter and evaluate its effect on system behaviour.

    Exercise 3: Record sensor readings

    Record ADC readings under different lighting conditions, such as room light, shaded conditions, and a phone torch, and comment on the sensitivity of the arrangement.

    7. Concluding Remarks

    Summary

    This exercise demonstrates analogue sensing using a voltage divider, signal scaling for PWM actuation, and basic decision-making using a MATLAB Function block. It reinforces the complete signal chain from sensor to scaling, logic, and actuator.

    You have explored both proportional control, where LED brightness changes continuously, and threshold-based control, where the output is switched according to a simple rule. These are two common embedded control strategies that build directly on the relationship between sensor data, model logic, and hardware behaviour.