HCPCA9685 - Library for PCA9685 16ch 12bit PWM controller

Useful guides, libraries, and example sketches to support our Arduino based products.
johnhb
Posts: 1
Joined: Mon Nov 09, 2020 12:43 am

Re: HCPCA9685 - Library for PCA9685 16ch 12bit PWM controller

Post by johnhb » Sun Dec 20, 2020 9:32 am

I simply want to operate one sg90 servo at a time for a few seconds to move a model train point. I want to connect 11 servos via the hcpca9685 module so I can use just two Arduino pins. Is the hcpca9685 library for Arduino suitable.

andrew
Site Admin
Posts: 1374
Joined: Sun Aug 05, 2012 4:15 pm

Re: HCPCA9685 - Library for PCA9685 16ch 12bit PWM controller

Post by andrew » Mon Dec 21, 2020 9:48 am

Yes, you can independently control up to 16 servos per module. If you haven't already got an Arduino board then I would recommend using an ATMega328 based Arduino such as an Uno or Nano with this library.
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

Juan1344
Posts: 1
Joined: Sat Nov 19, 2022 5:04 am

Re: HCPCA9685 - Library for PCA9685 16ch 12bit PWM controller

Post by Juan1344 » Sat Nov 19, 2022 5:13 am

Hi i am a bit of a noob with all this but
i am trying to download the
HCPCA9685 library but it isn't working. I downloaded the file above but then what do I do?

andrew
Site Admin
Posts: 1374
Joined: Sun Aug 05, 2012 4:15 pm

Re: HCPCA9685 - Library for PCA9685 16ch 12bit PWM controller

Post by andrew » Sat Nov 19, 2022 8:46 am

downloaded the file above but then what do I do?


If you've managed to download the file correctly you should have a ZIP file named HCPCA9685.ZIP

Don't unzip the file, instead in the Arduino IDE click on the 'Sketch' menu option then select Include Library->Add .ZIP Library

In the file requester that opens up, navigate to wherever you downloaded the ZIP file and open it.

