Sometimes we
have to interface a variety of sensors with microcontroller for monitoring
different environmental conditions such as temperature, humidity, light
intensity , motion, color etc. most of the sensors are making its output pin
HIGH or LOW when the desired value is met or detected. For example a motion
sensor makes its output pin HIGH when a motion is detected in its covering area
and otherwise its pin is pulled LOW. So
this kind of sensors is very easy to interface with microcontroller because
only we have to check the sensors output pin status. Following examples shows
how to interface different sensors with AVR microcontroller.
1)
Interfacing PIR sensor with AVR
A PIR sensor is used to detect the
motion of living things around its covering area. Normaly it has three pins namely Vcc, GND and
OUT. When the sensor detects a motion, it makes its OUT pin HIGH. So we can
simply check the OUT pin status to know whether the motion is detected or not.
The following points and program illustrates how to interface a PIR sensor with
AVR microcontroller
·
At first point we have to decide what we have to when a motion is detected by the
motion sensor
·
Here we are going to detect the motion sensor
output and when it detects we glow a LED
·
I am decided to interface the motion sensor OUT
pin with PA0 of the PORTA of AVR. So make PA0 as input pin. We use DDRA
register to set the direction of PA0 as input. For that any of the following
code can be used
DDRA=0x00; //makes all
the PORTA pins as input
DDRA=0xFE; //makes PA0
input and all other pins are output
DDRA |=(0<<PA0);
// this code helps us to make PA0 of PORTA as input without altering the
value of
other bits in DDRA register
·
Next we have to choose a pin in PORTB to connect
the LED which will glow when the senor makes a HIGH output at its OUT pin. I
decided PB0 of PORTB to connect with LED. So we have to make this pin as out
pin in PORTB. The following are methods to do so
DDRB=0xFF; //makes all
the PORTB pins as output
DDRB=0x01; //makes PB0
output and all other pins are input
DDRB |=(1<<PB0);
// this code helps us to make PB0 of PORTB as output without altering the
Values of
other bits in DDRB register
·
Next we have to monitor the PA0 pin of PORTA
whether it is set or not. For that we have to check the PINA register. For this
case we don’t want to check all value of PINA register because we need only PA0
of the PINA register is set on not. For doing that we can use the following
code.
If (PIN_is_set (PINA, PA0))
{
}
This
code checks the PA0 of the PINA register is set (HIGH) or not. When it is high
the code in the brackets will run. So we are using IF condition to monitor the
PA0 pin of PORTA.
·
Next step
we have to glow the LED when the PA0 detects a HIGH in its pin. So LED is
connected to the PB0 pin of the PORTB.
If (PIN_is_set (PINA, PA0))
{
PORTB=0x01; // makes
PB0 high which glows LED and all other pins LOW
Or
PORTB|=(PB0<<1); //
this makes PB0 high without effecting the other pins of PORTB
}
The complete code and diagram of
PIR sensor interfacing is as given below.
The complete Code
#include <avr/io.h>
int main(void)
{
DDRA|=(0<<PA0); //making PA0 as input pin
DDRB|=(1<<PB0); //making PB0 as output to connect LED
PORTB|=(0<<PB0); // LED is off by sending zero to PB0
while(1) //infinite loop
{
if(bit_is_set(PINA,PA0)) //checking whether PA0 is set
{
PORTB|=(1<<PB0); //switch ON LED
}
}
}