Need help with output to follow input

Hello everyone,

I'm very new to this arduino coding and i really need help with one of my projects. Basically my project is to take in signal(in Pulse Width Modulation) from a Pixhawk and send it to 2 motors through an ESC(Electronic Speed Controller), and I also need to have a switch(signal also in PWM) to turn the motors off.

The problem I am facing now is that even when the switch allows signal to pass through, my output does not follow the input, even though the LED of pin 13 is working.

The following is my code. (The board I'm using is Arduino UNO)

int motorAOut = 5;
int motorBOut = 6;
int motorA = 9;
int motorB = 10;
int motorControl =11;
boolean A=0;
boolean B=0;

void setup()
  {
    pinMode(motorAOut, OUTPUT);
    pinMode(motorBOut, OUTPUT); 
    pinMode(motorA, INPUT);
    pinMode(motorB, INPUT);
    pinMode(motorControl,INPUT);
    pinMode(13, OUTPUT);
  }

void loop()
  {
    int ControlSwitch = pulseIn(motorControl, HIGH);
    
    if(ControlSwitch>1500)                    //My motor control switch have a Period of 20 millisecond, 
      {                                               //ON state having a pulse of 2 millisecond and OFF being 1.
        A = digitalRead(motorA);
        digitalWrite (motorAOut,A);
        B = digitalRead(motorB);
        digitalWrite (motorBOut,B);
        digitalWrite (13,HIGH);
      }
    else 
      {
        digitalWrite (motorAOut,LOW);
        digitalWrite (motorBOut,LOW);
        digitalWrite(13,LOW);
      }
      

  }

The problem I am facing now is that even when the switch allows signal to pass through, my output does not follow the input, even though the LED of pin 13 is working.

What do you mean by “does not follow”? Which frequency is your PWM in?

Why the use of pulseIn? Why not a simple digitalRead?

I’m not that familiar with the pulseIn function. According to the decription, it will wait till the pin goes LOW (after it detected HIGH), If you keep the button / switch pushed, it will have a timeout of one second.

If timing is critical, use a simple AND gate (e.g. 74HC08); one input controlled by Arduino (on/off) and the other input connected to the PWM input. Output goes to the ESC.

Hi, Welcome to the forum.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png? How have you wired your switch?

We need to see how you have connected the components. Please not a fritzy picture. Can you post a link to specs/data on the ESC please? Can you post a link to specs/data on the Pixhawk please?

Thanks... Tom... :)

sterretje: What do you mean by "does not follow"?

My aim is to have the output be the same as the input. How ever from the results when I tested the board with an oscilloscope and function generator, it isn't the same

sterretje: Which frequency is your PWM in?

Both signal is in 50Hz, 20 millisecond period, signal sent to ESC is a pulse ranging from 1 millisecond to 2 millisecond and signal from switch is also a pulse of either 1 millisecond or 2 millisecond.

sterretje: Why the use of pulseIn? Why not a simple digitalRead?

As the signal of my switch is also in PWN I doubt it will work.

sterretje: If timing is critical, use a simple AND gate (e.g. 74HC08); one input controlled by Arduino (on/off) and the other input connected to the PWM input. Output goes to the ESC.

Im trying to eliminate the use of another external AND gate. If there is a way to incorporate that into the arduino board please do tell.

Hi,

  • So you have a PWM from the PixHawk, for MOTORA, and a separate PWM for MOTORB?
  • You want to use the Arduino to basically switch BOTH motors OFF and ON at the same time?
  • Why not just use a switch in each PWM line to the each motor to disconnect the motors?
  • If you need to why not use a DPDT relay?

The reason I am suggesting this is I am using ALL the information that you have provided. Can you answer each of the 4 questions please?

Thanks.. Tom... :)

TomGeorge: Hi,

  • So you have a PWM from the PixHawk, for MOTORA, and a separate PWM for MOTORB?
  • You want to use the Arduino to basically switch BOTH motors OFF and ON at the same time?
  • Why not just use a switch in each PWM line to the each motor to disconnect the motors?
  • If you need to why not use a DPDT relay?
  • Yes, control switch signal will also be coming from the pixhawk in PWM
  • Yes
  • A physical switch is impossible for the project I am doing thus the control switch will have to come from the pixhawk
  • I'm trying to eliminate the use of external circuits since space is of importance in the project.

Hi

Isn't the inclusion of an Arduino an external circuit? Or is there already a UNO there that you haven't told us about?

  • We need to see how you have connected the components. Please not a fritzy picture.
  • Can you post a link to specs/data on the ESC please?
  • Can you post a link to specs/data on the Pixhawk please

Please.. Tom... :)

I’m trying not to use anything else other than the arduino

  • Right now there is no circuit, I’m just testing the arduino with a function generator and oscilloscope
  • I do not hav the ESC with me right now, so i can’t find the specs
  • https://pixhawk.org/modules/pixhawk

If I understand the control PWM correctly: If there is no PWM (e.g. low permanently) it's a OFF condition. Of there is a 50Hz signal, it's an ON condition.

The below code is based on the above and the idea is to replace the pulseIn() function that you use. It makes use of millis() to measure the duration of the LOW period. It will not block like the pulseIn().

const byte controlPin = 2;

void setup()
{
  Serial.begin(9600);
  pinMode(controlPin, INPUT_PULLUP);
}

void loop()
{
  unsigned long currentTime = millis();
  static unsigned long previousTime = 0;

  if (digitalRead(controlPin) == LOW)
  {
    // if time measurement not started start it
    if (previousTime == 0)
    {
      previousTime = currentTime;
    }

    // if low for more than 20ms, there is no PWM signal
    if (currentTime - previousTime >= 20)
    {
      // switch off
      ...
      ...
    }
    else
    {
      // read and pass through
      ...
      ...
    }
  }
  else
  {
    // reset time measurement
    previousTime = 0;
    // read and pass through
      ...
      ...
  }
}

Not tested; use some serial.println statements for debugging if needed. I hope the comments are clear enough and that you can get the idea from it.

rebeccatay:
Basically my project is to take in signal(in Pulse Width Modulation) from a Pixhawk and send it to 2 motors through an ESC(Electronic Speed Controller),

This sounds as if you need to use pulseIn() to measure the width of the incoming pulse in microseconds and use servo.writeMicroseconds(width); to produce the same outgoing pulse.

…R