HCPCA9685 - Library for PCA9685 16ch 12bit PWM controller

Useful guides, libraries, and example sketches to support our Arduino based products.
admin
Site Admin
Posts: 865
Joined: Sun Aug 05, 2012 4:02 pm

HCPCA9685 - Library for PCA9685 16ch 12bit PWM controller

Post by admin » Mon Jun 13, 2016 3:49 pm

Image




Arduino Library for the PCA9685 16 Channel 12-bit PWM Servo Motor Driver.
Currently supported products:

16 Channel 12-bit PWM Servo Motor Driver Module (HCMODU0097) available from hobbycomponents.com

This library can be used to configure and control the NXP semiconductors PCA9685 16 channel 12 bit PWM controller. Additionally the library can initialised into a 'servo mode' which configures the device to produce PWM outputs that are compatible with those required to control a standard servo(s). Each servos position can then be controlled with just one library command.


You will need to download (please log in to download the library) and unzip this library to the Arduino development environments library area.

On Windows:
My Documents\Arduino\libraries\

On Mac:
Documents/Arduino/libraries/

Linux:
Usually found within the users home area under /Arduino/libraries/



Using the HCPCA9685 library

To use the library just include the HCPCA9685.h header file and then create an instance of the library. E.g:

Code: Select all

#include "HCPCA9685.h"
HCPCA9685 HCPCA9685(I2CAdd);

Where I2CAdd is the I2C slave address of the device. The PCA9685, and our module via jumper pads, can be set to one of 62 different slave addresses. For the HCMODU0097 the default address is 0x40.


To initialise the library add the following line to the setup() section of your sketch:

Code: Select all

HCPCA9685.Init();
At power up the device will be in sleep mode and so for the PWM output drivers to be on the device should be woken up using the following function:

Code: Select all

HCPCA9685.Sleep(false);


The following functions are available with this library:

Code: Select all

HCPCA9685.Init(Mode);
Initialises the I2C interface and puts the device in to a default state where:

Mode is the operating mode of the library. Passing no parameter, or passing the value DEFAULT_MODE will initialise the device into a default mode of operation. Use this mode if you want to use the device as a general purpose PWM controller.

Passing SERVO_MODE for the Mode parameter will initialise the library to produce PWM signals that are compatible with driving standard servo(s). Use this mode if you wish to use the device for driving servos.


Code: Select all

HCPCA9685.Sleep(Mode);
Sets the sleep state of the device where:

Mode is the required state. Valid values are:
true puts the device to sleep)
false brings the device out of sleep mode)

If the device is woken up from a previously set sleep state then this function will wait 500us for the oscillator to stabilise.


Code: Select all

HCPCA9685.SetPeriodFreq(Freq);
Sets the period frequency (prescaller) in Hz where:

Freq is the required frequency of on cycle. Valid values are:
24 (24Hz) to 1526 (1.526KHz)

These values are based on the devices internal 25MHz oscillator (default). If using an external clock you will need to adjust the value for OSC_FREQ found in the HCPCA9685.h header file.


Code: Select all

HCPCA9685.SetPreScaller(Period);


Sets the value for the prescaller where:

Period is the required period for one cycle. Valid values are:
0x03 to 0xFF

The prescaller value can be determined with the following formula: prescale value = (25MHz / (4096 * period frequency in Hz)) - 1


Code: Select all

HCPCA9685.Servo(Chan, Pos);
When the library has been initiliased to servo mode this function sets the ON time for the servo where:

Chan is the PWM output pin to change. Valid values are
0 (PWM output pin 0) to 15 (PWM output pin 15)

Pos is the required position of the servo. Valid values are
0 (servo fully anti-clockwise) to 480 (servo fully clockwise)

The maximum value is based in the default servo SERVO_TRIM_MAX and SERVO_TRIM_MIN trim settings found in the HCPCA9685.h header file. These default to 590 for SERVO_TRIM_MAX and 110 for SERVO_TRIM_MIN. They can be adjusted to match the minimum and maximum end-stop positions for your servo(s). If these values are changed then the new max value for Pos can be calculated as (SERVO_TRIM_MAX - SERVO_TRIM_MIN).


