📜 ⬆️ ⬇️

KeeBee. Making from scratch your own USB keyboard

A few months ago, my USB keyboard project was completed. Among other things, I designed the electronic circuits, designed the printed circuit boards, programmed the firmware, made the layout in CAD and assembled the device. The result is a comfortable keyboard that I use every day and affectionately call KeeBee:


Keyboard KeeBee in final form

Several project goals:

  1. Independent creation of the scheme.
  2. Writing keyboard firmware.
  3. Learn how the USB protocol works.

At work, I mainly develop software for cloud services, where many layers of real hardware are missing. Therefore, it is very pleasant to get rid of some abstractions and go down to the hardware level: here are real electronic devices that can be touched and used.

CAD study and layout


I really like the minimalist keyboard in the style of OLKB Planck and Preonic, which, due to the ortholinear arrangement of the keys, turn out to be very compact. I also knew right away that I wanted to use the Cherry MX Brown switches. Having in mind these two components of the design, I started playing with the key layouts in OpenSCAD . This is a great open source tool that works more like a programming language than a mouse WYSIWYG interface.

Taking the dimensions of the elements from the Cherry MX documentation, I made a top plate layout , then added switches and keys to get an idea of ​​what the final result would look like. The top plate is located above the keyboard PCB and serves as a good stabilizer for the switches.

Top plate design:



After adding keys:





Board prototyping and firmware design


I chose STM32F042K6T6 as the main microcontroller. This is about three dollars per chip, if you take from one piece. He has enough contacts for the matrix scan 69 keys (total 32 contacts). It runs on an ARM Cortex M0 processor and contains special USB peripherals for sending a USB signal without loading the main processor for this task. I bought a Nucleo dev board for prototyping with this chip before integrating it into my PCB design. Nucleo very conveniently lay down on the mock-up board and powered over USB.

I placed a small four-key circuit on the breadboard to test the diode circuit I was studying. Ignoring the USB side of the equation, the first step was to force the Cherry switches to safely turn on and off the four corresponding LEDs when the buttons are pressed.



The scan matrix is ​​a technique that needs to be used when you have more switches than contacts on the microcontroller.

When the matrix worked satisfactorily , it's time to work on USB.

The built-in firmware cycle is essentially as follows:

  1. Scan all the keys in the matrix.
  2. Match the location of the buttons with the corresponding characters in the selected layout (QWERTY, Dvorak, etc.).
  3. Take the matching result, generate USB HID Report packages and send to a USB peripheral device.
  4. Turn on the LED on the keyboard, if the key is pressed, turn it off - if not.

From main.cc :

static void scan_and_update() { scan_matrix.Scan(key_scans, row_count, column_count); keyboard.SendReport( key_pipeline.MapKeyScans(key_scans, key_count)); update_key_press_status(); } int main() { Init(); status_led.SetOk(true); while (true) { scan_and_update(); } } 

The keyboard.SendReport component actually transfers packets to the USB host. I tried hard to get USB to work properly. This protocol has many non-trivial layers that require precise timing and proper device identification . In the end, I had to run Wireshark to listen to all the USB packets coming into my Linux laptop and find out where everything was lost. An Internet search practically did nothing, most questions are answered like this: “Probably, your USB device has broken, you need to buy a new one.” If you are actually trying to design a USB device, such answers are not very helpful. I just had to immerse myself in the volume specifications of USB with a lot of unfamiliar terminology.

Having fidgeted for a while, I still made the keyboard with four keys correctly identify itself as a USB HID (Human Interface Device) and all clicks were transferred to the laptop correctly:



Registering as a USB vendor and getting an official device id is expensive . If you just have an amateur project, then you have to grab the ID of some similar device . I thought that “Gear Head” sounds cool, the more they release keyboards, so I chose them.



Circuit and PCB


Having received a more or less working firmware and a working prototype, it’s time to draw up a circuit and PCB design in KiCAD and make a real PCB. When I achieved that the scheme for 4 buttons works, there was a relatively simple task to put everything together:



After the development of the scheme and installation of sites for components, you need to produce a layout of the real printed circuit board:



KiCAD is able to beautifully render future motherboard in 3D:





There are a lot of great KiCAD tutorials. I started with the excellent video series Getting to Blinkey 4.0 from Chris Gammela, where he explains in detail all the steps involved in creating an LED elinking scheme in KiCAD from beginning to end.

PCB and component order


Having brought the circuit and design of the PCB to a satisfactory level, I began to place a bunch of orders:

  1. All components from the list of materials : switches, LEDs, diodes, microcontrollers, etc. I usually order such things on DigiKey .
  2. The circuit board itself. Quite a lot of services are ready to make you a prototype cheap. I have a great experience with OshPark and JLCPCB . For this project, I chose JLCPCB because of the price of this size, and also because they were allowed to choose a blue coating.
  3. All other details: covers and stuff. For this project, my brother-in-law helped the laser cut the top and bottom keyboard plates from 1/4 ”acrylic sheets. For the remaining parts, you can use the online services of laser cutting and 3D printing, if there is no access to the equipment.

The day when the parcel arrived with the fee is the best:





JLCPCB is a very affordable service. This design with the delivery of DHL from China cost less than $ 30, and the whole process took a little more than a week from downloading files to the arrival of the package.

Shurin took the DXF files from OpenSCAD and threw them into the laser cutter:



Final build


After receiving all the details, I started the assembly. The first step is to assemble the components of the printed circuit board: a soldering iron for large electronic components and a soldering station for surface mounting small components, such as an STM32 microcontroller.

The total assembly time of the board was about three hours - most of it was soldered to 70 diodes and switches.

I added a header for JTAG debugging, through which I connected JLINK Edu mini for microcontroller firmware using OpenOCD .

Then it's time for final testing and final assembly:







The son decided that this was a great train for his animals:





Results


From the initial idea to the final assembly, the project took about three months. It was extremely useful as a hobby to do what I still use every day at work.

All project files are published on GitHub , including firmware sources, printed circuit layouts, materials list, and CAD models.

Thanks for reading and nice hacks!

Source: https://habr.com/ru/post/441032/