Servo going back and forth while using 2 Arduinos to wirelessly communicate

Hi there, I am new to this and am a little stuck. I am following this YouTube tutorial for a year 12 project:

for this project I am trying to move a servo to push something wirelessly. I have set up the project exactly the same and it works fine (apart from the servo vibrating). the servo used in the video is too small so I have attached a MG995 servo. now that I have done this the servo continuously goes back and forth hitting the max degrees the servo can go, when I push the button it goes to one side like it's supposed to but when I release the button, it goes back to going back and forth. I want the servo to stay in one position until i press the button, can anybody let me know what to do or how to make it works. Here is the link to the code if that's helpful:

https://www.youtube.com/redirect?event=video_description&redir_token=QUFFLUhqbjFxSmFucFBUVXBPWkhER0lVMUFUSGFMUEdMd3xBQ3Jtc0trS016b2QwUkgzRUVZbW5TNFhUdTZubHBGMUpHTW83X3hmZnY1WEFCNm9pUTNSSnNGOFZRTmUyamlxa3BoaTc2OEdYb2tNOTBJOE5ERjZwbng3OXpadlFQY2FqcEU1VnBkcXhkVnBXUGFSU2JqazVodw&q=https%3A%2F%2Fkingstudio1993.blogspot.com%2F2019%2F12%2Fservo-motor-controlled-by-push-button_19.html

thanks :slight_smile:

receiver code (which controls the servo):

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Servo.h> // include the Servo library
Servo myServo1;
Servo myServo2;

int data[2];

RF24 radio(9,10);//check your pin number on RF24 github check you have the right
//pin number for the arduino you're using. this pin is diffrent for diffrent arduino models.
const uint64_t pipe = 0xF0F0F0F0D2L;

void setup()
{
myServo1.attach(5);
myServo2.attach(6);
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(1, pipe);
radio.startListening();
}

void loop(){
if(radio.available()){
bool done = false;
while (!done) {
done = radio.read(data,sizeof(data));

  if (data[1] == 170) {
   myServo1.write(data[1]);
  }else{
    myServo1.write(20);
  }

  if (data[0] == 170) {
   myServo2.write(data[0]);
  }else{
    myServo2.write(20);
  }

}
Serial.println(data[0]);
Serial.println(data[1]);
}
}

transmitter code if needed:

#include <SPI.h>
#include <RF24.h>
#include <nRF24L01.h>

int data[2];
RF24 radio(9,10);//check your pin number on RF24 github check you have the right
//pin for the arduino you're using. this pin number is diffrent for diffrent arduino models.

const uint64_t pipe = 0xF0F0F0F0D2L;

int buttonPin1 = A1;
int buttonPin2 = A2;
int buttonState1 = 0;
int buttonState2 = 0;

void setup()
{
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(pipe);
pinMode(buttonPin1, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
}

void loop()
{
buttonState1 = digitalRead(buttonPin1);

if (buttonState1 == HIGH)
{
data[1] = 170;
radio.write(data, sizeof(data));
}
if(buttonState1 == LOW)
{
data[1] = 20;
radio.write(data, sizeof(data));
}

buttonState2 = digitalRead(buttonPin2);
if (buttonState2 == HIGH)
{
data[0] = 170;
radio.write(data, sizeof(data));
}
if (buttonState2 == LOW)
{
data[0] = 20;
radio.write(data, sizeof(data));
}
Serial.println(data[0]);
Serial.println(data[1]);
}

Without posting your code, only you know the answer. If you truly are using 9V alkaline batteries to power them, dont. Upgrade to a better power supply, especially is you are using larger servos. They need more power.

I have tried with a usb 5v input which didn't work. i tried powering the servo separately and nothing worked and then I tried with a 12v, 2 amp input into the Arduino and it started smoking (which I think burnt out the voltage regulator) so I used another Arduino uno, what voltage would you recommend, should I try a 9v battery? , thanks for your help, I will post the code

Sadly, the answer to many of these queries is the same.

Powering the Arduino by the "barrel jack" or the "Vin" pin is a foolish mistake. It might work where the buttons are the only thing connected to the Arduino, but the servos require proper power and you are using the NRF modules.

If you have a 5 V supply, connect that to the servos, remembering that you must connect the ground of that to the Arduino ground. Do not connect it through the USB port as that is limited by the "polyswitch" on A UNO.

You should also connect your 5 V supply (could if necessary, be four NiMH batteries) to the "5V" pin on the Arduino except while you connect the UNO to your PC for programming and control.

Hi there, I have tested by powering the larger servo separately and it is unresponsive to the button (it is powered but doesn't move when I press the button, I have also grounded it to the Arduino and the power source), does the servo work through the 5v pin to turn it on and off, I'm not very good at code so am unsure if that could be the problem. any suggestions?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.