PWM output copy value on input (pulseIn)

Hi,
in my new Arduino MEGA project, I have to set 12 different PWM as output and 12 input that read these PWM.

It's partially working, because when I read an input, it have the save value (more or less) than the others input.

Here are my PWM therorical output:

//                        'A', 'B', 'C', 'D', 'E', 'F',   'G',  'H', 'J',   'K',  'L', 'M'
int tabPWMOutputValue[] = {10,  30,  50,  70,  90,  110,  130,  150,  170,  190,  210,  230};

And my input I should receive:

//                        'A', 'B', 'C', 'D', 'E', 'F',   'G',  'H', 'J',   'K',  'L', 'M'
int tabPWMInputValue[] =  {40,  230, 395, 550, 710, 870,  1025, 1185, 1350, 755, 1670, 1830};

When I plug (for example) the C output in A input, here is my values:

A: 394
B: 394
C: 395
D: 401
E: 400
F: 401
G: 394
H: 395
J: 395
K: 401
L: 401
M: 400

I should have 0 on every other input because I haven't plugged them but I have more or less the C value on every. It seems that there is interferences but I don't know why. Do you have any idea ?

I'm using Pulsein to read on pin: A0, A2, A4, A6, A9, A12, A15, 53, 50, 49, 46, 45.

I hope I'm clear enough so you can help me

Hi, @devpierrick
Welcome to the forum.

Why do you want to measure the duty cycle when you know what it is when you wrote analogWrite?

Can you please post your code.
To add code please click this link;

Tom... :smiley: :+1: :coffee: :australia:

I need to test connectivity between cables and if cable are properly make. For example, I have a cable with 4 pins male connector and 4 pins female connector. I want to ensure the connector is properly cabled, the A pin from male connected to the A pin from female.
I'm doing this by emiting frequency on male connector A, B, C, D and I'm reading it on female side.
If the A male frequency correspond to the A female input, then the cable is properly connected and I turn on a LED corresponding to A. I'm doing this for all other cable, my biggest cable have 12 pins so I have 12 input and 12 output.

Here is my code.

int PWMOutput1 =    13;
int PWMOutput2 =    12;
int PWMOutput3 =    11;
int PWMOutput4 =    10;
int PWMOutput5 =    9;
int PWMOutput6 =    8;
int PWMOutput7 =    7;
int PWMOutput8 =    6;
int PWMOutput9 =    5;
int PWMOutput10 =   4;
int PWMOutput11 =   3;
int PWMOutput12 =   2;

int PWMInput1 =     A0;
int PWMInput2 =     A2;
int PWMInput3 =     A4;
int PWMInput4 =     A6;
int PWMInput5 =     A9;
int PWMInput6 =     A12;
int PWMInput7 =     A15;
int PWMInput8 =     53;
int PWMInput9 =     50;
int PWMInput10 =    49;
int PWMInput11 =    46;
int PWMInput12 =    45;

int LEDOutputA =    22;
int LEDOutputB =    23;
int LEDOutputC =    24;
int LEDOutputD =    25;
int LEDOutputE =    26;
int LEDOutputF =    27;
int LEDOutputG =    28;
int LEDOutputH =    29;
int LEDOutputJ =    30;
int LEDOutputK =    31;
int LEDOutputL =    32;
int LEDOutputM =    33;

int A = 0;
int B = 1;
int C = 2;
int D = 3;
int E = 4;
int F = 5;
int G = 6;
int H = 7;
int J = 8;
int K = 9;
int L = 10;
int M = 11;

char alphabet[] = {'A', 'B', 'C', 'D','E', 'F', 'G', 'H', 'J', 'K', 'L', 'M'};
int tabPWMOutput[] = {PWMOutput1, PWMOutput2, PWMOutput3, PWMOutput4, PWMOutput5,PWMOutput6,PWMOutput7,PWMOutput8,PWMOutput9,PWMOutput10,PWMOutput11,PWMOutput12};

