Search code examples
arduinoteensy

Read Arduino Hardware specifications in C++


I recently started into C++ and Arduino Coding which makes a lot of fun! I have different boards for example an Arduino UNO R3, Arduino Mini and an Arduino compatible Teensy 3.2.

I'd like to make my program to automatically select specific pins. Over the EEPROM library (not sure if library is the right term, as I said im new to c++) I can get the EEPROM size of each board. Is there any way to get other datas as how many digital/analgs pins the board has which can be used as Input and Output pins?


Solution

  • The number of digital pins is defined in the NUM_DIGITAL_PINS macro.

    The number of analog input pins is defined in the NUM_ANALOG_INPUTS macro.

    The macros are located at(added by request of OP):

    Uno: https://github.com/arduino/Arduino/blob/1.6.12/hardware/arduino/avr/variants/standard/pins_arduino.h#L28-L29

    #define NUM_DIGITAL_PINS            20
    #define NUM_ANALOG_INPUTS 6
    

    Mini: https://github.com/arduino/Arduino/blob/1.6.12/hardware/arduino/avr/variants/standard/pins_arduino.h#L28

    #define NUM_DIGITAL_PINS            20
    

    and

    https://github.com/arduino/Arduino/blob/1.6.12/hardware/arduino/avr/variants/eightanaloginputs/pins_arduino.h#L25

    #include "../standard/pins_arduino.h"
    #undef NUM_ANALOG_INPUTS
    #define NUM_ANALOG_INPUTS 8
    

    Teensy 3.x: https://github.com/PaulStoffregen/cores/blob/1.31/teensy3/core_pins.h#L97-L127

    #if defined(__MK20DX128__)
    
    #define CORE_NUM_TOTAL_PINS     34
    #define CORE_NUM_DIGITAL        34
    #define CORE_NUM_INTERRUPT      34
    #define CORE_NUM_ANALOG         14
    #define CORE_NUM_PWM            10
    #elif defined(__MK20DX256__)
    #define CORE_NUM_TOTAL_PINS     34
    #define CORE_NUM_DIGITAL        34
    #define CORE_NUM_INTERRUPT      34
    #define CORE_NUM_ANALOG         21
    #define CORE_NUM_PWM            12
    #elif defined(__MKL26Z64__)
    #define CORE_NUM_TOTAL_PINS     27
    #define CORE_NUM_DIGITAL        27
    #define CORE_NUM_INTERRUPT      24  // really only 18, but 6 "holes"
    #define CORE_NUM_ANALOG         13
    #define CORE_NUM_PWM            10
    #elif defined(__MK64FX512__)
    #define CORE_NUM_TOTAL_PINS     64
    #define CORE_NUM_DIGITAL        64
    #define CORE_NUM_INTERRUPT      64
    #define CORE_NUM_ANALOG         27
    #define CORE_NUM_PWM            20
    #elif defined(__MK66FX1M0__)
    #define CORE_NUM_TOTAL_PINS     64
    #define CORE_NUM_DIGITAL        64
    #define CORE_NUM_INTERRUPT      64
    #define CORE_NUM_ANALOG         25
    #define CORE_NUM_PWM            22
    #endif
    

    and

    https://github.com/PaulStoffregen/cores/blob/1.31/teensy3/pins_arduino.h#L157-L158

    #define NUM_DIGITAL_PINS  CORE_NUM_DIGITAL
    #define NUM_ANALOG_INPUTS CORE_NUM_ANALOG
    

    The will be found in similar locations for other boards. You just need to check the build.variant value set in boards.txt for that board and then go to the folder of the same name under the variants folder of the board's platform.

    You might find some other useful things by looking through those variant files.