Announcement

Collapse
No announcement yet.

Servo Motor - How to Control Servo Motor

Collapse
X
Collapse
  •  

  • Servo Motor - How to Control Servo Motor


    Tutorial Video



    Hardware
    • PHPoC Blue or PHPoC Black
    • Micro USB to USB Cable (to upload source code to PHPoC Device)
    • Servo Motor
    • Jumper Cable


    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).
    How the servo motor works: servo motor can be controlled by generating PWM signal to signal wire of servo motor. The motor sepcification defines cycle time, minimum and maximum duty cycle.
    • 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.
    How to control servo motor:
    • 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.
    If you use PHPoC for the first time, see How To Use PSP.


    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(0WIDTH_MINPWM_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$widthPWM_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$widthPWM_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

    Other Resources
    Last edited by support; 02-09-2018, 08:47 AM.
      Posting comments is disabled.

    Categories

    Collapse

    Latest Articles

    Collapse

    • Arduino - RS-485 Expansion Board
      by support
      PES-2607 is an easy-to-use RS422/RS485 Expansion Board for Arduino Uno and Mega, which allows Arduino to exchange data with serial device via RS422 or RS485.
      Especially, Arduino does NOT use UART pins to communicate with RS422/RS485 expansion board. Therefore, users can use Arduino UART pins for other purposes.
      Moreover, A single Arduino Uno/Mega can communicate with multiple RS422/RS485 expansion boards (up to 14) without using Arduino UART pins.

      Library and examples for...
      11-13-2018, 02:45 PM
    • Arduino - RS-422 Expansion Board
      by support
      PES-2607 is an easy-to-use RS422/RS485 Expansion Board for Arduino Uno and Mega, which allows Arduino to exchange data with serial device via RS422 or RS485.
      Especially, Arduino does NOT use UART pins to communicate with RS422/RS485 expansion board. Therefore, users can use Arduino UART pins for other purposes.
      Moreover, A single Arduino Uno/Mega can communicate with multiple RS422/RS485 expansion boards (up to 14) without using Arduino UART pins.

      Library and examples for...
      11-13-2018, 02:44 PM
    • Arduino - RS-232 Expansion Board
      by support
      PES-2606 is an easy-to-use RS-232 Expansion Board for Arduino Uno and Mega, which allows Arduino to exchange data with serial device via RS-232.
      Especially, Arduino does NOT use UART pins to communicate with RS-232 expansion board. Therefore, users can use Arduino UART pins for other purposes.
      Moreover, A single Arduino Uno/Mega can communicate with multiple RS-232 expansion boards (up to 14) without using Arduino UART pins.

      Library and example for the RS-232 expansion board...
      11-13-2018, 02:43 PM
    • Arduino - Stepper Motor Controller
      by support
      PES-2605 is an easy-to-use stepper motor controller for Arduino Uno and Mega, which uses micro-stepping method to precisely control stepper motor.
      Library and example for the stepper motor controller are part of of PhpocExpansion library for Arduino. The library reference is available here.

      This tutorial shows how to use the step motor controller with an example of PhpocExpansion library for Arduino.


      Hardware Required...
      11-13-2018, 02:41 PM
    • Arduino - DC Motor Controller
      by support
      PES-2604 is an easy-to-use DC motor controller for Arduino Uno and Mega.
      Library and example for the DC motor controller are part of of PhpocExpansion library for Arduino. The library reference is available here.

      This tutorial shows how to use the DC motor controller with an example of PhpocExpansion library for Arduino.


      Hardware Required...
      11-13-2018, 02:40 PM
    • Arduino - Digital Input Board
      by support
      PES-2602 is an easy-to-use 4-port Input Expansion Board for Arduino Uno and Mega, which allows Arduino to monitor state of DC electric device. In addition, it can monitor NPN, PNP and dry contact(relay).
      Library and example for the 4-port input expansion board are part of of PhpocExpansion library for Arduino. The library reference is available here.

      This tutorial shows how to use 4-port input expansion board with an example of PhpocExpansion library for Arduino.

      ...
      11-13-2018, 02:39 PM
    Working...
    X