Code: Select all

HCPCA9685.Output(On_Time, Off_Time);
Sets the turn ON time and OFF time for ALL of the 15 PWM outputs where:

On_Time is the point at which to turn the outputs ON within the cycle period. Valid values are
0 to 4095 (decimal)

Off_Time is the point at which to turn the outputs OFF within the cycle period. Valid values are
0 to 4095 (decimal)



Code: Select all

HCPCA9685.Output(Chan, On_Time, Off_Time);
Sets the turn ON time and OFF time for one of the 15 PWM outputs where:

Chan is the PWM output pin to change. Valid values are
0 (PWM output pin 0) to 15 (PWM output pin 15)

On_Time is the point at which to turn the output ON within the cycle period. Valid values are
0 to 4095 (decimal)

Off_Time is the point at which to turn the output OFF within the cycle period. Valid values are
0 to 4095 (decimal)


Code: Select all

HCPCA9685.OutputOnTime(Chan, Time);
Sets the turn ON time for one of the 15 PWM outputs where:

Chan is the PWM output pin to change. Valid values are
0 (PWM output pin 0) to 15 (PWM output pin 15)

Time is the point at which to turn the output on within the cycle period. Valid values are
0 to 4095 (decimal)


Code: Select all

HCPCA9685.OutputOffTime(Chan, Time);

Sets the turn OFF time for one of the 15 PWM outputs where:

Chan is the PWM output channel pin to change. Valid values are
0 (PWM output pin 0) to 15 (PWM output pin 15)

Time is the point at which to turn the output off within the cycle period. Valid values are
0 to 4095 (decimal)


Code: Select all

HCPCA9685.OutputNotEnableState(State);
Sets the state of the 16 PWM outputs when the OE pin is driven high where:

State is the required state of the output pins. Valid values are:
OUTNE_LOW Output pins are pulled low when OE = 1
OUTNE_HIGH Output pins are pulled high when OE = 1
OUTNE_HIGH_IMPEDANCE Output pins are high-impedance when OE = 1


Code: Select all

HCPCA9685.OutputDrivers(Mode);
Sets the driver type for the PWM outputs where:

Mode is the required type. Valid values are:
OUTDRV_OPEN_DRAIN The 16 PWM outputs are configured with an open-drain structure.
OUTDRV_TOTEM_POLE The 16 PWM outputs are configured with a totem pole structure.


Code: Select all

HCPCA9685.OCH(Mode);
Sets the point at which the 16 PWM outputs change state where:

Mode is the required state. Valid values are:
OCH_STOP Outputs change on I2C STOP command.
OCH_ACK Outputs change on I2C ACK.


Code: Select all

HCPCA9685.Invert(Mode);
Inverts the state of the PWM output pins where:

Mode is the require state. Valid values are:
false The PWM output pins are not inverted.
true The PWM output pins are inverted.


Code: Select all

HCPCA9685.Enable_Sub1(Mode);
Sets whether or not the device will respond the I2C sub address 1 where:

Mode sets the required state. Valid values are:
false The device will not respond to I2C sub address 1.
true The device will respond to I2C sub address 1.


Code: Select all

HCPCA9685.Enable_Sub2(Mode);
Sets whether or not the device will respond the I2C sub address 2 where:

Mode sets the required state. Valid values are:
false The device will not respond to I2C sub address 2.
true The device will respond to I2C sub address 2.


Code: Select all

HCPCA9685.Enable_Sub3(Mode);
Sets whether or not the device will respond the I2C sub address 1 where:

Mode sets the required state. Valid values are:
false The device will not respond to I2C sub address 3.
true The device will respond to I2C sub address 3.


Code: Select all

HCPCA9685.Enable_AllCall(Mode);
Sets whether or not the device will respond the I2C ALLCALL address where:

Mode sets the required state. Valid values are:
false The device will not respond to I2C ALLCALL address.
true The device will respond to I2C ALLCALL address.


Code: Select all

HCPCA9685.SetSubAddress(SubAddress, Address);
Sets the I2C address for one of the 3 sub addresses where:

