Interfacing magnetic Sensor to control LED
For this week I will be interfacing a magnetic sensor with microcontroller
I planned to interface it on the door of the machine so machine can be turned off whenever its lead it open or user safety is at risk.
The sensor is normally open and whenever magnet is brought in its vicinity it switches to normally close.
Below is the image of magnetic sensor which I would be using.
/*
* Amol_Input_device
*
* Created: 27-April-17 8:28:27 AM
* Author : Amol Wadkar
*/
//#defines
#define F_CPU 1000000 //Define clock speed as 1Mhz
//Include Header
#include //Import header file required for AVR microcontroller
#include //Import header file required for delay function
int main(void)
{
DDRA = 0b10000000; //set PA7as output & all other pins as input
while (1) //Repeat the below actions continuously
{
if((PINA & (1<<3))==0b00000000) //When magnetic sensor is in NC
PORTA |= (1<<7);// Set PA7 high (Make LED ON)
else
PORTA &= ~(1<<7);// Set PA7 low (Make LED OFF)
}
}
Program the board using Arduino.
Working : Whenever magnet is in vicinity of magnetic sensor onboard LED glows and whenever it is moved away LED is turned off.