Tutorial Video
Hardware
- PHPoC Blue (+ USB WLAN) or PHPoC Black (+ Ethernet cable)
- Micro USB to USB Cable (to upload source code to PHPoC Device)
- Servo Motor
- Jumper wires
About Servo Motor
Stardard servo motor is used to control of angular (usually between 0 and 180 degrees). The servo motor used in this example includes three wires:
- VCC wire.
- GND wire.
- Signal wire (receives the PWM control signal from controller).
- If generating PWM signal with minimum duty cycle to signal wire, the servo motor rotate to 0 degree.
- If generating PWM signal with maximun duty cycle to signal wire, the servo motor rotate to 180 degree.
- If generating PWM signal with duty cycle between minimum and maximum value to signal wire, the servo motor rotate to position that is proportional to duty cycle.
- Harware timer and software timer on PHPoC device can generate PWM signal. Therefore, It just needs connect the HT pin (or selectable IO pin if using software timer) of PHPoC device to the signal wire of servo motor. And then use hardware/software timer to control servo motor.
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\03.html_image\03.ht_pwm_servo 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: this file contains source code of web page.It is only run in response to request from Web Browser.
- ht_pwm_servo.jpg: Wring diagram between PHPoC devices and servo motor. It is displayed on webpage.
index.php
[Full Code]
PHP Code:
<?php
include_once "/lib/sd_340.php";
define("PWM_PERIOD", 20000); // 20000us (20ms)
define("WIDTH_MIN", 600);
define("WIDTH_MAX", 2450);
$flags = 0;
$angle = 0;
um_read(0, 0, $flags, 4); // read flags (offset 0, 32bit integer)
um_read(0, 4, $angle, 4); // read angle (offset 4, 32bit integer)
if(!$flags)
{
ht_pwm_setup(0, (WIDTH_MIN + WIDTH_MAX) / 2, PWM_PERIOD, "us");
$flags |= 0x00000001; // set init flag
um_write(0, 0, int2bin($flags, 4)); // write flags (offset 0, 32bit integer)
$angle = 90;
um_write(0, 4, int2bin($angle, 4)); // write angle (offset 4, 32bit integer)
}
if(($cw = _GET("cw")))
$delta = -(int)$cw; // clock wise
else
if(($ccw = _GET("ccw")))
$delta = (int)$ccw; // counter clock wise
else
$delta = 0;
if($delta)
{
um_read(0, 4, $angle, 4); // read angle (offset 4, 32bit integer)
$angle += $delta;
if($angle > 180)
$angle = 180;
if($angle < 0)
$angle = 0;
um_write(0, 4, int2bin($angle, 4)); // write angle (offset 4, 32bit integer)
$width = WIDTH_MIN + (int)round($angle / 180.0 * (WIDTH_MAX - WIDTH_MIN));
ht_pwm_width(0, $width, PWM_PERIOD);
}
?>
<html>
<head>
<title>PHPoC / <?echo system("uname -i")?></title>
<meta name="viewport" content="width=device-width, initial-scale=0.5">
<style> body { text-align: center; } </style>
</head>
<body>
<h2>
HT / Tower Pro SG92R Micro Servo<br>
<img src="ht_pwm_servo.jpg"><br>
<a href="index.php?cw=45">-45'</a>
<a href="index.php?cw=15">-15'</a>
<?php printf("CW %d' CCW", $angle); ?>
<a href="index.php?ccw=15">+15'</a>
<a href="index.php?ccw=45">+45'</a>
</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:
- Read initial states (initialized or not) and current angle in flash memory
- Setup and initialize timer to generate PWM if not initialized
- Read direction (clockwise or counter-clockwise) and the increment from HTTP request
- Calculate new angle (new_angle = current_angle + increment)
- Update new angle value on flash memory
- Calculate PWM duty cycle based on angle value
- Generate PWM signal to control servo motor
PHP Code:<?php
include_once "/lib/sd_340.php";
define("PWM_PERIOD", 20000); // 20000us (20ms)
define("WIDTH_MIN", 600);
define("WIDTH_MAX", 2450);
$flags = 0;
$angle = 0;
um_read(0, 0, $flags, 4); // read flags (offset 0, 32bit integer)
um_read(0, 4, $angle, 4); // read angle (offset 4, 32bit integer)
if(!$flags)
{
ht_pwm_setup(0, (WIDTH_MIN + WIDTH_MAX) / 2, PWM_PERIOD, "us");
$flags |= 0x00000001; // set init flag
um_write(0, 0, int2bin($flags, 4)); // write flags (offset 0, 32bit integer)
$angle = 90;
um_write(0, 4, int2bin($angle, 4)); // write angle (offset 4, 32bit integer)
}
if(($cw = _GET("cw")))
$delta = -(int)$cw; // clock wise
else
if(($ccw = _GET("ccw")))
$delta = (int)$ccw; // counter clock wise
else
$delta = 0;
if($delta)
{
um_read(0, 4, $angle, 4); // read angle (offset 4, 32bit integer)
$angle += $delta;
if($angle > 180)
$angle = 180;
if($angle < 0)
$angle = 0;
um_write(0, 4, int2bin($angle, 4)); // write angle (offset 4, 32bit integer)
$width = WIDTH_MIN + (int)round($angle / 180.0 * (WIDTH_MAX - WIDTH_MIN));
ht_pwm_width(0, $width, PWM_PERIOD);
}
?>
The second piece of code prints the rotated angle value to Webpage
-
PHP Code:
<?php printf("CW %d' CCW", $angle); ?>
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
- Servo Motor - How to Control Servo Motor.
- Servo Motor - Controlling Servo Motor from Webpage using Hypertext.
- Servo Motor - Controlling Servo Motor via WebSocket with Graphic UI.
Other Resources