Blog

I used an Arduino as a signal generator to test a cheap USB scope

A cheap USB scope is only useful if you can tell whether the picture on the screen is the signal, or the scope. The easiest way I know to check is to feed it a frequency you already know, from an Arduino, and see whether the trace and the readout agree.

In May 2020 I did that with a 16 MHz Arduino and a Hantek 6022BE, in OpenHantek6022 3.1.0. I stepped the output from 7.8 kHz up to 4 MHz. At the low end the scope shows a clean 5 V square wave and nails the frequency. At 4 MHz the frequency footer still reads 4.002 MHz, but the shape is no longer a square — it looks like a triangle. That is the useful part.

The listing for this class of scope says 20 MHz bandwidth and 48 MSa/s. Those are the numbers I want to hold the captures against. I have written about this scope before, in my short oscilloscope intro, in an I2C debug on a Blue Pill, and in the IR station post, where I used it to measure a 38 kHz carrier.

Video thumbnail: an Arduino as a signal generator to test a cheap USB scope, 4.002 MHz

Full walkthrough (3:38). Loads from YouTube only after you press play. Open on YouTube. The narration in my videos is an AI voice; the sketch, captures and numbers are from my bench.

The idea

You do not need a dedicated function generator for this. An ATmega328P already has hardware timers that can toggle a pin at an exact frequency derived from the 16 MHz crystal. If you set the registers yourself, you can calculate the frequency on paper, flash the board, and use that pin as a known source.

I first did this with a messy sketch called LED_PWM.ino — a pile of commented-out Timer0 and Timer2 experiments. One of the captures even has the Arduino IDE next to the scope, with the 62.5 kHz Timer0 path running on a Nano. I have since cleaned that work into one sketch, ScopeSignalGenerator.ino, which uses only Timer1 and puts every frequency on pin D9, at 50 % duty, without breaking millis() and delay().

The screenshots below are still the 2020 ones. The frequencies match the new sketch because the math is the same. The pins in 2020 were not D9: 7.8 kHz, 62.5 kHz and 4 MHz came out of Timer0 on D6, 100 kHz from Timer2 on D3, and 727 kHz / 1 MHz from Timer2 on D11.

The math, for a 16 MHz ATmega328P

Timer1 is a 16-bit counter. In CTC mode it counts from 0 to OCR1A inclusive, then starts again. If you set the output-compare mode to toggle OC1A on compare match, pin D9 flips once per timer period. Two flips make one output cycle, so:

f_out = F_CPU / (2 * N * (1 + OCR1A))

This sketch always uses prescaler N = 1, and F_CPU is 16,000,000 Hz:

f_out = 16 000 000 / (2 * (1 + OCR1A))
      = 8 000 000 / (1 + OCR1A)

That gives these exact values — not rounded names. The "7 kHz" and "62 kHz" filenames are the informal ones from the 2020 folder.

Key in the serial menuOCR1AExact frequency2020 capture
7.8 kHz102316e6 / (2 × 1024) = 7812.5 Hz7khz.PNG
62.5 kHz12716e6 / (2 × 128) = 62500 Hz62khz.PNG
100 kHz7916e6 / (2 × 80) = 100000 Hz100khz.PNG
727 kHz1016e6 / (2 × 11) = 727272.73 Hz727khz.PNG
1 MHz716e6 / (2 × 8) = 1000000 Hz1mhz.PNG
4 MHz116e6 / (2 × 2) = 4000000 Hz4mhz.PNG

The 7.8 kHz and 62.5 kHz values are the same numbers you get from 8-bit Fast PWM on Timer0, f = F_CPU / (N * 256), with N = 8 and N = 1. That is what the 2020 sketch actually ran for those two captures.

The heart of the cleaned sketch is just this: stop Timer1, load OCR1A, then start CTC with toggle and no prescaler.

// CTC, toggle OC1A (D9) on compare match, prescaler = 1
TCCR1A = _BV(COM1A0);
TCCR1B = _BV(WGM12) | _BV(CS10);

ScopeSignalGenerator.ino, startTimer1Toggle()

Open the serial monitor at 115200 baud and type 1 to 6 to pick a frequency, or 0 to stop.

Short: the Timer1 formula, and why 7.8 kHz and 62.5 kHz are exact.

