HC-SR04 Ultrasound Sensor 4-Arduino

Ultra Simple Test Experiment —Ardu_Serie Episode #81

J3
Jungletronics
4 min readAug 25, 2019

--

Looking up how to use this sensor without delay?

Yeap, I admit this example is more than bare-bones in that it sends to serial the measurements data, but this lets you test it without any problems.

Fig 1. a awesome cheap sensor :D

Here is the

code

And here is the

Fritz Schema

Fig 2. Fritzing Simple Schematicto run the code above:)

And the

Lab

Fig 3. The lab. Hey, sorry for the serial inverted ”/

Here are some notes:

- This sensor is very popular among the Arduino tinkerers;- The module is about 45x20x15 mm in size and has a 4 pin connection;- There’s an Arduino library called NewPing that can make your life easier when using this sensor;- The ultrasonic sensor uses sonar to determine the distance to an object;- The HC-SR04 ultrasonic sensor uses sonar to determine distance to an object like bats do;- The HC-SR04 sends a ping as a submarine does and measure the time between sending and receiving anything back when an object is in front of the sensor;- It offers excellent non-contact range detection with high accuracy and stable readings in an easy-to-use package;- The HC-SR04 uses sound instead of light as the GP2D12 module does;- HC-SR04 is cheaper than the GP2D12;- Because using sound for its measurements we can reach up to 4 meters;- specifications:
Power Supply :+5V DC
Quiescent Current : <2mA
Working Current: 15mA
Effectual Angle: <15°
Ranging Distance : 2cm – 400 cm/1? – 13ft
Resolution : 0.3 cm
Measuring Angle: 30 degree
Trigger Input Pulse width: 10uS
Dimension: 45mm x 20mm x 15mm
- The time between the transmission and reception of the signal allows us to calculate the distance to an object;- How it Works 1: The transmitter (trig pin) sends a signal: a high-frequency sound. When the signal finds an object, it is reflected and…… the transmitter (echo pin) receives it;- One ping of the HC0SR04 actually exists of 8 pulses at 40 kHz to do the measurement;- How it works 2 - One ping of the HC0SR04 actually exists of 8 pulses at 40 kHz to do the measurement; To start a ping you need to provide a 10us pulse on the trigger input; When the distance is measured by the 8 pulses the HC0SR04 puts a pulse on the echo pin.
You can calculate the distance with the length of the echo pulse and the speed of sound; The speed of sound is 340 m/s or 2.9 micro seconds per mm; We have to divide the length of the pulse by 2.9 to get the result in mm; The ping is traveling towards an object and back to the sensor again; Because of this we need to divide the result by two; Between two pings we need to keep a 60ms measurement cycle;
- NOTE: If the HC-SR04 does not receive an echo then the output never goes low; Devantec and Parallax sensors time out after 36ms and I think 28ms respectively; If you use Pulsin then with no return echo the program will hang for 1 second which is the default timeout for Pulsin. You need to use the timeout parameter;- If you put a resistor, say 2k2 between E and T then only connect to T you can use the HC-SR04 from just one Arduino pin. Look up single pin operation of ultrasonic sensors;- Also if you are using a HC-SR04 with a PicAxe you need to up the clockspeed to at least 8MHz otherwise they don’t see the start of the echo pulse so pulsin never starts. The HC-SR04 works fine with a BS2 - by David Buckley:)

And There you have it!

Not much to say about this sensor. The internet is floated with revision about this awesome and simple to use a sensor device.

Be tuned!

Bye!

Related Posts:

24° Lego Episode — PitBot — A Star Is Born — Working at The First Structure in Our Sparring Robot

25° Lego Episode — PitBot Is Agressive? Well, No Worries! — Making PitBot bite!

26° Lego Episode — Dancing Good w/ PitBot — All The Secret for Replicate This Awesome Robot

Fig 4. The box, rule and Pibot HC-SR04 Ultra-Sound Sensor.

Credits & References

Ultrasonic Sensor HC-SR04 and Arduino Tutorial by https://howtomechatronics.com

HC-SR04 Ultrasonic Sensor by https://www.botshop.co.za

Lily∞Bot: Obstacle Avoidance With 4 Sonar (HC-SR04)

--

--

J3
Jungletronics

Hi, Guys o/ I am J3! I am just a hobby-dev, playing around with Python, Django, Ruby, Rails, Lego, Arduino, Raspy, PIC, AI… Welcome! Join us!