HCPCA9685 - Library for PCA9685 16ch 12bit PWM controller
Re: HCPCA9685 - Library for PCA9685 16ch 12bit PWM controller
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.
Re: HCPCA9685 - Library for PCA9685 16ch 12bit PWM controller
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.
Re: HCPCA9685 - Library for PCA9685 16ch 12bit PWM controller
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?
i am trying to download the
HCPCA9685 library but it isn't working. I downloaded the file above but then what do I do?
Re: HCPCA9685 - Library for PCA9685 16ch 12bit PWM controller
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.
-
- Posts: 2
- Joined: Sun Apr 02, 2023 4:04 pm
Re: HCPCA9685 - Library for PCA9685 16ch 12bit PWM controller
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
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
Re: HCPCA9685 - Library for PCA9685 16ch 12bit PWM controller
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:
- #include "HCPCA9685.h"
- #define I2CAdd1 0x40
- #define I2CAdd2 0x41
- HCPCA9685 HCPCA9685_1(I2CAdd1);
- HCPCA9685 HCPCA9685_2(I2CAdd2);
- // Instance pointer
- HCPCA9685 *ptr;
- byte srvDrv = 0;
- void setup()
- {
- HCPCA9685_1.Init(SERVO_MODE);
- HCPCA9685_1.Sleep(false);
- HCPCA9685_2.Init(SERVO_MODE);
- HCPCA9685_2.Sleep(false);
- }
- void loop()
- {
- // Assign an instance of the library depending on the value of srvD
- if(srvDrv)
- ptr = &HCPCA9685_2;
- else
- ptr = &HCPCA9685_1;
- //You can then reference objects within that instance like this:
- ptr->Servo(1, 250);
- }
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:
- #include "HCPCA9685.h"
- #define I2CAdd1 0x40
- #define I2CAdd2 0x41
- HCPCA9685 HCPCA9685_1(I2CAdd1);
- HCPCA9685 HCPCA9685_2(I2CAdd2);
- // Create an array of pointers
- HCPCA9685 *ptr[2];
- byte srvDrv = 0;
- void setup()
- {
- HCPCA9685_1.Init(SERVO_MODE);
- HCPCA9685_1.Sleep(false);
- HCPCA9685_2.Init(SERVO_MODE);
- HCPCA9685_2.Sleep(false);
- // Assign the instances of the library to the pointer array
- ptr[0] = &HCPCA9685_1;
- ptr[1] = &HCPCA9685_2;
- }
- void loop()
- {
- //You can then reference objects within that array where:
- // srvDrv = 0 would be HCPCA9685_1 &
- // srvDrv = 1 would be HCPCA9685_2
- ptr[srvDrv]->Servo(1, 250);
- }
The above sketches are untested but should hopefully work
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.
-
- Posts: 2
- Joined: Sun Apr 02, 2023 4:04 pm
Re: HCPCA9685 - Library for PCA9685 16ch 12bit PWM controller
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.
Re: HCPCA9685 - Library for PCA9685 16ch 12bit PWM controller
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.
Re: HCPCA9685 - Library for PCA9685 16ch 12bit PWM controller
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.Will this library work on a DUE?
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.
Re: HCPCA9685 - Library for PCA9685 16ch 12bit PWM controller
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!