What the 6022BE actually showed

All of the scope pictures below are OpenHantek6022 3.1.0, with the device title DSO-6022BE (FW0206). I am linking a Hantek 6022BL in the parts box: same 2-channel 20 MHz / 48 MSa/s analog section, plus a 16-channel logic analyzer. The screenshots are from the 6022BE.

7.8 kHz — a clean square

At 100 µs/div and 10 MS/s, this is an easy signal for the 6022. The high and low times on the badge are 63.9 µs and 64.0 µs (50 %). The zoom cursors read 128 µs, 7.81 kHz. The frequency footer is 7.819 kHz. Peak to peak is 5.01 V.

128 µs is exactly 7812.5 Hz. That is a good start: the Arduino is doing what the formula says, and the scope agrees.

OpenHantek 6022BE showing a clean 5 V square wave, frequency footer 7.819 kilohertz
7.8 kHz (exact 7812.5 Hz) on the 6022BE, 100 µs/div, 10 MS/s. Footer: 7.819 kHz, 5.01 Vpp.

62.5 kHz — and the sketch next to it

Step up to 10 µs/div, 24 MS/s. The badge is 8.04 µs and 7.96 µs (50 %). The footer is exactly 62.50 kHz, 4.97 Vpp. The zoom label says 16.0 µs (62.7 kHz); 16.0 µs is 62.5 kHz, and I would trust the footer here.

OpenHantek 6022BE showing a clean square wave, frequency footer 62.50 kilohertz
62.5 kHz on the 6022BE, 10 µs/div, 24 MS/s. Footer: 62.50 kHz, 4.97 Vpp.

The next picture is the same 8.04 µs + 7.96 µs badge and the same 62.50 kHz footer, with the Arduino IDE on the right. The sketch on the screen is LED_PWM, the 2020 file. The live lines are Timer0 with the prescaler set to 1, and analogWrite(led_pin, 127). The comment in the sketch already says 62500.00 Hz. The IDE status line says Arduino Nano on COM7.

This capture is at 200 mV/div and 1.05 Vpp, not 5 V. I have three captures in that lower-voltage group (this one, the 1 MHz close-up, and the 4 MHz shot). I am showing them as they are.

OpenHantek 62.50 kilohertz capture beside the Arduino IDE running LED_PWM on a Nano
Same 62.50 kHz signal, with the 2020 sketch visible in Arduino IDE 1.8.12. Nano on COM7. 200 mV/div, 1.05 Vpp.

100 kHz — frequency yes, duty cycle no

At 5 µs/div, 24 MS/s, the footer is 100.0 kHz and the cursors are 10.0 µs. That part is clean. The duty-cycle badge is not 50 %: it reads 3.21 µs and 6.79 µs, 32 %, and you can see the unequal high and low times on the trace. Peak to peak is still 4.97 V.

The 2020 Timer2 recipe for 100 kHz used mode 7 with OCR2A = 159, which does give 16e6 / 160 = 100 kHz, but the duty depends on OCR2B. The cleaned sketch does not repeat that: on D9 the 100 kHz output is 50 %.

OpenHantek 6022BE at 100.0 kilohertz with a 32 percent duty cycle
100.0 kHz, 5 µs/div, 24 MS/s, 4.97 Vpp. The frequency is right. The duty cycle in this capture is 32 %, not 50 %.

727 kHz — the edges start to soften

Expected frequency: 16e6 / 22 = 727272.73 Hz. The capture is at 1 µs/div and only 12 MS/s, because channel 2 was left on (it is idle, a few tens of millivolts — ignore its footer). The CH1 footer is 727.8 kHz, 4.97 Vpp. That is a measured reading, a little above the calculated 727.27 kHz. The zoom cursors read 1.37 µs, 732 kHz. The badge still says 50 % (667 ns + 667 ns), but the sides of the square are already sloped.

That is the first place you can see the scope working harder. 12 MS/s is only about 16 samples per cycle at this frequency, and the edges are no longer vertical.

OpenHantek 6022BE at 727.8 kilohertz with visibly sloped edges
727 kHz on the 6022BE, 1 µs/div, 12 MS/s. CH1 footer: 727.8 kHz. Zoom cursors: 1.37 µs (732 kHz). The edges are already softening.

