When I've come back to Arduino programming after a spell doing other things, I keep forgetting that I last experimented with a different type of board and get a "avrdude: stk500_getsync(): not in sync: resp=0x00" error.

I have one sketch which will always be compiled and uploaded against a specific piece of hardware. Therefore, I would like to specify the board type and preferably COM port as well, in my sketch.

Can it be done?

  • You can do this by customizing one of the Makefile-based build/upload paths that have been offered by the community as alternatives to the IDE - even if you choose to use the IDE as your normal tool (just be sure to close the serial monitor before trying to flash from the command line). Commented Dec 19, 2014 at 15:00

2 Answers 2


No, it cannot. Neither the compiler invocation nor the subsequent upload examine the code for the hardware in use, so there is no way of doing what you are asking.

What you can do is to create entries in a new boards.txt and programmers.txt using an existing core that specify all the parameters, and all you would have to do is select them in the IDE for everything to be set at once. See Google Code/Arduino/Platforms for more information.

  • It seems an obvious compiler directive to have, but if it doesn't exist, that's fine. I just needed to be sure.
    – Greg Woods
    Commented Dec 19, 2014 at 10:58
  • The compilers involved are fairly target specific, so by the time a directive is read it is "too late" to choose between say Due (ARM) and Uno (ATmega). But you could put in traps so that the build will fail with an informative custom error if the wrong one is selected. Commented Dec 19, 2014 at 16:57
  • I suppose then it's not a compiler directive I'm talking about, more of a directive to the IDE, but that then doesn't make the code very portable. Nicer fail is a good compromise though.
    – Greg Woods
    Commented Dec 19, 2014 at 20:28

Yes you could do this, but you won't get much support from the Arduino libraries. From what I have used, the board type is evaluated at library compile time.

But if you are willing to delve through the datasheets for all the boards you want to support, then writing a board-agnostic driver should not be impossible.

  • No, that is way too much work. I was just looking for shortcut, not more work! :)
    – Greg Woods
    Commented Dec 19, 2014 at 10:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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