Tutorial Video
Hardware
- PHPoC Blue (+ USB WLAN) or PHPoC Black (+ Ethernet cable)
- Micro USB to USB Cable (to upload source code to PHPoC Device)
- Ultrasonic Sensor
- Jumper wires
About Ultrasonic Sensor
Ultrasonic sensor HC-SR04 is used to measure distance to an object by using ultrasonic waves. The ultrasonic sensor HC-SR04 includes four pins:
- VCC pin.
- GND pin.
- TRIG pin: this pin receives a 10-microsecond pulse from controller to emit ultrasonic wave.
- ECHO pin: this pin outputs a pulse when receiving echo ultrasonic wave. The duration of pulse is used to calculate the distance.
- If receiving 10-microsecond pulse on TRIG pin , ultrasonic sensor automatically emits 40KHz ultrasonic wave.
- If there is an obstacle, ultrasonic wave is reflected back.
- Ultrasonic sensor measures duration of time from emiting ultrasonic wave to receiving the echo ultrasonic wave. And then ultrasonic sensor generate a pulse with the measured duration on ECHO pin.
- Connect TRIG pin of ultrasonic sensor to HT0 pin of PHPoC device. Set HT0 timer to "ouput pulse" mode.
- Connect ECHO pin of ultrasonic sensor to HT1 pin of PHPoC device. Set HT1 timer to "capture" mode.
- Use HT0 timer to generate 10-microsecond pulse.
- Use HT2 timer to capture the pulse from ECHO pin.
- Calculate distance based on the duration of captured pulse. The duration of pulse is the travel time of ultrasonic wave. Travel speed of ultrasonic wave is about 340 m/s. Travel distance is calculated according to the formula: distance = speed * time
- Distance to obstacle is half of the travel distance of ultrasonic wave.
Wiring Diagram

Quick Steps
Source code of this example is a part of PHPoC Support Packet (PSP). You need to:
- Download PHPoC Support Package.
- Upload example\p4s\02.html_text\03.ht_ultrasonic to PHPoC Blue/Black.
- Configure network parameters (e.g. WiFi SSID, password, IP address ...).
- Access webpage on PHPoC using Web Browser on your PC or smart phone (See How To).
Source Code
Source files includes:index.php file, which contains source code of web page. It is only run in response to request from Web Browser.
index.php
[Full Code]
PHP Code:
<?php
include_once "/lib/sd_340.php";
// setup trigger pulse timer
ht_ioctl(0, "set mode output pulse");
ht_ioctl(0, "set div us");
ht_ioctl(0, "set repc 1");
ht_ioctl(0, "set count 5 10"); // 10us pulse width
// setup echo capture timer
ht_ioctl(1, "reset");
ht_ioctl(1, "set div us");
ht_ioctl(1, "set mode capture toggle");
ht_ioctl(1, "set trigger from pin rise");
ht_ioctl(1, "set repc 4");
ht_ioctl(1, "start"); // we should start capture timer first
ht_ioctl(0, "start"); // start trigger pulse
usleep(100000); // sleep 100ms
// 1st capture value ("get count 0") is always zero.
// we should get 2nd capture value;
$us = ht_ioctl(1, "get count 1");
$dist = $us * 340.0 / 2; // us to meter conversion
$dist = $dist / 10000; // meter to centimeter conversion
?>
<html>
<head>
<title>PHPoC / <?echo system("uname -i")?></title>
<meta name="viewport" content="width=device-width, initial-scale=0.7">
<style> body { text-align: center; } </style>
</head>
<body>
<h2>
HT / HC-SR04 Ultrasonic Module<br>
<br>
<?php
printf("Pulse Width : %d us<br>\r\n", $us);
printf("Distance : %.1f cm<br>\r\n", $dist);
?>
<br><a href="index.php">reload</a><br>
</h2>
</body>
</html>
[Explanation]
Source code of index.php file is composed of HTML, CSS and PHPoC code.
PHPoC code is interpreted on on PHPoC device. It includes two piece of code.
The first piece of code does:
- Setup HT0 timer to generate pulse and HT1 timer to capture signal.
- Start capture timer
- Start the trigger pulse timer
- Get duration of captured pulse
- Calculate distance according to the duration of captured pulse
PHP Code:<?php
include_once "/lib/sd_340.php";
// setup trigger pulse timer
ht_ioctl(0, "set mode output pulse");
ht_ioctl(0, "set div us");
ht_ioctl(0, "set repc 1");
ht_ioctl(0, "set count 5 10"); // 10us pulse width
// setup echo capture timer
ht_ioctl(1, "reset");
ht_ioctl(1, "set div us");
ht_ioctl(1, "set mode capture toggle");
ht_ioctl(1, "set trigger from pin rise");
ht_ioctl(1, "set repc 4");
ht_ioctl(1, "start"); // we should start capture timer first
ht_ioctl(0, "start"); // start trigger pulse
usleep(100000); // sleep 100ms
// 1st capture value ("get count 0") is always zero.
// we should get 2nd capture value;
$us = ht_ioctl(1, "get count 1");
$dist = $us * 340.0 / 2; // us to meter conversion
$dist = $dist / 10000; // meter to centimeter conversion
?>
The second piece of code prints the distance from sensor to obstacle value to Webpage
-
PHP Code:
<?php
printf("Pulse Width : %d us<br>\r\n", $us);
printf("Distance : %.1f cm<br>\r\n", $dist);
?>
PHPoC code may add/update the content of HTML, CSS or JavaScript code. Once PHPoC code is interpreted in PHPoC, the remaining code is client-side code and it is returned to Web Browser. Web Browser receives this code and interpret it to display the webpage.
- HTML: describes the structure of Web pages
- CSS: describes how HTML elements are to be displayed
See Also
- Ultrasonic Sensor - How to Use Ultrasonic Sensor Sensor.
- Ultrasonic Sensor - Monitoring Distance from Webpage with Image.
- Ultrasonic Sensor - Monitoring Distance via WebSocket.
Other Resources