Skip to content

Interfacing LDR Sensor with ATmega2560 Development Board

Introduction

In this guide, we will learn how to interface an LDR (Light Dependent Resistor) sensor with the ATmega2560 development board. The LDR sensor is used to detect the intensity of light in its surroundings. By connecting an LDR sensor to the ATmega2560, we can measure the light level and use this information to control an LED. In this project, we will make the LED glow when no light is detected, and it will turn off when light is present.

Problem Statement

The objective of this project is to interface an LDR sensor with the ATmega2560 development board and control an LED based on the light intensity detected by the sensor. The LED should glow when no light is detected and turn off when light is present.

Requirements

To successfully complete this project, you will need the following components:

  • ATmega2560 development board.
  • LDR Sensor
  • Micro USB cable
  • Jumper wires

Circuit Diagram

  • Connect the LDR Sensor to the pin number 78 of ATmega2560 which is PORT A pin number 0.
  • We will use the internal LED for this project which is connected to PORT C pin number 0.

Code

  1. Open VS Code and paste the code into the file named lightsensor.c
  2. To compile and convert the source code into hex, run the following command in your terminal-
cmd
avr-gcc -Wall -g -Os -mmcu=atmega2560 -o lightsensor.hex lightsensor.c

You can download the code from here.

c
#ifndef F_CPU
#define F_CPU 16000000UL // set the CPU clock
#endif

#include <avr/io.h>     // Standard AVR IO Library
#include <util/delay.h> // Standard AVR Delay Library

int main(void)
{
    DDRC = DDRC | (1 << 0); // Makes PORTC0 as Output
    DDRA = DDRA & (~1 << 0); // Makes of PORTA0 as Input

    while (1) // infinite loop
    {
        if (bit_is_clear(PINA, 0)) // certain amount of light is detected
        {
            PORTC = PORTC | (1 << 0); // LOW ON PORTC0
        }
        else if (bit_is_set(PINA, 0)) // certain amount of light is not detected
        {
            PORTC = PORTC & (~1 << 0); // HIGH ON PORTC0
        }
    }
    return 0;
}
  1. Now open AVRDUDES and from the Programmer (-c) dropdown select Any usbasp clone with the correct VID/PID
  2. In the Flash section, click on three dots and navigate to the desired hex file, and select it. Finally, click on Program to burn the hex file in the microcontroller

Output

After uploading the code to the ATmega2560, the program will continuously read the output from the LDR sensor. If light is detected led will turn off and when there's light led will turn on. (You can change the sensitivity by rotating the knob on sensor)

Conclusion

By following this guide, you have successfully interfaced an LDR sensor with the ATmega2560 development board. You can now measure the light intensity using the LDR sensor and control an output based on the detected light level. This project can be extended further by incorporating additional features, such as adjusting the LED brightness based on the light intensity or integrating it into a larger automation system. Feel free to modify the code and customize it to suit your specific needs.