int tabPWMInput[] = {PWMInput1, PWMInput2, PWMInput3, PWMInput4, PWMInput5,PWMInput6,PWMInput7,PWMInput8,PWMInput9,PWMInput10,PWMInput11,PWMInput12};

int tabLEDOutput[] = {LEDOutputA, LEDOutputB, LEDOutputC, LEDOutputD, LEDOutputE,LEDOutputF,LEDOutputG,LEDOutputH,LEDOutputJ,LEDOutputK,LEDOutputL,LEDOutputM};

int tabPWMOutputValue[] = {10,  30,  50,  70,  90,  110,  130,  150,  170,  190,  210,  230};

//Case 0 et 9 (pour PWM 13 et 4) divisés par 2
//                        'A', 'B', 'C', 'D', 'E', 'F',   'G',  'H', 'J',   'K',  'L', 'M'
int tabPWMInputValue[] =  {40,  230, 395, 550, 710, 870,  1025, 1185, 1350, 755, 1670, 1830};

//int tabPWMInputValue[] =  {80,  230, 395, 550, 710, 870,  1025, 1175, 1335, 1495, 1670, 1830};

int tempInputValue[] =  {0,0,0,0,0,0,0,0,0,0,0,0};

int ledState[] = {0,0,0,0,0,0,0,0,0,0,0,0};

int setValueTo[] = {0,0,0,0,0,0,0,0,0,0,0,0};

int arraySize = 12;
int marge = 15;

int E1 = 6;
int M1 = 7;
int E2 = 5;
int M2 = 4;

unsigned long previousMillis = 0;        // will store last time 
const long interval = 1000;           // interval (milliseconds)
int seconds = 0;

void setup()
{
    Serial.begin(115200);
    //pinMode(M1, OUTPUT);
    //pinMode(M2, OUTPUT);

    for(int i = 0; i < arraySize; i++){      
      pinMode(tabPWMOutput[i], OUTPUT);
      pinMode(tabLEDOutput[i], OUTPUT);
      pinMode(tabPWMInput[i], INPUT);
      analogWrite(tabPWMOutput[i], tabPWMOutputValue[i]);
      digitalWrite(tabLEDOutput[i], HIGH);
      delay(50);
    }
    delay(100);
    //Cinématique de début
    for(int i = 0; i < arraySize; i++){      
      digitalWrite(tabLEDOutput[i], LOW);
      delay(50);
    }

    seconds = 0;
}

