I am working on a project for STEM class at my school, and for it, I need this servo motor to spin properly. However, when I run the code below, it has zero reliability. Rarely, the motor will complete a spin. Most of the time, it just twitches when the motion sensor senses something. Sometimes, it doesn't spin at all. Any suggestions will be appreciated!
#include <SoftwareSerial.h> #include <Servo.h>
int trigPin = 2;
int echoPin = 4;
long duration, cm, inches;
Servo servo;
Why do you have SoftwareSerial.h included? You're not using it and it interferes with Servo.h.
What is on pin 13? You attach the servo to that pin then you do digitalWrites to it. Why?
Also you only ever write(180) to the servo so when it has moved to that position it's not going to move again.
What servo are you using and how is it powered. Many servo problems are caused by insufficient power like trying to use the Arduino 5V pin or connecting through a breadboard.
In particular note the advice to Auto format code in the IDE and to use code tags when posting code here as it prevents some combinations of characters in code being interpreted as HTML commands such as italics, bold or a smiley character, all of which render the code useless
If the code exceeds the 9000 character inline limit then attach it to a post
slipstick:
Why do you have SoftwareSerial.h included? You're not using it and it interferes with Servo.h.
What is on pin 13? You attach the servo to that pin then you do digitalWrites to it. Why?
Also you only ever write(180) to the servo so when it has moved to that position it's not going to move again.
What servo are you using and how is it powered. Many servo problems are caused by insufficient power like trying to use the Arduino 5V pin or connecting through a breadboard.
Steve
Hello,
Thanks for the heads up on SoftwareSerial, will fix that.
Pin 13 is where the wires for the servo are. I am still a beginner to all this, which is why there are some rookie mistakes here, but pin 13 is only for connecting up the servo to the circut.
The servo is an micro servo SG90, and is connected via the breadboard to a 5V pin. It is done this way because there is also a motion sensor involved, so the 5V needs to power both. If I can't use 5V, is there another option in powering the motor?
mrcolz:
If I can't use 5V, is there another option in powering the motor?
It requires its own power supply. If you have a 5V phone charger or any other 5V power adapter it should do the job - just remember the common ground (GND of Arduino must be connected to GND/- of the power supply).
Danois90:
It requires its own power supply. If you have a 5V phone charger or any other 5V power adapter it should do the job - just remember the common ground (GND of Arduino must be connected to GND/- of the power supply).
Thanks. Also, here is a video of my problem. It might be fixed after I try this, but here is how it started. (Keep in mind when I first start it up, the motor turns quite a bit the first couple of times a signal is given, but then it starts doing this):
If you still have problems after you have made all the suggested changes post your latest code and a photo showing clearly how things are actually connected.
slipstick:
If you still have problems after you have made all the suggested changes post your latest code and a photo showing clearly how things are actually connected.
Steve
Hello,
After doing some clean up and following some advice here, I got the motor working. Besides some obvious goofs, the main problem was that I didn't understand how the motor worked in terms of turning, so I kept sending it to 180 degrees, and never back to 0.