servo motor beaglebone

Servo Motor Control Using BeagleBone Black

Are you planning on building some kind of an actuated control system or a robot? the least you may need is a servo motor. So, In this tutorial, I will show you how to control a servo motor using the PWM of BeagleBone Black. If you are using a kernel version > 3.8, please run the below commands to update the device tree overlays present in the /lib/firmware directory.

Now check for the PWM overlays in the firmware directory as shown below:

Above we can see that, there are 3 PWM overlays out of which we will use the BB-PWM0 for this demonstration.

We can load the overlay two ways, one is by using “echo” to manually load the overlay and another is by updating the uEnv.txt file in the boot directory. The drawback of manually loading the overlay is that it will be loaded for that instance or session, but when you reboot the Beaglebone, the overlay will be unloaded by default and you will have to again load it manually.

Hence, I’d like to load the overlay every time when I initialize, power up or reboot the BeagleBone. So, that we can always keep the PWM function active for our application to use. Therefore, I prefer the latter method and it can be done as follows:

Open the uEnv.txt file in the boot directory as shown below:

In the uEnv.txt file, append or update this line as shown below and then reboot your BeagleBone,

After you reboot, you should be able to check if the overlay has loaded as shown below:

You can see that the BB-PWM0 has been loaded in slot 4. So far so good 🙂

The BB-PWM0 overlay corresponds to the use of P9_22(ehrpwm0A) and P9_21(ehrpwm0B).  It means that both of these PWM’s can be controlled by pwmchip0. To figure out which PWM pins are mapped for a particular overlay, you can view the dts source files at

Now let’s go into the PWM directory and perform the following steps to export pwm0. Also, note that pwm0 corresponds to P9_22 and pwm1 corresponds to P9_21.

Export pwm0 by using echo as shown below:

In the pwm0 directory shown below, we can configure the pwm0 by setting the polarity, duty cycle, period and enable or disable it.

Before going into configuring the pwm0 parameters and seeing the  servo motor in action, make sure you have setup the hardware as shown in image below

servo motor control using beaglebone
Fig1. This image shows the connection diagram between the BeagleBone Black and HS-422 servo Motor

1. PWM Configuration :

  1. duty_cycle: The duty cycle is the period of on time to off time within a pulse cycle. This value is responsible for the amount of power delivered to the load. In the BeagleBone this value is set in the scale of nano seconds. Servo Motors work differently compared to DC Motors i.e. the pulse width determines the position of the shaft of the servo motor, unlike DC Motors in which the pulse width determines the speed of rotation.
  2. enable: This is used to enable or disable the pwm0. Setting this as ‘1’ enables and ‘0’ disables.
  3. period: This is the frequency of PWM and is set in the scale of nano seconds.
  4. polarity: By default the polarity is normal, however, it can also be changed to inverse. This kind of polarity setting has been in effect for kernel versions > 3.8.

Let us now understand how the servo motor (HS-422) reacts for different pulse width timings i.e. duty cycle. This concept generally applies to most of the servo motors, however, in some cases, it may differ.

servo motor duty cycles

Fig 2. Ref – Electrical Stack Exchange

The above image shows the different positions of the servo motor shaft corresponding to their duty cycle. In the next section, let us move the servo motor’s shaft to different positions and test it corresponding to the duty cycle values shown in the above image.

2. Test the Servo Motor using the PWM0

First, let us set the period or frequency as 50 Hz. The period is 1/50 = 0.02 s = 20ms. So, when we convert 20ms to nano seconds, it is equal to 20000000ns. Hence, we assign the period equal to 20000000 as shown below:

Now let’s enable the pwm0 as shown below:

Then I assigned different duty cycle values as shown below, and tested if the movements of the position of the shaft match as that shown in Fig 2. And it works flawlessly 🙂

The below video demonstrates how the servo motor’s direction changes with respect to different duty cycles as explained above.

I hope this tutorial was useful and in the case of any queries, please feel free to comment below and I will get back to you as soon as possible. For more updates on  BeagleBone concepts and projects, feel free to follow and subscribe to my blog.

Leave a Reply

Your email address will not be published. Required fields are marked *