Raspberry Pi 3 Cookbook for Python Programmers - Third Edition
by Steven Lawrence Fernandes, Tim Cox
The servo class
The servo code is based on the PWM driver that Adafruit uses for their module; however, it is not Python 3 friendly, so we need to create our own version. We will use wiringpi2 I2C driver to initialize and control the I2C PWM controller. We define the registers that we will need to use (see the datasheet for the PCA9685 device), as well as its default bus address, 0x40 (PWMADR), and a PWM frequency of 50 Hz (PWMHZ).
Within our servo class, we initialize the I2C driver in wiringpi2 and set up our devPWM device on the bus. Next, we initialize the PWM device itself (using pwmInit()). We have to calculate the prescaler required for the device to convert the onboard 25 MHz clock to a 50 Hz signal to generate the PWM frequency we ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access