Controlling extra inputs and outputs on Arduino Uno and Mega 2560 via software

7

During the development of an Arduino project, I needed more digital (input and output) terminals than those indicated on the board.

Is it possible to control, via software, extra digital (pin) terminals on the Arduino Uno and Arduino Mega 2560 boards, without the need to solder anything on the board, use shields, or external components?

    
asked by anonymous 24.04.2014 / 18:22

2 answers

6

It is possible to use more digital terminals (pins) on the Arduino Uno and Mega 2560 boards, in addition to those indicated on the board, turning the analog inputs (A0, A1, A2 ...) into digital inputs or outputs.

For the Arduino Uno, simply use the following numbers for each of the analog terminals:

  • A0: 14
  • A1: 15
  • A2: 16
  • A3: 17
  • A4: 18
  • A5: 19

For example, the code:

pinMode(14, OUTPUT);
digitalWrite(14, 1);
pinMode(15, INPUT);

Causes terminal A0 to become a digital output, and writes 1 to it. Finally, it also transforms terminal A1 into a digital input (to be used with digitalRead).

Already for the Arduino Mega 2560:

  • A0: 54
  • A1: 55
  • A2: 56
  • A3: 57
  • A4: 58
  • A5: 59
  • A6: 60
  • A7: 61
  • A8: 62
  • A9: 63
  • A10: 64
  • A11: 65
  • A12: 66
  • A13: 67
  • A14: 68
  • A15: 69

So, in the Arduino Mega 2560, the code:

pinMode(69, OUTPUT);
digitalWrite(69, 1);
pinMode(54, INPUT);

Causes terminal A15 to become a digital output, and writes 1 to it. Finally, it also transforms terminal A0 into a digital input (to be used with digitalRead).

You can also use the names of analog terminals instead of numbers, if desired. So in both the Arduino Uno and the Arduino Mega 2560, the code:

pinMode(A0, OUTPUT);
digitalWrite(A0, 1);
pinMode(A1, INPUT);

Causes terminal A0 to become a digital output, and writes 1 to it. Finally, it also transforms terminal A1 into a digital input (to be used with digitalRead), as observed by @ carlos-delfin.

I hope it helps anyone who needs extra inputs or outputs on the projects!

    
24.04.2014 / 18:24
6

To use Analog ports as digital ports, simply use their names as reference in the same way as using analog, that is, in the commands pinMode (), digitalRead () and digitalWrite () use as follows:

void setup(){
   pinMode(A1,OUTPUT);
   pinMode(A3,INPUT);
}
void loop(){
.....
leitura = digitalRead(A1);
.....
digitalWrite(A3, LOW);
.....
}
    
25.04.2014 / 16:00