SubAddress is one of the 3 sub addresses to set the I2C address for. Valid values are:
SUBADR1 Selects sub address 1.
SUBADR2 Selects sub address 2.
SUBADR3 Selects sub address 3.

Address is and 8 bit byte representing the I2C address to set the selected sub address too. Only the 7 LSBs representing the I2C-bus sub address are valid.


Code: Select all

HCPCA9685.SetAllCallAddress(Address);
Sets the I2C address for the ALLCALL address where:

Address is and 8 bit byte representing the I2C address to set the ALLCALL address too. Only the 7 LSBs representing the I2C-bus ALLCALL address are valid.


Code: Select all

HCPCA9685.ExtClk();

Sets external clock mode allowing the device to be driven by an external clock source. When in external clock mode, it can only be cleared by recycling the power.


Code: Select all

HCPCA9685.I2CWriteReg(Register, Data);
Writes a byte of data to one of the devices registers where:

Register is the address of the register to write to.

Data is the 8 bit data to write to the register.



Code: Select all

byte Data = HCPCA9685.I2CReadReg(Register);
Reads one of the devices registers where:

Register is the address of the register to read from.

Returns an 8 bit byte containing the contents of the specified register.




Image

  1. /* FILE:    HCPCA9685_Servo_Example
  2.    DATE:    10/06/16
  3.    VERSION: 0.1
  4.    AUTHOR:  Andrew Davies
  5.  
  6.    Sketch created by Hobby Components Ltd (HOBBYCOMPONENTS.COM)
  7.    
  8. 10/06/16 version 0.1: Original version
  9.  
  10.  
  11. This example demonstrates how to use the HCPCA9685 library together with the PCA9685
  12. to control up to 16 servos. The sketch will initialise the library putting it into
  13. 'servo mode' and then will continuously sweep one servo connected to PWM output 0
  14. back and forth. The example has been written particularly for the 16 Channel 12-bit
  15. PWM Servo Motor Driver Module (HCMODU0097) available from hobbycomponents.com
  16.  
  17. To use the module connect it to your Arduino as follows:
  18.  
  19. PCA9685...........Uno/Nano
  20. GND...............GND
  21. OE................N/A
  22. SCL...............A5
  23. SDA...............A4
  24. VCC...............5V
  25.  
  26. External 5V Power for the servo(s) can be supplied by the V+ and GND input of the
  27. screw terminal block.
  28.  
  29. PLEASE NOTE: Depending on your servo it is possible for this sketch to attempt
  30. drive the servo beyond its end stops. If your servo is hitting its end stops then
  31. you should adjust the the min and max values in this sketch.
  32.  
  33. You may copy, alter and reuse this code in any way you like, but please leave
  34. reference to HobbyComponents.com in your comments if you redistribute this code.
  35. This software may not be used directly for the purpose of selling products that
  36. directly compete with Hobby Components Ltd's own range of products.
  37.  
  38. THIS SOFTWARE IS PROVIDED "AS IS". HOBBY COMPONENTS MAKES NO WARRANTIES, WHETHER
  39. EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
  40. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ACCURACY OR LACK OF NEGLIGENCE.
  41. HOBBY COMPONENTS SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR ANY DAMAGES,
  42. INCLUDING, BUT NOT LIMITED TO, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY
  43. REASON WHATSOEVER.
  44. */
  45.  
  46.  
  47. /* Include the HCPCA9685 library */
  48. #include "HCPCA9685.h"
  49.  
  50. /* I2C slave address for the device/module. For the HCMODU0097 the default I2C address
  51.    is 0x40 */
  52. #define  I2CAdd 0x40
  53.  
  54.  
  55. /* Create an instance of the library */
  56. HCPCA9685 HCPCA9685(I2CAdd);
  57.  
  58.  
  59. void setup()
  60. {
  61.   /* Initialise the library and set it to 'servo mode' */
  62.   HCPCA9685.Init(SERVO_MODE);
  63.  
  64.   /* Wake the device up */
  65.   HCPCA9685.Sleep(false);
  66. }
  67.  
  68.  
  69. void loop()
  70. {
  71.   unsigned int Pos;
  72.  
  73.   /* Sweep the servo back and forth from its minimum to maximum position.
  74.      If your servo is hitting its end stops then you  should adjust the
  75.      values so that the servo can sweep though its full range without hitting
  76.      the end stops. You can adjust the min & max positions by altering
  77.      the trim values in the libraries HCPCA9685.h file*/
  78.   for(Pos = 10; Pos < 450; Pos++)
  79.   {
  80.     /* This function sets the servos position. It takes two parameters,
  81.      * the first is the servo to control, and the second is the servo
  82.      * position. */
  83.     HCPCA9685.Servo(0, Pos);
  84.     delay(10);
  85.   }
  86.  
  87.   for(Pos = 450; Pos >= 10; Pos--)
  88.   {
  89.     HCPCA9685.Servo(0, Pos);
  90.     delay(10);
  91.   }
  92. }



