Problem with Program SM-S4315M motor

Learning programming
The coding of the sample example for servo motor is unable to program the sm-s4315m motor :frowning:
Need help. I wanna program it with arduino to move from 0 to 120 degree and then wait for 5 seconds then it turn from 120 degree to 0 degree.
What should i do?
#include <Servo.h>
Servo servo1; // servo control object
int position=0;

void setup()
{

servo1.attach(9);
}

void loop()
{

for(position = 0; position <=120; position += 5)
{
servo1.write(position); // Move to next position
delay(100); // Short pause to allow it to move
}
delay(5000);
// Tell servo to go to 0 degrees, stepping by 5 degree

for(position = 120; position >= 0; position -= 5)
{
servo1.write(position); // Move to next position
delay(100); // Short pause to allow it to move
}
delay(5000);
}

How is it connected? Are you trying to power it from Arduino 5V pin?

yup the connection is red to 5v, black to gnd and white to arduino pin 9

Don't power a servo (or any motor) from the Arduino 5v pin - it cannot provide enough current and you may damage your Arduino or cause it to work erratically. Give the servo a separate power supply with a common GND with the Arduino.

...R

how about the coding? the coding not working at all. the motor suddenly turn cw very fast then very slow then turn ccw then stop. it is not looping well

mickeylalaa:
how about the coding? the coding not working at all.

You MUST sort out the power supply first. If you are powering the servo from the Arduino 5v pin that could be the cause of the erratic behaviour - as I already mentioned in Reply #3

I donโ€™t immediately see a problem with your program but for the future please use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

โ€ฆR

thanks, but currently the motor is not moving when i power it with a 5v power supply. only when i connected to the arduino board of 5v then the motor is moving (unstable).
What should i look out for now? =(

mickeylalaa:
thanks, but currently the motor is not moving when i power it with a 5v power supply.

That suggests that you have not connected the motor GND to the Arduino GND.

...R

it shows zero voltage to the motor when i connect the 5v power supply from battery to red pin of motor and lack gnd to arduino gnd. hence the motor is not moving

mickeylalaa:
it shows zero voltage to the motor when i connect the 5v power supply from battery to red pin of motor and lack gnd to arduino gnd. hence the motor is not moving

Please make a simple pencil drawing showing how everything is connected and post a photo of the drawing.

And show on the drawing where you are measuring the voltage.

...R