How to check if nano board is in use?

I'm trying to add a check in the code to make sure that the proper board has been selected.

This code work fine for Mega 2560:

#if !defined(__AVR_ATmega2560__)
#error Must use ATMEGA 2560
#endif

I tried this for the nano board:

#if !defined(__AVR_ATmega328P__) || !defined(__AVR_ATmega328__)
#error Must use a Nano
#endif

but when checking the code the error message "Must use a nano" is trigged even if the board "Arduino Nano" is selected.

Suggestions?

328P is an uno

Dont know the Definition of nano though. Try out without p since the CPU is only 328

Where did you find the board constant names AVR_ATmega328P and AVR_ATmega328 ?

1 Like

aarg:
Where did you find the board constant names AVR_ATmega328P and AVR_ATmega328 ?

By searching the .h files. But it does not seem to be the correct name.

ARDUINO_AVR_UNO works for uno. but i can't find a list of others

i see it used as a compiler -D option.

1 Like

thehardwareman:
By searching the .h files. But it does not seem to be the correct name.

...and which .h file is it in?

Arduino\hardware\tools\avr\avr\include\avr\io.h

Turn on verbose mode
Build ANY sketch targeted to NANO
Look at the options passed to the compiler, find the -D option containing "BOARD" and "NANO"

Many boards use 328P not only Nano. You need the BOARD define, NOT the CPU define.

Regards,
Ray L.

1 Like

RayLivingston:
Turn on verbose mode
Build ANY sketch targeted to NANO
Look at the options passed to the compiler, find the -D option containing "BOARD" and "NANO"

Many boards use 328P not only Nano. You need the BOARD define, NOT the CPU define.

Regards,
Ray L.

Did not thought about that - thanks.

Found

.... -DARDUINO=10812 -DARDUINO_AVR_NANO ...
#if !defined(__AVR_ATmega328P__) || !defined(__AVR_ATmega328__)
#error Must use a Nano
#endif

The if statement would only be false when both AVR_ATmega328P and AVR_ATmega328 were defined.