Pocket Frequency Generator on a CH32V203 Makes 1 Hz to 30 MHz Square Waves in Hardware

User Avatar Aswinth Raj
Author
Share
Pocket Frequency Generator Built With CH32V203 Microcontroller

A square wave generator is one of the most used tools on any bench, and Alun Morris has shown how little it can take to build one. His Mega Hurts frequency generator is a pocket-sized, USB-C powered box that outputs square waves from 1 Hz to 30 MHz with an adjustable duty cycle. There is no dedicated clock generator or PLL chip in it. The whole job is done by a low-cost CH32V203F6P6 RISC-V microcontroller, a rotary encoder and a 128 x 64 OLED.

How it works

The CH32V203 runs its timer from a 120 MHz clock, and the output pin (PA0) is driven by timer 2 channel 1 in PWM mode. Because the wave is made by the timer hardware, the CPU is free to handle the encoder and the display, and the output does not jitter with the firmware. For each requested frequency, the firmware works out the best combination of prescaler, period and compare values. Changes are applied glitch-free: all three values land together at the next timer update, so sweeping the knob does not throw out a broken cycle.

Duty can be set in 1% steps and is shown to 0.01%. The screen also shows N, the number of timer counts per period, which is the real resolution available at that frequency. At low frequencies there are thousands of counts to play with. Near the top of the range there are only a handful, which is the honest trade-off of making a wave from a timer.

The user interface is a single encoder with a push button. A menu offers eight decade bands from 1 Hz+ to 10 MHz+, but once you are tuning, the knob can move the frequency anywhere in the range. The encoder accelerates: a slow turn changes the frequency by one step, a fast spin by up to 60 steps. The output is off at power-on and turns on as soon as you pick a band or change a value.

Small details worth copying

  • Two clock options. The default build uses the internal oscillator, which is about 1% accurate. A second build uses an optional 8 MHz crystal for accurate output. The firmware checks the system clock at startup and shows CLOCK MISMATCH instead of running at the wrong frequency if the crystal does not start.
  • OLED burn-in protection. The display polarity flips on every power-on, and the last state is kept in a user option byte, so no pixel stays lit for months.
  • No programmer needed. The chip is flashed over USB through its built-in bootloader from a PlatformIO project, and the frequency planning maths has host-side unit tests.

Morris is open about the limit: at 30 MHz the edges are poor because of the chip's output slew rate, so the top of the range is better for clocking digital logic than for clean measurements. Output is on an optional BNC socket. He notes that the firmware was written together with Claude Code.

Our take

Most hobby signal generators either use a DDS module or an Si5351 clock chip. Mega Hurts shows that for square waves, a modern 32-bit microcontroller's timer is already enough, and that the clever part is the frequency planner that picks the timer values. If you own a CH32V203 board, this is a weekend build that ends up as a tool you will actually use, whether for clocking a logic chip, testing an optocoupler or checking a PWM input. The parts list (a CH32V203 dev board, an OLED, an encoder and a 3D-printed case) keeps the barrier low.

Image credit: Alun Morris, cropped and resized.

About the author: Aswinth Raj is an electronics engineer and the editor of CircuitDigest, where he has published more than 150 hands-on projects and tutorials across Arduino, PIC, ESP32, Raspberry Pi and EV battery systems. He selects the stories on this page for what engineers and makers can actually learn or build from. Connect on LinkedIn

Have any question related to this Article?

Add a comment

No account needed. Your comment appears once a moderator has reviewed it.

Never shown publicly.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.