DS3120 Servo % PCA9685 calculating goto values

Hi Group,
For my pan/tilt project I am using two DS3120 servo's and the PCA9685 controller. and using the HCPCA9685 libray
Using the HCPCA9685.Servo(servo, Pos) command,
Servo is easy, a number from 0-15, but Pos is mory tricky. The question is how do I calculate the correct value for a position between 0 and 180 degrees movement/
Thanks foor your comments


Since the default range of values for pos are 0 - 480 then maybe what you need is
pos = map(angle, 0, 180, 0, 480)

Unless your invisible code modifies the default range or isn't working with an angle, in which case it's over to you.


I would do a practical examination with the standard servo-library to find out:
what is the minum pulsewidth in microseconds
and the maximum pulsewidth in microseconds

The technical specs
say pulsewidth range 500-2500us

the standard-range is 1000 µsec to 2000 µsec

some weeks ago I had a servo which was able to turn 180 degrees but only If I enlarged the pulsewidth range from the standard 1000 µsec to 2000 µsec
to go beyond the standard-range with 750 µsec to 2400 µsec

best regards Stefan

So I have rewritten my code to include the map statement.
The first part moves the lever from 0 to the max

#include <HCPCA9685.h>
#define I2CAdd 0x40
HCPCA9685 HCPCA9685(I2CAdd);
#define servoMin 0
#define servoMax 180
void setup() {
  HCPCA9685.Init(SERVO_MODE); /* Wake the device up */
  Serial.begin(9600); // Start Communication
  HCPCA9685.Servo(2, 0); // move servo to 0 degrees
  delay(25); // wait some time (its in all examples
  for (float deg = servoMin; deg <= servoMax; deg = deg += 0.1) {
    //    unsigned int nmap = map(deg, servoMin, servoMax, 0, 480);
    //    HCPCA9685.Servo(2, nmap);
    Serial.print(deg); // printit on the stdout
    Serial.print(" - ");
    //    Serial.println(nmap);
  //  unsigned int nmap = map(90, servoMin, servoMax, 0+45, 480-45);
  //  HCPCA9685.Servo(2, nmap);

void loop() {
  // put your main code here, to run repeatedly:



But the last command should place the lever at the 90degrees position but its way off.

You should post code by using code-tags
There is an automatic function for doing this in the Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting

click on the pencil Icon to actiavte edit-mode of your posting.
then delete the pure text and do the three steps like described above

best regards Stefan

No idea where slipstick has taken the range 0,480 from.
The PCA9685-Chip has 12 Bit resolution which is 4096

Anyway "way off" is a much too unprecise description of what is happening.
It should be 90 degrees. How many degrees do you get with the code above?

but maybe it is this

deg = deg += 0.1;

This is a mixture between

deg = deg + 0.1;


deg += 0.1;

best regards Stefan


plus examples using values from that range. Of course it could be an error in the library documentation. I've never used that library myself.


Hello Stefan,
I tried to do that but wiht no result. Its a simple procedure, I know, but it dit no work for me.

You are working on an informatic project. And what is most needed in an informatic project is information.

You tried what to do?
and what was the result?
best regards Stefan

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.