2. Basic Knowledge - zykrah/pcb-guide GitHub Wiki

UPDATE: I WILL CONTINUE THIS GUIDE HERE. THIS GUIDE MAY NOW BE OUTDATED.

Disclaimer, this will likely be a poor/entry-level explanation of aspects of EE (Electrical Engineering) and other information relevant to keyboard PCB design. I recommend using other sources (e.g. Googling/YouTube) if you want proper, comprehensive explanations.

Many of the terms used throughout the guide will be explained in this section, so I recommend going over this section and educating yourself to avoid future confusion. Feel free to come back to this section if there’s some sort of term or concept you don’t understand in the middle of the guide. I may also miss a few things, so this section is definitely subject to change in the future.

Voltage, Current, Resistance (+ Resistors)

In case you don't already know about the basics of electronics, I would highly advise looking through YouTube/Google for resources on learning them. Having a solid understand of electronics basics sets the foundation for what you can learn in terms of PCB design.

I would recommend at least understanding these terms/concepts before continuing:

  • Circuits (open vs closed)
  • Switches (what they do as an electrical component in general)
  • Voltage (V), Current (I), Resistance (R)
  • Resistors
  • AC Power vs DC Power
  • Electric circuit diagrams

Diodes

Better resource: What is a Diode? | Fluke

Diodes basically allow current to only flow in one direction. They are essential to the functionality of a switch matrix (will be explained further later in this guide).

Capacitors

Better resource: Capacitors Explained - The basics how capacitors work working principle

Capacitors (or ‘caps’ for short) are mainly used in keyboard PCBs to reduce noise (unwanted change of a signal/current due to electromagnetic interference, or "EMI") in a circuit. This will be explained more further down the page (decoupling capacitors).

Image examples of what capacitors look like in real life:

image

Ground [WIP]

(maybe) Explanation of grounding, what the purpose is. Return currents?

Light Emitting Diodes (LEDs) [Optional]

If you’re living in the 21st century, you probably know what Light Emitting Diodes (LEDs) are. They take a voltage, and emit light. They can be pretty bright for how small they are, but can also be power hungry at full brightness if used in large quantities/arrays.

ARGB (adressable red, green, blue) LEDs, e.g. WS2812(B) LEDs, will consume more power because they actually consist of three LEDs, one for red, green and blue. They also contain a controller on-board to interpret a data signal that requires further power.

I won’t be covering how to add a per-key RGB LED switch matrix as it is out of the scope of this guide, but I may make a follow up tutorial in the future. Other resources exist online already if you want to learn how (or you can just ask people), but I wouldn’t recommend attempting it for a first PCB.

NOTE: Dumb/common anode LEDs require a matrix and dedicated RGB controller chip, whereas ARGB LEDs such as WS2812s have onboard controllers.

[WIP] Add images

Printed Circuit Boards (PCBs)

Good videos about PCBs (second video is pretty informative): How Do PCBs Work? / What is a PCB?

Essentially, PCBs are made of core fiberglass material (most commonly FR4) with various layers of copper in which traces connect various components together.

Layers

Complex PCBs such as PC motherboards can have upwards of 14 layers, however PCBs can get as simple as just 1 layer. For keyboards, 2 layers is generally all you need.

Vias

Vias act as pathways between layers, connecting traces and allowing signals to hop from layer to layer. They are important, but should only be used when necessary. I'll be going over via usage further in depth later. What are PCB vias?

The below image shows a 6 layer pcb, and how vias connect traces between the layers:

image

WIP:

  • SMT/D and THT
  • Component “form factors” (packages)
  • Symbols
  • Footprints

USB

This is an optional (but interesting and informative) video on USB and how it applies to keyboards: How does a USB keyboard work?.

USB2.0

Modern keyboards generally operate at USB 2.0 speeds. USB (2.0) connections consist of the following four lines: one for power (+/5V/VCC), one for ground (-/GND) and a pair of data lines. Below is the pinout of a USB-A connector:

image

Micro-USB

Smaller USB connectors are preffered for devices such as keyboards or phones. Micro-USB is an example of one that used to be very popular.

Below is the pinout of a Micro USB connector:

image

Type-C

Many keyboards nowadays opt to use a USB Type-C connector. Type-C connectors have a few extra pins required for hitting USB 3+ speeds that aren’t important for USB 2.0 operation.

These are the pins on a standard Type-C connector required for a USB 2.0 keyboard:

image

Type-C connections running on USB 2.0 devices also require 5.1k pullup resistors between the CC pins and ground. You'll see this in the schematic for the keyboard later in the guide.

There are different types of Type-C connectors (e.g. top mount, mid mount), but I'll go over those in the next section.

MicroController Units (MCUs) [WIP]

Integrated Circuits (ICs)

Great resource to read: Integrated Circuits - Sparkfun

Essentially, an Integrated Circuits are the black chips you see on PCBs. They have many small internal components that work together to perform certain functions. MCUs take inputs and give outputs. They are the brains of most PCBs. ICs can range in complexity, from simple logic gates (e.g. AND/OR gates) to desktop CPUs.

Image of two different types of ICs:

image

MCUs

I highly recommmend watching these videos, as I won't be explaining MCUs too indepth. The following videos should give you a good fundamental understanding of MCUs: This video gives you a good idea of what a MCU consists of and how it functions. This other video has useful information, however, the MSP430 section is irrelevant.

The microcontroller is the brain of a keyboard PCB. MCUs are a subcategory of integrated circuits that can range in size, complexity and capability.

For keyboards PCBs, 32-64 pin MCUs (E.g. ATMega32u4, STM32Fxxx) are generally used for their small size, relative affordability & availability, low power consumption and firmware support.

Before you move on, it's a good idea you at least recognise and/or understand the following terms/concepts:

  • Integrated Circuit (IC)
  • Microcontroller (MCU)
    • RAM
    • ROM/Flash storage
    • GPIO (General Purpose Input Output)
    • Clock/frequency

IC Packages

ICs come in a wide variety of packages with varying sizes and usecases. "Packages" are used to divide ICs into categories/formfactors.

Note: Sometimes you might see extra letters added to the package. E.g."TQFP" or "LQFP" vary slightly, but both fall under the same general "QFP" category. The same applies to other packages. QFN/QFP are just generalisations.

In terms of the common keyboard MCUs, most are either QFN or QFP.

The below image shows some common IC packages

image

Common keyboard MCUs (brief)

