Help getting i2c, or specifically pca9685 servo HAT to work

I’m using wiringpi, and so far I’ve had good luck with it.

I want more control over my servo (and to add more than one eventually) so I opted for this Pi attachment. I looked over the datasheet and I understand somewhat how to talk to it, but I can’t get anything to work. I also tried using a pca9685 library but it doesn’t seem to help my situation.

I’m just curious if anyone has had success with this before and if you have sage wisdom.

//fd = wiringPiI2CSetup(0x40);
  fd = pca9685Setup(300, 0x40, 50);

// i've tried writing a ton of different values to it...
  for (int i = 0; i < 180; i++) {
    ofLog() << i;
    //wiringPiI2CWriteReg16 (fd, 0x00, i);
    pwmWrite(316, (int)ofMap(i, 0, 180, 5, 25));


    ofSleepMillis(100);
  }

This library does work, and I can get the examples to run, though I can’t figure out how to integrate it into an OF project

Hi @prismspecs,
Have you had a look at ofxGPIO yet? I’ve had lots of success with this add-on for i2c, SPI, controlling pins, and related on a Pi3 and PiZeroW. And many thanks to @kashim for creating and maintaining this library! I usually re-create and re-work Adafruit code inside a new class in my oF project, but you may be able to just include the Adafruit header and copy their library to your project src directory. Hope this helps.

Hey Tim, thank you for taking the time to reply.

I was previously using ofxGPIO but ran into a problem of it not being able to drive a servo. I suppose now that I’m using the i2c servo hat I can try going back to that platform. Your advice is a bit over my head, mind if I ask for clarity on some things?

Are you referring to this library: https://github.com/adafruit/Adafruit-PWM-Servo-Driver-Library

Do you happen to have any example code from you projects that use the servo hat? I can’t seem to get anything working. Also, if you’re using the Adafruit library, what is the point of the ofxGPIO library?

Hi @prismspecs,
You can use the wiringpi lib in an oF application if you #include “wiringpi.h” in ofApp.h, and if you add the following PROJECT LINKER FLAGS to the config.make file:
PROJECT_LDFLAGS=-Wl,-rpath=./libs
PROJECT_LDFLAGS += -lwiringPi

I set up a Pi3 with Stretch created a simple project much like the one here:


The project compiled and worked well after I pulled the various pins LOW in ofApp::setup() with pullUpDownControl(). Wiringpi was installed with Stretch.

Then I downloaded and installed Reinbert’s pca9685 lib, added #include “pca9685.h” to ofApp.h, and added the following PROJECT LINKER FLAGS in the config.make file:
PROJECT_LDFLAGS += -lwiringPiPca9685

The project compiled, and pca9685Setup() returned an int. This is about as much testing as I can do without a pca9685.

OK hope this helps. Let us know if you get it working!