Image
HCPCA9685.zip
You do not have the required permissions to view the files attached to this post.

nifty1a
Posts: 1
Joined: Sun Jan 21, 2018 1:06 am

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

Post by nifty1a » Sun Jan 21, 2018 1:10 am

Aren't the descriptions and the examples of the two function transposed?





Code:
HCPCA9685.Output(On_Time, Off_Time);




Sets the turn ON time and OFF time for one of the 15 PWM outputs where:

Chan is the PWM output pin to change. Valid values are
0 (PWM output pin 0) to 15 (PWM output pin 15)

On_Time is the point at which to turn the output ON within the cycle period. Valid values are
0 to 4095 (decimal)

Off_Time is the point at which to turn the output OFF within the cycle period. Valid values are
0 to 4095 (decimal)





Code:
HCPCA9685.Output(Chan, On_Time, Off_Time);



Sets the turn ON time and OFF time for ALL of the 15 PWM outputs where:

On_Time is the point at which to turn the outputs ON within the cycle period. Valid values are
0 to 4095 (decimal)

Off_Time is the point at which to turn the outputs OFF within the cycle period. Valid values are
0 to 4095 (decimal)

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

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

Post by andrew » Mon Jan 22, 2018 9:42 am

I've now corrected it - thanks for point it out.
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

1085243
Posts: 2
Joined: Tue Nov 20, 2018 6:44 pm

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

Post by 1085243 » Tue Nov 20, 2018 6:58 pm

Won't compile and gives me an error about wire.h being ambiguous. I recalled the AdaFruit PCA9685 servo demos using a "wire.h," but I'm not sure how to correct the problem. Seems like some dependency conflicts or something, if I'm not mistaken... "WireBase.h" is called and is having some sort of unit conversion errors, I guess.

EDIT: Okay, I'm seeing where the problem is coming from; I'm using the Blue Pill STM32F103CB instead of the Arduino (NOT PURCHASED FROM HC), so this library has been designed to only work with the standard Arduino libraries; the IDE is linking to all the "Arduino_STM32" versions of Wire.H and the like, and that's causing the compiler to call foul.

Code: Select all

Arduino: 1.8.6 (Windows 10), Board: "Generic STM32F103C series, STM32F103CB (20k RAM. 128k Flash), Serial, 72Mhz (Normal), Fastest (-O3)"

C:\Users\Dude\Documents\Arduino\libraries\HCPCA9685\HCPCA9685.cpp: In member function 'void HCPCA9685::Output(byte, unsigned int, unsigned int)':

C:\Users\Dude\Documents\Arduino\libraries\HCPCA9685\HCPCA9685.cpp:163:28: error: call of overloaded 'write(unsigned int)' is ambiguous

   Wire.write(On_Time & 0xFF); 

                            ^

C:\Users\Dude\Documents\Arduino\libraries\HCPCA9685\HCPCA9685.cpp:163:28: note: candidates are:

In file included from C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire/Wire.h:42:0,

                 from C:\Users\Dude\Documents\Arduino\libraries\HCPCA9685\HCPCA9685.cpp:32:

C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire/utility/WireBase.h:111:12: note: size_t WireBase::write(uint8)

     size_t write(uint8);

            ^