The Arduino IDE should then automatically install the library to your Documents/Arduino/Librarys folder (assuming you're using Windows).

If it did install correctly you will then find the library example sketchs under File->Examples->HCPCA9685
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

pastysmuggler
Posts: 2
Joined: Sun Apr 02, 2023 4:04 pm

Re: HCPCA9685 - Library for PCA9685 16ch 12bit PWM controller

Post by pastysmuggler » Sun Apr 02, 2023 4:14 pm

I am using 2 PCA9685s with my project and it depends upon an input (a push-button read at the beginning of the loop) as to whether the servo I want to activate is connected to either the first or second board.

Is there any way to insert a variable into the call

HCPCA9685_1.Servo(1, 250)

where the instance of the driver (either HCPCA9685_1 or HCPCA9685_2) is held in a variable 'srvDrv'. So something like this:

HCPCA9685_(srvDrv).Servo(1, 250)

I know this doesn't work - as the compiler shows an error. But it there a way to achieve this end?

I know I could use an 'if' loop but would rather not as this means duplicating code where inserting a variable would be much more efficient!

Thanks

andrew
Site Admin
Posts: 1374
Joined: Sun Aug 05, 2012 4:15 pm

Re: HCPCA9685 - Library for PCA9685 16ch 12bit PWM controller

Post by andrew » Mon Apr 03, 2023 9:14 am

Is there any way to insert a variable into the call

HCPCA9685_1.Servo(1, 250)

where the instance of the driver (either HCPCA9685_1 or HCPCA9685_2) is held in a variable 'srvDrv'. So something like this:

HCPCA9685_(srvDrv).Servo(1, 250)

I don’t believe there is any direct way to index instances of classes but you could do it using pointers. One way would be to create a pointer and then assign it to one of the instances depending on the value of srvDrv like this:

  1. #include "HCPCA9685.h"
  2.  
  3. #define  I2CAdd1 0x40
  4. #define  I2CAdd2 0x41
  5.  
  6.  
  7. HCPCA9685 HCPCA9685_1(I2CAdd1);
  8. HCPCA9685 HCPCA9685_2(I2CAdd2);
  9.  
  10. // Instance pointer
  11. HCPCA9685 *ptr;
  12.  
  13. byte srvDrv = 0;
  14.  
  15.  
  16. void setup()
  17. {
  18.   HCPCA9685_1.Init(SERVO_MODE);
  19.   HCPCA9685_1.Sleep(false);
  20.   HCPCA9685_2.Init(SERVO_MODE);
  21.   HCPCA9685_2.Sleep(false);
  22. }
  23.  
  24. void loop()
  25. {
  26.   // Assign an instance of the library depending on the value of srvD
  27.   if(srvDrv)
  28.     ptr = &HCPCA9685_2;
  29.   else
  30.     ptr = &HCPCA9685_1;
  31.  
  32.  
  33.   //You can then reference objects within that instance like this:
  34.   ptr->Servo(1, 250);
  35. }


If you want to go one step further you could create a pointer array that points to each instance then use your srvDrv variable to index the array like this:

  1. #include "HCPCA9685.h"
  2.  
  3. #define  I2CAdd1 0x40
  4. #define  I2CAdd2 0x41
  5.  
  6.  
  7. HCPCA9685 HCPCA9685_1(I2CAdd1);
  8. HCPCA9685 HCPCA9685_2(I2CAdd2);
  9.  
  10. // Create an array of pointers
  11. HCPCA9685 *ptr[2];
  12.  
  13. byte srvDrv = 0;
  14.  
  15.  
  16. void setup()
  17. {
  18.   HCPCA9685_1.Init(SERVO_MODE);
  19.   HCPCA9685_1.Sleep(false);
  20.   HCPCA9685_2.Init(SERVO_MODE);
  21.   HCPCA9685_2.Sleep(false);
  22.  
  23.   // Assign the instances of the library to the pointer array
  24.   ptr[0] = &HCPCA9685_1;
  25.   ptr[1] = &HCPCA9685_2;
  26. }
  27.  
  28. void loop()
  29. {
  30.   //You can then reference objects within that array where:
  31.   // srvDrv = 0 would be HCPCA9685_1 &
  32.   // srvDrv = 1 would be HCPCA9685_2
  33.   ptr[srvDrv]->Servo(1, 250);
  34. }

The above sketches are untested but should hopefully work
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

pastysmuggler
Posts: 2
Joined: Sun Apr 02, 2023 4:04 pm

Re: HCPCA9685 - Library for PCA9685 16ch 12bit PWM controller

Post by pastysmuggler » Mon Apr 03, 2023 3:32 pm

Absolutely brilliant - thank you very much indeed. The array pointer looks the better option to me and is similar to the way in which I did this using the <Servo.h> library when driving the servos directly from the Arduino for an earlier project.

tillytony
Posts: 1
Joined: Mon Jul 24, 2023 8:51 pm

Re: HCPCA9685 - Library for PCA9685 16ch 12bit PWM controller

Post by tillytony » Mon Jul 24, 2023 8:53 pm

I am using your library on a couple projects using unos and megas. But im starting to need more GPIO and cpu speed. Will this library work on a DUE? MAny thanks.

andrew
Site Admin
Posts: 1374
Joined: Sun Aug 05, 2012 4:15 pm

Re: HCPCA9685 - Library for PCA9685 16ch 12bit PWM controller

Post by andrew » Tue Jul 25, 2023 7:57 am

Will this library work on a DUE?
I can't remember if it was ever tested with the Due but it compiles ok and it just uses the standard Arduino wire library to communicate with the controller so I don't see any reasons for it not to work.

If you do have any issues just let me know and I'll fix it.
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

elwebman
Posts: 1
Joined: Fri Nov 03, 2023 11:29 am

Re: HCPCA9685 - Library for PCA9685 16ch 12bit PWM controller

Post by elwebman » Sat Nov 04, 2023 6:20 am

Thanks for sharing the fix. I did download the cpp file and it works like a charm. For me it started to give problem when using a Arduino Nanno 33 BLE Sense. With your fix no longer compiler issues and the code works perfectly!

Post Reply

Return to “Arduino”