5

Different boards have different number of pins.

Is there a way to know this number in code (at least digital and analog pins), or do I have to determine board type (like in this thread) and know the number of pins by myself?


Reasoning for this is that I'm creating prototype using Uno and using 2-3 analog pins for temperature readings, but I want to be able to use the same code for different board in the future with eg. 8 analog pins, so I don't have to remember to change pin numbers in code and maybe solder it convenient way etc. Also being able to use for eg. "last available analog pin" for certain job could help keep code/schematics nice and clean.

  • you might be able to detect floating analog inputs, but it's better to glance at the board. – dandavis Apr 3 '17 at 17:29
10

It is quite easy actually:

Serial.print("number of digital pins: ");
Serial.println(NUM_DIGITAL_PINS);
Serial.print("number of analog inputs: ");
Serial.println(NUM_ANALOG_INPUTS);

Note that these are compile-time constants defined as macros in a file named pins_arduino.h. There is one such file for every board supported by the Arduino environment. Just to take Curt J. Sampson's example, if someone “made a board with an MCU with 100 digital IO pins available, yet if only ten of these are brought out to pins on the board”, then the macros would tell you you have 10 digital pins. Assuming of course that you installed the proper support package for that particular board.

| improve this answer | |
  • Great, thats what I was looking for! But why on Uno this code returns "20 digital pins" if I see 14 (0-13)? – madneon Apr 3 '17 at 19:42
  • @madneon, My guess is that these macros are counting the 6 PWM pins as 6 additional pins. – user15483 Apr 3 '17 at 19:55
  • 2
    @madneon: because the analog inputs A0 through A6 are also the digital pins 14 through 19. – Edgar Bonet Apr 3 '17 at 19:56
  • This is very neat. Didn't know that. But does it really show all connected pins of the chip or does it just tell about how many theoretical i/o's are present on the atmega? – ansi_lumen Apr 4 '17 at 1:58
  • @ansi_lumen: it's the number of available pins on the board, not on the chip. – Edgar Bonet Apr 4 '17 at 7:25
2

In the ultimate case, a "board" is not something you can determine programmatically in software; it's not possible in general to tell if, e.g., a particular pin on the microcontroller is connected to a corresponding head pin on the board.

That said, as Edgar Bonet point out, the Arduino board definitions used in the IDE will generally do what you want. Just keep in mind, these are just someone saying "the board should be like this"; a cheap clone board, for example, might be different without a new and proper board definition.

With running code on the microcontroller itself, examining what it can see of the board, you can usually tell which microcontroller you're using. But that just tells you about the chip and its pins, and not about where leads from those pins might go. It sounds to me as if you're asking to find a way to see if your particular microcontroller (e.g., an ATmega328P) is on, say, an Arduino Uno or some other board where certain MCU pins may or may not be hooked up to board pins. The MCU can't tell whether any of its particular pins are connected to headers on the board. As a more specific example, someone could build a board with an MCU with 100 digital IO pins available, yet if only ten of these are brought out to pins on the board, the others are not easily usable. uino.cc/en/Main/ArduinoBoardUno) or [Arduino Nano]

For your application, you're thinking in a good direction when considering how to make things easier for others using your code. However, I'd suggest that in the long run it's better just to have clear definitions in an obvious place in your sketch (at the top of the file in a single-file sketch, perhaps in a config.ino in a multi-file sketch, or passed in to an initialization function for a library) that says explicitly what pins are used and is easy to change for others. Having the default pins used by the sketch move around if I load the sketch on a different board strikes me as rather confusing.

Keep in mind your audience: the people who are going to be using this on a board different from yours are Arduino hobbyists, and so they're already (or will soon become :-P) familiar with the idea of pins, pin definitions in code, and pin conflicts.

| improve this answer | |
  • I added more details in last section of question. – madneon Apr 3 '17 at 16:02
1

You can identify the host processor at run time - each one has its own signature - but not necessarily the board it's running on (consider an Uno and a breadboard-uino for example). So you'd have to make each kind of board you use distinguishable in some way, such as jumpering 3 pins each high or low to make a 3-bit binary code representing one of 8 board types. The next solution is to define that code or some other identifier at compile time with a #define statement, and use the identifier in one or more #ifdef statements to compile board-specific pin definitions.

If you plan on using standard boards the IDE knows about, the IDE pre-defines some useful symbols for you. Here's the compile command the IDE used for an empty sketch for an UNO (I've reformatted it for readability):

"/Applications/Arduino.app/Contents/Java/hardware/tools/avr/bin/avr-g++"
-c
-g
-Os
-Wall
-Wextra
-std=gnu++11
-fpermissive
-fno-exceptions
-ffunction-sections
-fdata-sections
-fno-threadsafe-statics
-MMD
-flto
-mmcu=atmega328p
-DF_CPU=16000000L
-DARDUINO=10612
-DARDUINO_AVR_UNO
-DARDUINO_ARCH_AVR

The last four lines make the equivalent of #defines for the clock-frequency, the IDE version number, the Arduino-board, and the overall MCU architecture, respectively.

So in your code, a block like:

#ifdef ARDUINO_AVR_UNO
   // pin definitions for Uno here
#endif

will only be compiled if you selected "Arduino/Genuino Uno" as your board type in the IDE. There are similar symbols for other board types. So for a Nano, ARDUINO_AVR_NANO would be defined instead.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.