PCB Piano
Last update: 24/06/2025

The goal of this project is to create a small piano-style synthesizer, powered by a CR2032 coin cell.
Hardware

Like previous projects, this was done on a two layer board using KiCAD, prioritising the compact dimensions of the device. The MCU is the ATMega328pb which was chosen for its on-board touch sensing peripheral (PTC), relatively low power and its wide use. A debug LED was also connected directly to the MCU.
Audio output is generated by the MCU's PWM peripheral and is passed through a class D CMOS amplifier before the buzzer with the space added for a filter. In the board design, the pin used for this did not have access to any of the PWM peripherals to a modification needed to be made (yellow wire).
An important part of this project are the capacitive touch buttons which are the main user interface of the device. They were designed following the guidelines set out in the ATMega application documentation with hatching to reduce the overall capacitance within the same area and a full ground plane behind to prevent touches being registered from behind.
Firmware
The firmware for this project is written using the AVR SDK on bare-metal to gain experience with this type of firmware and to ensure fine control over device function.
Touch sensing was the first part of the firmware worked out. An unforeseen issue was that the open-source SDK did not offer any bindings for the PTC and its use was not documented outside of using code generation in the IDE. Instead, a custom solution was designed that only requires a digital pin.
First the pin is pulled high and then immediately set to high impedance to charge the key capacitor. The pin is next pulled low and the external and internal charges allowed to equalise over a few 10s of microseconds. If, after this period, the pin reads high then the key is deemed not pressed. If the pin reads low then less charge accumulated on the key due to the effect of the user's finger and is deemed pressed.
The PWM peripheral is controlled to generate the tones of the device. Square waves are generated by changing the period of the wave directly based on the key pressed, using a LUT of periods for each note. For other waveforms, a strategy of direct digital synthesis will be used to modulate the PWM period to approximate their frequency content.