Tuesday, January 31, 2012

Why doesn't my circuit work?

Okay, so I finally managed to build something that didn't work! This is actually neat, since it represents a problem-solving challenge to be overcome, and something that goes beyond colouring in the lines. Well, apparently I couldn't colour in the lines, which is how I got here.

It looks like a fairly simple circuit. Basically, the motor is connected to ground at one end, and via a transistor to a 5V source at the other. Pin 9 there goes to the transistor, so pulsing that slowly ought to turn the motor on and off. There is also a diode, and I don't know what the hell it's for. To be more accurate, I don't know why it's necessary here, or what it's doing in this circuit. In general, the concept seems straitforward enough. I'm also not 100% confident I have the diode, the transistor or the various resistors the right way around.

The resistors, I'm pretty sure, could get plugged in backwards and still operate normally. The diode I realise will simply not allow current passed if plugged in backwards. Given that if I skip the transistor the motor spins, the diode 'must' be the right way in (assuming it's working correctly). So the fault seems to be with the transistor.

However, no matter how I connect the  transistor, it doesn't work correctly. One way, the motor is dead. The other way, the motor spins constantly. Total ignoring of the collector (Pin 9 there). Question: is it possible to break a transistor by sending current the wrong way through? How about with diodes?

Now, I did check. The transistor in NPN, which as far as I can tell translates as "on by default", so there is one explanation: Pin 9 is broken. I tried Pin 6, no luck. So I tried coding up a flashing LED to match the Pin 9 signal. The LED flashes as required, so unless both Pin 9 and Pin 6 are physically damaged on the Arduino, it shouldn't be that.

So, now I'm stuck. Without a multimeter (or maybe just some LEDs and alligator clips) I'm pretty much unable to determine which parts of my circuit are receiving the expected current. Did I short something out? Is my diode in backwards? What current is coming out of Pin 9?

Answers to all these exciting newbie questions, and more, in weeks to come.

In the meantime, to all you electrical engineers out there, I hope you enjoyed this brief sojourn into electrical bafflement :)

PS: Some people who spotted these photos on G+ have pointed out that the diode is likely present to protect against a back-spike of power coming FROM the motor as it spins down.


  1. http://en.wikipedia.org/wiki/Flyback_diode has a good explanation of why the diode is there (essentially: to prevent a large voltage spike in response to a discontinuity in current through an inductor).

  2. Also, if you switch to using pin 1 or 0, then you will light up the tx/rx LEDs on the board.

  3. Were you doing CIRC-03 from the Oomlaut/SparkFun inventor's guide? I guess the lesson sort of has a built-in additional lesson in errata and the slowness of bugfixes in the physical/electronics world... From http://www.sparkfun.com/products/10173 :

    "Note: There is an error with CIRC-03. Omit the 10K resistor and hook pin 9 directly to the base of the transistor."

  4. Hmm. An NPN transistor probably ought to have it's emitter connected to ground and the collector connected to one lead of the motor with the other lead of the motor connected to V+. You would then control the transistor with the base.

    You should know that the base-emitter junction is basically a diode. When you induce a current through that diode, you cause the collector to emitter path to turn on and pass current at a much higher rate (say 200x, this amplification is called Hfe or beta or gain).

    So, if your motor and transistor is hooked up as I describe above, you could attach the base to a logic pin via a resistor (say 1Kohm) and control the motor that way, turning it on with a logic 1 and off with a logic 0.

    Usually, motor control is done this way, switching the low side, when using an NPN transistor. You can do it the other way with a PNP transistor and switch the high side.

    And, yes, usually a diode is placed in parallel, reversed biased, with the motor (or across the collector/emitter terminals) in order to sink current spikes when the transistor shuts off.

    I hope that helps.

  5. Thanks all... the real world is certainly a lot trickier than I had hoped! :) The comments have really helped me improve my understanding of what's going on.

    I've also been reading up a bit more on motor control. It seems like I will need to build up to creating an H-Bridge circuit for each motor to get best control over motion. However, in the meantime I might go with something super-simple just to have the excitement of having the robot, you know, move. Even a little bit. :)