The answer to your question is basically yes, you can use ATmega I/O pins as +/- power supply terminals for other circuits. But there's a limit to what you can power that way (20mA per pin and 100mA total per ATmega). In practice this means you can only power the simplest circuits (single LEDs per pin, and not many LEDs in total, for example). As a general rule, you should really look for other ways to power anything else.
Also, there's a problem with the citation you gave. That 40mA figure is the absolute maximum current per pin, but that should be avoided. Safest is to use about 20mA of maximum current per pin. Also, the ATmega pins should not have more than 200mA flowing through Vcc and GND (also an absolute maximum that should also be avoided).
Usually, you should either power other stuff off of the regulated +5V that also feeds the ATmega (up to the regulator capacity that depends on the Arduino board - 500mA for the Arduino Uno - 1A for LM7805 regulators - and so on). You could also use the unregulated power in some cases. Then you should use digital pins on the ATmega to switch transistors on and off which in turn will switch your load. Sometimes you can use transistors to switch relays, that in turn will switch even larger loads.
Answer from Ricardo on Stack ExchangeVideos
The answer to your question is basically yes, you can use ATmega I/O pins as +/- power supply terminals for other circuits. But there's a limit to what you can power that way (20mA per pin and 100mA total per ATmega). In practice this means you can only power the simplest circuits (single LEDs per pin, and not many LEDs in total, for example). As a general rule, you should really look for other ways to power anything else.
Also, there's a problem with the citation you gave. That 40mA figure is the absolute maximum current per pin, but that should be avoided. Safest is to use about 20mA of maximum current per pin. Also, the ATmega pins should not have more than 200mA flowing through Vcc and GND (also an absolute maximum that should also be avoided).
Usually, you should either power other stuff off of the regulated +5V that also feeds the ATmega (up to the regulator capacity that depends on the Arduino board - 500mA for the Arduino Uno - 1A for LM7805 regulators - and so on). You could also use the unregulated power in some cases. Then you should use digital pins on the ATmega to switch transistors on and off which in turn will switch your load. Sometimes you can use transistors to switch relays, that in turn will switch even larger loads.
Step one: read the data sheet for the microcontroller. In there is a nice diagram that shows you exactly how the IO pins work.
The pins can work in 2 modes: input, and output. You're interested in output mode.
The drive state of a CMOS output pin consists of two MOSFETs, one connecting the pin to Vcc and the other connecting it to GND. Only one of the MOSFETs is ever turned on at a time, so the output pin is either connected to Vcc through one MOSFET, or connected to GND through the other.
When it's connected to Vcc it's said to be sourcing current, since it's at a positive potential compared to ground and current can flow out of the pin to light an LED (say), and when it's connected to GND it's said to be sinking current because current can flow into the pin to get to ground.
As an example of how it all works, consider how you would connect up and power a capacitative humidity sensor.
These sensors require a square wave at around 1KHz to operate. The humidity defines the impedance, and as part of an impedance divider (like a resistor divider) the output voltage is relative to the humidity.
Now, they don't just want any square wave, but require a square wave that reverses polarity around a virtual ground point, and that means reversing the polarity of the power across the sensor at 1KHz. How can you do that with an Arduino? The answer is simple:
Connect the sensor to two IO pins rather than 1 IO pin and GND.
Both pins are set as output, and one is set HIGH with the other set LOW. So the voltage across it is 5V.
Now you switch the outputs over, so the one that was HIGH is now LOW, and the one that was LOW is now HIGH. The power across the sensor has been reversed! It's now (effectively) -5V (compared to what it was before).