small medium large xlarge

Back to: All Forums  Arduino
Generic-user-small
03 Jan 2012, 21:04
David Keasey (4 posts)

I’m new at this, so please bear with… I suspect the answer is more about programming style than fundamental characteristics of the Arduino, but I’ll ask anyway.

In a lot of the book examples you use the Arduino output pins as current sinks, rather than sources. For example, when controlling an LED, you wire the board, and set up the logic so that when a pin is LOW an LED will come on, and when that pin is HIGH the LED will be off. I’ve seen it done just the opposite, however, in tutorials from other sources.

Is there a particular reason to use the Arduino output pin as a current sink instead of a current source?

Thanks!

Maik_schmidt_avatar2_pragsmall
04 Jan 2012, 07:09
Maik Schmidt (122 posts)

Hi David!

I’m afraid I do not fully understand your question. To turn on a LED you have to set the pin it’s connected to to HIGH. That’s what I do in the examples. Do you have a particular piece of sample code that you do not understand?

Cheers, Maik

Generic-user-small
04 Jan 2012, 18:03
David Keasey (4 posts)

Sorry, Maik, but I think I got confused by a sentence in the book… I was looking at the discussion of the UnreliableSwithch.pde sketch on page 58, where you say that the led_state variable is set to LOW when the LED is on, and HIGH when it is off, and also seeing in your code that you used the led_state variable as a parameter in the digitalWrite statement.

After looking it over some more, I understand my error.

As a practical matter, however, I don’t see why one couldn’t use the pin as a sink… just hook the LED cathode to the pin, the anode to +5 Volts, and throw in a series resistor for protection. With the pin HIGH, the LED would be off; LOW would take the pin to ground and the LED should light. It makes for confusing code, but wouldn’t that work?

Maik_schmidt_avatar2_pragsmall
04 Jan 2012, 18:32
Maik Schmidt (122 posts)

As a practical matter, however, I don’t see why one couldn’t use the pin as a sink… just hook the LED cathode to the pin, the anode to +5 Volts, and throw in a series resistor for protection. With the pin HIGH, the LED would be off; LOW would take the pin to ground and the LED should light. It makes for confusing code, but wouldn’t that work?

That won’t work, because diodes - and a Light Emitting Diode is still a diode - let current pass only in one direction. So you have to wire it the right way.

Generic-user-small
04 Jan 2012, 19:12
David Keasey (4 posts)

Are you sure about that, Maik? Last I checked, if I wire the anode of a Light Emitting Diode to a +5 volt source, and the cathode to ground (through the appropriate resistor), it will light. If you wire it the other way, with the cathode at +5 Volts, the anode to ground, the LED won’t light.

Just to be sure, I wrote a quick sketch to do just this, and it does work. Just the void loop part:

void loop() { digitalWrite (12,LOW); delay(500); digitalWrite (12,HIGH); delay(1500); }

I wired the anode of the LED to +5 volts, then wired from the cathode through a 330 Ohm resistor to pin12. The sketch runs just fine, and the LED blinks for 1/2 second on, 1.5 seconds off.

Anyway, back to the basic question, is there a good reason other than programming style that one should avoid using arduino output pins as current sinks?

Thanks!

Maik_schmidt_avatar2_pragsmall
05 Jan 2012, 07:40
Maik Schmidt (122 posts)

Hm, I am surprised that you could wire the LED the “wrong” way.

Anyway, I had to do some research myself to learn about the Arduino’s sink/source behavior and I’ve found a nice explanation: http://liudr.wordpress.com/2011/10/16/sourcing-and-sinking-current/

Generic-user-small
05 Jan 2012, 18:01
David Keasey (4 posts)

Thanks for that link! I’m guessing that some microcontrollers use diode protection on their pins, which would prevent using them as current sinks. The ATMEGA chips must not do this.

Having a little programming experience, I expect that using the pins as sinks would result in some very confusing code. If one were to use this “feature” I would want to see very clear code documentation and commenting to make sure that someone reviewing the code later could have a very good understanding of what is being done and just why this unconventional approach is the best way to do it. C code can be bewildering enough as it is, especially when the programmer is showing off to demonstrate just how few lines are needed for a particular task.

You must be logged in to comment