My code doesnt work

hi
i have a problem with arduino uno and hcpca9685. i upload my code and it is done succesfully but my servo doesnt work. can you help me? why the servo motor doesnt work?

Wiring? Power issue?
Please provide a schematic / wiring diagram with all connections including every single power and ground. Please provide details about which servo, which power supply, which Arduino etc.

Code?
Please provide your code; don't forget to use code tags as described in How to get the best out of this forum when posting your code.

2 Likes

How are you powering the Arduino? How are you powering the servo?
The servo needs to be powered externally (+5V, GND), being sure to tie the grounds together with the Arduino (D pin to servo signal, ground).

Hi @alij22,

welcome to the arduino-forum.

If your HCPCA9685 looks different you will have to post a picture and a datasheet of your HCPCA9685 device.

The microcontroller-world is not superstandardised like USB-devices.
You have to take care of more details than just

"does the plug fit into the socket?"

That is the reason why you need to post all the detail-information.

I'm pretty sure that you agree and will follow the way how to solve your problem mimimum 200 minutes faster.
This requires to invest 20 minutes of your precious time to read how to speedup solving your problems.

Directly after registering you got presented informations how to speed up solving your problem.
You should really read it.

best regards Stefan

here's an article using servos with the PCS9685 for model RRing skip to pg 16 of the article so see code.





http://www.catb.org/~esr/faqs/smart-questions.html

Try really, really hard to read one of those images of text. Now imagine doing that on a smartphone, which many are using when helping on this forum.
Now, repeat after me, "I will never, ever, ever, post photos of code or error messages again. I solemnly promise. May the moderators ban me for life if I do".

Get it? Don't do it.
Thank you.

4 Likes

Where did you get the library HCPCA9685.h? (the popular library does not have HCPCA9685_Servo_Example.ino)

What address are you using on the mux?

Try a simple example from the popular library (this link):

// PCA9685-Arduino Simple Example
#include "PCA9685.h"
PCA9685 pwmController;                  // Library using default B000000 (A5-A0) i2c address, and default Wire @400kHz

void setup() {
    Serial.begin(115200);               // Begin Serial and Wire interfaces
    Wire.begin();
    pwmController.resetDevices();       // Resets all PCA9685 devices on i2c line
    pwmController.init();               // Initializes module using default totem-pole driver mode, and default disabled phase balancer
    pwmController.setPWMFrequency(100); // Set PWM freq to 100Hz (default is 200Hz, supports 24Hz to 1526Hz)
    pwmController.setChannelPWM(0, 128 << 4); // Set PWM to 128/255, shifted into 4096-land
    Serial.println(pwmController.getChannelPWM(0)); // Should output 2048, which is 128 << 4
}

void loop() {
}
1 Like

this pc/documents/arduino/libraries/hcpca9685

From where did you load the library onto your computer?

i cant remember

Open this folder... pc/documents/arduino/libraries/hcpca9685

Inside, you will see a readme.md... open it and read the contents, which might contain a URL.

Is it:

https://forum.hobbycomponents.com/viewtopic.php?t=2034

with

/* FILE:    HCPCA9685_Servo_Example
   DATE:    10/06/16
   VERSION: 0.1
   AUTHOR:  Andrew Davies
 
   Sketch created by Hobby Components Ltd (HOBBYCOMPONENTS.COM)
   
10/06/16 version 0.1: Original version
 
 
This example demonstrates how to use the HCPCA9685 library together with the PCA9685
to control up to 16 servos. The sketch will initialise the library putting it into
'servo mode' and then will continuously sweep one servo connected to PWM output 0
back and forth. The example has been written particularly for the 16 Channel 12-bit
PWM Servo Motor Driver Module (HCMODU0097) available from hobbycomponents.com
 
To use the module connect it to your Arduino as follows:
 
PCA9685...........Uno/Nano
GND...............GND
OE................N/A
SCL...............A5
SDA...............A4
VCC...............5V
 
External 5V Power for the servo(s) can be supplied by the V+ and GND input of the
screw terminal block.
 
PLEASE NOTE: Depending on your servo it is possible for this sketch to attempt
drive the servo beyond its end stops. If your servo is hitting its end stops then
you should adjust the the min and max values in this sketch.
 
You may copy, alter and reuse this code in any way you like, but please leave
reference to HobbyComponents.com in your comments if you redistribute this code.
This software may not be used directly for the purpose of selling products that
directly compete with Hobby Components Ltd's own range of products.
 
THIS SOFTWARE IS PROVIDED "AS IS". HOBBY COMPONENTS MAKES NO WARRANTIES, WHETHER
EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ACCURACY OR LACK OF NEGLIGENCE.
HOBBY COMPONENTS SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR ANY DAMAGES,
INCLUDING, BUT NOT LIMITED TO, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY
REASON WHATSOEVER.
*/
 
 
/* Include the HCPCA9685 library */
#include "HCPCA9685.h"
 
/* I2C slave address for the device/module. For the HCMODU0097 the default I2C address
   is 0x40 */
#define  I2CAdd 0x40
 
 
/* Create an instance of the library */
HCPCA9685 HCPCA9685(I2CAdd);
 
 
void setup()
{
  /* Initialise the library and set it to 'servo mode' */
  HCPCA9685.Init(SERVO_MODE);
 
  /* Wake the device up */
  HCPCA9685.Sleep(false);
}
 
 
void loop()
{
  unsigned int Pos;
 
  /* Sweep the servo back and forth from its minimum to maximum position.
     If your servo is hitting its end stops then you  should adjust the
     values so that the servo can sweep though its full range without hitting
     the end stops. You can adjust the min & max positions by altering
     the trim values in the libraries HCPCA9685.h file*/
  for(Pos = 10; Pos < 450; Pos++)
  {
    /* This function sets the servos position. It takes two parameters,
     * the first is the servo to control, and the second is the servo
     * position. */
    HCPCA9685.Servo(0, Pos);
    delay(10);
  }
 
  for(Pos = 450; Pos >= 10; Pos--)
  {
    HCPCA9685.Servo(0, Pos);
    delay(10);
  }
}

Did you see HCPCA9685_Servo_Example.ino on that web site?

Yep. I copied it into #15 from that site. It was about halfway down the page, at the bottom of the article, and just above the comments.

(sorry... i see the title plain as yoghurt now.)

1 Like