Tutorial Video
Hardware
- PHPoC Blue or PHPoC Black
- 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\01.php_task\03.ht_pwm_servo to PHPoC Blue/Black.
- Click "Run" button on PHPoC Debugger.
Source Code
Source files includes:
- init.php: this file is run when PHPoC system is powered or reset. It is used to specify which file is run is system loop.
- task0.php: this file is run in system loop of PHPoC devices.
init.php
This file is run when PHPoC system is powered or reset. It is used to specify that task0.php is run is system loop.
PHP Code:
<?php
system("php task0.php");
?>
task0.php
[Full Code]
PHP Code:
<?php
if(_SERVER("REQUEST_METHOD"))
exit; // avoid php execution via http request
include_once "/lib/sd_340.php";
define("PWM_PERIOD", 20000); // 20000us (20ms)
define("WIDTH_MIN", 600);
define("WIDTH_MAX", 2450);
echo "PHPoC example : P4S-34X / HT / Tower Pro SG92R Micro Servo\r\n";
ht_pwm_setup(0, WIDTH_MIN, PWM_PERIOD, "us");
echo "CCW ";
for($angle = 0; $angle <= 180; $angle += 45)
{
echo $angle, " ";
$width = WIDTH_MIN + (int)round($angle / 180.0 * (WIDTH_MAX - WIDTH_MIN));
ht_pwm_width(0, $width, PWM_PERIOD);
sleep(1);
}
echo "\r\n";
echo "CW ";
for($angle = 180; $angle >= 0; $angle -= 45)
{
echo $angle, " ";
$width = WIDTH_MIN + (int)round($angle / 180.0 * (WIDTH_MAX - WIDTH_MIN));
ht_pwm_width(0, $width, PWM_PERIOD);
sleep(1);
}
echo "\r\n";
?>
[Explanation]
Source code of this file does:
- Setup and initialize HT0 timer to generate PWM signal.
- Increase/Decrease angle by 45 degree (to rotate motor in counter-clockwise/clockwise).
- Print rotated angle to PHPoC Debugger's console.
- Calculate PWM duty cycle based on angle value
- Generate PWM signal to control servo motor
See Also
- Servo Motor - Controlling Servo Motor from Webpage using Hypertext.
- Servo Motor - Controlling Servo Motor from Webpage with Image.
- Servo Motor - Controlling Servo Motor via WebSocket with Graphic UI.
Other Resources