Ultrasonic Sensor allow you measure the distance to an object by using sound waves. It measures distance by sending out (transmitter) a sound wave at a specific frequency and listening (receiver) for that sound wave to bounce back.
Reading PIR Sensors
The HC-SR04 Ultrasonic Module has 4 pins, Ground, VCC, Trig and Echo. The Ground and the VCC pins of the module needs to be connected to the Ground and the 5 volts pins on the Arduino Board respectively and the trig and echo pins to any Digital I/O pin on the Arduino Board.In principle, the operation is as follows:
- sends a ping sound Trigger pin
- If an obstacle is trapped the wave is reflected back.
- the Echo pin receive the signal returned
The time taken allows us to determine the distance between the sensor and the barrier.
connection with Arduino:
| Arduino Pin | Sensor Pin |
| POWER 5V | VCC |
| Digital 7 | Trig |
| Digital 8 | Echo |
| GND | GND |
/* * Ultrasonic Sensor HC-SR04 and Arduino * * */ // defines pins numbers const int trigPin = 7; const int echoPin = 8; // defines variables long duration; int distance; void setup() { pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output pinMode(echoPin, INPUT); // Sets the echoPin as an Input Serial.begin(9600); // Starts the serial communication } void loop() { // Clears the trigPin digitalWrite(trigPin, LOW); delayMicroseconds(2); // Sets the trigPin on HIGH state for 10 micro seconds digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Reads the echoPin, returns the sound wave travel time in microseconds duration = pulseIn(echoPin, HIGH); // Calculating the distance distance= duration*0.034/2; // Prints the distance on the Serial Monitor Serial.print("Distance: "); Serial.println(distance); }



No comments:
Post a Comment