Servo Motor Control (OP-AN-013)

From openPicus Wiki
Jump to: navigation, search



Application Note PDF

Source Code


Thanks to the openPicus framework, in short time user can develop internet-enabled movable systems, without worrying about microcontroller internal registers and TCP/IP Stack settings. The focus is moved on user application, and not on firmware setup.

Also in a hobby-environment, every user can build up a system with servo motors, using just a soldering iron, few wires and connectors, a FLYPORT module (both Wi-Fi and Ethernet can be used) and a miniUSB programmer.

The standard webserver was modified to control the servos by a simple browser.

Required Hardware

  • openPicus FLYPORT
  • HS-422 servo motors

Required Software and Libreries


  • OpenPicus Free IDE 2.2+



Used in the application note, the digital servo HS-422 is a popular low-cost and low-power position controlled servo motor.

It allows to assign the angle of the motor using different value of FLYPORT PWM signal. The chosen model allows to rotate the motor within 180-degree range.

The association between PWM Duty cycle values and motor position angle is:

Angle = (PWMDuty – 5) * ( 180 / 20 ) = 9 * (PWMDuty – 5) //[°]

This formula permits to understand how the motor is controlled:

  • PWM Duty Cycle = 5 moves the motor to its 0° angle
  • PWM Duty Cycle = 25 moves the motor to its 180° angle

All the values, included between these two values, permit to choose the right angle, simply using the openPicus framework function:

PWMDuty(float dutyToUse, int pwmToUse);


A simple webserver example has been developed to test the use of the HS-422 servos in order to control 3 motors in real time and from a remote position. There are 3 slider controls with a range of values between 5.0 and 25.0, with steps of 0.5 on the webpage.

Implementation OP-AN-013 1.png

IMPORTANT: Since HTML5 control is used, be sure your browser can correctly handle these type of elements.

The HTML code of the first motor command is:

<input id=”pwm0” type=”range” min=5max=25step="0.5" value=5
onmouseup=”newAJAXCommand('pwms.cgi?pwm=0&value='+pwm0.value);” />

The event onmouseup is used to raise the callback function (the cgi request) inside the FLYPORT HTTPApp.c firmware,passing two arguments: the pwm number, and its new duty cycle value, taken from the slider value of the web page.

The function HTTPExecuteGet of HTPPApp.c is executed every time a slider position is changed. Inside this function, if the updater file is pwms.cgi, two variables are read from and processed with a switch-case statement.

Hardware connections

The hardware connections are very basic, thanks to the digital input of the chosen servo model. In fact, the HS-422 servos has only 3 wires:

  • The Black wire is the GND connection
  • The Red wire is the VCC connection, in our case the 5V
  • The Yellow wire is the PWM input connection, that we used to connect the pins:
    • p17 for PWM1 and the first motor
    • p4 for PWM2 and the second motor
    • p6 for PWM3 and the third motor
Implementation OP-AN-013 2.png
Implementation OP-AN-013 3.png



Personal tools