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!