his program for interfacing Arduino with ultra sonic sensor with Arduino.
Technical Details
1.Working Voltage : 5V(DC)
2.Static current: Less than 2mA.
3.Output signal: Electric frequency signal, high level 5V, low level 0V.
4.Sensor angle: Not more than 15 degrees.
5.Detection distance: 2cm-450cm.
6.High precision: Up to 0.3cm
7.Input trigger signal: 10us TTL impulse
8.Echo signal : output TTL PWM signal
Mode of connection:
1.VCC
2.trig(T)
3.echo(R)
4.GND
Program
/*-------calculation-----------
speed of sound = 340.29 m/sec.
Here the total distance travelled by sound = sum of
distance from sensor to object and object to sensor.
But both are same . so we need only one distance.
then speed become 170.15 m/sec.
converts m/sec to cm/microseconds.
then (170*100)/1000000 = 0.01715cm/us.
from above equation time taken to
travel distance 1CM= 1/0.01715cm = 58.3us.
then distance = duration of received pulse / 58.3
HC-SR04 Ping distance pinouts to arduno:
VCC to arduino 5v
GND to arduino GND
Echo to Arduino pin 7
Trig to Arduino pin 8
result 0 indicates out of range condition
*/
char g[4];unsigned int a,duration;
#define echoPin PE_4 // Echo Pin
#define trigPin PD_2 // Trigger Pin
void setup()
{
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop()
{
digitalWrite(trigPin, LOW);
digitalWrite(trigPin, HIGH); //sends signal 10 us to excite ultra sonic sensor
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);//reads duartion pulse in Microseconds
a = duration/58;
inttoascii();
Serial.write('D'); //it indicate start of data.Not necessary
Serial.print(g); //distance in CM
Serial.write(0xd); //new line
delay(50); //Delay 50ms before next reading.
}
void inttoascii ()
{
g[3]='\0';
g[2]=(a%10)+0x30;
g[1]=a/10%10+0x30;
g[0]=a/100%10+0x30;//MSB section
}