Using Servo Motor in simple 'If, Else' Statement

Hello,

I am fairly new to Arduino, so please bear with me.
I am trying to make a servo motor travel to 3 degrees when 12v power is supplied to pin 2.
Then, when the power is removed from pin 2, the servo should travel to 40 degrees.

For some reason, the motor usually has no problem traveling to 3 degrees when the power source is on; however, when the power source turns off, it will not travel to 40 degrees.

This is the code that I am running. Does anyone know a better way to do this?

#include <Servo.h>

const int servoPin = 9;  // The pin connected to the servo
const int powerPin = A2;  // The pin connected to the external power source
Servo myServo;  // Create a servo object

void setup() {
  pinMode(powerPin, OUTPUT);
  digitalWrite(powerPin, LOW);  // Initially set the power pin to LOW (disabled)
  myServo.attach(servoPin);  // Attach the servo object to the servo pin
}

void loop() {
  bool powerOn = digitalRead(powerPin);

  if (powerOn) {
    // If the power is on, move the servo to 32 degrees
    moveServo(3);
  } else {
    // If the power is off, move the servo to 0 degrees and stabilize
    moveServo(40);
  }

  // A small delay to avoid rapid fluctuations
  delay(100);
}

void moveServo(int degrees) {
  // Enable power to the servo motor
  digitalWrite(powerPin, HIGH);

  // Move the servo to the desired position (in degrees)
  myServo.write(degrees);

  // Disable power to the servo motor after the movement is complete
  digitalWrite(powerPin, LOW);
}

Good job posting the code properly. Most new members don't bother with reading the forum guide lines. Thank you and welcome to the forum.

How are you dropping the 12V to 5V so that the input is not damaged?

Please post a schematic or wiring diagram. Written descriptions are always more ambiguous than a drawing. Hand drawn, photographed and posted is fine. Include all pin names/numbers, components, their part numbers and/or values and power supplies.

1 Like

DO NOT supply 12V to any Arduino pin !

It is probably too late for pin 2 of your Arduino and possibly other parts of it

1 Like

I am connecting 12V power directly to pin 2 on the Arduino board.
I did not realize this may damage it...

Here is my current setup.
I did not realize that a 12v input to the Arduino board would damage it.
I've since unplugged that input.

Here is what my setup did look like though...

OUCH !

Is there a way to run a test on my board to make sure that nothing is damaged?

Hello

Try, download and run the BlinkWithOutDelay example from the IDE first.

You just never know what parts of the processor were damaged and to what extent.

It might work OK today but might start acting funny a few days from now.

After you get a new Uno, read about Resistor voltage dividers to drop a higher voltage signal to a lower voltage.

A servo needs an external power supply. Neither USB or internal 5V has the current capability for a servo or servos.

How are you powering the servo? It looks like from the code you are trying to power it directly from an arduino pin. IO pins can give ~13ma, which is no where near enough to power a servo directly.

Update to Everyone on this thread.
I have got everything working correctly.
The problem now is that there is the faint ticking noise coming from the Servo motor.
I am using a fresh Arduino board...not the one that I fried.

The servo is being powered directly by the board using the 5V output.
I have had no issues powering small servos directly using the Arduino.

This is not a small servo. You should never power servos of any size directly from the 5v port anyways. Use an external power supply

What would you recommend is the best way to externally power a servo of this sort.
Also here is a photo and diagram of the setup.


My main issue here is the faint audible ticking noise that I am getting from the servo which is there constantly, no matter whether it in the 3 or 40 degree position.

Use a plug in power supply with at least a 1a rating, or find a 4 aa batterypack

See this thread.

Ok, I understand with the power supply.

What about the ticking noise?
I am finding that this is a common occurrence with servos being used in Arduino setups.

The only solutions that I have seen are in dampening the servo using something to quite the noise.

Is there a good way to re-write this code so that the servo detaches after moving to the correct position?

#include <Servo.h>

const int servoPin = 9;  // The pin connected to the servo
const int powerPin = A2;  // The pin connected to the external power source
Servo myServo;  // Create a servo object

void setup() {
  pinMode(powerPin, OUTPUT);
  digitalWrite(powerPin, LOW);  // Initially set the power pin to LOW (disabled)
  myServo.attach(servoPin);  // Attach the servo object to the servo pin
}

void loop() {
  bool powerOn = digitalRead(powerPin);

  if (powerOn) {
    // If the power is on, move the servo to 32 degrees
    moveServo(3);
  } else {
    // If the power is off, move the servo to 0 degrees and stabilize
    moveServo(40);
  }

  // A small delay to avoid rapid fluctuations
  delay(100);
}

void moveServo(int degrees) {
  // Enable power to the servo motor
  digitalWrite(powerPin, HIGH);

  // Move the servo to the desired position (in degrees)
  myServo.write(degrees);

  // Disable power to the servo motor after the movement is complete
  digitalWrite(powerPin, LOW);
}

The ticking noise is very likely from the lack of a proper power supply.