N5JLD dot NET

What I do in my Spare Time

User Tools

Site Tools


electronics:mcu:arduino

Electronics Reference: Arduino & Embedded Prototyping

HARDWARE SAFETY & LOGIC LEVEL MANDATE: Classic Arduino boards (`Uno`, `Nano`, `Mega`) operate at `5.0 V` logic, whereas modern boards (`ESP32`, `Nano 33`, `Zero`, `Due`, `RP2040`) operate strictly at `3.3 V` logic. Always verify GPIO logic voltage tolerance, observe per-pin current limits (`20 mA – 40 mA` max), and never drive motors, relays, or solenoids directly from GPIO pins.

1. Arduino Board Comparison Matrix

Select the appropriate Arduino or Arduino-compatible microcontroller board based on logic voltage, processing speed, memory, and hardware peripherals:

Board Model Microcontroller Logic Voltage Clock Speed Flash / SRAM Digital / PWM / Analog Pins Hardware Serial / Buses
Arduino Uno R3 `ATmega328P` (8-bit AVR) 5.0 V 16 MHz 32 KB / 2 KB 14 Digital (6 PWM), 6 Analog (10-bit) 1× UART, 1× SPI, 1× I2C
Arduino Uno R4 WiFi / Minima `Renesas RA4M1` (32-bit ARM Cortex-M4) 5.0 V 48 MHz 256 KB / 32 KB 14 Digital (6 PWM), 6 Analog (14-bit), 1× DAC 1× UART, 1× SPI, 1× I2C, 1× CAN, Wi-Fi/BLE (R4 WiFi)
Arduino Nano (Classic) `ATmega328P` (8-bit AVR) 5.0 V 16 MHz 32 KB / 2 KB 22 Digital (6 PWM), 8 Analog (10-bit) 1× UART, 1× SPI, 1× I2C (breadboard-friendly)
Arduino Mega 2560 `ATmega2560` (8-bit AVR) 5.0 V 16 MHz 256 KB / 8 KB 54 Digital (15 PWM), 16 Analog (10-bit) 4× Hardware UART, 1× SPI, 1× I2C
Arduino Pro Micro / Leonardo `ATmega32U4` (8-bit AVR) 5.0 V (or 3.3V/8MHz) 16 MHz 32 KB / 2.5 KB 20 Digital (7 PWM), 12 Analog (10-bit) Native USB HID (emulates keyboard/mouse/joystick)
Arduino Nano ESP32 / ESP32-S3 `ESP32-S3` (Dual-Core 32-bit Xtensa) 3.3 V (Not 5V tolerant) 240 MHz 16 MB / 512 KB 14 Digital (all PWM), 8 Analog (12-bit) 2× UART, 1× SPI, 1× I2C, Wi-Fi, Bluetooth LE
Arduino Nano RP2040 Connect `RP2040` (Dual-Core ARM Cortex-M0+) 3.3 V (Not 5V tolerant) 133 MHz 16 MB / 264 KB 20 Digital (all PWM), 8 Analog (12-bit) 2× UART, 2× SPI, 2× I2C, onboard 6-axis IMU & Wi-Fi

2. Powering Arduino Boards Safely

Understanding the difference between `VIN`, the DC Barrel Jack, `5V`, and `3V3` pins prevents burnt onboard voltage regulators:

  • USB Port (`5.0 V`): Supplies regulated `5.0 V` directly (protected by a `500 mA` resettable polyfuse on Uno/Mega). Ideal for programming and low-power sensors.
  • DC Barrel Jack / `VIN` Pin (`7.0 V – 12.0 V` Recommended): Feeds into the board's onboard linear voltage regulator (`AMS1117` / `NCP1117`).
    • Dropout Warning: Because the onboard linear regulator drops ~`1.5 V`, supplying `5.0 V` into `VIN` will undervolt the microcontroller at `3.5 V`—supply at least `7.0 V` to `VIN`.
    • Thermal Limit: Avoid powering `VIN` above `12.0 V` (even though datasheets list up to `20 V`), as high voltage drops cause the linear regulator to overheat rapidly when driving shields or displays.
  • Regulated `5V` Pin: Can be used as a power output (up to ~`400 mA` via USB or ~`800 mA` via `VIN`) OR as a direct regulated `5.0 V` power input from an external buck converter—bypassing the onboard linear regulator completely.
  • `3V3` Pin: Output-only regulated `3.3 V` rail (`50 mA – 150 mA` max on classic AVR boards; up to `500 mA+` on modern 32-bit boards).

3. GPIO Pin Modes, Current Limits & Interfacing

3.1 Absolute GPIO Current Limits

  • 8-Bit AVR (`Uno` / `Nano` / `Mega`): Maximum `20 mA` continuous (`40 mA` absolute peak) per GPIO pin, and `200 mA` total combined across all pins.
  • 32-Bit ARM / ESP32 / RP2040: Maximum `6 mA – 12 mA` per GPIO pin (`40 mA` on ESP32).
  • High-Current Loads: Never connect DC motors, servos, solenoids, relays, or long LED strips directly to a GPIO pin. Always switch power through an N-Channel MOSFET, NPN BJT transistor, optocoupler relay module, or dedicated H-bridge motor driver (`DRV8833`, `TB6612FNG`) with a separate external power supply sharing a common `GND`.

3.2 Floating Inputs & Pull-Up / Pull-Down Resistors