These are some common microcontrollers that keyboards use. I'll discuss the pros and cons of each MCU in the following list in the Design Considerations section, but here is the list for now:

  • AVR family (AVR)
    • ATmega16U2 / ATmega32U2
    • ATmega16U4 / ATmega32U4 (what I'll be focusing on for this guide)
    • AT90USB64 / AT90USB128
    • AT90USB162
  • STM family (ARM)
    • STM32F072
    • STM32F042
    • STM32F103
    • STM32F303
    • STM32F411
    • STM32F401
  • Raspberry family (ARM)

MCU Pinouts and Datasheets [WIP]

Want to know more about reading datasheets? Watch this.

Every MCU will have a piece of documentation called the datasheet. It basically describes what the MCU's capabilities are, its required components, routing guidelines, size/shape, pinout, etc. It's a good idea to get familiar with reading datasheets, as they provide lots of useful information.

For example, have a look at the ATMega32u4 datasheet.

A MCU pinout is a term used to refer to any piece of information that explains what each pin on the controller does. It will tell you what components to connect and where. E.g. You can use it to find all the GPIO pins (pins used for switch matrix), which pins to connect D+/D- traces to, which pins to connect your oscillator to, etc.

NOTE: All components have datasheets and pinouts, not just microcontrollers.

The following diagram is a pinout diagram for the ATMega32u4 (by hadi):

image

Decoupling Capacitors

Better resource: What is the Use of a Decoupling Capacitor?

Decoupling capacitors are used to help provide ICs with a steady voltage/supply of power. MCUs, for example, are an IC that require a steady voltage to protect the sensitive circuitry inside. EMI can cause noise throughout the PCB/circuit and lead to an unsteady voltage. By placing decoupling capacitors close to the controller, they can effectively reduce noise by creating a small power loop. Supplementary power is supplied when the voltage drops too low and excess power is absorbed when voltage is too high.

Placement/routing of decoupling capacitors is important. Correct placement and routing allows them to properly fulfil their role/purpose. While the chances are low, controllers may not function correctly if supplied voltage is too unstable. There are many resources online regarding decoupling capacitor placement/routing (one is linked above), but I’ll be going over how you should layout them later on (specifically for the MCU we will be using in this guide).

Placed close to the MCU, one 100nF capacitor for each VCC (power) pin is generally used in combination with a 10uF bulk capacitor (placed closer to the USB connector). This will vary from controller to controller, so the datasheet can give information in regards to exactly what value capacitors to use and where to place them. Examining known working examples of decoupling capacitor placement/tracing is also helpful.

Note: “decoupling capacitor” or is just a name given to certain capacitors with that specific role, they aren’t a special type of capacitor.

The following image demonstrates an electrical diagram of how a decoupling capacitor is connected and reduces noise (less voltage fluctuation):

image

Crystal Oscillator/Resonator

Microcontrollers work in sync with a clock, similar to our heartbeat. Generally an external component called a crystal oscillator is used to provide the clock: a constant, accurate, high frequency oscillating signal to the MCU. The required oscillator will depend on the MCU (read the datasheet).

Note: MCUs generally have their own internal oscillators, however, they are usually not accurate/reliable enough for USB operation. Though, some MCUs do have a viable internal oscillator.

What crystal oscillators can look like:

image

Resonators can also be used to provide a clock to the MCU.

Oscillators/resonators connect to 2 pins on the MCU: XTAL1 and XTAL2. Hence, the traces connecting them are generally referred to as XTAL traces.

Due to the high frequency nature of crystal oscillators/resonators/XTAL traces, they are prone to EMI and should be traced carefully to ensure proper operation.

Data traces

The D+ and D- traces coming from the USB connector are a differential pair. They should be routed parallel to eachother and be as close in length as possible. More on good routing practices for data traces later (when we get to it in the guide).

Terminating resistors are normally also required on the data lines (commonly two 22 ohm resistors, one for each line). They are generally placed close to the MCU, but it depends on the chip.

You can google more information about usb data traces/differential pairs. E.g. This article by Altium.

WIP: basic photo/diagram

EEPROM/Flash Storage

This storage is used to store information used by/with programs like VIA/VIAL. E.g. keymap, layout, macros, etc.

Note: Not all MCUs support emulating EEPROM on the chip's flash. Some chips require an extra component that acts specifically as an external EEPROM. I'll also discuss this more later.

Bootloader [WIP]

  • Flashing headers e.g. AVR-ISP
  • SCLK, MOSI, MISO

Pinout of an 6 pin AVR ISP header:

image

Reset Circuit/Switch [WIP]

MCUs generally have a RESET pin that enables debugging functions. E.g. putting the chip into bootloader mode.

Ground fills [WIP]

2 layer keyboard PCBs generally use a ground fill on both the top and bottom layers.

Electrostatic Discharge (ESD) Protection [WIP]

A Complete Guide to ESD Protection

Keyboard PCBs generally have an ESD chip close to the USB connector. This chip just protects the circuit from electrostatic discharge.

Polyfuse/Resettable Fuse [WIP]

A fuse is a component generally found on keyboard PCBs that ensures the entire circuit doesn't get overwhelmed with current/power. Polyfuses (resettable fuses) are used on keyboard PCBs so that the fuse doesn't have to be replaced if it trips.

Design Considerations ->