Detect the Dark/Light by Using the LDR sensor

  1. Introduction:

A Light Dependent Resistor (LDR), also known as a photoresistor, is a type of resistor whose resistance varies depending on the amount of light falling on its surface. It is one of the most basic and widely used sensors in electronics, particularly in light-sensitive circuits and applications.

The core principle of an LDR is based on photoconductivity, where the material of the resistor decreases its electrical resistance as it absorbs light. In bright light conditions, the LDR’s resistance is low, allowing more current to pass through. In contrast, in darkness or low light conditions, the resistance of the LDR increases significantly, reducing the current flow.

Key Characteristics of LDR:

  • Sensitivity to Light: The LDR is highly sensitive to light intensity, making it ideal for detecting changes in ambient light.

  • Non-linear Response: The resistance of an LDR does not change linearly with light intensity. This characteristic should be considered when designing circuits that require precise light measurements.

  • Simple Integration: The LDR can be easily integrated into circuits, often as part of a voltage divider, to convert changes in light into a measurable voltage.



  1. Scope:

The scope of this experiment includes:

  • Understanding the working principle of the LDR sensor in a voltage divider circuit.

  • Interfacing the LDR with the Raspberry Pi Pico's ADC pin.

  • Writing and executing MicroPython code to read the LDR values and control an LED.

  • Testing the system in varying light conditions to observe the behavior of the LED.

  • Applying the knowledge gained in this experiment to other light-sensitive applications.



  1. Prerequisite:

3.1. Hardware:

  • Pico microcontroller

  • LED

  • Resistor (220 KΩ)

  • Breadboard

  • LDR Sensor

  • Jumper wires

  • USB cable (to connect the computer)

3 .2. Software:

  • Thonny Software(For micro python)

  • MicroPython firmware installed on Raspberry Pi Pico

  • ubuntu Terminal(For embedded C)

  • CMake,Pico SDK,GNU Arm Embedded Toolchain,WSL.

  1. Connection Details:

LDR Connection:

  • Connect one terminal of the LDR to the 3.3V pin on the Pico.

  • Connect the other terminal of the LDR to an ADC pin (e.g., ADC0 which corresponds to GPIO 26).

  • Connect a resistor (e.g., 10kΩ) from the ADC pin to GND to form a voltage divider circuit.

LED Connection:

  • Connect the anode (long leg) of the LED to a GPIO pin (e.g., GPIO 15).

  • Connect the cathode (short leg) of the LED to GND through a current-limiting resistor (e.g., 330Ω).



  1. Working Principle:

The working principle of this experiment is based on the behavior of the LDR in a voltage divider circuit:

  • LDR Characteristics: The resistance of the LDR decreases with an increase in light intensity and increases with a decrease in light intensity.

  • Voltage Divider: The LDR and a fixed resistor form a voltage divider, which converts the varying resistance of the LDR into a varying voltage.

  • ADC Reading: The Raspberry Pi Pico reads this voltage through its ADC pin, converting the analog signal into a digital value.

  • Threshold Comparison: The microcontroller compares the ADC value against a predefined threshold. If the value is below the threshold (indicating darkness), the LED is turned ON.

LDR and Voltage Divider:

  • The LDR is part of a voltage divider circuit where the output voltage varies with the light intensity.

  • When light falls on the LDR, its resistance decreases, causing the output voltage at the ADC pin to increase.

  • Conversely, when it's dark, the LDR’s resistance increases, and the output voltage at the ADC pin decreases.

ADC Reading:

  • The Raspberry Pi Pico's ADC reads this voltage and converts it into a digital value.

  • In MicroPython, ldr.read_u16() provides a 16-bit value (0 to 65535) corresponding to the analog voltage. In C, adc_read() gives a 12-bit value (0 to 4095).

Threshold Value:

  • The threshold value is a predefined ADC reading that separates "dark" from "light."

  • If the ADC reading (ldr_value) is below the threshold, the environment is considered dark, and the LED is turned ON.

  • If the ADC reading is above the threshold, the environment is considered bright, and the LED is turned OFF.

  1. Application code:

  • MicroPython:

from machine import Pin, ADC

import time


# Define the LED and LDR pin

LED_PIN = 15

LDR_PIN = 26

THRESHOLD = 2000 # Adjust this value based on your environment


# Initialize the LED pin as output

led = Pin(LED_PIN, Pin.OUT)


# Initialize the LDR pin as an ADC input

ldr = ADC(Pin(LDR_PIN))


while True:

# Read the ADC value from the LDR

ldr_value = ldr.read_u16() # 16-bit value (0 to 65535)


# If the LDR value is below the threshold, it is dark, turn on the LED

if ldr_value < THRESHOLD:

led.value(1) # Turn ON the LED

else:

# If the LDR value is above the threshold, it is bright, turn off the LED

led.value(0) # Turn OFF the LED


# Small delay to avoid rapid toggling

time.sleep(0.1)


  • Embedded C:

#include "pico/stdlib.h"

#include "hardware/adc.h"


#define LED_PIN 15

#define LDR_ADC_PIN 26

#define THRESHOLD 2000 // Adjust this value based on your environment


int main() {

stdio_init_all();


// Initialize the LED pin as output

gpio_init(LED_PIN);

gpio_set_dir(LED_PIN, GPIO_OUT);


// Initialize the ADC

adc_init();

adc_gpio_init(LDR_ADC_PIN);

adc_select_input(0); // Select ADC0 for LDR input


while (true) {

// Read the ADC value

uint16_t ldr_value = adc_read();


// If the LDR value is below the threshold, it is dark, turn on the LED

if (ldr_value < THRESHOLD) {

gpio_put(LED_PIN, true);

} else {

// If the LDR value is above the threshold, it is bright, turn off the LED

gpio_put(LED_PIN, false);

}


// Small delay to avoid rapid toggling

sleep_ms(100);

}

return 0;

}






  1. Hardware Image:



  1. Output:

The LED will automatically turn ON in the dark and turn OFF in the light based on the intensity detected by the LDR.

  1. Video Demonstration:





  1. Appendix:

  • Common CMakeLists:

cmake_minimum_required(VERSION 3.12)


# Pull in SDK (must be before project)

include(pico_sdk_import.cmake)


project(pico_experiments C CXX ASM)


set(CMAKE_C_STANDARD 11)

set(CMAKE_CXX_STANDARD 17)


if (PICO_SDK_VERSION_STRING VERSION_LESS "1.3.0")

    message(FATAL_ERROR "Raspberry Pi Pico SDK version 1.3.0 (or later) required. Your version is ${PICO_SDK_VERSION_STRING}")

endif()


# Initialize the SDK

pico_sdk_init()


add_compile_options(-Wall

        -Wno-format          # int != int32_t as far as the compiler is concerned because gcc has int32_t as long int

        -Wno-unused-function # we have some for the docs that arent called

        )

if (CMAKE_C_COMPILER_ID STREQUAL "GNU")

    add_compile_options(-Wno-maybe-uninitialized)

endif()


# Hardware-specific examples in subdirectories:

add_subdirectory(expt_10_LCD)

  • CMakeLists:

# Define the minimum CMake version required

cmake_minimum_required(VERSION 3.12)


# Set the project name and C standard

project(LDR_LED_Control C CXX ASM)

set(CMAKE_C_STANDARD 11)

set(CMAKE_CXX_STANDARD 17)


# Specify the path to the Pico SDK

pico_sdk_init()


# Add your source file to the project

add_executable(LDR_LED_Control main.c)


# Link the Pico SDK to your project

target_link_libraries(LDR_LED_Control pico_stdlib hardware_adc)


# Enable USB serial output if needed (optional)

pico_enable_stdio_usb(LDR_LED_Control 1)


# Enable UART serial output if needed (optional)

pico_enable_stdio_uart(LDR_LED_Control 0)


# Create the output .uf2 file

pico_add_extra_outputs(LDR_LED_Control)

  1. References:



These references provide comprehensive info

Class Schedules:
# Topic Date & time Action

Curriculum

0% Completed (0/1)