IDE not allowing D2, D3, etc as pin name

Sometimes I define a pin with its more explicit name like 'D2' instead of just '2'. Just found that's not working here. I'm using IDE 1.8.19.

const int myPin = D2;

void setup()
{
}

void loop()
{
}


/*
  C:\Program Files (x86)\Arduino\arduino-builder -dump-prefs -logger=machine -hardware C:\Program Files (x86)\Arduino\hardware -hardware C:\Users\terry\AppData\Local\Arduino15\packages -hardware C:\Users\terry\Dropbox\Electronics\Arduino\SKETCHES\hardware -tools C:\Program Files (x86)\Arduino\tools-builder -tools C:\Program Files (x86)\Arduino\hardware\tools\avr -tools C:\Users\terry\AppData\Local\Arduino15\packages -built-in-libraries C:\Program Files (x86)\Arduino\libraries -libraries C:\Users\terry\Dropbox\Electronics\Arduino\SKETCHES\libraries -fqbn=arduino:avr:uno -vid-pid=2341_0043 -ide-version=10819 -build-path C:\Users\terry\AppData\Local\Temp\arduino_build_893520 -warnings=all -build-cache C:\Users\terry\AppData\Local\Temp\arduino_cache_939873 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.arduinoOTA.path=C:\Users\terry\AppData\Local\Arduino15\packages\arduino\tools\arduinoOTA\1.3.0 -prefs=runtime.tools.arduinoOTA-1.3.0.path=C:\Users\terry\AppData\Local\Arduino15\packages\arduino\tools\arduinoOTA\1.3.0 -prefs=runtime.tools.avr-gcc.path=C:\Users\terry\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7 -prefs=runtime.tools.avr-gcc-7.3.0-atmel3.6.1-arduino7.path=C:\Users\terry\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7 -prefs=runtime.tools.avrdude.path=C:\Users\terry\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17 -prefs=runtime.tools.avrdude-6.3.0-arduino17.path=C:\Users\terry\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17 -verbose C:\Users\terry\Dropbox\Electronics\Arduino\SKETCHES\MY SKETCHES\MISC_or_UNASSIGNED\TestPinName\TestPinName.ino
  C:\Program Files (x86)\Arduino\arduino-builder -compile -logger=machine -hardware C:\Program Files (x86)\Arduino\hardware -hardware C:\Users\terry\AppData\Local\Arduino15\packages -hardware C:\Users\terry\Dropbox\Electronics\Arduino\SKETCHES\hardware -tools C:\Program Files (x86)\Arduino\tools-builder -tools C:\Program Files (x86)\Arduino\hardware\tools\avr -tools C:\Users\terry\AppData\Local\Arduino15\packages -built-in-libraries C:\Program Files (x86)\Arduino\libraries -libraries C:\Users\terry\Dropbox\Electronics\Arduino\SKETCHES\libraries -fqbn=arduino:avr:uno -vid-pid=2341_0043 -ide-version=10819 -build-path C:\Users\terry\AppData\Local\Temp\arduino_build_893520 -warnings=all -build-cache C:\Users\terry\AppData\Local\Temp\arduino_cache_939873 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.arduinoOTA.path=C:\Users\terry\AppData\Local\Arduino15\packages\arduino\tools\arduinoOTA\1.3.0 -prefs=runtime.tools.arduinoOTA-1.3.0.path=C:\Users\terry\AppData\Local\Arduino15\packages\arduino\tools\arduinoOTA\1.3.0 -prefs=runtime.tools.avr-gcc.path=C:\Users\terry\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7 -prefs=runtime.tools.avr-gcc-7.3.0-atmel3.6.1-arduino7.path=C:\Users\terry\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7 -prefs=runtime.tools.avrdude.path=C:\Users\terry\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17 -prefs=runtime.tools.avrdude-6.3.0-arduino17.path=C:\Users\terry\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17 -verbose C:\Users\terry\Dropbox\Electronics\Arduino\SKETCHES\MY SKETCHES\MISC_or_UNASSIGNED\TestPinName\TestPinName.ino
  Using board 'uno' from platform in folder: C:\Users\terry\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6
  Using core 'arduino' from platform in folder: C:\Users\terry\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6
  Detecting libraries used...
  "C:\\Users\\terry\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10819 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-IC:\\Users\\terry\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.6\\cores\\arduino" "-IC:\\Users\\terry\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.6\\variants\\standard" "C:\\Users\\terry\\AppData\\Local\\Temp\\arduino_build_893520\\sketch\\TestPinName.ino.cpp" -o nul
  Generating function prototypes...
  "C:\\Users\\terry\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10819 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-IC:\\Users\\terry\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.6\\cores\\arduino" "-IC:\\Users\\terry\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.6\\variants\\standard" "C:\\Users\\terry\\AppData\\Local\\Temp\\arduino_build_893520\\sketch\\TestPinName.ino.cpp" -o "C:\\Users\\terry\\AppData\\Local\\Temp\\arduino_build_893520\\preproc\\ctags_target_for_gcc_minus_e.cpp"
  "C:\\Program Files (x86)\\Arduino\\tools-builder\\ctags\\5.8-arduino11/ctags" -u --language-force=c++ -f - --c++-kinds=svpf --fields=KSTtzns --line-directives "C:\\Users\\terry\\AppData\\Local\\Temp\\arduino_build_893520\\preproc\\ctags_target_for_gcc_minus_e.cpp"
  Compiling sketch...
  "C:\\Users\\terry\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7/bin/avr-g++" -c -g -Os -Wall -Wextra -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10819 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-IC:\\Users\\terry\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.6\\cores\\arduino" "-IC:\\Users\\terry\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.6\\variants\\standard" "C:\\Users\\terry\\AppData\\Local\\Temp\\arduino_build_893520\\sketch\\TestPinName.ino.cpp" -o "C:\\Users\\terry\\AppData\\Local\\Temp\\arduino_build_893520\\sketch\\TestPinName.ino.cpp.o"
  TestPinName:1:19: error: 'D2' was not declared in this scope
  const int myPin = D2;
                   ^~
  C:\Users\terry\Dropbox\Electronics\Arduino\SKETCHES\MY SKETCHES\MISC_or_UNASSIGNED\TestPinName\TestPinName.ino:1:19: note: suggested alternative: 'A2'
  const int myPin = D2;
                   ^~
                   A2
  exit status 1
  'D2' was not declared in this scope

*/

Same with any digital Uno pin. Anyone able to reproduce please?

On the UNO you can't use D2, you have to use 2.
if you want to use D2, then you need to define that name

#define D2 2
or
const byte D2 = 2;

On some other boards, you have to use Dx (and the associated value might not be x)


please use code tags when posting code or the output of the Serial monitor or the compilation log

Some finger trouble initially, so quickly edited.

tx - sorry for the quick reaction

an example where it's defined (nodemcu)

but if you go look at the same file for the UNO / AVR core, you just have the analog pins and LED_BUILTIN

1 Like

In post #2 of this thread
[Pin names to pin numbers](In post #2 of this thread Pin names to pin numbers

I read: "The Arduino core already carries out a translation internally, so that pin designations (which you call pin numbers, labeled on the Arduino board) are mapped to internal CPU port and port bit. The mapping changes between different Arduino models, which is why you need a board definition file."

I was almost certain that I'd used D2 etc as a direct substitute for '2' etc. But if others also find that my my minimal sketch fails to compile, then I must be mistaken.

probably on some other board than a UNO or you had included some sort of #define for the pins ?

Thanks, guess so.

Yes. I've used D2 on an ESP8266 in the IDE without having to define it. So it's different with different board types.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.