In this experiment we are going to examine a ping sensor. If you look closely at the picture below you will notice a T on the lower left of the board and an R on the lower right. Those are labels on the silver cylinders.
The T stands for ‘transmit’ and the R stands for – maybe you already guessed – ‘receive’.
Maybe you heard that there is a way of calculating how far a lightning storm is from you based on the time between seeing a lightning bolt and hearing it. The number of seconds between seeing and hearing it divided by 5 is the number of miles away it is. If we count 1-mississippi, 2-mississippi, up to 10 - we know the storm is 2 miles away. We can do this because we know sound travels at 340 meters per second. You may have noticed that if you are high up in the stands of an arena, our vision of the drummer hitting the drums doesn’t jive with what we hear. Anyway ….
So this sensor sends out a ultrasonic (sound) ping on the transmit then counts how long it takes that sound to be heard on the ‘receive’ end. So it emits a ping. That sound bounces off a wall, and gets ‘heard’ on the receive side. Then, with our board we can determine how far away that wall is.
There are many uses of ping sensors, particularly in automobiles, including:
The particular ping sensor we are using is the extremely common HC-SR04. The HC-SR04 needs 5 volt input. Recall that our esp8266 board uses 3 volt logic. Fortunately, there is one pin (labeled USB) that provides 5 volt output.
Because of the limits of the circuit drawing program I use, I show the wire connections in front of the sensor. It is better to place these behind the sensor so the wires don’t interfere with the clean line-of-sight of the sensor. Be sure that the TRIG sensor pin is connected to pin 2 and the ECHO pin to pin 16.
int TRIGGER = 2;
int ECHO = 16;
void setup() {
Serial.begin (115200);
pinMode(TRIGGER, OUTPUT);
pinMode(ECHO, INPUT); pinMode(BUILTIN_LED, OUTPUT);
}
void loop() {
long duration, distance;
digitalWrite(TRIGGER, LOW);
delayMicroseconds(2);
digitalWrite(TRIGGER, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGGER, LOW);
duration = pulseIn(ECHO, HIGH);
distance = (duration/2) / 29.1;
Serial.print("Centimeter: ");
Serial.println(distance);
delay(200);
} ### What You Should See When you first upload the code, open the serial monitor. You should see the distance between the sensor and an object (for example, your hand or a book).
Can you implement the device shown in this video
frequency = 100 + distance * 9.3
Let’s see if that is right.1: This remix by Ron Zacharski