Problem with cascading visual rotary encoders

userHead Dkrigou.Har 2025-01-07 22:20:20 80 Views1 Replies

Hello !

i'm experiencing problems when trying to use 2 rotary encoders in i2c cascade to pilot 2 servo motors.


 

it works perfectly with one encoder and one servo, one encoder and 2 servos, but not with 2 encoders and 2 servos.

the 2 encoders have strange behaviour and no servo moves.
 

i'musing a PCA9685 i2c servo driver

attached is my code, 
 

could you help me?

thanks !

best regards

 

 

//#include <Wire.h>                         // Wire Library for I2C Communications
#include <Adafruit_PWMServoDriver.h>      // Adafruit PWM Library for servos
#include <DFRobot_VisualRotaryEncoder.h>  // Library for DFrobot Encoder Potentiometer


// Variables and constants declaration *********************************************


// Servos Valves


#define MIN_PULSE_WIDTH  771  //650
#define MAX_PULSE_WIDTH  2193  //2350
#define FREQUENCY        50

Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

// Define Valves Encoder Inputs


DFRobot_VisualRotaryEncoder_I2C PotServo(/*iicAddr = */0x54, /*iicBus = */&Wire);
//DFRobot_VisualRotaryEncoder_I2C PotServo2(/*iicAddr = */0x55, /*iicBus = */&Wire);

// Define Servo Outputs on PCA9685 board
const int ServoPin = 0; 
const int Servo2Pin = 1;


// Servo valves *************************************************************************

void moveServo(DFRobot_VisualRotaryEncoder_I2C Encoder, int S_Pin)
{
 // with  Encoders

   if(Encoder.detectButtonDown()){
   // Reset encoders
     Encoder.setEncoderValue(0);
}
  
   int pulse_wide, pulse_width;

   uint16_t encoderValue = Encoder.getEncoderValue();
   Serial.print("The encoder currently counts: ");
   Serial.println(encoderValue);
   


   
  pulse_wide = map(encoderValue, 0, 1023, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH);
  pulse_width = int(float(pulse_wide) / 1000000 * FREQUENCY * 4096);
 
 

   pwm.setPWM(S_Pin, 0, pulse_width);
   Serial.print("PulseWide: ");
   Serial.println(pulse_wide);
    Serial.print("PulseWidth: ");
   Serial.println(pulse_width);

   delay(10);
   
 }

 

  
  
// Main Code *************************************************************************

void setup() {
 // put your setup code here, to run once:
 
 
 Serial.begin(9600);           // start serial

 pwm.begin();                  // start PWM
 pwm.setPWMFreq(FREQUENCY);    // set PWM Frequency

 
// Servos Encoders communication check 
//while( NO_ERR != PotServo.begin() && NO_ERR != PotServo2.begin()){
while( NO_ERR != PotServo.begin() ){

   Serial.println("Communication with Encoder failed, please check connection");
   delay(2000);
 }
 Serial.println("Begin Encoders ok!");


 PotServo.setEncoderValue(0);
 //PotServo2.setEncoderValue(0);

 

 // Set the 4 encoder gain coefficient : Accuracy range: 1~51, the minimum is 1 (light up one LED about every 2.5 turns), the maximum is 51 (light up one LED every one detent rotation
 PotServo.setGainCoefficient(51);
 //PotServo2.setGainCoefficient(51);

}

 


void loop() {
 // put your main code here, to run repeatedly:
moveServo(PotServo, ServoPin);
moveServo(PotServo2, Servo2Pin);
}

2025-01-08 16:53:58

Hello


 

i FOUND THE PROBLEM !


 

i did'nt understand that there is a physical switch on the back of the encoder (SEN0502) to set the i2c address !!


 

both encoder were on the same address


 

now i changed one encoder to 0x55 and it works !

userHeadPic Dkrigou.Har