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.
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.
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.
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);
}