2

Following the arduino starter kit, one of the projects uses a push button to on/off a motor.

In the end they suggest to try to use a potentiometer to control the speed of the motor. I already did it, but I am not sure how I should use the capacitors(I placed one with the pot).

In one of the previous examples they used 2 100uF capacitors(one for the pot. and another for a servo motor) and I was wondering if this would be the same case.

This is my circuit: enter image description here enter image description here enter image description here

The code:

const int POT_PIN = A0;
const int MOTOR_PIN = 9;
int motorSpeed = 0;
int potVal = 0;

void setup()
{
  pinMode(MOTOR_PIN, OUTPUT);
}

void loop()
{
  potVal = analogRead(POT_PIN);

  motorSpeed = map(potVal, 0, 1023, 0, 255);

  analogWrite(MOTOR_PIN, motorSpeed);
}

Previous example with capacitors: enter image description here enter image description here

Code:

#include <Servo.h>

Servo myServo;

int const POT_PIN = A0;
int potVal;
int angle;

int alpha = 0.8;
int rawReading;

void setup()
{
  myServo.attach(9);

  Serial.begin(9600);
}

void loop()
{
  rawReading = analogRead(POT_PIN);
  potVal = alpha*potVal + (1-alpha)*rawReading;
  Serial.print("potVal: ");
  Serial.print(potVal);

  angle = map(potVal, 0, 1023, 0, 179);
  Serial.print(", angle: ");
  Serial.println(angle);

  myServo.write(angle);
  delay(15);
}
6
  • You shouldn't need capacitors with either in this case. Perhaps if you showed the "previous example" we could explain why they were needed there. Sep 21, 2014 at 15:44
  • I added the info about the other example.
    – blfuentes
    Sep 21, 2014 at 15:59
  • I don't see any use for that capacitor at the potentiometer. I would probably add one in parallel to the 9V battery.
    – Gerben
    Sep 21, 2014 at 16:39
  • 1
    As far as the capacitor for the potentiometer, it is probably there to keep the analog value smooth to avoid small amounts of interference (chaining 0.0048828125 of a volt) that would cause the motor to seem a little jumpy @Gerben. A software solution would work fine for this purpose, too. Sep 21, 2014 at 21:36
  • @AnnonomusPenguin in that case I'd expect an smaller cap between ground and the output of the potentiometer.
    – Gerben
    Sep 22, 2014 at 18:33

2 Answers 2

4

Ah, now I see. Your confusion in this case is perfectly understandable. They threw a couple of capacitors into the circuit without explaining why they were added in the first place. In order to understand why capacitors were added you need to go a little deeper into electronics theory, specifically capacitance and inductance, what they are and how they work.

Inductance is, to put it succinctly, resistance (not related to the resistance in resistors, measured in ohms) to change in current by sacrificing or absorbing voltage. If current is 0, then inductance will try to keep it at 0, if at 1A then it will try to keep it at 1A, and so on. Inductance is caused by the magnetic fields generated by electricity flowing through a conductor, and hence every conductor exhibits some measure of inductance. (Coils have much higher inductance though, since the magnetic fields of the loops reinforce each other. And a "core" material such as ferrite or iron intensifies the magnetic field.)

Now, this isn't a large problem if your conductors are short, but as they get longer their inductance can cause power issues. Sudden power demand increases are unable to be accommodated because the supply can't put the increased current through the inductance quickly enough. This is where capacitance comes in.

Capacitance is resistance to change in voltage by sacrificing or absorbing current. When something tries to reduce the voltage either through power consumption or because of high-speed switching, capacitance holds the voltage as close to constant as it can by pulling or dumping electricity out of or into the circuit. Capacitance is caused by charges between two conductors attracting each other through a non-conductive material.

So what happens is that a circuit needs bursts of power to work, and if the wires connecting the supply to the circuit have too much inductance then the circuit will be starved of power. We balance this by placing a capacitance close to the circuit so that it can supply power to the circuit for the short time while the supply is busy overcoming the inductance of the wires.

The servo, being a mechanical device, needs a decent amount of power. We put a bulk capacitor near it on the supply lines to help provide this power. The analog input connected to the potentiometer needs much, much less power, so the inductance there isn't a problem and we forgo the capacitor there.

2
  • @blacai A pity the long explanation, while it contains some truth, is irrelevant. A 100µF capacitor would have negligible impact in maintaining power (as a simple calculation of the energy stored would show). Maybe if it was 10,000µF. The capacitors serve to reduce interference on the supply, which could affect the microcontroller, but you would be hard pressed to find any difference if you omitted them.
    – Milliways
    Sep 22, 2014 at 6:55
  • In your opinion the previous example with 2x100µF, those are not needed?
    – blfuentes
    Sep 23, 2014 at 6:25
0

Do you want to use a pushbutton to control the speed of the motor?

Then one way to go would be to increment a variable by say 10 every time the switch is pressed.

In pseudocode:

// In the setup section:
speed =0

// In the main loop:
Read pushbutton
if different from previous state then speed = speed + 5
if speed > 255 then speed = 0 // Go back to zero if over 255

analogWrite(pin x, speed)  // This sets the PWM value

If you want to have a more fancy control then you could increment speed every second by 5 while the pushbutton remains pressed, and also go back to zero speed in small decrement instead of in a single step.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.