C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire/utility/WireBase.h:121:12: note: size_t WireBase::write(int)

     size_t write(int);

            ^

C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire/utility/WireBase.h:131:10: note: void WireBase::write(char*) <near match>

     void write(char*);

          ^

C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire/utility/WireBase.h:131:10: note:   no known conversion for argument 1 from 'unsigned int' to 'char*'

C:\Users\Dude\Documents\Arduino\libraries\HCPCA9685\HCPCA9685.cpp:164:26: error: call of overloaded 'write(unsigned int)' is ambiguous

   Wire.write(On_Time >> 8);

                          ^

C:\Users\Dude\Documents\Arduino\libraries\HCPCA9685\HCPCA9685.cpp:164:26: note: candidates are:

In file included from C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire/Wire.h:42:0,

                 from C:\Users\Dude\Documents\Arduino\libraries\HCPCA9685\HCPCA9685.cpp:32:

C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire/utility/WireBase.h:111:12: note: size_t WireBase::write(uint8)

     size_t write(uint8);

            ^

C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire/utility/WireBase.h:121:12: note: size_t WireBase::write(int)

     size_t write(int);

            ^

C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire/utility/WireBase.h:131:10: note: void WireBase::write(char*) <near match>

     void write(char*);

          ^

C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire/utility/WireBase.h:131:10: note:   no known conversion for argument 1 from 'unsigned int' to 'char*'

C:\Users\Dude\Documents\Arduino\libraries\HCPCA9685\HCPCA9685.cpp:165:29: error: call of overloaded 'write(unsigned int)' is ambiguous

   Wire.write(Off_Time & 0xFF); 

                             ^

C:\Users\Dude\Documents\Arduino\libraries\HCPCA9685\HCPCA9685.cpp:165:29: note: candidates are:

In file included from C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire/Wire.h:42:0,

                 from C:\Users\Dude\Documents\Arduino\libraries\HCPCA9685\HCPCA9685.cpp:32:

C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire/utility/WireBase.h:111:12: note: size_t WireBase::write(uint8)

     size_t write(uint8);

            ^

C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire/utility/WireBase.h:121:12: note: size_t WireBase::write(int)

     size_t write(int);

            ^

C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire/utility/WireBase.h:131:10: note: void WireBase::write(char*) <near match>

     void write(char*);

          ^

C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire/utility/WireBase.h:131:10: note:   no known conversion for argument 1 from 'unsigned int' to 'char*'

C:\Users\Dude\Documents\Arduino\libraries\HCPCA9685\HCPCA9685.cpp:166:27: error: call of overloaded 'write(unsigned int)' is ambiguous

   Wire.write(Off_Time >> 8);

                           ^

C:\Users\Dude\Documents\Arduino\libraries\HCPCA9685\HCPCA9685.cpp:166:27: note: candidates are:

In file included from C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire/Wire.h:42:0,

                 from C:\Users\Dude\Documents\Arduino\libraries\HCPCA9685\HCPCA9685.cpp:32:

C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire/utility/WireBase.h:111:12: note: size_t WireBase::write(uint8)

     size_t write(uint8);

            ^

C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire/utility/WireBase.h:121:12: note: size_t WireBase::write(int)

     size_t write(int);

            ^

C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire/utility/WireBase.h:131:10: note: void WireBase::write(char*) <near match>

     void write(char*);

          ^

C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire/utility/WireBase.h:131:10: note:   no known conversion for argument 1 from 'unsigned int' to 'char*'

C:\Users\Dude\Documents\Arduino\libraries\HCPCA9685\HCPCA9685.cpp: In member function 'void HCPCA9685::Output(unsigned int, unsigned int)':

C:\Users\Dude\Documents\Arduino\libraries\HCPCA9685\HCPCA9685.cpp:184:28: error: call of overloaded 'write(unsigned int)' is ambiguous

   Wire.write(On_Time & 0xFF); 

                            ^

C:\Users\Dude\Documents\Arduino\libraries\HCPCA9685\HCPCA9685.cpp:184:28: note: candidates are:

