Featured post

Objectives and content

Thursday 7 July 2016

USD


Ultrasonic sensor for measuring distance: This circuit is developed by interfacing ultrasonic sensor“HC-SR04” with AVR microcontroller. This sensor uses a technique called “ECHO” which is something you get when sound reflects back after striking with a surface. We know that sound vibrations cannot penetrate through solids. So what happens is, when a source of sound generates vibrations they travel through air at a speed of 220 meters per second. These vibrations when they meet our ear we describe them as sound. As said earlier these vibrations cannot go through solid, so when they strike with a surface like wall, they are reflected back at the same speed to the source, which is called echo.
Ultrasonic sensor “HC-SR04” provides an output signal proportional to distance based on the echo. The sensor here generates a sound vibration in ultrasonic range upon giving a trigger, after that it waits for the sound vibration to return. Now based on the parameters, sound speed (220m/s) and time taken for the echo to reach the source, it provides output pulse proportional to distance. As shown in figure, at first we need to initiate the sensor for measuring distance, that is a HIGH logic signal at trigger pin of sensor for more than 10mS, after that a sound vibration is sent by sensor, after a echo, the sensor provides a signal at the output pin whose width is proportional to distance between source and obstacle.
This distance is calculate as, distance (in cm) = width of pulse output (in mS) / 58. Here the width of the signal must be taken in multiple of mS  (micro second or 10^-6).

Expt-1
1.      Title: Distance Measurement using Ultrasonic sensor HC-SR04.
2.      Components required:
Sl. No.
Category
Item name/value
Quantity
Remarks
1
IC
ATMEGA32
1

Power supply
 5v

3
Resistor
10K        
2
4
Display
JHD_162ALCD (16x2LCD)
1
5
Capacitor
0.1uf
2
6

22pf
2
7

1000uf, 24Volt
1
8
Crystal Oscillator
16MHz
1
9
USD sensor
HC-SR04
1
10
PROGRAMMER
AVR-ISP
1

3.      Procedure to code:
a.       write header to enable data flow control over pins
b.       assign controller crystal frequency attached
c.        include header to enable delay function in program
d.       define “enable” to 5 pin of PORTD, since it Is connected to LCD enable pin
e.        define “registerselection” to 6 pin of PORTD, since is connected to LCD RS pin
f.        declare interger 'pulse' to access all though the program
g.        declare interger 'i' to access all though the program
h.       set portB as output pins
i.         giving delay of 50ms
j.         Taking portD as output except D2
k.       enable interrupt0
l.         set interrupt triggering logic change
m.     store digital output
n.       displaying digital output as temperature in 16*2 lcd
o.       Clear Screen \
p.       tell lcd we are using 8bit command /data mode
q.       LCD SCREEN ON and courser blinking
r.         enable global interrupts
s.        Under infinite loop
                                                                           i.      trigger the sensor for 15usec
                                                                          ii.      get the distance based on formula on introduction
                                                                        iii.      display name
                                                                        iv.      shift cursor to 1 shell of second line
                                                                         v.      display name
                                                                        vi.      send command for putting variable number in LCD(variable number, in which character to send_a_string (SHOWA);
                                                                      vii.      tell the display to show character(replaced by variable number) after positioning send_a_string ("cm ")
                                                                     viii.      retun to first line first shell
t.         interrupt service routine when there is a change in logic level at INT0
u.       when logic from HIGH to LOW
v.       disabling counter
w.      count memory is updated to integer
x.       resetting the counter memory
y.       when logic change from LOW to HIGH
z.        enable counter


4.      Code:

/*
C Program for Distance Measurement using Ultrasonic Sensor and AVR Microocntroller
*/
#ifndef F_CPU
#define F_CPU 16000000UL
#endif
#include <avr/io.h>
#include <avr/interrupt.h>
//#define F_CPU 1000000
#include <util/delay.h>
#include <stdlib.h>
#define enable 5
#define registerselection 6
void send_a_command(unsigned char command);
void send_a_character(unsigned char character);
void send_a_string(char *string_of_characters);
static volatile int pulse = 0;
static volatile int i = 0;
int main(void)
{
//DDRA = 0xFF;
DDRB = 0xFF;
DDRD = 0b11111011;
_delay_ms(50);
MCUCR|=(1<<ISC00);
GICR|=(1<<INT0);
TCCR1A = 0;
uint16_t COUNTA = 0;
char SHOWA [16];
send_a_command(0x01); //Clear Screen 0x01 = 00000001
_delay_ms(50);
send_a_command(0x38);
_delay_ms(50);
send_a_command(0b00001111);
_delay_ms(50);
sei();
while(1)
{
_delay_ms(500);
PORTD|=(1<<PIND0);
_delay_us(15);
PORTD &=~(1<<PIND0);
COUNTA = pulse/(58*16);
send_a_string ("  USDM  ");
send_a_command(0x80 + 0x40 + 0);
send_a_string ("DISTANCE=");
if (COUNTA<30)
{
itoa(COUNTA,SHOWA,10);
send_a_string(SHOWA);
send_a_string ("cm   ");
_delay_ms(100);
}
else {send_a_string ("Undef");
_delay_ms(100);}
send_a_command(0x80 + 0);
}
}
ISR(INT0_vect)
{
if (i==1)
{
TCCR1B=0;
pulse=TCNT1;
TCNT1=0;
i=0;
}
if (i==0)
{
TCCR1B|=(1<<CS10);
i=1;
}
}
void send_a_command(unsigned char command)
{
PORTB = command;
PORTD &= ~ (1<<registerselection);
PORTD |= 1<<enable;
_delay_ms(8);
PORTD &= ~1<<enable;
PORTB = 0;
}
void send_a_character(unsigned char character)
{
PORTB = character;
PORTD |= 1<<registerselection;
PORTD |= 1<<enable;
_delay_ms(8);
PORTD &= ~1<<enable;
PORTB = 0;
}
void send_a_string(char *string_of_characters)
{
while(*string_of_characters > 0)
{
send_a_character(*string_of_characters++);
}
}


5.      Simulation:



No comments:

Post a Comment