void loop()
{

  /*
  //Moteurs
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval){    
    previousMillis = currentMillis;
    seconds++;
  }

  //Sens horaire
  if(seconds < 420){
  //if(seconds < 15){
    digitalWrite(M1,HIGH);
    digitalWrite(M2,HIGH);
    //analogWrite(E1, 255);   //PWM Speed Control
    //analogWrite(E2, 255);   //PWM Speed Control
    digitalWrite(E1, HIGH);
    digitalWrite(E2, HIGH);
  }
  //Sens trigonométrique
  //else if(seconds < 30){
  else if(seconds < 840){
    digitalWrite(M1,LOW);
    digitalWrite(M2,LOW);
    //analogWrite(E1, 255);   //PWM Speed Control
    //analogWrite(E2, 255);   //PWM Speed Control
    digitalWrite(E1,  HIGH);
    digitalWrite(E2,  HIGH);
  }
  //end of rotations
  else{
    //analogWrite(E1, 0);   //PWM Speed Control
    //analogWrite(E2, 0);   //PWM Speed Control
    digitalWrite(E1, LOW);
    digitalWrite(E2, LOW);
  }
  //Fin Moteurs
  //*/
  
  //*
  //Leds
  for(int i = 0; i < arraySize; i++){
    //Lecture d'une impulsion haute
    tempInputValue[i] = pulseIn(tabPWMInput[i], HIGH );//, 100000L);
    delay(300);
    
    //Si XX est dans sa plage de valeur mais que son -1 a à peu près la même valeur, c'est que le -1 a prit la valeur de XX donc on met le -1 à 0
    /*
    if(tempInputValue[i] + marge > tabPWMInputValue[i] && tempInputValue[i] - marge < tabPWMInputValue[i]){
      for(int j = 0; j < arraySize; j++){
        if(j - 1 >= 0 && j <= arraySize - 1)
          if(tempInputValue[j - 1] + marge > tabPWMInputValue[i] && tempInputValue[j - 1] - marge < tabPWMInputValue[i]){
            if(tempInputValue[j + 1] + marge > tabPWMInputValue[i] && tempInputValue[j + 1] - marge < tabPWMInputValue[i]){
              tempInputValue[j] = 0;
            }
            //Serial.print("ici\t");
            //Serial.print(tempInputValue[j]);            
          }else{
            setValueTo[j] = 0;
          }
      }

    }
    else{
      setValueTo[i] = 0;
      //tempInputValue[i]  = 0;
    }
    //*/

    Serial.print(alphabet[i]);
    Serial.print(": ");
    Serial.println(tempInputValue[i]);
    //Serial.print(": ");
    //Serial.println(setValueTo[i]);
    
    //*
    if(tempInputValue[i] + marge > tabPWMInputValue[i] && tempInputValue[i] - marge < tabPWMInputValue[i]){
      digitalWrite(tabLEDOutput[i], HIGH);
      ledState[i] = HIGH;
    }else{      
        digitalWrite(tabLEDOutput[i], LOW);
        ledState[i] = LOW;
    }
    //*/

    /*
    if(ledState[D] == HIGH && ledState[E] == HIGH && ledState[F] == HIGH
    && ledState[A] == LOW && ledState[B] == LOW && ledState[C] == LOW
    && ledState[G] == LOW && ledState[H] == LOW && ledState[J] == LOW
    && ledState[K] == LOW && ledState[L] == LOW && ledState[M] == LOW){      
        digitalWrite(tabLEDOutput[G], HIGH);
        ledState[G] = HIGH;
    } 
    if(ledState[D] == HIGH && ledState[E] == HIGH && ledState[F] == HIGH
    && ledState[A] == HIGH && ledState[B] == HIGH && ledState[C] == LOW
    && ledState[G] == HIGH && ledState[H] == HIGH && ledState[J] == HIGH
    && ledState[K] == HIGH && ledState[L] == HIGH && ledState[M] == HIGH){      
        digitalWrite(tabLEDOutput[C], HIGH);
        ledState[C] = HIGH;
    }
    //*/
    
  }
  //Fin Leds
  //*/
}

Hi,
What is your cable?
There may be distortion of the PWM signal going through the cable.

Why don't you just do a pull ONLY A High, Check other end A, B, C, D, etc
Then pull ONLY B HIGH, check A, B, C, D, etc.

Using PWM you may have cross-feed problems between parallel wires.

Tom... :smiley: :+1: :coffee: :australia:

Cables are closed to each other yes but your solution seems to work perfectly, by sending only one PWM signal at once :slight_smile:
Thanks

Doing a simple digitalWrite/digitalRead to check for continuity would accomplish the same thing and probably be easier to implement. This project has been discussed before on the forum...

Yes but a simple digitalWrite only send a same signal. In this case, I want to be sure that cable aren't inverted, that's why I need a different signal for each cable. Then I can check for continuity and correct wiring

I am not sure what that means. If you have a cable of N wires, end A connected to a set of pins and end B connected to a different set of pins, you declare all pins as INPUT_PULLUP so every pin should read HIGH.

You then systematically set each pin from end A as an OUTPUT and LOW and sample all of the other pins. Only the corresponding pin on end B should read LOW. If you want to be very complete, you can even sample the other A pins to make sure there is not a short between them. You then return that pin HIGH and proceed to the next pin...

No PWM required.