Servo motor not moving.

I'm working on a kit project. Learning. The project is a LOCK that gets close with a switch and opens with three knocks on a piezo (? :slight_smile: ). There is extensive debugging through serial and LEDs confirm the different status points so I can fairly be sure that all the rest works. I planned to take ahead this example for porting to Nano Every so I need to understand better and make it work.

The servo motor (out of the kit) should rotate 90° form open to close when the close command is given and viceversa. Only change I made on the drawing was to change the connection because the motor wires are stuck together and cannot be position as shown. It does not move.

Cannot check straight away the motor alone. I used in a previous project so it should be in working conditions.

I attach the sketch and the actual breadboard picture with schematics.
Tks
F


// Sketch PROGETTO 12 - FS20200217

#include <Servo.h> // import the library
Servo myServo; // create an instance of the Servo library

const int piezo = A0; // pin the piezo is attached to
const int switchPin = 2; // pin the switch is attached to
const int yellowLed = 3; // pin the yellow LED is attached to
const int greenLed = 4; // pin the green LED is attached to
const int redLed = 5; // pin the red LED is attached to

int knockVal; // variable for the piezo value
int switchVal; // variable for the switch value

// variables for the high and low limits of the knock value
const int quietKnock = 10;
const int loudKnock = 100;

boolean locked = false; // variable to indicate if locked or not
int numberOfKnocks = 0; // how many valid knocks you've received

void setup() {
myServo.attach(9); // attach the servo to pin 9

// make the LED pins outputs
pinMode(yellowLed, OUTPUT);
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(switchPin, INPUT); // set the switch pin as an input

Serial.begin(9600); // start serial communication for debugging

digitalWrite(greenLed, HIGH); // turn the green LED on
myServo.write(0); // move the servo to the unlocked position

Serial.println("the box is unlocked!"); //DEBUG print status to the Serial Monitor
}

void loop() {
// if the box is unlocked
if (locked == false) {
switchVal = digitalRead(switchPin); // read the value of the switch pin

if (switchVal == HIGH) {
locked = true; // if the button is pressed, lock the box i.e. set the locked variable to "true"

// change the status LEDs
digitalWrite(greenLed, LOW);
digitalWrite(redLed, HIGH);
myServo.write(90); // move the servo to the locked position

Serial.println("the box is locked!"); //DEBUG print status to the Serial Monitor

delay(1000); // wait for the servo to move into position
}
}

// if the box is locked
if (locked == true) {
knockVal = analogRead(piezo); // check the value of the piezo

if (numberOfKnocks < 3 && knockVal > 0) { // if there are not enough valid knocks
if (checkForKnock(knockVal) == true) { // check to see if the knock is in range
numberOfKnocks++; // increment the number of valid knocks
}
// DEBUG print status of knocks
Serial.print(3 - numberOfKnocks);
Serial.println(" more knocks to go");
}
}

// if there are three knocks
if (numberOfKnocks >= 3) {
locked = false; // unlock the box
myServo.write(0); // move the servo to the unlocked position
delay(20); // wait for it to move

// change status LEDs
digitalWrite(greenLed, HIGH);
digitalWrite(redLed, LOW);

Serial.println("the box is unlocked!"); //DEBUG

numberOfKnocks = 0;
}
}

// this function checks to see if a detected knock is within max and min range
boolean checkForKnock(int value) {
// if the value of the knock is greater than the minimum, and larger
// than the maximum
if (value > quietKnock && value < loudKnock) {
digitalWrite(yellowLed, HIGH); // turn the status LED on
delay(50);
digitalWrite(yellowLed, LOW);

Serial.print("Valid knock of value "); // DEBUG print out the status
Serial.println(value);

return true; // return true
}
// if the knock is not within range
else {

Serial.print("Bad knock value "); // DEBUG print status
Serial.println(value);

return false; // return false
}
}

flavios:
Cannot check straight away the motor alone. I used in a previous project so it should be in working conditions.

It worked before.
It does not work now.
I think you should check the motor alone.
Unplug all the wires from the arduino. (take more pics before so you can plug it back in the same place after testing)
Plug in the servo.
Load up the sweep tutorial.
Let us know what happens.

You may need to provide more power to servo motor by using the extra power source

As advised I went to run the servo test and saw my mistake. I inverted polarity on the servo (as you can see in the picture). Stupid of me. >:(

But the SWEEP sample -very to the point for a test!!!(thks) - does not work either. Even - I put a 9V battery on the side. Will try again using "extra power" (should I connect 4xAA batteries?) . Is it possible that exposure to inverted polarity fried the servo?

I should buy a new one and run the tests afresh. Any advice of which one? Given the opportunity I could buy a step by step motor with low power but more versatility (360°?) )that will work with the library?

Tks
F

If you use the extra power for servo motor, You need to connect GND together, See how to use multiple power source

Tks this clarifies a lot for future tests and applications.

Saved you link for much needed reference. You inspired me to buy a "three pack" :wink: - and also for other actuators "extra power" should be key.

I imagine that in real life the use of servo motors is not limited to one line but multiple.
F

Its a hobby servo, not a servomotor (very different beast), and hobby servos need at least 1A of current for
even the smallest. You can't skimp on power for motors or servos.

flavios:
You inspired me to buy a "three pack" :wink:

Any time I destroy a component (let out the magic smoke) I try to purchase multiples.
If I broke it once, I might break it again and rather than have more delay in the project, multiples let me move forward.