Radio from scratch · Chapter 12

Inside the dongle

Before you startThis chapter connects the hardware to ideas you already know:

Since chapter 3 we've taken the I/Q numbers for granted: they arrive over USB, and we process them. This chapter opens the box. The surprise is how little in it is new. The dongle uses exactly the tools you've already learned (mixing, filtering, sampling, decimating), partly with electronics and partly with numbers, to deliver a 2.4 MHz slice of radio from anywhere between 24 and 1766 MHz.

Inside are two chips: a tuner (Rafael Micro R820T2), which handles the radio waves, and an RTL2832U, which turns them into numbers and talks USB. Between them they share one tiny component that sets the pace for everything: a 28.8 MHz crystal (chapter 11's metronome).

Two words you'll meet from here on. Chips are controlled through registers: small numbered memory slots, each holding one byte, where writing a number changes a setting (one register picks the gain, three more set the tuning, and so on). And the two chips talk to each other over I2C ("I-squared-C"): a simple two-wire connection that lets one chip read and write another chip's registers. Our computer never talks to the tuner directly; it asks the RTL2832U to pass messages along (chapter 13).

Before the dongle: the antenna and the first amplifier

Everything in this chapter starts at the dongle's antenna socket. What arrives there was caught by an antenna: a piece of metal in which the passing radio wave pushes electrons back and forth (chapter 1's "what is actually spinning"), making a tiny voltage, often a millionth of a volt or less. How long an antenna should be and how to aim it is the subject of ham lesson 6, lesson 7 and the antenna lab. Here, one question. The station arrives together with the antenna's own noise (chapter 3), and then every part of the receiver adds noise of its own. Whose noise wins?

Predict: a weak satellite is buried in the dongle's own noise. You add an amplifier with 41 dB of gain right at the antenna. What should happen to the dongle's gain knob?

Down. The amplifier did its job before the dongle added any noise; the knob's only job now is chapter 3's: fill the ruler without clipping. This site's satellite radio clipped 13% of its recorded numbers on 16 October because nobody turned it down.

The part that adds the least noise wins, as long as it comes first. A low-noise amplifier (LNA) at the antenna adds very little noise of its own and makes everything it hears, the station and the antenna's noise, much bigger. By the time the dongle adds its own noise, the signal is so far above it that it hardly counts. How much noise a part adds is its noise figure, in dB: 0 dB would be a perfect part; the SAWbird+ NOAA amplifier on this site's satellite antenna is rated 1.1 dB, with 41 dB of gain (Nooelec's datasheet). Here's what that looks like, measured:

This is the noise across the 9.6 MHz this site's satellite receiver (a HackRF, a bigger cousin of the dongle, behind the SAWbird) hears, with no satellite overhead. Far from 137.5 MHz (below 135.5 or above 141) is the receiver's own noise. Across the middle, the amplified noise of the antenna stands about 27 dB higher, 500 times the power, with the filter's sloping edges in between. So the receiver adds less than 0.01 dB to the noise of the station, and turning it up or down changes nothing that matters, only where things sit on the ruler. That's the whole case for an amplifier at the antenna. (Move one around a chain with a lossy cable in the noise-figure lab; the physics of where noise comes from is ham lesson 11.)

Two things go with it. The picture's flat-topped shape is a filter: the SAWbird keeps 2.6 MHz around 137.5 MHz before amplifying, because an amplifier makes everything bigger, including strong FM stations and pagers that would otherwise clip the amplifier or the dongle (chapter 3). And the gains after the amplifier go down: on this receiver, the measured sweet spot was the HackRF's own amplifiers at 16 and 12 dB, about 7 counts RMS on the ruler. Turned up much further, it ran out of room: at about 70 counts RMS the 27 dB lead had shrunk to 9. If strong stations still overload, an attenuator (a part that weakens everything by a fixed number of dB) belongs after the amplifier, never before it, where it would cost signal-to-noise.

The first amplifier decides the noise. Put a quiet one at the antenna, filter before it, and turn every gain after it down until nothing clips.

Why does the first amplifier decide?
  1. Every part adds some noise of its own; what matters is how big that noise is compared with the signal at that point.
  2. An amplifier with gain G makes the signal, and the noise it arrives with, G times bigger.
  3. The next part then adds its own noise, unchanged, so compared with the now G-times-bigger signal it counts G times less.
  4. So each part's noise counts divided by all the gain in front of it, and the first part's counts in full.
  5. Measured on this site's receiver: behind the SAWbird, the antenna's noise stands 27 dB above the receiver's own, so the receiver adds 0.008 dB. Without an amplifier in front, the receiver's noise would count in full.

The signal's journey

Once the antenna and the first amplifier have delivered a signal strong enough to beat the receiver's own noise, it enters the dongle's two chips. How do those two chips get a 98.7 MHz radio wave down to the 2.4 MS/s USB stream we've been using since chapter 3?

Predict: the dongle's converter (the part that takes chapter 3's snapshots and turns them into numbers) works with 8 bits, 28.8 million times a second. Where does the tuner put a 98.7 MHz station before it's turned into numbers?

A few MHz, slow enough for the 8-bit converter: the intermediate frequency (IF), the next section's subject. Then the RTL2832U does chapter 5's mixing, chapter 6's filter and chapter 7's decimation in digital form.

Step through the seven stages. The picture shows what the radio band looks like at each point, for the dongle tuned to 98.45 MHz (with 98.7 FM the station we care about, in yellow):

Notice the pattern: analog electronics do the rough work, numbers do the precise work (the analog half is its own course: Electronics from scratch). The tuner's job is to bring a slice of radio down from 98 MHz (far too fast to digitize cheaply) to a few MHz, where an 8-bit converter can sample it. From then on, it's chapter 5 (the digital mixer), chapter 6 (a filter) and chapter 7 (decimation), done inside the RTL2832U before the data ever reaches your computer.

Why an intermediate frequency, and the image problem

Why not just build a filter that picks out 98.7 MHz directly? Because a sharp filter whose center you can move anywhere from 24 to 1766 MHz is very hard to build. A sharp filter at one fixed frequency is easy. So radios since the 1920s have done it the other way round: move the signal to a fixed frequency, the intermediate frequency (IF), and filter there. This trick is called the superheterodyne, and it's in almost every radio ever made.

Moving the signal is done by mixing with a local oscillator running 3.57 MHz above the station (this tuner's IF is 3.57 MHz). But there's a catch you already know: an analog mixer multiplies by a plain wave, one shadow, so everything lands in two places (↩ ch. 5). That means two different radio frequencies end up on the IF: the one you want, 3.57 MHz below the oscillator, and its mirror, 3.57 MHz above it. The mirror is called the image:

The defense is a tracking filter in front of the mixer (green): a broad filter that follows your tuning and weakens everything far away, including the image, 7.14 MHz above you. It's not very sharp (that's the whole problem), so a strong station sitting exactly on your image can still sneak through as a faint ghost. In the driver, choosing this filter's setting for each frequency band is the job of a lookup table (BANDS).

The oscillator runs at 102.02 MHz so that 98.45 MHz lands at a 3.57 MHz IF. Which other frequency, in MHz, also lands at 3.57 MHz (the image)?

102.02 + 3.57 = 105.59 MHz: the same distance on the other side of the oscillator. With one shadow, the mixer can't tell them apart, so the tuner's filters must remove 105.59 MHz before mixing.

Making any frequency from one crystal: the PLL

In software (chapter 5), making a local oscillator at any frequency was just typing a number into Math.cos. In hardware, how does the tuner chip make 102.02 MHz to listen to 98.45 FM — or 1093.32 MHz for aircraft at 1090 — when you can't pack a million quartz crystals into a $30 stick? Inside the dongle there is only one 28.8 MHz crystal, and in chapter 14 our driver will have to program the gear ratios that turn that one crystal into any frequency on the dial. The circuit that does it is a phase-locked loop (PLL): a fast, adjustable oscillator (the VCO) that's constantly compared with the crystal and nudged until its speed is an exact multiple of it. Think of a gear train: the crystal turns at one speed, and the gears set the ratio.

Type any frequency and see exactly how the tuner makes it. These are the same steps and numbers our driver uses:

The VCO runs between 1.77 and 3.54 GHz and is divided by a power of two. Which divider gives a 102.02 MHz oscillator?

102.02 × 32 = 3264.6 MHz, inside the range (×16 = 1632 MHz is too low, ×64 = 6.5 GHz too high). Exactly one power of two always fits, because the range spans exactly 2×.
The math, step by step

\[ f_\text{IF} = f_\text{LO} - f_\text{RF}, \qquad f_\text{image} = f_\text{LO} + f_\text{IF} = f_\text{RF} + 2 f_\text{IF} \]

\[ f_\text{LO} = \frac{f_\text{VCO}}{2^k}, \quad 1.77\,\text{GHz} \le f_\text{VCO} \lt 3.54\,\text{GHz}, \qquad \frac{f_\text{VCO}}{2 f_\text{xtal}} = N + \frac{\text{frac}}{65\,536} \]

