Has anyone gotten a PCA9685 I2C servo driver to work with Pi4J?

393 views
Skip to first unread message

Michael Sloan

unread,
Aug 21, 2016, 7:13:17 PM8/21/16
to Pi4J
I have tried both the AdaFruit version and Keyestudio version of the PCA9685 I2C 16 X 12 bit servo driver with the examples provide with Pi4J.  At first I thought is was the device, but not with two devices behaving the same exact way.
The program I am running is the PCA9685GpioServoExample provided in the  /opt/pi4j/examples folder.
The program runs with sudo pi4j --run PCA9685GpioServoExample.java, and presents the menu.  Anytime I try and move the servo through manual control (m), approach neutral position (n) or sweep (p) the servo turn completely to the right (clockwise) and jams.
No other input will move the servo after that.....
Pressing (i) for servo info doesn't seem to provide any information what-so-ever...

Any ideas??

MS

unread,
Aug 23, 2016, 5:58:55 PM8/23/16
to Pi4J
Does anyone know why this might cause an NullPointerException for the line gpioProvider.reset();?

This is a truncated version of PCA9685GpioExample.java that is included in the examples folder after pi4j install.
The original runs fine from the command line, but when I try and run this from NetBeans I receive the Exception error.
Its being run on a R-P1 2 Model B.

package pca9685test08232016;

import com.pi4j.gpio.extension.pca.PCA9685GpioProvider;
import com.pi4j.gpio.extension.pca.PCA9685Pin;
import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinPwmOutput;
import com.pi4j.io.gpio.Pin;
import com.pi4j.io.i2c.I2CBus;
import com.pi4j.io.i2c.I2CFactory;
import java.math.BigDecimal;

public class PCA9685test08232016 {
    @SuppressWarnings("resource")
    public static void main(String[] args) {
        
        BigDecimal frequency = new BigDecimal("48.828");
        BigDecimal frequencyCorrectionFactor = new BigDecimal("1.0578");
        
        I2CBus bus = I2CFactory.getInstance(I2CBus.BUS_1);
        PCA9685GpioProvider gpioProvider = new PCA9685GpioProvider(bus, 0x40, frequency, frequencyCorrectionFactor);
       
        gpioProvider.reset();


        gpioProvider.setPwm(PCA9685Pin.PWM_00, 900);
        gpioProvider.setPwm(PCA9685Pin.PWM_01, 1500);
        gpioProvider.setPwm(PCA9685Pin.PWM_02, 2100);
        
    }
    
}

Steve Kim

unread,
Sep 10, 2016, 2:46:54 AM9/10/16
to Pi4J
Hi MS,

I had the same problem;therefore, the way I got around that was to have the gpio1 as a pwm(this is the sample program from pi4j version 1.1). And I got 74hc154E(demultiplexer: it has one input with 16 different outputs, and 4 addresses to control 16 outputs.) to control 10 servos by having one input as a pwm(gpio1), and 4 addresses were controlled by gpio 0, 2, 3, and 4. I think you hit the idea.
After little modification of the pwm sample program, the servos are moving independently. Important note is that make sure to have a separate power supply for the demultiplexer and servos.

V.S.

unread,
Apr 24, 2017, 4:37:42 PM4/24/17
to Pi4J
Steve, I'm having the same issue as M.S. with a turnigy servo, a pi-zero and the PCA9685.

It seem you use the same code but different hardware... Can you please describe the "little modification of the pwm sample program" that made your program work?

With the PCA9685GpioExample (and a bit of adjustment of the range) the servo works fine, but NOT with the PCA9685GpioServoExample source.

A bit confused since both examples provision the pins as PWM out. Issue appears only when accessing the pin via the servo interface

V
Reply all
Reply to author
Forward
0 new messages