In file included from C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire/Wire.h:42:0,

                 from C:\Users\Dude\Documents\Arduino\libraries\HCPCA9685\HCPCA9685.cpp:32:

C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire/utility/WireBase.h:111:12: note: size_t WireBase::write(uint8)

     size_t write(uint8);

            ^

C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire/utility/WireBase.h:121:12: note: size_t WireBase::write(int)

     size_t write(int);

            ^

C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire/utility/WireBase.h:131:10: note: void WireBase::write(char*) <near match>

     void write(char*);

          ^

C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire/utility/WireBase.h:131:10: note:   no known conversion for argument 1 from 'unsigned int' to 'char*'

C:\Users\Dude\Documents\Arduino\libraries\HCPCA9685\HCPCA9685.cpp:185:26: error: call of overloaded 'write(unsigned int)' is ambiguous

   Wire.write(On_Time >> 8);

                          ^

C:\Users\Dude\Documents\Arduino\libraries\HCPCA9685\HCPCA9685.cpp:185:26: note: candidates are:

In file included from C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire/Wire.h:42:0,

                 from C:\Users\Dude\Documents\Arduino\libraries\HCPCA9685\HCPCA9685.cpp:32:

C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire/utility/WireBase.h:111:12: note: size_t WireBase::write(uint8)

     size_t write(uint8);

            ^

C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire/utility/WireBase.h:121:12: note: size_t WireBase::write(int)

     size_t write(int);

            ^

C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire/utility/WireBase.h:131:10: note: void WireBase::write(char*) <near match>

     void write(char*);

          ^

C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire/utility/WireBase.h:131:10: note:   no known conversion for argument 1 from 'unsigned int' to 'char*'

C:\Users\Dude\Documents\Arduino\libraries\HCPCA9685\HCPCA9685.cpp:186:29: error: call of overloaded 'write(unsigned int)' is ambiguous

   Wire.write(Off_Time & 0xFF); 

                             ^

C:\Users\Dude\Documents\Arduino\libraries\HCPCA9685\HCPCA9685.cpp:186:29: note: candidates are:

In file included from C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire/Wire.h:42:0,

                 from C:\Users\Dude\Documents\Arduino\libraries\HCPCA9685\HCPCA9685.cpp:32:

C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire/utility/WireBase.h:111:12: note: size_t WireBase::write(uint8)

     size_t write(uint8);

            ^

C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire/utility/WireBase.h:121:12: note: size_t WireBase::write(int)

     size_t write(int);

            ^

C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire/utility/WireBase.h:131:10: note: void WireBase::write(char*) <near match>

     void write(char*);

          ^

C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire/utility/WireBase.h:131:10: note:   no known conversion for argument 1 from 'unsigned int' to 'char*'

C:\Users\Dude\Documents\Arduino\libraries\HCPCA9685\HCPCA9685.cpp:187:27: error: call of overloaded 'write(unsigned int)' is ambiguous

   Wire.write(Off_Time >> 8);

                           ^

C:\Users\Dude\Documents\Arduino\libraries\HCPCA9685\HCPCA9685.cpp:187:27: note: candidates are:

In file included from C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire/Wire.h:42:0,

                 from C:\Users\Dude\Documents\Arduino\libraries\HCPCA9685\HCPCA9685.cpp:32:

C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire/utility/WireBase.h:111:12: note: size_t WireBase::write(uint8)

     size_t write(uint8);

            ^

C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire/utility/WireBase.h:121:12: note: size_t WireBase::write(int)

     size_t write(int);

            ^

C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire/utility/WireBase.h:131:10: note: void WireBase::write(char*) <near match>

     void write(char*);

          ^

C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire/utility/WireBase.h:131:10: note:   no known conversion for argument 1 from 'unsigned int' to 'char*'

C:\Users\Dude\Documents\Arduino\libraries\HCPCA9685\HCPCA9685.cpp: In member function 'void HCPCA9685::OutputOnTime(byte, unsigned int)':

C:\Users\Dude\Documents\Arduino\libraries\HCPCA9685\HCPCA9685.cpp:209:25: error: call of overloaded 'write(unsigned int)' is ambiguous

   Wire.write(Time & 0xFF); 

                         ^

