Last Updated on 31 May 2019 by Suffocation
Controlling a servo with the Raspberry Pi 3.
This is purely about the basic technique; a mobile robot could be an example of its application.
The servo should be controlled via software PWM here to keep the hardware PWMs free for other functions.
In addition, the servo should not be controlled via integer values as usual, but via a degree measurement in relation to the servo's centre position.
The image below shows what is meant by this.

I will not go into the actual workings of a servo here; this can be read up on if needed at http://rn-wissen.de/wiki/index.php/Servos or other websites.
Required hardware
- PI3 (or older, please pay attention to the correct pins)
- Standard Servo
- Diverse Cables (Pre-assembled Pin Header Cables)
Circuit arrangement
In my case, the servo is controlled via GPIO 27. The Raspberry Pi supplies the power. For larger servos, an external power supply might need to be used.
The GPIOs are only valid in conjunction with pi4j and wirePi

Software
I am using BlueJ on the Pi itself as my development environment. I have created the following classes with it.

Code
A First Test
A good start is always to put code found online into a main function to see if anything works at all. I have done this in the form of the FirstTest class. It cycles through all the positions of the servo. The functionality is not encapsulated here.
Encapsulated code
As the next step, I took the code from the first test and put it into a class with a few changes. I also implemented the conversion of absolute values into degrees here. For testing the class, I created another main class that calls everything.
Servo Class
Class for controlling the servo. The two most important methods are:
- setPos – Set the position, value range from posMin to posMax where (posMax-posMin+1) is the centre position.
- setDeg - This method projects the specified degree number onto the possible positions (currently very inaccurate 🙁 )
Test Class
Class with a main method for testing the Servo class. In the main method, all positions are traversed, with positioning taking place via the angle relative to the zero position.
Fingertips
Positioning using the PI4j library is very imprecise. For the servo's 180° range of motion, only about 20 (+-2) positions are available. This is roughly 9° per position. It might be advisable to test beforehand to see which positions are best suited for the current purpose. If the exact angle is important, it is better to measure it beforehand rather than calculate it using the `setDeg` method.
There are other libraries (Servoblast, ...) that might be more precise. You could also consider changing the programming language and then addressing the respective code via the native interface. As a next attempt, Servoblast sounds promising at first.
Next steps
PWM in Thread
I tried to run the software PWM within a thread, but without success. The servo refuses to work. I'm still unclear about the cause of this. However, I assume that the timer is being ruined as a result.
ServoBlast
After various tests with different frequencies, I have decided ServoBlaster od. It is better to use the PI variant (PiBits). The installation is as simple as follows.
Programming example
This is where a code example with ServoBlast should actually follow. However, as I have currently stopped working on the PI and am using the ESP8266 and ESP32, please refer to the following links:
Troubleshooting
Execution failed
If the Java exec function does nothing, here's a good link for bug fixing.
http://www.javaworld.com/article/2071275/core-java/when-runtime-exec—won-t.html
Problems with println
Furthermore, it might help not to use the println method, but rather the print method. This is because at least my PI couldn't interpret the line break correctly. However, print(„5=100\n“) did work.
Related Posts
Sources
http://razzpisampler.oreilly.com/ch05.html
https://github.com/Pi4J/pi4j/blob/master/pi4j-example/src/main/java/MaestroServoExample.java
https://phyks.me/2015/05/controlling-servomotors-on-a-raspberry-pi.html
https://github.com/richardghirst/PiBits/tree/master/ServoBlaster
http://rn-wissen.de/wiki/index.php/Servos
Attachment
Pin assignment

Considering the article is over a year old, I find it a bit of a shame that you still have to read „… to come“ in some places.
It also seems like something is missing from the „gistpen id“ lines, or I don't understand it.
Hello and thank you for your comment.
Yes, the age of the post is a bit of an issue. I started this on my last holiday and then didn't get around to finishing it, or I've been more involved with the ESP8266/ESP32 recently. Unfortunately, I haven't gotten around to a documented test with ServoBlast yet, maybe on the next holiday ;) . I've adjusted the post accordingly to inform readers about this. The two pages that describe the use of ServoBlast quite well from my perspective are now linked again.
The Gistpen ID thing is annoying. One version of Gistpen unfortunately didn’t work, which resulted in all source code pages no longer being displayed. For this reason, I unfortunately had to deactivate it and, as always, I didn't have time to revise the old pages. On new pages, I now offer a download. Long story short, I have reactivated Gistpen in the current version and it seems to be working again.
Greetings SFA