10

I want an I/O pin to return current in a circuit, similarly to the function of ground.

Which of these will achieve what I require?

pinMode(pinnumber, OUTPUT)
digitalWrite(pinnumber, LOW)

or

pinMode(pinnumber, input)
rajat
  • 203
  • 1
  • 2
  • 7

1 Answers1

9

Your first snippet is the correct solution.

pinMode(pinnumber,OUTPUT)
digitalWrite(pinnumber,LOW)

Be careful though, you can only sink up to 6mA or 9mA per pin (as per the documentation ["Input and Output" section], I never tried more). If you need more current, use a transistor (this looks like a good example, you just need to invert the output pin to HIGH).

When the pin is configured as input it is supposed to be high impedance, not what you want.

FredP
  • 248
  • 2
  • 6
  • Thanks, the current also comes from the IO pins of Arduino. Therefore, i don't think 40mA limit will be a issue. – rajat Mar 05 '14 at 10:30
  • @rajat Doing some sort of LED matrix ? Be careful, assuming you plug (for example) a high current LED between two pins (at least without a proper current limiting resistor), you could still damage the chip. Not even mentioning the case where you create a short circuit. – FredP Mar 05 '14 at 10:35
  • It is a pressure sensor matrix, it has 10 16:1 multiplexers. The current only comes from the Arduino USB cable and no external power supply.Therefore, i don't think high current will be a issue. Can you shed some light on what possibly can cause a short circuit? – rajat Mar 05 '14 at 10:37
  • @rajat something like that would create a short if pin "1" and "2" are connected : pinMode(pinnumber1,OUTPUT); pinMode(pinnumber2,OUTPUT); digitalWrite(pinnumber1,LOW); digitalWrite(pinnumber2,HIGH); PS : do not try this at home, do not harm innocent arduini, do not sue me, etc... – FredP Mar 05 '14 at 10:43
  • i was doing exactly this right now. Runs to check if innocent Arduino is fine. Thank god, it's fine :). Why would this damage the Arduino ? So, we can't supply any current when the pin is configured as OUTPUT? – rajat Mar 05 '14 at 10:55
  • PS (oops too late for edit) : read "connected directly", just to prevent any misunderstanding – FredP Mar 05 '14 at 10:56
  • @rajat If you do as I (do not) suggest, basically it's like plugging a wire between the + and -/0/GND poles of your battery/power supply. Except here the wire is your chip. If there is (sufficient) resistance between the pins you should be ok. But I'm starting to suspect some confusion here, maybe you could post another question with (the relevant part of) your schematic. – FredP Mar 05 '14 at 10:59
  • 2
    I was a bit surprised by the 6/9mA number. Turns out you’re correct for the Arduino Due, but for the AVR based Arduinos (Uno, etc), which I suspect are still in the vast majority, that number is more like 40mA. – microtherion Mar 06 '14 at 21:55
  • @microtherion Indeed, I was similarly mistaken originally (see the editing history), the Due uses a rather different chip than the other Arduini. – FredP Mar 07 '14 at 07:14
  • Setting a digitalPIN to LOW give 0V. Not GND!! – Peter Apr 23 '21 at 13:08