C:\Users\Dude\Documents\Arduino\libraries\HCPCA9685\HCPCA9685.cpp:209:25: note: candidates are:

In file included from C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire/Wire.h:42:0,

                 from C:\Users\Dude\Documents\Arduino\libraries\HCPCA9685\HCPCA9685.cpp:32:

C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire/utility/WireBase.h:111:12: note: size_t WireBase::write(uint8)

     size_t write(uint8);

            ^

C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire/utility/WireBase.h:121:12: note: size_t WireBase::write(int)

     size_t write(int);

            ^

C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire/utility/WireBase.h:131:10: note: void WireBase::write(char*) <near match>

     void write(char*);

          ^

C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire/utility/WireBase.h:131:10: note:   no known conversion for argument 1 from 'unsigned int' to 'char*'

C:\Users\Dude\Documents\Arduino\libraries\HCPCA9685\HCPCA9685.cpp:210:23: error: call of overloaded 'write(unsigned int)' is ambiguous

   Wire.write(Time >> 8);

                       ^

C:\Users\Dude\Documents\Arduino\libraries\HCPCA9685\HCPCA9685.cpp:210:23: note: candidates are:

In file included from C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire/Wire.h:42:0,

                 from C:\Users\Dude\Documents\Arduino\libraries\HCPCA9685\HCPCA9685.cpp:32:

C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire/utility/WireBase.h:111:12: note: size_t WireBase::write(uint8)

     size_t write(uint8);

            ^

C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire/utility/WireBase.h:121:12: note: size_t WireBase::write(int)

     size_t write(int);

            ^

C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire/utility/WireBase.h:131:10: note: void WireBase::write(char*) <near match>

     void write(char*);

          ^

C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire/utility/WireBase.h:131:10: note:   no known conversion for argument 1 from 'unsigned int' to 'char*'

C:\Users\Dude\Documents\Arduino\libraries\HCPCA9685\HCPCA9685.cpp: In member function 'void HCPCA9685::OutputOffTime(byte, unsigned int)':

C:\Users\Dude\Documents\Arduino\libraries\HCPCA9685\HCPCA9685.cpp:232:25: error: call of overloaded 'write(unsigned int)' is ambiguous

   Wire.write(Time & 0xFF); 

                         ^

C:\Users\Dude\Documents\Arduino\libraries\HCPCA9685\HCPCA9685.cpp:232:25: note: candidates are:

In file included from C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire/Wire.h:42:0,

                 from C:\Users\Dude\Documents\Arduino\libraries\HCPCA9685\HCPCA9685.cpp:32:

C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire/utility/WireBase.h:111:12: note: size_t WireBase::write(uint8)

     size_t write(uint8);

            ^

C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire/utility/WireBase.h:121:12: note: size_t WireBase::write(int)

     size_t write(int);

            ^

C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire/utility/WireBase.h:131:10: note: void WireBase::write(char*) <near match>

     void write(char*);

          ^

C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire/utility/WireBase.h:131:10: note:   no known conversion for argument 1 from 'unsigned int' to 'char*'

C:\Users\Dude\Documents\Arduino\libraries\HCPCA9685\HCPCA9685.cpp:233:23: error: call of overloaded 'write(unsigned int)' is ambiguous

   Wire.write(Time >> 8);

                       ^

C:\Users\Dude\Documents\Arduino\libraries\HCPCA9685\HCPCA9685.cpp:233:23: note: candidates are:

In file included from C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire/Wire.h:42:0,

                 from C:\Users\Dude\Documents\Arduino\libraries\HCPCA9685\HCPCA9685.cpp:32:

C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire/utility/WireBase.h:111:12: note: size_t WireBase::write(uint8)

     size_t write(uint8);

            ^

C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire/utility/WireBase.h:121:12: note: size_t WireBase::write(int)

     size_t write(int);

            ^

C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire/utility/WireBase.h:131:10: note: void WireBase::write(char*) <near match>

     void write(char*);

          ^

