Quick help please: servo and resistor

Hi Guys,

Can you give me a quick help please. I am trying to synchronize a servo with a push button: when I press button the servo starts to move to a certain position and hold there, when I press again it moves back to initial position accordingly. I think the code is working because the servo did what i expected. However there is one thing I am not sure, when I upload the code to servo it will make a 'buzz' noise and I feel vibration from the servo. Also during the waiting time between two 'button pressing', it will again make that 'buzz' noise and vibration.

I am not sure if this is something to do with my code, or the servo itself. The 'buzz' and vibration make me guess if the servo received an order (code) to turn anti-clock and clockwise simultaneously, so it couldn't decide which way to go and herein vibrated in the beginning (once code uploaded before any pressing) and during the waiting time (between two pressing). Just my guess.

This is my servo code (I imported a servo code called VarSpeedServo from internet), can you see anything wrong with it ?

VarSpeedServo servo1;          // create servo object to control nozzle angle

// Assign Pins 
const int servo1Pin = 9;         // the output pin to control servo1

void ServoSweep(){
  Reading = digitalRead(ButtoninPin);
  if (Reading == HIGH && CurrentTime - ServoTime > DebounceTime)           
  {
    ServoTime = millis();
    servo1.write(30, 100, true);  // rotate servo1 to 30, speed of 100, wait until done (servo1 to open nozzle gate, action must be synchronized with solenoid and LED) 
  }
  if (  ( CurrentTime - ServoTime) > DischargeTime )
  {
    servo1.write(0, 100, true); // rotate servo1 back to 0, speed of 100, wait until done (again action must be synchronized)
    ServoTime = millis();
  }
  LastReading = Reading;
}

And another thing is, can you teach me or give me a simple example to calculate the resistor required for the circuit ? For example, in the case of LED connection, say Arduino supplies +5V and can not take current more than 40mA, if I connect Arduino to a standard small LED (don't know its resistance), how to work out the resistor required to be connected with the LED ? And how about the connection with a solenoid of 12V DC (120mA) ?

Thank you very much !

Regards, Jeff

Hi Guys,

I guess I might know the answer of my first question: the servo is also connected to a capacitor, and when I slightly knock the capacitor and those connected jumper wires, the buzz and vibration will disappear for a while and then come back…maybe I should check my connections ? I have attached a picture of how I connect my servo. Thank you.

Before you do anything, give the servo its own power. It should never be powered from the Arduino’s 5V. Servo “funnies” can often be attributed to insufficient power: the Arduino voltage may be ok, but there’s likely to be more current required than the Arduino can supply.

See pic attached…

Many servos- small V2.jpg

Servo's do make a buzzing sound sometimes, just because of how it is built and works, so this is normal. if you want to stop the buzzing, the best way is to call servo.detach() when you know you are done with it. You could also try slightly different angles to see if it cures the buzzing, but once it is under load, the buzzing will start again.

To wire up your LED really depends on the forward voltage of the LED. This is usually anything from 2 to 3.5 volts, so you would subtract that from your supply voltage and then use ohm's law to calculate the appropriate resistor. most "regular" LED's usually take a max of 20mA but is pretty bright from 10mA onwards. Anything between 125 and 1000 ohm should give you a decent glow on a regular LED. Go big, and reduce it if you think it can go brighter. If it stops shining, you've gone too small :-)

To drive your solenoid you would need a transistor, and a seperate supply for the 12V. If you use a NPN transistor, you can connect the base of the transistor through a resistor (once again depends on the transistor, but 500-1000 ohm should work) to your Arduino, connect the emitter to both the arduino ground and the 12v ground, and connect the solenoid between 12v and the collector. You may also want to connect a diode across the solenoid with the cathode (the stripy side) to the 12v side. This is to help discharge the coil when it is switched off, otherwise it could damage the transistor, or even your microprocessor.

See http://playground.arduino.cc/uploads/Learning/relays.pdf for a circuit. Replace the relay with the solenoid.