0

I have two Arduinos, two buttons and one servo motor.

I want to control a servo motor with two Arduino's when I pressed the buttons. I linked Arduino PWM pins (first Arduino PWM pin is 9, and second one is 3) to the servo motor signal input.

When I pressed button 1 servo motor moving a few, or pressed button 2. but the signal isn't clear, and servo motor doesn't move correctly that I assigned degree.

What can I do for the solution?

My first Arduino code is here:

#include<Servo.h>

#define Buton 8

Servo myServo;

void setup() {
  pinMode(Buton, INPUT);
  myServo.attach(3);
}

void loop()
{
  if (digitalRead(Buton) == 1)
  { 
    myServo.write(90);
  }
  else
  {
    myServo.write(180);
  }

}

and second arduino code is:

#include<Servo.h>

#define Buton 8

Servo myServo;

void setup() {
  pinMode(Buton, INPUT);
  myServo.attach(9);
}

void loop()
{
  if (digitalRead(Buton) == 1)
  { 
    myServo.write(90);
  }
  else
  {
    myServo.write(180);
  }

}
3
  • 1
    You do you have 2 Arduino's in the first place? Do you really need both?
    – chrisl
    Jul 23, 2019 at 13:53
  • we have to use mcu.but it's not have to be arduino. does it change if we use a different mcu's? Jul 23, 2019 at 14:06
  • 1
    that does not answer the question why you need two arduinos
    – jsotola
    Jul 23, 2019 at 16:20

1 Answer 1

2

You can't just connect random pins together like that. Connecting two output pins together is a recipe for disaster. Best case scenario: nothing works. Worst case scenario: you break one or both Arduinos.

Only one Arduino can control a servo. If you want a second Arduino to make something happen to the servo you have to make it tell the Arduino that is controlling the servo that something should happen.

That could be a simple output of the second Arduino connected to an input of the first Arduino, in which case it kind of acts like a button. Or you could create some kind of communication channel between them - maybe using a Serial connection, or a Wire (I2C) connection. There's many ways, and which is best depends on how much control one Arduino needs to exert over the other.

6
  • thank you for your answer @Majenko, but let me explain. we are a rocket team and we have to use two flight control system. so we use two arduinos for this rocket. first arduino is main controller and second is backup system. we have different sensors but we have one motor to deployment. we want to use second arduino if the first arduino won't work. I don't know if it's descriptive? Jul 23, 2019 at 14:02
  • 1
    Then you need to have some device that sits between the two arduinos and the servo and selects which Arduino is allowed to talk to the servo. You could have one Arduino in "standby" monitoring the other Arduino and, if it fails, flip the "switch" that allows it to control the servo and have it take over.
    – Majenko
    Jul 23, 2019 at 14:08
  • @Giray Hakan When you are talking about backup in case of failure, you first must define the cases of failure, that you want to mitigate here. There might be many different ways, in that the system can fail and which have to be sensed and mitigated in a different way.
    – chrisl
    Jul 23, 2019 at 14:11
  • 2
    You could use a MUX chip. Or a relay. There's many solutions both mechanical and silicon based.
    – Majenko
    Jul 23, 2019 at 14:36
  • 1
    ok, thank you, i will try. Jul 23, 2019 at 15:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.