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:



Interface a LCD



Expt-1
1.      Title: Interface 16X2 LCD using 8 data pin
2.      Components required:
Sl. No.
Category
Item name/value
Quantity
1
IC
ATMEGA32
1
2

7805
1
3
Resistor
10K        
1
4

330R
1
5
Capacitor
0.1uf
2
6

22pf
2
7

10uf, 24Volt
1
8
Crystal Oscillator
16MHz
1
9
LCD
16X2
1
10
Battery
9Volt
1

3.      Procedure & code:

#ifndef F_CPU
#define F_CPU 16000000UL
#endif
#include <avr/io.h>
#include <stdlib.h>
#include <util/delay.h>
#define E 5 //giving name “enable” to 5 pin of PORTD, since it Is connected to LCD enable pin
#define RS 6 //giving name “registerselection” to 6 pin of PORTD, since is connected to LCD RS pin
void send_a_command(unsigned char command);
void send_a_character(unsigned char character);
void send_a_string(char *string_of_characters);

int main()
{
int keypressed=0;
char buffer[10];
DDRD |= 1<<RS;//set PD6 as output pin
DDRD |= 1<<E;// set PD5 as output pin
send_a_command(0x01); //Clear Screen 0x01 = 00000001
_delay_ms(50);
send_a_command(0x38);//telling lcd we are using 8bit command /data mode
_delay_ms(50);
send_a_command(0b00001111);//LCD SCREEN ON and courser blinking
send_a_string("Working for 16X2");//displaying a string
send_a_command(0x80 + 0x40 +0);// moving courser to second line of LCD
_delay_ms(50);
keypressed=1234;
itoa(keypressed,buffer,10);
send_a_string(buffer);
send_a_command(0x80 +0);//move courser to first line.
_delay_ms(50);
send_a_command(0x01); //Clear Screen 0x01 = 00000001
return(0);          
}      
void send_a_command(unsigned char command)
{
PORTB = command;
PORTD &= ~ (1<<RS); //putting 0 in RS to tell lcd we are sending command
PORTD |= 1<<E; //telling lcd to receive command /data at the port
_delay_ms(50);
PORTD &= ~1<<E;//telling lcd we completed sending data
PORTB= 0;
}
void send_a_character(unsigned char character)
{
PORTB= character;
PORTD |= 1<<RS;//telling LCD we are sending data not commands
PORTD |= 1<<E;//telling LCD to start receiving command/data
_delay_ms(50);
PORTD &= ~1<<E;//telling lcd we completed sending data/command
PORTB = 0;
}
void send_a_string(char *string_of_characters)
{
while(*string_of_characters > 0)
{
send_a_character(*string_of_characters++);
}
}

4.      Circuit:

5.      Simulation:





Expt-2
1.      Title: Interface 16X2 LCD using 4 data pin
2.      Components required:
Sl. No.
Category
Item name/value
Quantity
1
IC
ATMEGA32
1
2

7805
1
3
Resistor
10K        
1
4

330R
1
5
Capacitor
0.1uf
2
6

22pf
2
7

10uf, 24Volt
1
8
Crystal Oscillator
16MHz
1
9
LCD
16X2
1
10
Battery
9Volt
1

3.      Procedure & code:

#ifndef F_CPU
#define F_CPU 16000000UL
#endif
#include <avr/io.h>
#include <util/delay.h>
#include "lcd.h"//download lcd.h and lcd.c
int main()
{
char buffer[10];
uint8_t rpm=30,d=0,duty;
lcd_init(LCD_DISP_ON_CURSOR);// initialize LCD
do
{
       if(rpm>90)
       {
              rpm=30;
       }
duty=rpm;
lcd_clrscr();// Clear screen
lcd_gotoxy(0,0);// goto 1st line 1st column
_delay_ms(50);
lcd_puts("RPM=");
itoa(rpm,buffer,10); //buffer loaded with RPM variable value
lcd_puts(buffer); //display RPM value
_delay_ms(50);
lcd_clrscr();// Clear screen
lcd_gotoxy(0,1);// goto 2nd line 1st column
lcd_puts(" Duty=");
d=((duty*100)/255); //percentage of rpm calculated as duty cycle
itoa(d,buffer,10); //buffer is loaded with duty cycle value
lcd_puts(buffer); //display duty cycle valu
lcd_puts("%");
_delay_ms(50);
rpm+=10;
} while (1);
return(0);          
}


4.      Circuit:

5.      Simulation: