Servo motor control with multiple Arduino

Hi, I want to control 1 servo motor with 2 Arduinos. In a certain condition, the first Arduino will control the servo, and in another condition, the second Arduino will control the servo. Please do not ask why I use 1 servo and 2 Arduinos. I did not buy these hardwares for now, I am just coding but I realised that there might be a problem when I run the code. The first Arduino will write 0 to the servo and finish its job. After a while, the second Arduino will write 180 (for examle) to the servo. In this case, which command would the servo obey? Would it stay at 0 or go to 180, or anywhere else? Thank you :slight_smile:

A servo is commanded by a series of pulses (50 per second IIRC) with a varying width (from about 400 microsecs to 2400 microsecs). The Servo library produces this train of pulses.

If two Arduinos send two trains of pulses the servo will be hopelessly confused so you need to ensure that the first Arduino stops sending pulses and sets its servo pin to INPUT (i.e. high impedance) before the second Arduino starts sending a train of pulses.

You can do that with code like this

myServo.detach();
pinMode(servoPin, INPUT);

You should also have some means for the Arduinos to communicate so they would know when it is their turn to control the servo. The simplest way might be to use on the Arduinos as the master and have one of its I/O pins send a LOW to the other when the other may create the pulses - assuming the slave Arduino has its receiving pin set as INPUT_PULLUP. You will also need a GND connection between the Arduinos.

...R

Sure, and it will definitly work when both masters decide to use the servo .. let's see how that works out.

Robin2:
A servo is commanded by a series of pulses (50 per second IIRC) with a varying width (from about 400 microsecs to 2400 microsecs). The Servo library produces this train of pulses.

If two Arduinos send two trains of pulses the servo will be hopelessly confused so you need to ensure that the first Arduino stops sending pulses and sets its servo pin to INPUT (i.e. high impedance) before the second Arduino starts sending a train of pulses.

You can do that with code like this

myServo.detach();

pinMode(servoPin, INPUT);




You should also have some means for the Arduinos to communicate so they would know when it is their turn to control the servo. The simplest way might be to use on the Arduinos as the master and have one of its I/O pins send a LOW to the other when the other may create the pulses - assuming the slave Arduino has its receiving pin set as INPUT_PULLUP. You will also need a GND connection between the Arduinos.

...R

Thanks for your reply! So, time for the second question. Is there any other solution without any direct communication between Arduinos? Because Arduinos (kind of) should not know what the other one is doing. Is there a switch or mini relay which can be controlled electronically by the second Arduino so that it can cut off the connection between the servo and the first Arduino before it sends any command to the servo?

And the second arduino would do the same ... so fun guaranteed ...

yusufbudakli:
Is there a switch or mini relay which can be controlled electronically by the second Arduino so that it can cut off the connection between the servo and the first Arduino before it sends any command to the servo?

You could use a single-pole double-throw relay (the standard sort that are usually used with Arduinos) to switch the servo pin from one Arduino to the other. But the operation of the relay must be left with one of the Arduinos.

...R

Robin2:
You could use a single-pole double-throw relay (the standard sort that are usually used with Arduinos) to switch the servo pin from one Arduino to the other. But the operation of the relay must be left with one of the Arduinos.

...R

Thank you so much! So do that size of relays cause any noise in the servo control signal? Or in general, do those relays cause noise and wrong data transmission in high frequency electronics?

yusufbudakli:
Thank you so much! So do that size of relays cause any noise in the servo control signal? Or in general, do those relays cause noise and wrong data transmission in high frequency electronics?

A servo signal is not high frequency.

I have not tried this idea but I suspect it should work fine.

...R

Robin2:
A servo signal is not high frequency.

I have not tried this idea but I suspect it should work fine.

...R

Thank you Robin, I appreciate!