If I'm running out of pins on an Arduino, short of buying a second one or getting a Mega board, is there a way to get more connected to the one board? Do I need to worry about overloading it?
8 Answers
Have you used up all your analog pins as well? They can also be used as digital input/output by just referring to them as digital pins pins 14 to 19.
After you used all of them as well you might consider using a shift register IC. These chips will convert serial data into parallel data. The 8 bit versions will take serial data on three pins and output parallel data on 8 pins, giving you 5 extra pins. A very nice explanation is done by Dave Clausen at NYC Resistor.
There is actually a possibility of overloading your arduino. Not by using too much pins, but by drawing too much current from the pins, by hooking up too much stuff to your pins. The Atmega168 datasheet says that the pins have an absolute maximum current of 40mA. This would be equivalent to two LED's in parallel. Anything more than one LED (20mA) should be switched through a transistor or transistor array. This is also explained in the video mentioned above and by Tom Igoe.
-
\$\begingroup\$ Also, the ATMega168 has a maximum current of 200mA through the Vcc and Gnd pins (each), so driving or sinking more than ten LEDs through an Arduino is also probably a bad idea (given the common current of 20mA used to light LEDs). \$\endgroup\$– LouCommented Nov 4, 2009 at 21:12
Sparkfun sells a digital/analog multiplexer that allows 16 pins to be controlled by five on the Arduino. It is bidirectional so you may also use it for input.
I've been toying with using this... http://www.neufeld.newton.ks.us/electronics/?p=241 Arduino I/O expansion using I2C. Looks real promising, I havent gotten around to ordering the chips yet. It's also a really good tutorial on using I2C.
If you do end up getting another microcontroller, I like the sanguino for at least four reasons:
- it has a bunch more I/O than the Atmega 168/368 (32 general purpose pins, up from 14). Like the Arduino, 8 pins can do ADC, 6 can do PWM, but there is an additional hardware serial port.
- it's $5 cheaper*,
- with 64K of flash, I never even come close to running out of memory, and
- you get to put it together yourself from a kit.
The Arduino Mega does have more PWM pins -- 14, but I don't think you'd multiplex those.
--
*Sanguino seems to go for $25, Arduino for $30, although you'll need a $20 USB-to-TTL cable if you need to use a USB port to program it, where the Arduino has USB built in.
I wrote a library to control DS2406 1wire switches.
With one of these, you can use a single pin to switch a much larger number of devices. It'll be slower than flipping the IO pin directly, of course, and it'll use more memory since you have to address the devices over that port (this can be traded off for more speed if you'd like).
There are many options for expanding the number of inputs and outputs you have available. For an analog input it is common to use analog multiplexers (like the 4051, available as 74HC4051 and CD4051) which allow you to switch between 8 analog sources using 3 digital lines and 1 analog input.
One commonly used option for digital IO is to add TTL (or CMOS) shift registers. Shift registers come in two main varieties, the "serial in, parallel out" (like the 74HC595) which are useful for extending the number of digital outputs your project has and "parallel in, serial out" (like the 74HC165) which allows you to add more digital inputs.
Tom Igeo has a nice write up on Controlling Lots of Outputs from a Microcontoller
You can overload the Arduino's output by trying to 'fanout' too far. Each IC you connect to an output draws current from that output. If you load lots of ICs onto the same output (or set of outputs) they might try to draw more than the Arduino's outputs can handle. To remedy this problem you can place non-inverting buffer chips (like the 74LS07) between some of the ICs.
If you are driving LEDS, you might look into charlieplexing.
This product is a simple and cheap way to add pins to your Arduino. There is an easy-to-use open source library for accessing the new pins: