more than 14 pins on an arduino uno

i need 18 outputs on my uno but there are only 14 is there a way to do that or should i get a mega or a duo

You can configure the analog inputs (A0-A5) as digital outputs with pinMode().

There are many ways and lots of tricks to reduce the number of pins. Tell us about your project, we probably know how to reduce the number of used pins.

What are the requirements for the pins?

  • input (freq. of change)
  • output (freq. of change)

i want to control 18 sets of 3 leds all separately from each other. also with 6 toggle switches to start individual patterns. this is what i have so far its only for 9 right now cause its a little easier to mange

// traffic adviser control 

int a = 5;
int b = 6;
int c = 7;
int d = 8; 
int e = 9;
int f = 10;
int g = 11;
int h = 12;
int i = 13;
const int button1 = 2;  
const int button2 = 3;
const int button3 = 4;
const int button4 = 14;
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;

void setup() 
{
  pinMode(a, OUTPUT) ;
  pinMode(b, OUTPUT) ;
  pinMode(c, OUTPUT) ;
  pinMode(d, OUTPUT) ;
  pinMode(e, OUTPUT) ;
  pinMode(f, OUTPUT) ;
  pinMode(g, OUTPUT) ;
  pinMode(h, OUTPUT) ;
  pinMode(i, OUTPUT) ;
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  pinMode(button3, INPUT);
  pinMode(button4, INPUT);
}

void loop(){
  
  buttonState1 = digitalRead(button1);
  
  if (buttonState1 == HIGH) {
  
  digitalWrite(a, HIGH);
  delay(150);  
  digitalWrite(b, HIGH);
  delay(150);  
  digitalWrite(c, HIGH);
  delay(150);  
  digitalWrite(d, HIGH);
  delay(150);  
  digitalWrite(e, HIGH);
  delay(150);  
  digitalWrite(f, HIGH);
  delay(150);  
  digitalWrite(g, HIGH);
  delay(150);  
  digitalWrite(h, HIGH);
  delay(150);   
  digitalWrite(i, HIGH);
  delay(150);
  digitalWrite(a, LOW);
  delay(150);
  digitalWrite(b, LOW);
  delay(150);
  digitalWrite(c, LOW);
  delay(150);
  digitalWrite(d, LOW);
  delay(150);
  digitalWrite(e, LOW);
  delay(150);
  digitalWrite(f, LOW);
  delay(150);
  digitalWrite(g, LOW);
  delay(150);
  digitalWrite(h, LOW);
  delay(150);
  digitalWrite(i, LOW);
  delay(300);
  
  }
  buttonState2 = digitalRead(button2);
  
  if (buttonState2 == HIGH) {
    
  digitalWrite(i, HIGH);
  delay(150);   
  digitalWrite(h, HIGH);
  delay(150);   
  digitalWrite(g, HIGH);
  delay(150);  
  digitalWrite(f, HIGH);
  delay(150);  
  digitalWrite(e, HIGH);
  delay(150);  
  digitalWrite(d, HIGH);
  delay(150);
  digitalWrite(c, HIGH);
  delay(150);
  digitalWrite(b, HIGH);
  delay(150);
  digitalWrite(a, HIGH);
  delay(150);
  digitalWrite(i, LOW);
  delay(150);
  digitalWrite(h, LOW);
  delay(150);
  digitalWrite(g, LOW);
  delay(150);
  digitalWrite(f, LOW);
  delay(150);
  digitalWrite(e, LOW);
  delay(150);
  digitalWrite(d, LOW);
  delay(150);
  digitalWrite(c, LOW);
  delay(150);
  digitalWrite(b, LOW);
  delay(150);
  digitalWrite(a, LOW);
  delay(500);
}
buttonState3 = digitalRead(button3);
  
  if (buttonState3 == HIGH) {
    
  digitalWrite(e, HIGH);
  delay(150);  
  digitalWrite(d, HIGH);
  delay(0);
  digitalWrite(f, HIGH);
  delay(150);
  digitalWrite(g, HIGH);
  delay(0);
  digitalWrite(c, HIGH);
  delay(150);
  digitalWrite(b, HIGH);
  delay(0);
  digitalWrite(h, HIGH);
  delay(150);
  digitalWrite(a, HIGH);
  delay(0);
  digitalWrite(i, HIGH);
  delay(150);
  digitalWrite(e, LOW);
  delay(150);
  digitalWrite(d, LOW);
  delay(0);
  digitalWrite(f, LOW);
  delay(150);
  digitalWrite(g, LOW);
  delay(0);
  digitalWrite(c, LOW);
  delay(150);
  digitalWrite(b, LOW);
  delay(0);
  digitalWrite(h, LOW);
  delay(150);
  digitalWrite(a, LOW);
  delay(0);
  digitalWrite(i, LOW);
  delay(500);
     
}
buttonState4 = digitalRead(button4);
  
  if (buttonState4 == HIGH) {
  
  
 
 
 
 
 
 
 
  
}
}

you can use - http://arduino.cc/en/tutorial/ShiftOut - to get more lines

Using 18 outputs can be done when the analog pins are used (as digital pins).
But you need also switches, so you extra hardware fpr more pins.

The ShiftOut examples as robtillaart mentioned is the best option.
You can have a lot of shifting registers cascaded. If you follow the example it is not hard to do. I didn't use the capacitor on the Latch pin.

For 18 leds, I would use 3 shiftout registers. That needs only 3 output pins for the Arduino and the result is 24 shiftout outputs.
The other Arduino pins can be used for switches and other things.

If you don't want extra hardware, and you want something very tough to code, you can combine the switches with the leds. But that will lead you into a lot of troubles. Using those shiftout registers is the normal solution to this problem.

6 toggle switches offers 64 combinations.
Do you really need that number of modes ?
If you really need only 6 modes, you can read this using 1 single analog input pin, those 6 switches and some resistors.
That will free up some pins for other uses.

I've attached an example of how to use 6 push buttons connected to one analogue pin using just 6 resistors.
This will allow you to detect when one of the buttons is pressed and released (it has a state where none are pressed). As mentioned in the diagram, you can only see one button even if two or pressed. If you do press more than one, then the highest numbered switch will take priority.
This is useful for pressing to select one of 6 modes.

@Tom
in your experience, what is the max number of switches on can connect to one analog port this way reliably?
e.g. using all 2K2 resistors?

The way the resistors are chosen is to evenly distribute the switches over the analogue range - so when you press button 6 it will be 5*(6-6)/6 volts. If you press button 4 it will be 5*(6-4)/6 volts and so on.
Because of the way the switch tree is designed to be correct even if you press multiple buttons, you end up with a whole bunch of different values to keep the analogRead() values evenly distributed. Using the same method, you can get up to 20 switches in a tree whilst keeping the buttons around 48LSB apart. The more switches you add the closer you pack them in to the analog range and you may fall foul of resistor tolerances.

I've attached an excel spreadsheet which calculates the resistor values (rounded to E12 values) for a given number of switches.

analogswitchtree.xlsx (15.8 KB)

Thanks Tom,
Could be a the basis for a simple shield

Why don't you use the resistors in parallel? That way (if you chose the resistors correctly) you can also detect switch combinations and don't have to 'give priority' to higher numbers...

fkeel:
Why don't you use the resistors in parallel? That way (if you chose the resistors correctly) you can also detect switch combinations and don't have to 'give priority' to higher numbers...

Because that way you are limited to the number of switches you can have to about 8 due to the resolution of the A/D. Also getting resistors that accurate is a problem.