Sub topic 16
Beyond the Uno: Nano, Mega, Leonardo, and 3.3V boards
This chapter assumes you have read the earlier chapters on the Uno's pins, power, USB path, logic levels, and shared serial pins. It uses those ideas to show which Uno assumptions hold on other boards and which do not. Check the exact numbers below against the datasheet or the Arduino documentation for the specific board you own, because clones and revisions vary.
Why the Uno is a poor template for every board
Most Arduino tutorials silently assume an Uno R3. That means an ATmega328P microcontroller, 5V logic, a USB-to-serial chip next to the USB port, and pin numbers tied to a specific header layout. Other boards change one or more of these. A sketch that compiles for another board can still behave wrongly or damage hardware if you carry the assumptions across.
The fastest way to avoid trouble is to ask four questions about any new board:
- What is the logic voltage, and what is the maximum voltage a pin tolerates?
- How does USB reach the microcontroller: through a separate bridge chip or directly?
- Which pin numbers are digital, analog, PWM, I2C, and SPI on this board?
- How is the board powered, and what regulator sits behind each power pin?
The boards at a glance
| Board | Microcontroller | Logic level | Digital pins | Analog inputs | USB path |
|---|---|---|---|---|---|
| Uno R3 | ATmega328P | 5V | 14 (6 PWM) | 6 | Separate bridge chip |
| Nano | ATmega328P | 5V | 14 (6 PWM) | 8 | Separate bridge chip on the board |
| Mega 2560 | ATmega2560 | 5V | 54 (15 PWM) | 16 | Separate bridge chip |
| Leonardo | ATmega32U4 | 5V | 20 | 12 | Built into the microcontroller |
| Due | SAM3X8E | 3.3V | 54 (12 PWM) | 12 | Separate chip, plus a native USB port |
| Nano 33 IoT | SAMD21 | 3.3V | 14 | 8 | Built into the microcontroller |
Pin counts depend on how you count shared pins, so treat the table as a guide to scale, not a precise specification.
Nano: the Uno in a breadboard-sized package
The classic Nano uses the same ATmega328P as the Uno, so sketches usually run unchanged. The differences are physical and electrical at the edges.
- It has pins on both long edges and plugs straight into a breadboard. There is no barrel jack and no stackable shield headers.
- It has two extra analog inputs, A6 and A7. These are analog-only. You cannot use them with
digitalWrite()orpinMode(..., OUTPUT), and they have no internal pull-up resistor. - Power comes from the USB connector (Mini-B or Micro-B, depending on the revision) or from the VIN pin. VIN accepts a wider range on some revisions than on others, so check yours.
- Many low-cost Nano clones use a CH340 bridge chip instead of the original FTDI chip. Your computer then needs a CH340 driver on some operating systems, and the board appears under a different port name.
- Some Nano boards ship with an older bootloader. In the Arduino IDE you then choose "ATmega328P (Old Bootloader)" under Tools > Processor, or uploads fail with a sync error.
// Reads the two analog-only pins that exist on the Nano but not on the Uno.
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.print("A6=");
Serial.print(analogRead(A6));
Serial.print(" A7=");
Serial.println(analogRead(A7));
delay(500);
}On an Uno this sketch does not compile, because A6 and A7 are not defined there. That compile error is useful: it shows the board definition is part of what your sketch depends on.
Mega 2560: more of everything
The Mega keeps the Uno's 5V logic and the same shield-compatible header positions for the first pins, then adds a lot more.
- Four hardware UARTs:
Serial,Serial1,Serial2,Serial3. On the Uno, one UART is shared between USB and pins 0 and 1. On the Mega,Serialstill connects to USB, butSerial1toSerial3are free for GPS modules, Bluetooth modules, and other serial devices. - 16 analog inputs, A0 to A15.
- I2C moves to pins 20 (SDA) and 21 (SCL). Many Uno shields assume A4 and A5, and those shields' I2C will not work on a Mega unless the shield also exposes the dedicated SDA and SCL header pins and you wire them correctly.
- SPI moves to pins 50 (MISO), 51 (MOSI), 52 (SCK), and 53 (SS). The ICSP header carries the same signals. An Uno SD-card shield that expects SPI on pins 11 to 13 will not work by stacking alone.
- More flash and RAM, so larger sketches and arrays fit.
// Mega 2560 only: Serial1 is a second hardware UART on pins 19 (RX1) and 18 (TX1).
void setup() {
Serial.begin(9600); // USB to the computer
Serial1.begin(9600); // a device wired to pins 18 and 19
}
void loop() {
if (Serial1.available()) {
Serial.write(Serial1.read()); // forward device data to the Serial Monitor
}
}Leonardo: USB handled by the microcontroller
The Leonardo uses the ATmega32U4, which has USB hardware inside the chip. There is no separate USB-to-serial bridge, and this changes behavior you may have relied on.
Serialis a virtual serial port over native USB. It is not tied to pins 0 and 1. Those pins areSerial1.- The serial port disappears and reappears when the board resets. The computer sees the board disconnect and reconnect, so the port can change while you are uploading.
- At startup, the sketch can run before the computer has opened the port. Text printed in
setup()may be lost. - The board can act as a USB keyboard or mouse through the
KeyboardandMouselibraries. This is the main reason to choose it. - I2C is on pins 2 (SDA) and 3 (SCL), which are also exposed at the header near AREF. Pins 2 and 3 are therefore not free for general use if you use I2C.
A common fix for lost startup output is to wait for the port before printing:
// Leonardo and other boards with native USB.
void setup() {
Serial.begin(9600);
while (!Serial) {
; // wait for the Serial Monitor to open
}
Serial.println("Ready");
}
void loop() {
}Be careful with this pattern. If the board runs without a computer attached, while (!Serial) blocks forever and the rest of the sketch never runs. Use it only while debugging, or add a timeout such as while (!Serial && millis() < 3000).
Another gotcha is that a sketch which uses Keyboard.print() in loop() can keep typing into whatever window has focus, including your editor, and make it hard to upload a fix. Add a guard such as a button check before sending keystrokes, and know how to double-tap the reset button to enter the bootloader.
3.3V boards: the logic level changes
Boards such as the Due, many ARM-based Nano 33 boards, the ESP32 family, and the Raspberry Pi Pico family run their logic at 3.3V. The earlier chapter on logic levels applies directly here, and the stakes are higher because most 3.3V pins are not 5V tolerant.
- A 5V signal on a 3.3V-only pin can damage the pin or the whole chip. Check the datasheet for the stated tolerance.
- A 3.3V output may be too low for a 5V device to read as HIGH. A 5V device often needs an input of roughly 0.7 times its supply, which is about 3.5V, so a 3.3V signal is marginal.
- Pins often source less current than the Uno's 20 mA. Some pins on boards in this class are limited to a few milliamps, so an LED and resistor that work on an Uno may need a larger resistor or a transistor.
- The ADC reference is 3.3V, not 5V. The conversion in the Uno example,
raw * (5.0 / 1023.0), gives wrong voltages. The ADC may also have a different resolution. Many 3.3V boards default to 10 bits for compatibility, but they can support 12 bits or more, so check the range before converting.
When you must connect a 5V sensor output to a 3.3V input, use a level shifter or a resistor voltage divider. Going from 3.3V up to a 5V input usually calls for a level shifter, not a divider.
// Portable voltage conversion: keep the reference and resolution in one place.
const float ADC_REF_VOLTS = 3.3; // change to 5.0 on an Uno
const int ADC_MAX = 1023; // change if you set a higher resolution
void setup() {
Serial.begin(9600);
}
void loop() {
int raw = analogRead(A0);
float volts = raw * (ADC_REF_VOLTS / ADC_MAX);
Serial.print(volts, 2);
Serial.println(" V");
delay(500);
}Choosing a board
The choice follows from the constraint that matters most.
A porting checklist
Before moving a working Uno project to another board, go through these checks:
- Select the correct board and processor in the IDE, and confirm the port.
- Check the logic voltage of the board and of every connected part.
- Look up where
SDA,SCL,MOSI,MISO,SCK, andSSare on the new board, and use the named constants (SDA,SCL,LED_BUILTIN) instead of fixed numbers where you can. - Confirm which pins support PWM and analog input.
- Re-check the current limit of each pin and the output of each power pin.
- Check how the board starts and resets, especially if it has native USB.
Exercise
You move a project from an Uno to a 3.3V board. It reads a potentiometer on A0 and prints raw * (5.0 / 1023.0). The pot is wired between the board's 3.3V pin and GND. At full turn the sketch prints about 5.0 V when the pot is really at 3.3 V. What is wrong, and what two things would you check to fix it?
Show answer
The conversion assumes a 5V ADC reference, but the 3.3V board's ADC uses a 3.3V reference, so full scale (raw 1023) is 3.3 V, and the sketch scales it to 5 V. Change the reference to 3.3 in the formula. Also check the ADC resolution the board is configured for, because the maximum raw value is 1023 only at 10 bits, and it is 4095 at 12 bits.
Exercise
A Leonardo sketch prints a startup message in setup() with Serial.println, but the Serial Monitor never shows it. Why does this happen on a Leonardo but not on an Uno, and what is a safe fix?
Show answer
The Leonardo's USB hardware is inside the microcontroller, so the serial port is created by the sketch and reappears after a reset, and the computer may not have opened it yet when setup() prints. On an Uno a separate bridge chip keeps the port open. A safe fix is while (!Serial && millis() < 3000); after Serial.begin(), which waits briefly for the monitor but does not block forever when no computer is attached.