All courses

Arduino C++ · Lesson 37 of 40

Move sketches between board families

Debugging, Testing, and Portability

An Arduino API does not erase hardware differences

Before moving from a classic Uno R3 to another board, review:

  • Logic voltage, absolute input ratings, and power-rail current capacity.
  • Pin names, available PWM outputs, ADC resolution/reference, and which pins support interrupts.
  • Integer widths, RAM/flash size, EEPROM availability or emulation, and floating-point behavior.
  • UART/USB differences, I2C/SPI pin assignments, and peripheral count.
  • Whether a library supports the new core and whether it directly accesses AVR registers.

Use digitalPinToInterrupt(pin) rather than a literal interrupt number. Use LED_BUILTIN where appropriate. An AVR Timer1 register example and util/atomic.h are architecture-specific, not portable Arduino C++. Avoid solving a compiler error by deleting the protection around shared data.

Prove each layer again

Save the old board version, select the new board/core, compile a minimal sketch, and test I/O with level-compatible components. Add libraries individually. Recalculate ADC conversion and resistor currents for the new voltage. Replace a board-specific EEPROM API with that core's supported persistence method, including any required commit operation.

For an ISR-shared multi-byte value on an 8-bit AVR, volatile prevents inappropriate compiler caching; it does not make a read atomic. Copy the value within a short critical section, restoring the prior interrupt state, and do slow formatting or printing afterward. Do not place delay, Serial printing, or a full sensor transaction inside an ISR.

Exercise: Create a porting note for an Uno R3 sketch moved to a 3.3 V target. Identify the exact new board and list its supply, GPIO voltage, A0 range, PWM resolution, interrupt pins, and supported libraries using that board's official pinout. Mark unknowns as unknown until checked.

Official references for further study

A useful reminder

Clean Code: Use descriptive constants instead of magic numbers

Instead of calling digitalWrite(13, HIGH) directly in your code, define descriptive constants like const int LED_PIN = 13; at the top of your file. This makes your code self-documenting and extremely easy to update if you change your wiring.