C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire/utility/WireBase.h:131:10: note:   no known conversion for argument 1 from 'unsigned int' to 'char*'

Multiple libraries were found for "Wire.h"
 Used: C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\Wire
 Not used: C:\Program Files (x86)\Arduino\hardware\Arduino_STM32-master\STM32F1\libraries\WireSlave
exit status 1
Error compiling for board Generic STM32F103C series.

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
Last edited by 1085243 on Tue Nov 20, 2018 7:21 pm, edited 2 times in total.

1085243
Posts: 2
Joined: Tue Nov 20, 2018 6:44 pm

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

Post by 1085243 » Tue Nov 20, 2018 7:18 pm

Is there anyway to use HCPCA9685 with STM32's, like the Blue Pill?

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 » Wed Nov 21, 2018 9:48 am

I've fixed the errors but the library wasn't written for the STM32 so there's no guarantee that it will work. Can you download the attached file (HCPCA9685.cpp) and replace it with the one in your library folder. If you're using Windows it should be located here:

C:\Users\<username>\Documents\Arduino\libraries\HCPCA9685\

If it works could you please let me know and I'll update the library.

HCPCA9685.cpp
You do not have the required permissions to view the files attached to this post.
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

tamtheram
Posts: 1
Joined: Tue Sep 10, 2019 6:58 pm

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

Post by tamtheram » Sat Sep 14, 2019 8:17 pm

I am a newbie to this but I don't see how I can add a second module on the I2C bus how
is the object named? This is done on Adafruit like this:
Adafruit_PWMServoDriver pwm1 = Adafruit_PWMServoDriver(0x40);
Adafruit_PWMServoDriver pwm2 = Adafruit_PWMServoDriver(0x41);
and used thus:
pwm1.begin();
pwm1.setPWMFreq(1600); // This is the maximum PWM frequency
pwm2.begin();
pwm2.setPWMFreq(1600); // This is the maximum PWM frequency
but I can't figure out how to do it with your library.
Tom

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 » Sun Sep 15, 2019 7:29 am

You can do something similar with the HCPCA9685. What you're actually doing is making two copies (instances) of the library, giving each instance a different name, and then assigning one to the I2C address of the first module and the other to the I2C address of the second module. Here is an example where I've created two instances of the library and called them HCPCA9685_1 and HCPCA9685_2:

  1. // Include the HCPCA9685 library
  2. #include "HCPCA9685.h"
  3.  
  4. // Define the I2C addresses for each module
  5. #define  I2CAdd_1 0x40
  6. #define  I2CAdd_2 0x41
  7.  
  8. // Create 2 instances of the library, one for each module
  9. HCPCA9685 HCPCA9685_1(I2CAdd_1);
  10. HCPCA9685 HCPCA9685_2(I2CAdd_2);
  11.  
  12. void setup()
  13. {
  14.   // Initialise both modules
  15.   HCPCA9685_1.Init(SERVO_MODE);
  16.   HCPCA9685_2.Init(SERVO_MODE);
  17.  
  18.   // Wake both devices up
  19.   HCPCA9685_1.Sleep(false);
  20.   HCPCA9685_2.Sleep(false);
  21.  
  22.  
  23.   // Set servo 0 on module 1 to position 200
  24.   HCPCA9685_1.Servo(0, 200);
  25.  
  26.   // Set servo 3 on module 2 to position 250
  27.   HCPCA9685_2.Servo(3, 250);
  28. }
  29.  
  30. void loop()
  31. {
  32. }
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

YYM01
Posts: 1
Joined: Sun Sep 13, 2020 11:02 am

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

Post by YYM01 » Sun Sep 13, 2020 11:12 am

I was trying to adapt this library to work on the Arduino mega 2560, but with no luck. Is there a method of defining it to the mega?

Issac

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 Sep 14, 2020 1:31 pm

The library will work fine with the Mega but the I2C pins (SDA & SCL) are on different pins. You can connect it like this:


PCA9685...........Mega
GND.....................GND
OE.........................N/A
SCL......................21
SDA.....................20
VCC....................5V

You don't need to change anything in the example sketch.
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

Post Reply

Return to “Arduino”