M2 variant files updated

Notice: the content of the following post is outdated. Please see the instructions in the Macchina M2 documentation or the the README on GitHub for current information.

First off, a big thank you to @t_doust for putting together these files. Tony has been working on a library for the 6 GPIO drivers and started modifying the core Arduino “Variant” files to be more M2-specific.

The result is a much cleaner, easy to set up, and less confusing development experience. We don’t need to use the “SamNonDuePin” library anymore. The following 2 pieces of code do the exact same thing, but you’ll see that the second is simpler.

If you select “Arduino Due (Native USB Port)” from the Tools->Board menu in the Arduino IDE, you’d use this code:

[details=Code using “SamNonDuePin” library

  Demonstrates using "Non-Due" pins for Button inputs and LED outputs.
  Also found here:

#include "Arduino.h"
#include "SamNonDuePin.h"

// constants won't change. They're used here to
// set pin numbers:
const int SW1 = X1;                // Pushbutton SW1
const int SW2 = PIN_EMAC_ERX1;     // Pushbutton SW2

const int Yellow =  X0;      // the number of the LED pin
const int Red =  32;         // the number of the LED pin

// others are: 32(RED), X0(YELLOW), 27(YELLOW), 24(YELLOW), 23(GREEN), 12(RGB_GREEN), 5(RGB_BLUE), 11(RGB_RED)

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
int buttonState2 = 0;        // variable for reading the pushbutton status

void setup() {

  pinModeNonDue(Yellow, OUTPUT);
  pinMode(Red, OUTPUT);

  pinModeNonDue(SW1, INPUT);
  pinModeNonDue(SW2, INPUT);

  digitalWriteNonDue(Yellow, LOW);
  digitalWrite(Red, LOW);

void loop() {

  buttonState = digitalReadNonDue(SW1);
  if (buttonState == HIGH) {  // NOT pressed
    digitalWriteNonDue(Yellow, HIGH);   // turn LED OFF:
  else {
    digitalWriteNonDue(Yellow, LOW);    // turn LED ON:

  buttonState2 = digitalReadNonDue(SW2);
  if (buttonState2 == HIGH) {
    digitalWrite(Red, HIGH);     // turn LED OFF:
  else {
    digitalWrite(Red, LOW);     // turn LED ON:



Here is the code you can use if you select “M2” from the Tools->Board menu in the Arduino IDE.

[details=Code with M2 board definitions - notice it is a bit simpler.]```cpp

Demonstrates using “Non-Due” pins for Button inputs and LED outputs using M2 board Defs.

// constants won’t change. They’re used here to
// set pin numbers:
const int SW1 = Button1; // Pushbutton SW1
const int SW2 = Button2; // Pushbutton SW2

const int Yellow = DS3; // the number of the LED pin
const int Red = DS2; // the number of the LED pin

// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
int buttonState2 = 0; // variable for reading the pushbutton status

void setup() {

pinMode(Yellow, OUTPUT);
pinMode(Red, OUTPUT);

pinMode(SW1, INPUT);
pinMode(SW2, INPUT);

digitalWrite(Yellow, LOW);
digitalWrite(Red, LOW);

void loop() {

buttonState = digitalRead(SW1);
if (buttonState == HIGH) { // NOT pressed
digitalWrite(Yellow, HIGH); // turn LED OFF:
else {
digitalWrite(Yellow, LOW); // turn LED ON:

buttonState2 = digitalRead(SW2);
if (buttonState2 == HIGH) {
digitalWrite(Red, HIGH); // turn LED OFF:
else {
digitalWrite(Red, LOW); // turn LED ON:



**So how do you get "M2" to show up in your Tools->Boards menu in the Arduino IDE ?** 

Just paste this:


into the "additional Boards Manager URLs" field under "Preferences", and then select "Board Manager" under Tools->Boards, and install "M2 by Macchina".

This [guide](https://www.macchina.cc/guide/m2/getting-started), although slightly outdated will walk you through that process.

**What are the pin names now?**

We'll be making a nice table for you to refer to in our guide, but for now, they can be seen in the source here:


Note that this is a work-in-progress, so keep that in mind. 

**How can I report bugs, do pull requests, or generally help improve things?** 

We are keeping the M2 board files in this [repo](https://github.com/macchina/macchina.github.io). Feel free to contribute. 

We'll be periodically making "official" updates to the files as we go. [Here](https://forum.arduino.cc/index.php?topic=409715.0) are the instructions we followed to set up the files needed by the "Boards Manager" feature of the Arduino IDE.
1 Like

This is awesome!!! A lot of time I spent in early development was simply trying to figure out what pins did what so this will definetely lower the barrier for other folks coming in. Nice work @t_doust!

This sounds very helpful!

Looking at that git repo, I find it confusing to see versioned files and binaries(!) checked in to a repository that is conventionally designated for a web site. Before reading here that it is intended development occurs within that repository, I was wondering where these files came from and what the source was.

Based on the workflow as I understand it, I would recommend splitting the board files folder to another repository and using GitHub Releases to handle the hosting of the zip. The eliminates all the confusion from above, will help with versioning as new version are released, and better matches GitHub’s recommended usage of GitHub Pages.

I tend to agree with this. The _2.0.1 at the end of the directory stood out right away. When that version is bumped it will result in git doing an rm/add on every file in the repo. That’ll get ugly quick.

Thanks guys, great feedback. I was in a hurry (or lazy) and just threw these into a single repo. As you suggested, we’ll move the working folder to a different repo. I’ll need to look into Github Releases next. One other thing that we’ll need to do is recreate the JSON file for every release. Wonder if there is some fancy automated method here? Then again, it is not difficult nor time consuming.

I’ve been working on a release manager for OSHChip!!!
Right now it automates tagging the release and building the json against a template.
It’s pretty specific to the OSHChip code right now but I can clean it up this week.

Travis CI, FTW.1 Also, offer to help still stands, builds/automated deployment of artifacts has also been part of my recent projects.

1 The other options are valid and good too, Travis just seems to be the most popular (unless you need a Windows build system), is free for OSS, and is simple.

You guys are awesome :grinning: Thank you both for your offer to help. What is the next step? I’ll PM you to sort that out.

@t_doust Are you on GitHub? If you are, could you direct me to your user? I would like to set you as the author of the commit adding these files to give you attribution.

Hi Adam
Yes i am on GitHub still coming to terms with it. I think this is what you are after “https://github.com/TDoust”. Not sure if i really should take owner ship as the author because i bashed it out without going back to tidy it up. The files are a work in progress & still need to be finessed. Will get back to it soon. Enough said i talk too much.


GitHub lists every user who contributes code to a repository (example). I’ll admit most projects do not bother as strongly to preserve this information, but it is both accurate, and I think appropriate, to mark you as the author on the commit I have in mind to give you credit. Later cleanup will have new own commits their own author. Unfortunately, you have not public commits on GitHub so I cannot retrieve the information I need. It is public information (when you know where to look), but it includes an e-mail address so I will PM you.

Note: GitHub commits have both an “Author” and a “Committer”, so even when I set some other individual as the author, I am still recored as the person who created the commit.

I have reorganized these board files to for easier deployment, maintenance, and smaller download size.

I don’t yet a have M2 or any other embedded device so if someone could try out the following to let me know things still work, I will know I can continue.

First download the Arduino SAM Boards (32-bit ARM Cortex-M3) boards (providing the “Arduino Due”) if you do not already have it.

Then add:


as a package source and download Macchina Boards in the Board Manager.
You can now choose Macchina M2 (Native USB Port) as your board. Everything should work as did with the M2 board in the original post.

Note: I changed the names so you should be able to test this release without uninstalling the official M2 release in the OP.

With this the boards archive is being served by GitHub pages where it is automatically published by Travis CI then a release is tagged. package_macchina_index.json was still manually constructed (if this works I will move on to automating creating that; it is a little more complicated because that file is supposed to list all version of the board files, not the latest).

Hi Adam
I have tried your package_macchina_index.json file in the Arduino IDE as well as the Microsoft Visual Studio IDE. All seems to work however there are no standard Libraries folders included with your release like SPI or WIRE. by including them with the .json file the end user does not then need to download these libraries. Was this intentional or an over-site. It would be nice if the .json file included some or all of the official M2 library files as listed on the M2 GitHub site that way the official M2 Libraries will then reside in the library folder with the package revision & any libraries modified by users would reside in the standard library folder in documents. i know this could be confusing to newbies but would enable 2 copies of a library to reside on ones computer. the official ones & any modified libraries with the same name to reside in the users documents Arduino Library folder.
Yours & others thoughts
Regards Tony

1 Like

Thanks for testing this!!

With regard to my work, this was intentional. My first step was recreating what was already released using a different process (I did not see libraries included with the package in the original post, if I some how missed these please let me know).

I also noticed that libraries could be included. It is on my list of things to look into; I’m glad to here there is someone else also considering bundling them.


When I click over “variant.h” and then “show original”, I see a “Page not found” here in your post.

Even here: https://github.com/macchina/Macchina_Arduino_Boards when I click over “Pin names”.

Best regards,

Thanks for pointing this out! The current link is: https://github.com/macchina/Macchina_Arduino_Boards/blob/master/sam/variants/m2/variant.h

I have also updated the README over on GitHub.

So… I accidentally found a potentially NASTY thing that happens if you have both the standard Arduino Due and the M2 board files installed. You see, the IDE will happily compile Due sketches either way as pretty much everything is properly defined either way. I compiled a sketch for M2 and then forgot about that and left it that way. Fast forward a couple of days and I tried to compile a Due sketch. It compiled fine. My SPI communication was broken. I check, the chip select pin is not going low. I check, the pin number is right. I check the pin. Sure enough, it doesn’t toggle. I try a small sketch that does nothing but toggle the pin. No toggling. So I try toggling a different pin. No luck. After a lot of head scratching (and, hey, my hair was thinning as it is!) it finally dawned on me… I’m trying to use pin 34. But, wouldn’t you know, the M2 board file totally redefines digital pin numbers so pin 34 didn’t go to the proper place. Switching the board to Arduino Due (Native) caused the proper behavior.

The trouble here is that sketches compile perfectly well either way so it’s way too easy to forget to switch the board if you use both. I can see other people doing this. Believe me, I already field tons of queries about why due_can doesn’t work only to find out that someone is trying to compile for Arduino MEGA. People are going to run into this minefield. I just lost HOURS to this problem. I’m not blaming anyone but me. I should have checked this. It’s just so subtle that it’s easy to miss and the only thing that changes is that digital I/O goes insane. I thought for sure I had a couple of bad Arduino Due’s.

Do the M2 board files define anything I could key on to warn me if I accidentally do this?

Definitely an unfortunate situation. If it is a big enough concern, we would revisit the decision not to strive for Due compatibility (though that is only a band-aid in this situation).

On the next release, ARDUINO_SAM_DUE will cease to be defined.

Additionally, there will be MACCHINA_M2 defined for the release board and MACCHINA_M2_BETA defined for the beta board (#29).

Will any of the above suit you?

I am trying to get the last of the library reorganization done so I can release the new board changes and the libraries all at the same time. If it is holding you up, I can cut a release sooner.

Yeah, that’d be perfect.

#ifdef MACCHINA_M2
#error You're compiling for the M2 dummy!


#error You're compiling for Due not M2! Stopping the compilation!