using a relay and servo with arduino

I’m having an issue with running both of these on 1 arduino. My program runs fine the first time and then it messes up… the servo just stops and starts shaking, the pump doesn’t come on anymore. I’m assuming it has something to do with using the 3.3v port on the arduino to run the pump. I am assuming I need to use the 5. port to run the servo and the relay should use less power. However I am a NOOB so I am prob wrong. ha Here is my sketch. I don’t do many sketches so excuse the mess… and my code.

#include <Servo.h>

Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created

int pos = 0; // variable to store the servo position

#include <AFMotor.h>
AF_DCMotor motor(2, MOTOR12_64KHZ); // create motor #2, 64KHz pwm

void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println(“Motor test!”);
motor.setSpeed(200); // set the speed to 200/255

{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}

}
void loop() {

for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(180); // tell servo to go to position in variable ‘pos’
delay(1500); // waits 15ms for the servo to reach the position

delay(4000); //stay in this position 4 sec.
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees

myservo.write(0); // tell servo to go to position in variable ‘pos’
delay(1500); // waits 15ms for the servo to reach the position

Serial.print(“tick”);
motor.run(FORWARD); // turn it on going forward
delay(3000);

Serial.print(“tack”);
motor.run(RELEASE); // stopped
delay(5000);
}

Hey Tate2326,

The first thing I notice on your diagram was, you are using the Arduino 5V power out right.. I would suggest that you use a dedicated power supply for the servo as the servo may require a lot of current.

Secondly, may I know your relay module spec? maybe it needed 5V supply to turn on. You know you could always share the 5V power right?

Third, Could you used the code tag.. It would help a lot the people who's willing to help you.

forth, Add the link to the library you are using please.

Thanks,

Ashraf

My fault, didn’t know there was a code tag.

I’m using the vilros sg90 servo. I could run the servo off the external 8AA batteries I guess. Would I just connect the + to the battery +?

#include <Servo.h> 

Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
 
int pos = 0;    // variable to store the servo position 


#include <AFMotor.h>
AF_DCMotor motor(2, MOTOR12_64KHZ); // create motor #2, 64KHz pwm


void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("Motor test!");
motor.setSpeed(200); // set the speed to 200/255

{ 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
} 

}
void loop() {

 
  for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees 
                                   // in steps of 1 degree 
    myservo.write(180);              // tell servo to go to position in variable 'pos' 
    delay(1500);                    // waits 15ms for the servo to reach the position 


  delay(4000);                         //stay in this position 4 sec.
  for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees 
                                  
    myservo.write(0);              // tell servo to go to position in variable 'pos' 
    delay(1500);                       // waits 15ms for the servo to reach the position 
  


  
Serial.print("tick");
motor.run(FORWARD); // turn it on going forward
delay(3000);

Serial.print("tack");
motor.run(RELEASE); // stopped
delay(5000);
}

Here is the relay I’m using. I connect the hot line for the servo to the 8 AA battery pack… It shook really bad… however it made the relay work. Not sure what would cause that.

how about you tell us what are you trying to accomplish.. To be honest I do not see the reason why you needed to use the AFMotor library as you are connecting the motor through a relay.. I used to work in a Relay making factory.. I know that those relay done like to be toggle that fast. If for any reason you need to control the speed of the motor pump consider the use of a transistor. However if you need to control the direction too... you need a H Bridge circuit.

The reason why the relay shook was cause your are using pin 2 to control the relay right. The code that you posted show me that you are using pin 2 to make a 64Khz pwm. That causes the relay to switch on and off really fast as high as 64Khz.

from the picture i can say that your relay needed 5VDC to activate thats why 3.3V supply you use previously did not work.

I'm trying to make a duck hunting decoy that will move (the water pump...) and the head will pivot (the servo...). Both of which are on a delay and a loop.

The servo is connected through pin 9

The regulator in the Arduino isn't powerful. Better to use an external regulator. With what do you power the Arduino?

I am using 8AA batteries to power the arduino and the bilge pump. I'm trying to figure out how to power the relay switch and the servo with the same batteries.

I'm also getting this error message when trying to download my program... first time i've ever seen this error message.

Arduino: 1.6.5 (Windows 7), Board: "Arduino Uno"

Sketch uses 4,584 bytes (14%) of program storage space. Maximum is 32,256 bytes.

Global variables use 273 bytes (13%) of dynamic memory, leaving 1,775 bytes for local variables. Maximum is 2,048 bytes.

avrdude: ser_open(): can't open device "\.\COM4": The system cannot find the file specified.

#include <Servo.h>

Servo myservo;

const uint8_t servoPin = 9;
const uint8_t pumpPin = 2;

uint8_t pos = 0;
bool clockwise = true;

boolean pumpState = false;

unsigned long previousServoTime = 0;
unsigned long previousPumpTime = 0;
const unsigned long tick = 3000;
const unsigned long tack = 5000;
unsigned long pumpDelay = tick;

void setup()
{
  myservo.attach( servoPin);
  pinMode ( pumpPin, OUTPUT);
  digitalWrite ( pumpPin, pumpState);
}

void loop()
{
  unsigned long timeNow = millis();
  if ( timeNow - previousServoTime >= 15)
  {
    myservo.write( pos);
    ( clockwise) ? pos++ : pos--;
    if ( pos == 180 || pos == 0)  clockwise != clockwise;
    previousServoTime = timeNow;
  }
  if ( timeNow - previousPumpTime >= pumpDelay)
  {
    previousPumpTime = timeNow;
    pumpState != pumpState;
    pumpDelay = ( pumpDelay == tack) ? tick : tack;
    digitalWrite ( pumpPin, pumpState);
  }
  
}

If I can ever get my computer to communicate with my arduino again I will try this code. not sure what is the matter now.

hello I want to control several servo motor through 4relay and arduino,how can I set up their circuit?

freesheida1984:
hello
I want to control several servo motor through 4relay and arduino,how can I set up their circuit?

There is nothing in this thread you revived from 5 years ago that has much to do with your question. Please make a new post. Before you do that, please read the sticky threads at the top of the forum, to learn how to make an effective post.