-1

I made a sketch to switch on all digital and analog pins, and found out several pins did not work as expected. Note: I manually put a jumper wire from each digital and analog pin (one at a time) to a resistor, LED and GND pin.

What I noticed was that some pins did not work (D3, D4, A4, A6 and A7) and pin A3 was only lit about half intensity. I tried two different Nanos and they performed both equally (also pin A3 at half intensity).

I also tried the jumper wires directly touching the pins (without a breadboard in between, to rule out a breadboard problem). Same behavior (so the breadboard is ok).

My questions:

  1. How come pins D3, D4, A4, A6 and A7 are not output VCC (3.3V) ?
  2. How can I switch pins these pins on?
  3. What is the reason pin A3 is blinking at half intensity?
  4. How can I switch this pin fully on?
  5. Bonus question: I used numbers, why are the pin names A0..A7 and D0..D12 not defined in Arduino IDE?

Test sketch:

const int NR_OF_PINS = 22;

int pins[NR_OF_PINS]   = 
// 0  1  2  3  4  5  6   7   8   9  10 11   12  13  14  15  16  17  18  19  20  21    
//D0 D1 D2 D3 D4 D5 D6  D7  D8  D9 D10 D11 D12 D13  A0  A1  A2  A3  A4  A5  A6  A7
 { 1, 2, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 19, 20, 21, 22, 23, 24, 25, 26 };


void setup() 
{
  for (int n = 0; n < NR_OF_PINS; n++)
  { 
    pinMode(pins[n], OUTPUT);
    digitalWrite(pins[n], HIGH);
  }
}

void loop() 
{
}
Michel Keijzers
  • 12,954
  • 7
  • 40
  • 56

1 Answers1

2

You don't need an array, and your "base" pin is wrong - you start at pin 1, not pin 0. Also pins A6 and A7 can only be analog inputs, not digital outputs.

It's simpler to write:

void setup() {
    for (uint8_t i = 0; i < NUM_DIGITAL_PINS; i++) {
        pinMode(i, OUTPUT);
        digitalWrite(i, HIGH);
    }
}

void loop() {
}

That will work correctly on all Arduino boards regardless of the number of pins, since the board definition defines how many pins there are, and pins are always contiguously numbered from 0.

Majenko
  • 105,095
  • 5
  • 79
  • 137
  • Thanks for the answer (btw, as a side question, I noticed there is not a NUM_ANALOG_PINS declared). Why not? (and also I thought analog pins could be used as digital pins, but not the other way around). – Michel Keijzers Aug 24 '19 at 17:15
  • 1
    It's defined as NUM_ANALOG_INPUTS not NUM_ANALOG_PINS. – Majenko Aug 24 '19 at 17:18
  • 1
    And the NUM_DIGITAL_PINS incorporates all the analog pins that can be digital inputs and outputs. – Majenko Aug 24 '19 at 17:18
  • thanks (quite confusing to be honest, but at least now it's more clear). However, I rather have it this way... initially I thought my soldering work was a mess ;-) – Michel Keijzers Aug 24 '19 at 17:20
  • 1
    https://arduino.stackexchange.com/a/36683/37523 – VE7JRO Aug 24 '19 at 17:20
  • 1
    @MichelKeijzers Well, an analog input may not be a physical pin. There are chips around that have temperature sensors (for example) built in that connect to the ADC MUX - so an analog input exists that connects to that sensor, but it doesn't have a "pin" as such - so calling them analog pins is a misnomer. – Majenko Aug 24 '19 at 17:23
  • @Majenko ok clear (and sounds logical), I just have Arduino's and STMs ;-) ... Btw, just tested and in NUM_DIGITAL_PINS are all digital pins and analog pins [A0..A5] Pins A6 and A7 are analog only (or at least not in the range of NUM_DIGITAL_PINS). – Michel Keijzers Aug 24 '19 at 17:27