User Manual

Adapting Sketches to M0 &
M4
The ATSAMD21 and 51 are very nice little chips, but fairly new as Arduino-compatible cores go. Most sketches &
libraries will work but here’s a collection of things we noticed.
The notes below cover a range of Adafruit M0 and M4 boards, but not every rule will apply to every board (e.g. Trinket
and Gemma M0 do not have ARef, so you can skip the Analog References note!).
Analog References
If you'd like to use the ARef pin for a non-3.3V analog reference, the code to use is analogReference(AR_EXTERNAL)
(it's AR_EXTERNAL not EXTERNAL)
Pin Outputs & Pullups
The old-style way of turning on a pin as an input with a pullup is to use
pinMode(pin, INPUT)
digitalWrite(pin, HIGH)
This is because the pullup-selection register on 8-bit AVR chips is the same as the output-selection register.
For M0 & M4 boards, you can't do this anymore! Instead, use:
pinMode(pin, INPUT_PULLUP)
Code written this way still has the benefit of being
backwards compatible with AVR.
You don’t need separate versions
for the different board types.
Serial vs SerialUSB
99.9% of your existing Arduino sketches use Serial.print to debug and give output. For the Official Arduino SAMD/M0
core, this goes to the Serial5 port, which isn't exposed on the Feather. The USB port for the Official Arduino M0 core is
called
SerialUSB
instead.
In the Adafruit M0/M4 Core, we fixed it so that Serial goes to USB so it will automatically work just fine.
However, on the off chance you are using the official Arduino SAMD core and
not
the Adafruit version (which really,
we recommend you use our version because it’s been tuned to our boards), and you want your Serial prints and
reads to use the USB port, use
SerialUSB
instead of
Serial
in your sketch.
If you have existing sketches and code and you want them to work with the M0 without a huge find-replace, put
#if defined(ARDUINO_SAMD_ZERO) && defined(SERIAL_PORT_USBVIRTUAL)
// Required for Serial on Zero based boards
#define Serial SERIAL_PORT_USBVIRTUAL
#endif
right above the first function definition in your code. For example:
© Adafruit Industries https://learn.adafruit.com/adafruit-feather-m0-wifi-atwinc1500 Page 56 of 77