1

I am trying to control a servo motor with the following code:

#include <Servo.h>

Servo servo;

int const inputPin = 3;

void setup() {
  Serial.begin(9600);
  servo.attach(9);
  pinMode(inputPin, INPUT);
  servo.write(0);
}

void loop() {
  if (digitalRead(inputPin)) {
    Serial.println("HIGH");
    servo.write(40);
  } else {
    Serial.println("LOW");
    servo.write(0);
  }
}

It takes input from pin 3 and controls the servo with pin 9. When pin 3 is connected to a pushbutton, as shown in the following image, it works exactly as expected; for as long as the button is held down, the servo stays at 40° and when the button is released the servo returns to 0°.

It's a little difficult to see in the images, but the resistor on row 24 is connected to ground, not power. enter image description here

However, if I replace the button with an Adafruit RF M4 Receiver, as shown in the following image, the servo behaves differently. When I hold the button on the remote down, the servo attempts to move to 40°, but returns to 0° before trying again. From the serial output, I can see that the pin is in fact oscillating between HIGH and LOW. Circuit with receiver

If I remove the commands to the servo and just use the buttons to control the serial output, the code behaves correctly. With both the pushbutton and the RF M4 Receiver, the output says HIGH when the button is held down and LOW when it is released. So the issue only occurs when I use both the receiver and a servo motor, but not one or the other.

int const inputPin = 3;

void setup() {
  Serial.begin(9600);
  pinMode(inputPin, INPUT);
}

void loop() {
  if (digitalRead(inputPin)) {
    Serial.println("HIGH");
  } else {
    Serial.println("LOW");
  }
}

I am using an Arduino Uno R3, this receiver, and this remote.

Edit: The schematic, as requested. I'm new to Arduino, so I apologize if the schematic is a little sloppy.

schematic

simulate this circuit – Schematic created using CircuitLab

12
  • I will guess you probably have RF feedback getting into the control loop. No idea of how it gets there. Please post an annotated schematic.
    – Gil
    Commented Jan 17, 2023 at 18:42
  • @Gil, I'm working on making the schematic. Do you think I need the pull down resistor for the RF receiver? Can that be causing the issue?
    – A P
    Commented Jan 17, 2023 at 18:46
  • No sure, I am not familiar with the parts or circuit you are using. If that is connected to an Arduino port pin that can switch it low or high.
    – Gil
    Commented Jan 17, 2023 at 18:53
  • remove the servo and the servo code... light an LED instead
    – jsotola
    Commented Jan 17, 2023 at 20:54
  • @jsotola It works as expected with an LED. I'm only having an issue when I'm using the receiver to control a servo.
    – A P
    Commented Jan 17, 2023 at 20:58

0

Your Answer

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

Browse other questions tagged or ask your own question.