A digital input pin configured as `pinMode(pin, INPUT)` with nothing connected acts like a tiny antenna, picking up static electricity and stray electromagnetic fields—causing random `HIGH`/`LOW` readings:

  • Internal Pull-Up (`INPUT_PULLUP` — Recommended): Enables the microcontroller's built-in `20 kΩ – 50 kΩ` resistor tied to $V_{CC}$. Connect your pushbutton or limit switch between the GPIO pin and `GND`.
    • Logic Inversion Note: With `INPUT_PULLUP`, the unpressed pin reads `HIGH` (`1`) and pressing the button pulls it `LOW` (`0`).
  • External Pull-Down Resistor (`10 kΩ`): If active-HIGH logic is required, connect a `10 kΩ` resistor from the GPIO pin to `GND` and wire the switch between the GPIO pin and $V_{CC}$.

3.3 Analog Input (ADC) & PWM Output (`analogWrite`)

  • ADC Resolution:
    • Classic AVR (`10-bit`): Maps `0 V – 5.0 V` to integers `0` through `1023` (`~4.88 mV` per step).
    • Modern 32-bit (`12-bit`): Maps `0 V – 3.3 V` to integers `0` through `4095` (`~0.80 mV` per step).
  • PWM (`analogWrite`): Outputs a digital square wave (typically `490 Hz` or `980 Hz` on AVR) with a duty cycle from `0` (0% / always OFF) to `255` (100% / always ON)—not a true analog DC voltage. Add an RC low-pass filter (`10 kΩ` + `10 µF`) if true smooth analog DC is needed.

4. Hardware Pins & Communication Bus Map (Uno / Nano)

Keep these dedicated hardware pins clear when assigning general-purpose digital I/O:

Pin(s) Hardware Function Design Restriction / Notes
`D0 (RX)` & `D1 (TX)` Hardware `UART` Serial Connected to the USB-to-Serial bridge (`CH340` / `ATmega16U2`). Disconnect peripherals on `D0`/`D1` while uploading sketches over USB.
`D2` & `D3` Hardware External Interrupts (`INT0`, `INT1`) Reserve for rotary encoders, hall-effect RPM sensors, radio PPM/PWM receivers, and emergency stop buttons.
`D3, D5, D6, D9, D10, D11` Hardware `PWM` (`~`) Note that `D5` and `D6` share Timer0 with `delay()` and `millis()`, running at `980 Hz` instead of `490 Hz`.
`D10 (SS), D11 (MOSI), D12 (MISO), D13 (SCK)` Hardware `SPI` Bus `D13` is also tied to the onboard `LED_BUILTIN`. Keep `D10` configured as `OUTPUT` whenever acting as SPI Master.
`A4 (SDA)` & `A5 (SCL)` Hardware `I2C` Bus Internal `10 kΩ` pull-ups are enabled automatically by the `Wire.h` library (`5.0 V` on Uno/Nano).

5. Firmware Architecture & Non-Blocking Code Standards

Avoid writing blocking delays in embedded control firmware—using `delay(1000)` freezes the processor for an entire second, causing missed button presses, dropped serial packets, and unresponsive safety stops:

  • Non-Blocking Timing (`millis()`): Track elapsed time using `unsigned long` timestamps (`if (millis() - previousMillis >= interval)`) so sensors, serial telemetry, and actuators run concurrently.
  • Hardware Interrupts (`attachInterrupt`): Keep Interrupt Service Routines (ISRs) ultra-short—only set a `volatile bool` flag or increment a counter inside the ISR, never call `Serial.print()` or `delay()` inside an ISR.
  • Avoid `String` Objects on Low-RAM MCUs: On 8-bit boards with only `2 KB` of SRAM (`Uno` / `Nano`), dynamic `String` concatenation fragments heap memory and causes random crashes after hours of runtime. Use fixed-size `char[]` arrays with `snprintf()` instead.
  • Watchdog Timer (WDT): Enable the hardware Watchdog Timer (`avr/wdt.h` or `esp_task_wdt.h`) on deployed robotics/field telemetry nodes so the microcontroller automatically resets itself if software ever hangs.

6. Arduino Bench Bring-Up & Troubleshooting Checklist

  1. [ ] Verify Board & Port Selection: Confirm the exact Board, Processor bootloader (`ATmega328P` vs. `ATmega328P (Old Bootloader)` on Nano clones), and COM/tty port in the Arduino IDE / PlatformIO before uploading.
  2. [ ] Check `5.0 V` vs. `3.3 V` Logic Compatibility: Confirm no `5.0 V` sensor or `HC-SR04` echo pin is wired directly into a `3.3 V` (`ESP32` / `RP2040` / `STM32`) GPIO pin without a voltage divider or logic level shifter.
  3. [ ] Verify Common Ground (`GND`): Ensure external servo/motor battery packs share a direct ground connection with the Arduino `GND` pin (erratic servo twitching is almost always caused by a missing common ground).
  4. [ ] Isolate High-Current Noise: Add a `470 µF – 1000 µF` electrolytic capacitor across servo/motor power rails and a `0.1 µF` ceramic capacitor across the Arduino's `5V` and `GND` pins to prevent brownout resets when motors start up.
  5. [ ] Serial Monitor Baud Rate Match: Confirm `Serial.begin(115200);` in `setup()` matches the baud rate selected in the Serial Monitor (garbled `⸮⸮⸮` characters indicate a baud rate mismatch).

Electronics Reference: 1. Diodes & Transistors | 2. Integrated Circuits (ICs) | 3. Communication Buses (UART, SPI & I2C) | 4. Voltage Regulators & Power Supply Design | 5. Operational Amplifiers (Op-Amps) | 6. Sensors & Breakout Modules | 7. PCB Design & Soldering Standards | 8. Arduino & Embedded Prototyping

electronics/mcu/arduino.txt · Last modified: by Jon