Skip to main content

Microcontroller Design

For successful and elegant controller circuitry.

Microcontroller Basics

Controller such as ATMEGA32U4 are often used as low-cost, low-power processing units for mechanical keyboard PCBs.
They often require various components of their own in order to function properly.

Choosing the Correct Controller

Space and function constraints will require different choices of controllers.

ATMEGA SeriesProsCons
atmega32u2
  • Small form factor.
  • Powerful enough for most smaller keyboards.
  • Not enough pins for large matrices.
  • Not much lower price than 32u4.
  • No I2C support.
atmega32u4
  • 25 I/O pins, slightly larger than 32u2.
  • Enough pins for most keyboard matrices.
  • Increased size, and more required components.
  • Slightly more expensive than 32u2.
at90usb1286
  • 40+ I/O pins. Handles the largest matrices just fine.
  • Extremely large size.
  • Very expensive.
atxmega32a4u
  • Same size as 32u4, but requires no crystal.
  • No crystal means less chance of failure.
  • Overall small footprint.
  • Many I/O pins.
  • Two separate I2C busses.
  • Not QMK compatible.
    • Runs perfectly on Keyplus.
  • Requires 3.3v voltage regulator.

When in doubt, pick 32u2 for sub-60%, 32u4 for 60-90%, and 1286 for 90+% keyboards.
When using 32u2, it is strongly recommended to bind B7 to PWM backlight, since it is the only pin known to work with QMK flawlessly.

Controller Layout - The Basics

  • Place the controller and its components in a position that allows for proper placement and breakout.
  • All decoupling capacitors must be placed as close to VCC/GND pin pairs on the controllers as possible. (Detailed below)
  • Break out other pins with even spacing and pattern in order to minimize wasted space.
  • Try to keep "dirty ground current", such as from RGB LEDs or backlighting, away from the controller. For example, run a separate ground plane for the controller, and join it together with the rest of the ground close to the USB port.

Crystal Layout

General

  • The crystal is one of the most sensitive parts of the controller circuit.
  • Place the crystal in a well-shielded position away from other traces.
  • Do not run any traces close to, through, or under the crystal area unless absolutely necessary.
  • Crystal and its decoupling capacitors should be fairly close to the controller.
  • Crystal layout should be relatively symmetrical.
  • "Dirty ground current" should never run through the crystal.
  • Do not use vias in crystal traces, since they mess with the fine capacitance requirements.
  • The backside of the area should be covered with a clean ground plane, and if possible, frontside as well.

Crystal Decoupling Capacitors

Controller Decoupling Capacitors

Placement is important

  • The microcontroller decoupling capacitors exist to prevent the controller from unleashing plenty of noise into the VCC and GND channels.
    • Yes, a whole board can fail if these lines are affected enough.
  • Place each decoupling capacitor as close to its respective VCC/GND pin pair as possible.

Choosing the right values

  • There are many conflicting sources for what value capacitors to place at what pin.
  • The Teensy2.0, a successful example, uses 4x 0.1uf and 1x 1uf to decouple its ATMega32u4, which has 4 VCC/AVCC pins and one UVCC pin.
  • From this, we infer that each VCC/AVCC pin needs a 0.1uf capacitor.
  • We also infer that the UVCC pin needs a large capacitor.
  • The Silabs document states that the total capacitance for a USB device should not exceed 10uf.
  • Larger capacitance is not always better. Smaller can be sufficient, and larger may not be able to adapt to high frequency changes in voltage.
  • From personal experience, IATMega32u2 (VCC, AVCC, UVCC) with two 0.1uf and one 4.7uf capacitors works fine.
  • 4.7uf may be a safe value to use as the large capacitor, since adding more capacitors for AREF and UCAP pins and many 0.1uf capacitors for RGB lighting won’t make the capacitance exceed 10.
  • Consult the official documentation and working prototypes for "correct" quantity and values.

Regarding placement of the large capacitor

  • Placing the largest decoupling capacitor far from the microcontroller makes it a power reservoir for the entire board.
  • Placing the largest decoupling capacitor close to the microcontroller makes it a power reservoir for mainly the controller.
  • Most sources will say that the largest decoupling capacitor should be placed close to the controller instead of near the USB port.

Sources

http://www.atmel.com/Images/Atmel-2521-AVR-Hardware-Design-Considerations_ApplicationNote_AVR042.pdf
https://blog.adafruit.com/2012/01/24/choosing-the-right-crystal-and-caps-for-your-design/
https://electronics.stackexchange.com/questions/20255/how-far-is-too-far-when-routing-traces-for-crystals-and-how-asymmetrical-is
http://www.atmel.com/Images/Atmel-8128-Best-Practices-for-the-PCB-Layout-of-Oscillators_ApplicationNote_AVR186.pdf
https://www.pjrc.com/teensy/schematic.html
https://www.silabs.com/documents/public/application-notes/AN0046.pdf