1 MHz — still readable

Same 1 µs/div and 12 MS/s, still with CH2 on. The badge is a neat 500 ns + 500 ns (50 %). The zoom cursors read 1.01 µs (995 kHz). The CH1 footer is 1.000 MHz, 4.97 Vpp. The wave is a trapezoid now — you can see the rise and fall — but you can still read it as a square, and the frequency is right.

OpenHantek 6022BE at 1 megahertz with trapezoid edges
1 MHz, 1 µs/div, 12 MS/s. CH1 footer: 1.000 MHz, 4.97 Vpp. Cursors: 1.01 µs (995 kHz).

A second capture of the same 1 MHz, with CH2 off this time, is at 24 MS/s and 500 ns/div. The footer is 1.001 MHz, the cursors are 1.00 µs (1.00 MHz), and there is undershoot on the falling edge. This one is at 200 mV/div and 1.06 Vpp, like the Nano side-by-side shot.

OpenHantek 6022BE 1 megahertz close-up at 24 megasamples per second with undershoot on the falling edge
A closer 1 MHz look, 500 ns/div, 24 MS/s. Footer: 1.001 MHz, 1.06 Vpp.

4 MHz — the frequency is right, the shape is not

This is the one I would show someone who is about to trust a cheap USB scope at a few megahertz. Timebase 500 ns/div, 24 MS/s, 200 mV/div. The footer is 4.002 MHz, 1.08 Vpp. The zoom cursors read 246 ns, 4.07 MHz. A 4 MHz square should be 250 ns per cycle; 167 ns + 83.3 ns on the badge adds up to 250.3 ns, so the period is still there.

The picture, though, is a triangle. The duty-cycle badge says 67 %, which I would not believe — once the wave is no longer a square, that badge is not a duty cycle any more.

The 6022 listing says 48 MSa/s and 20 MHz bandwidth. 4 MHz is well inside 20 MHz, so this is not "the analog front end cannot see 4 MHz". At 24 MS/s you only get about six samples per cycle. That is not enough to draw a square wave, and it is enough for the software to measure a period close to 250 ns. The footer, 4.002 MHz, is actually very close to the calculated 4 MHz.

OpenHantek 6022BE at 4.002 megahertz, the waveform looking like a triangle
4 MHz on the 6022BE, 500 ns/div, 24 MS/s. Footer: 4.002 MHz, 1.08 Vpp. Zoom: 246 ns (4.07 MHz). The shape is a triangle, not a square.
Short: the same Arduino, from 7.8 kHz up to 4 MHz, and the point where the square becomes a triangle.
Short: at 4 MHz the 6022 still reads 4.002 MHz, but the trace is no longer a square.

What I take from this

  • For kilohertz work — 7.8 kHz, 62.5 kHz, 100 kHz — the 6022BE in OpenHantek showed a clean 5 V square and a frequency footer I would trust.
  • Around 727 kHz to 1 MHz the frequency is still right, and the edges start to look like the scope, not like the pin.
  • At 4 MHz I would trust the frequency footer (4.002 MHz) and I would not trust the shape, or the duty-cycle badge.
  • The software did not always run at the 48 MSa/s on the listing. These captures are 10, 12 or 24 MS/s. Leaving a second channel on cut the 727 kHz and 1 MHz shots to 12 MS/s.
  • A logic analyzer is great for a digital period. It will not show you this analog story. I used one heavily on the IR station envelope. For "does this square wave still look like a square?" you want the scope.
  • If you need to see a clean few-megahertz square, this is the class of problem a higher-sample-rate bench scope is for. I did not use a Rigol in this test. There is one in the parts box as an upgrade option, and the differences I can state are only what the two listings say: 200 MHz and 1 GSa/s, with its own screen, versus 20 MHz and 48 MSa/s on a PC USB scope.

I would do this again the next time I unbox a cheap scope: pick a pin, set a timer, and walk the frequency up until the picture falls apart.

Get the code

The cleaned sketch is ScopeSignalGenerator.ino. It is written for a 16 MHz Uno or Nano. The serial menu is 115200 baud, the output is pin D9. The original 2020 file, LED_PWM.ino, is the one I actually ran on the bench for these screenshots.

If you build this, the parts are below. I would love to hear how far your scope stays honest.