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

Asked

Viewed 9,249 times

7

During the development of an Arduino project, I needed more digital terminals (inputs and outputs) in addition to those indicated on the board.

You can control, via software, more extra digital terminals (pins) on the Arduino Uno and Arduino Mega 2560 boards, without the need to weld something on the board, use Shields, or external components?

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, transforming analog inputs (A0, A1, A2...) into digital inputs or outputs.

For Arduino Uno, just 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);

Make the A0 terminal turn into a digital output, and write 1 to it. Finally, it also transforms the A1 terminal 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);

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

Analogue terminal names can also be used instead of numbers if desired. So, both in the Arduino Uno and in the Arduino Mega 2560, the code:

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

Make the A0 terminal turn into a digital output, and write 1 to it. Finally, it also transforms the A1 terminal into a digital input (to be used with digitalRead), as noted by @carlos-Delfino.

I hope it helps those in need of more inputs or extra outputs in the projects!

6

For use of analog ports as digital ports, simply use their names as reference in the same way as using analog ones, i.e., 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);
.....
}
  • Well remembered! I will add this information to the answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.