The eChook nano code has been updated for compatibility with the Arduino Nano Every. Download here.
Towards the end of 2019 the Arduino Nano Every was released as a successor to the venerable Arduino Nano. This is significantly cheaper (£11 at time of writing) than an older ATMEGA328p based genuine Arduino Nano.
There were two prompts for this update:
1. The ‘Cheap Chinese’ Arduino Nanos aren’t looking quite so cheap with the current chip shortage. Where they used to be available for £1.50 you’ll now be hard pressed to find one for under £5, and that’s if you’re buying a few. With this in mind it’s hard to argue against going for a branded Arduino nano compatible board, such as the Maker Nano(£7) which has a useful array of led’s and extra bits if you’re using it for development or a DFRduino Nano, which is confusingly more expensive at £9, or indeed the Genuine Arduino Nano Every, for £12.50 (£11 without soldered headers), which has the benefit of supporting Arduino development.
2. One team (you know who you are!) bought a batch of Arduino Nano Every boards for their eChooks, understandably believing the sales pitch of 100% compatible with the Arduino Nano, only to find they’re not 100% compatible at all!
Code Updates
There are two major changes required to make the code compatible with the Arduino Nano Every. The biggest change is the Hardware serial port. The Atmega328p microcontroller on the old Nanos has a single hardware serial port which is shared with the USB programming interface and the TX and RX pins that are used for bluetooth communication. This was accessed in code via the Serial object.
The Arduino Nano Every uses a far more powerful and better featured ATmega4809 microcontroller that has multiple hardware serial ports, and uses different ports for the USB and the TX and RX pins. The USB serial is accessible through the Serial object, but the bluetooth data now needs to be sent out via the Serial1 object.
To achieve compatibility I’ve created a SerialA object and referenced it to either Serial or Serial1 depending on the selected target microcontroller using compiler directives:
#if defined(__AVR_ATmega4809__)
#define NANO_EVERY
// References Serial1 to SerialA for the Arduino Nano Every
HardwareSerial &SerialA = Serial1;
#else
// References Serial to SerialA for the Arduino Nano 328p
HardwareSerial &SerialA = Serial;
#endif
When using the Every, data is sent to the bluetooth module, but is no longer available to the USB. On the plus side, this allows any debug messages to be sent out through the USB without them being lost in the binary data stream – a nice feature for development.
The other change involves interrupts. Every digital pin on the Every can be used as an interrupt, and when using the Arduino attachInterrupt() routine, they are identified by pin number, instead of interrupt number as on the old Nano. The same compiler directives have been used to set the interrupts depending on target microcontroller:
#ifdef NANO_EVERY
attachInterrupt(2, motorSpeedISR, RISING);
attachInterrupt(3, wheelSpeedISR, RISING);
#else
attachInterrupt(0, motorSpeedISR, RISING);
attachInterrupt(1, wheelSpeedISR, RISING);
#endif
I’ve taken the opportunity to make a few other small improvements to the code. Comments have been updated, some legacy code supporting the prototype eChook boards (v1.1) has been removed without breaking compatibility for them, and the handling of button presses has been improved making better use of the Bounce2 library features.
While there are no immediate benefits to using the Arduino Nano Every over an old Nano, it is a newer, faster and more feature rich microcontroller that could open some new avenues of development – it’s a really nice little board.