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.

Shell

1

2

3

4

sudo apt update

sudo apt install bb-cape-overlays

zcat/proc/config.gz|grepCONFIG_BONE_CAPEMGR

CONFIG_BONE_CAPEMGR=y

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

Shell

1

2

3

4

5

6

root@beaglebone:~# cd /lib/firmware/

root@beaglebone:/lib/firmware# ls -l

total2100

-rw-r--r--1root root1198Jul2814:55BB-PWM0-00A0.dtbo

-rw-r--r--1root root1198Jul2814:55BB-PWM1-00A0.dtbo

-rw-r--r--1root root1170Aug1115:06BB-PWM2-00A0.dtbo

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:

Shell

1

root@beaglebone:~# nano /boot/uEnv.txt

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

Shell

1

2

####uuid=07ad9f91-64fe-44e2-b60e-f8e4e34183ce

cape_enable=bone_capemgr.enable_partno=BB-PWM0

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

Shell

1

2

3

4

5

6

root@beaglebone:~# cat /sys/devices/platform/bone_capemgr/slots

0:PF-----1

1:PF-----1

2:PF-----1

3:PF-----1

4:P-O-L-0Override Board Name,00A0,Override Manuf,BB-PWM0

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 bb.org-overlays.

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:

Shell

1

2

3

4

root@beaglebone:/sys/class/pwm/pwmchip0# echo 0 > export

root@beaglebone:/sys/class/pwm/pwmchip0# ls

device export npwm power pwm0 subsystem uevent unexport

root@beaglebone:/sys/class/pwm/pwmchip0#

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

Shell

1

2

3

4

5

6

7

8

root@beaglebone:/sys/class/pwm/pwmchip0/pwm0# ls -l

total0

-rw-r--r--1root root4096Aug1203:52duty_cycle

-rw-r--r--1root root4096Aug1203:52enable

-rw-r--r--1root root4096Aug1203:52period

-rw-r--r--1root root4096Aug1203:52polarity

drwxr-xr-x2root root0Aug1203:52power

-rw-r--r--1root root4096Aug1203:52uevent

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

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

1. PWM Configuration :

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.

enable: This is used to enable or disable the pwm0. Setting this as ‘1’ enables and ‘0’ disables.

period: This is the frequency of PWM and is set in the scale of nano seconds.

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.

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:

Shell

1

2

3

root@beaglebone:/sys/class/pwm/pwmchip0/pwm0# echo 20000000 > period

root@beaglebone:/sys/class/pwm/pwmchip0/pwm0# cat period

20000000

Now let’s enable the pwm0 as shown below:

Shell

1

2

3

root@beaglebone:/sys/class/pwm/pwmchip0/pwm0# echo 1 > enable

root@beaglebone:/sys/class/pwm/pwmchip0/pwm0# cat enable

1

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.