In this guide, we will learn how to interface an IR (Infrared) sensor with the ATmega2560 development board. An IR sensor is commonly used to detect infrared radiation emitted by objects. By connecting an IR sensor to the ATmega2560, we can detect the presence or absence of objects in front of the sensor and use this information for various applications, such as proximity sensing, obstacle detection, and automation.
The objective of this project is to interface an IR sensor with the ATmega2560 development board and utilize the sensor's output for object detection or proximity sensing.
To successfully complete this project, you will need the following components:
ATmgea2560 development board.
IR Sensor
Micro USB cable
Jumper wires
Connect the IR Sensor to the Pin 78 of ATmega2560 which is PORT A pin number 0 (PA0).
We will use the internal buzzer in this case which is connected to PORT H pin number 2. (Make sure jumper on J29 is connected and buzzer is active low)
Open VS Code and paste the code into the file named ir_proximity.c
To compile and convert the source code into hex, run the following command in your terminal-
avr-gcc -Wall -g -Os -mmcu=atmega2560 -o ir_proximity.hex ir_proximity.c
You can download the code from here.
#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)
{
DDRH = DDRH | (1 << 2); // Makes pin 2 of PORTH as Output
DDRA = DDRA & (~1 << 0); // Makes of pin 0 of PORTA as Input
PORTA = PORTA | (1 << 0); // Makes pin 0 of PORTA as Pullup
while (1) // infinite loop
{
if (bit_is_clear(PINA, 0)) // if obstacle is detected is pressed
{
PORTH = PORTH | (1 << 2); // HIGH ON PORTH PIN 2
}
else if (bit_is_set(PINA, 0)) // if obstacle is not detected
{
PORTH = PORTH & (~1 << 2); // LOW ON PORTH PIN 2
}
}
return 0;
}
Now open AVRDUDES and from the Programmer (-c) dropdown select Any usbasp clone with the correct VID/PID
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
After uploading the code to the ATmega2560, the program will continuously read the output from the IR sensor. If any object is in the proximity of the sensor, buzzer will beep. (You can change the range by rotating the knob on sensor)
By following this guide, you have successfully interfaced an IR sensor with the ATmega2560 development board. You can now utilize the sensor's output to detect objects or proximity and integrate it into your projects or applications. This opens up possibilities for various applications, such as obstacle avoidance robots, automated systems, or interactive projects.