Where it breaks

The PLL can't hit every frequency exactly: its fraction has 65,536 steps per crystal multiple. This chapter's script shows the leftover error: 4 Hz at 98.45 MHz, but 176 Hz at 1089.75 MHz. For FM that's nothing. For a Morse signal heard as a 700 Hz beat (chapter 10), 176 Hz moves the pitch by a quarter, and for a narrow digital signal it can matter. Receivers that care measure the error and correct it in software, with chapter 5's mixer.

Lab: explore the whole range

The same dongle, from aircraft to plane transponders. For each frequency, below the spectrum is how the tuner's PLL is set up to reach it (computed from where the dongle is actually tuned, 0.25 MHz below your choice).

At 1090 MHz you won't see a steady hump: aircraft transponders send short bursts of pulses, a few microseconds long, which show up as flickers of energy across the band.

In code

Show the real code (optional: everything it does is explained above)

The PLL setup, from our tuner driver (setPll()): find the divider, split the ratio into whole and fraction, write them, check for lock:

let mixDiv = 2, divNum = 0;
for (; mixDiv <= 64; mixDiv <<= 1) {                               // smallest power of two that puts the VCO in range
  if (freq * mixDiv >= vcoMin && freq * mixDiv < vcoMax) { for (let d = mixDiv; d > 2; d >>= 1) divNum++; break; }
}
const vcoFreq = freq * mixDiv;
const vcoDiv = Math.floor((this.xtal + 65536 * vcoFreq) / (2 * this.xtal));   // ratio × 65536, rounded
const nint = Math.floor(vcoDiv / 65536), sdm = vcoDiv % 65536;               // whole part, fraction
const ni = Math.floor((nint - 13) / 4), si = nint - 4 * ni - 13;
await this.writeReg(0x14, ni + (si << 6));
await this.writeReg(0x16, sdm >> 8);
await this.writeReg(0x15, sdm & 0xff);
// … wait 10 ms, read register 2, check bit 6 ("locked")

Testing hardware code without hardware. The chapter's script runs this real driver against a fake tuner: an object that pretends to be the I2C bus, records every register write, and always answers "locked". Then it checks that the driver wrote exactly what the calculator above predicts. Faking the hardware like this is how drivers get tested automatically.

Where this chapter's numbers come from. The numbers this chapter computes come from a short checking script behind this page, which also checks each claim and stops if one is wrong. Its output:

   98.45 MHz: ×32, VCO 3.2646 GHz, integer 56, fraction 44419 → reg 0x14 = 0xca, off by   4.3 Hz  (driver wrote the same)
  118.45 MHz: ×16, VCO 1.9523 GHz, integer 33, fraction 58618 → reg 0x14 = 0x5, off by -17.1 Hz  (driver wrote the same)
   162.3 MHz: ×16, VCO 2.6539 GHz, integer 46, fraction  4915 → reg 0x14 = 0x48, off by -11.0 Hz  (driver wrote the same)
     440 MHz: × 4, VCO 1.7743 GHz, integer 30, fraction 52656 → reg 0x14 = 0x44, off by -78.1 Hz  (driver wrote the same)
 1089.75 MHz: × 2, VCO 2.1866 GHz, integer 37, fraction 63078 → reg 0x14 = 0x6, off by -175.8 Hz  (driver wrote the same)
below 24.08625 MHz there's no divider big enough: pll(20 MHz) = null
image of 98.45 MHz: 105.59 MHz (oscillator + 3.57 lands on the same IF)
measured: the amplified antenna noise is 27.4 dB above the receiver's own, which adds 0.008 dB to it
all checks passed

Check yourself

Why do radios move the signal to an intermediate frequency instead of filtering it where it is?

A sharp filter that can be moved across a huge range is very hard to build; a sharp filter at one fixed frequency is easy. So the signal is moved to the fixed frequency instead.

Listening at 100.0 MHz, where is the image?

The oscillator is at 103.57 MHz; its mirror on the other side is 103.57 + 3.57 = 107.14 MHz.

Why does the dongle's range stop at about 24 MHz at the bottom?

The VCO can't go below 1.77 GHz, and the biggest divider is 64: 1.77 GHz ÷ 64 = 27.7 MHz for the oscillator, which is 24.1 MHz minus the 3.57 MHz IF.

The analog mixer makes two copies. Why doesn't the digital mixer?

The digital mixer multiplies by a full spinning arrow (both shadows, I and Q), which moves everything in one direction only. The analog mixer can only multiply by one shadow (chapter 5).

Last updated