relay and electromagnet problem

Im using the arduino to activate a relay which in turn is activating an electromagnet. the relay is powered off the 5v from the arduino and the magnet the vin which in this case is 9v. and yes i have the transistor circuit for the relay.

whenever i try powering the arduino with only one power source and then activate the coil, the arduino suddenly resets. what could be causing this?

my only guess is that the coil is drawing to much power away from the arduino and so it simply resets. i suppose a resistor between the coil and vin could solve this but im not sure. any ideas?

thanks

heres my code btw:

#include <MegaServo.h>
MegaServo servo1;
MegaServo servo2;
MegaServo servo3;
int ledPin = 13;
int inPin = 5;
int val = 0;
int relay = 7;

void setup()
{

pinMode(ledPin, OUTPUT);
pinMode(inPin, INPUT);
pinMode(relay, OUTPUT);

}

void loop()
{
val = digitalRead(inPin); // read input value
if (val == HIGH) {
digitalWrite(ledPin,HIGH);

servo1.attach(9);
servo2.attach(10);
servo3.attach(11);
servo1.write(45);
servo2.write(45);
servo3.write(35);
delay(2000);
servo1.write(28.5);
servo2.write(28.5);
servo3.write(18.5);
delay(500);
digitalWrite(relay,HIGH);
delay(3000);
servo1.write(45);
servo2.write(45);
servo3.write(35);
delay(2000);
servo1.write(55);
servo2.write(55);
servo3.write(25);
delay(1000);
servo1.write(65);
servo2.write(65);
servo3.write(15);
delay(3000);
servo1.write(29);
servo2.write(29);
servo3.write(-21);
delay(3000);
digitalWrite(relay,LOW);
servo1.write(45);
servo2.write(45);
servo3.write(35);
delay(2000);
digitalWrite(ledPin,LOW);

servo1.detach();
servo2.detach();
servo3.detach();
}
}

Try using two different voltage sources.

hi budsiskos,

As pakrats suggest and you have surmised, your resets are probably due to power supply issues

There are some things in your code that are not causing the resets but should be be there.

You are writing a value of -21 to a servo, valid values must be greater than or equal to zero.

Also, you trying to write a floating point value to the servos (28.5), this will converted to an integer (28) by the compiler but the decimal point values should be removed so you don't confuse yourself

Why are you detaching the servos at the end of your loop? Servos loose their holding power if they are not pulsed and usually should be attached in setup and remain attached throughout.

Agree on the separate power supplies approach. Alternatively, you can try a big capacitor (>1000uF or so) on the Vin line to supply that instantaneous burst of current needed to turn the stuff on.

well no matter the servo values they work out and -21 is a working output that goes much further than 0. the 28.5 was a long shot but i was trying to get it to be very accurate.

i only want to use 1 power-supply.

well no matter the servo values they work out and -21 is a working output that goes much further than 0.

That capability is not intended or documented. The documentation for servo write method states: "the value to write to the servo, from 0 to 180" .
If you do want to change the range of the servo you should use the attach method to change the minimum pulse width. The undocumented functionality you are using will probably disappear in a later release of the library.

(see http://www.arduino.cc/en/Reference/Servo for help with the library methods common to Servo and MegaServo)

the 28.5 was a long shot but i was trying to get it to be very accurate.

the value after the decimal point will be ignored, the servo will do the same thing as if you entered the integer value 28

i only want to use 1 power-supply.

you need to make sure your supply is capable of powering all your devices. Its difficult to provide more advice on this without knowing how much current all your devices use.

As above, your "1 power supply" is going to be an issue if you don't make it work like a 2 power supply.

If you are using a standard 9V battery, they do not give you very much instantaneous current, and as such your coil will rob the rest of the circuit.

Take 2 diodes from the battery, and wire one to your Arduino as normal, and the other will serve as the feed for your coil. Throw a lower ohmage, higher wattage resister in line after the diode. 47[ch937] at 2 Watts for ex. The other side of that resistor is your new vin for your coil. Throw a 3300µF cap across that and ground.

It will take a few seconds to charge the cap (200ma max drain now) but when the coil fires, your Arduino shouldn't reset.

This will work fine but depends on timing between firings.

If it sounds like a lot extra, its not really, but its necessary if you want a single PS to do both.