thumbnail image

  • Home
  • About 
    • Mission
    • Global Community
    • Updates
  • ACE-Lab 
    • Exercises
    • Own ACE-Lab
  • ACE-CORE
  • …  
    • Home
    • About 
      • Mission
      • Global Community
      • Updates
    • ACE-Lab 
      • Exercises
      • Own ACE-Lab
    • ACE-CORE
Contact

  • Home
  • About 
    • Mission
    • Global Community
    • Updates
  • ACE-Lab 
    • Exercises
    • Own ACE-Lab
  • ACE-CORE
  • …  
    • Home
    • About 
      • Mission
      • Global Community
      • Updates
    • ACE-Lab 
      • Exercises
      • Own ACE-Lab
    • ACE-CORE
Contact
  • 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.

    Contents

    1. Learning Outcomes 2. Requirements 3. Hardware Set-Up 4. Simulink Set-Up and LDR Testing 5. MATLAB Function and LDR Testing 6. Additional Exercises 7. Concluding Remarks

    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.

    4. Simulink Set-Up and LDR Testing

    In this initial exercise, you will develop a Simulink model to control LED brightness using PWM based on the analogue reading from the LDR. As the measured LDR value increases, the brightness of the LED also increases.

    Model Build A

    Add an Analog Input block

    Set the pin number to A0 and the sample time to 0.01. This reads the sensor voltage and outputs a value between 0 and 1023.

    Insert a Gain block

    Set the gain to 255/1023. This scales the ADC reading into the 0–255 range needed for PWM duty cycle control.

    Connect to a PWM Output block

    Change the PWM pin to 9. The PWM block uses the scaled 0–255 value to generate a proportional PWM signal on the Arduino.

    Add two Display blocks

    Connect one display to the analogue output and the other after the gain block so you can view both the raw sensor reading and the scaled PWM value in real time.

    Deploy and test

    Deploy the model to the Arduino, then adjust the light level incident on the LDR and observe the PWM output and LED brightness change.

    Observe the mapping direction

    Be aware that the LDR-to-LED mapping may operate in the opposite direction to the intended behaviour. The next task addresses this using a MATLAB Function block.

    Simulink model for LDR testing
    Initial Simulink model for analogue LDR sensing and PWM LED brightness control.
    Download Simulink File

    5. MATLAB Function and LDR Testing

    The previous example is now updated to include a MATLAB Function block after the gain. Instead of sending the scaled value directly to the PWM output, the signal is passed through a small piece of code that decides whether the output should be on or off.

    Model Build B

    Add a MATLAB Function block

    Place the MATLAB Function block between the gain and PWM blocks so that simple decision-making can be applied before the actuator output.

    Replace the default function code

    Use the threshold-based logic shown below so that the output passes through only when the input is on one side of the midpoint.

    Deploy the updated model

    Run the model on the Arduino and observe how the PWM output switches according to the LDR-derived input level.

    MATLAB Function block code

    function y = fcn(u)
    
    if u <= cast(255/2,'like',u)
    
        y = u;
    
    else
    
        y = cast(0,'like',u);
    
    end

    Understand the MATLAB Function block

    • u is the input from the Gain block, scaled to 0–255.
    • 255/2 represents the midpoint of the PWM range, 127.5.
    • If the input is at or below the midpoint, the value is passed through.
    • If the input is above the midpoint, the output is forced to zero.
    Simulink model for MATLAB Function and LDR testing
    Updated Simulink model including a MATLAB Function block for threshold-based behaviour.
    Download Simulink File

    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.

Terms and Conditions Returns & Refunds Privacy Policy

Powered By
Cookie Use
We use cookies to ensure a smooth browsing experience. By continuing we assume you accept the use of cookies.
Learn More