arduino, AZ3166, IOT, MXChip

Using the MXChip AZ3166 Azure DevKit with Arduino – coding with pin names instead of numbers

This post is part of my series on the MXChip AZ3166 DevKit – the device has lots of built in sensors, but this series focusses on interactions with external devices through the physical pins on the edge connector.

Part 1: Getting started

Part 2: Mapping virtual Arduino pins to physical digital I/O pins

Part 3: Reading analog values through physical pins

Part 4: Pulse-width modulation and servos

I own a couple of edge connector breakout boards – one from Kitronik, shown below..

Kitronik Edge Connectors

…and the other is from Sparkfun, shown below.

Sparkfun Edge Connectors

In part 2, I looked at how the physical pins on the edge connector mapped to numbers that I could use in my Arduino code. But I wasn’t really happy with this – it seemed a little unintuitive to access a green LED through the (seemingly arbitrary) virtual pin number 19.

Fortunately I’ve found a better solution. After some hunting through the source code, I’ve discovered a file named PinNames.h. This file is buried way way way down in the bowels of my computer – it’s located on my machine at the directory below:


And this file provided me with the solution to my problem – I was able to find all the reserved variable names corresponding to pins and features on-board the AZ3166.

So instead of having to access the green LED through pin 19, instead I can access it through the alias RGB_G which the Arduino IDE already knows about.

And if I’m using it to produce a PWM signal, I can access it through PWM_OUT – which makes a lot of sense too.

From analysing the PinNames.h file, I’ve gathered these aliases together into a single image, shown below. This shows the aliases in the column named “Reserved Arduino variable name” below.

AZ3166 Edge Connector with Arduino AliasesAs well as naming all the LED pins, this matrix makes it clear that there are:

  • 2 analog pins (PA4 and PA5),
  • 11 digital pins (PB3, PB6, PC13, PB10, PB2, PB7, PB13, PB14, PB15, PB8, PB9),
  • I2C pins (PB8 and PB9),
  • SPI pins (PB13, PB14, and PB15),
  • 2 button pins (PA4 and PA10), and
  • 1 PWM pin (PB3).

How is this useful?

Previously, if I’d wanted to turn on the user LED on my AZ3166, I could have written code like this:

int USER_LED = 45;
void setup() 
  pinMode(USER_LED, OUTPUT);
void loop() 
  digitalWrite(USER_LED, HIGH);

I’ve declared the USER_LED as 45, but it’s a bit weird – someone coming to the code will maybe wonder why I’ve used this number when there’s not 45 pins on the edge connector.

But now if I want to turn on the user LED on my AZ3166, I can write code like:

void setup() 
  pinMode(LED_USER, OUTPUT);
void loop() 
  digitalWrite(LED_USER, HIGH);

Because the variable LED_USER isn’t declared in the code, it’s more obvious that it’s a reserved Arduino variable, and the number 45 isn’t thrown into the mix to add unnecessary cruft to the code.

Now I have a nice (and supported) way to clearly identify intent in my code, and I just need to make sure that I hook the right pin up to any external device (using my table above).

Yes – it’s just one less line! But it all counts towards cleaner and more standard code. I’ll be using this matrix in future to make my device code better.

3 thoughts on “Using the MXChip AZ3166 Azure DevKit with Arduino – coding with pin names instead of numbers

  1. Hi Jeremy, this is Liya, PM for the AZ3166 DevKit here. Nice article! Would love to get in touch with you and probably have your write-ups contribute to our official docs 😉

Comments are closed.