README.md
1| Supported Targets | ESP32 | ESP32-S3 |
2| ----------------- | ----- | -------- |
3
4# HC-SR04 Example
5
6(See the README.md file in the upper level 'examples' directory for more information about examples.)
7
8The capture module in MCPWM peripheral is designed to accurately log the time stamp on the hardware side when an event happens (compared to GPIO ISR which requires a software-based logging method). Each capture unit has three channels, which can be used together to capture IO events parallelly.
9This example shows how to make use of the HW features to decode the pulse width signals generated from a common HC-SR04 sonar range finder -- [HC-SR04](https://www.sparkfun.com/products/15569).
10
11The signal that HC-SR04 produces (and what can be handled by this example) is a simple pulse whose width indicates the measured distance. A pulse is required to send to HC-SR04 on `Trig` pin to begin a new measurement. Then the pulse described above will be sent back on `Echo` pin for decoding.
12
13Typical signals:
14
15```
16Trig +-----+
17 | |
18 | |
19 -----+ +-----------------------
20Echo +-----+
21 | |
22 | |
23 -----------------+ +-----------
24
25 +--------------------------------------->
26 Timeline
27```
28
29## How to Use Example
30
31### Hardware Required
32
33* An ESP development board
34* HC-SR04 module
35
36Connection :
37
38```
39 +------+ +---------------------------------+
40+-------+ | | |
41| | VCC +--------------+ 5V |
42+-------+ | | |
43 + Echo +----=====>----+ GPIO18 (internal pull up) |
44 | | | |
45 + Trig +----<=====----+ GPIO19 |
46+-------| | | |
47| | GND +--------------+ GND |
48+-------| | | |
49 +------+ +---------------------------------+
50```
51
52### Build and Flash
53
54Run `idf.py -p PORT flash monitor` to build, flash and monitor the project.
55
56(To exit the serial monitor, type ``Ctrl-]``.)
57
58See the [Getting Started Guide](https://docs.espressif.com/projects/esp-idf/en/latest/get-started/index.html) for full steps to configure and use ESP-IDF to build projects.
59
60## Example Output
61
62```
63I (314) hc-sr04: HC-SR04 example based on capture function from MCPWM
64I (324) hc-sr04: Echo pin configured
65I (324) gpio: GPIO[19]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0
66I (334) hc-sr04: Trig pin configured
67I (344) hc-sr04: trig task started
68I (444) hc-sr04: Pulse width: 419us, Measured distance: 7.22cm
69I (544) hc-sr04: Pulse width: 419us, Measured distance: 7.22cm
70I (644) hc-sr04: Pulse width: 416us, Measured distance: 7.17cm
71I (744) hc-sr04: Pulse width: 415us, Measured distance: 7.16cm
72I (844) hc-sr04: Pulse width: 415us, Measured distance: 7.16cm
73I (944) hc-sr04: Pulse width: 416us, Measured distance: 7.17cm
74I (1044) hc-sr04: Pulse width: 391us, Measured distance: 6.74cm
75```
76
77This example runs at 10Hz sampling rate. out of range data is dropped and only valid measurement is printed.
78
79## Troubleshooting
80
81For any technical queries, please open an [issue] (https://github.com/espressif/esp-idf/issues) on GitHub. We will get back to you soon.
82