Radio · developer guide · Build for your own dongle → HackRF Pro
Build for HackRF Pro
The RTL-SDR guide drove a $30 dongle by writing its chips' registers one by one. A HackRF Pro is the other kind of radio: a microcontroller and an FPGA run Great Scott Gadgets' firmware, and you talk to that firmware instead. You send a handful of USB requests (a frequency, a sample rate, three gains) and samples stream back. Our driver is one file of TypeScript (radio/src/hackrf.ts, about 120 lines), no libhackrf, and it runs the server SDR behind Spectrum Lab today.
Why a second radio
RTL-SDR (Nooelec)
2.4 MHz of spectrum at once, 8-bit. 24–1766 MHz. A spike at the center frequency. Receive only. You program the RTL2832U demodulator and the R820T tuner yourself.HackRF Pro
Up to 20 MS/s: here 9.6 MS/s, an 8 MHz window, every FM station from 94 to 103 MHz in one picture. A TCXO (stable clock), no DC spike. 8-bit at full speed. It can transmit, which this driver never does. The firmware does the chip-level work.Wider isn't more sensitive: still 8 bits per sample, so a strong station and a faint one share the same 48 dB or so of range. What you gain is coverage: a whole band at once, so a scanner can watch every channel instead of hopping between them.
Opening it
USB vendor 0x1d50, product 0x6089: the same IDs as the HackRF One, which is how one driver serves both. Claim interface 0 and ask who it is:
const dev = (await navigator.usb.requestDevice({ filters: [{ vendorId: 0x1d50, productId: 0x6089 }] }));
await dev.open(); await dev.selectConfiguration(1); await dev.claimInterface(0);
const v = await dev.controlTransferIn({ requestType: "vendor", recipient: "device", request: 15, value: 0, index: 0 }, 255);
new TextDecoder().decode(v.data).replace(/\0.*$/, ""); // "v2.4.0": the firmware version
Request 14 reads a board ID (One or Pro). In Node the same calls work through the usb package's WebUSB object, which is what our server does.
The vendor requests we use
Every control is a vendor request to the device, numbered as in libhackrf's hackrf.c. Values travel in the setup packet's value/index, or as a few little-endian bytes of data.
| # | Request | How |
|---|---|---|
| 1 | Transceiver mode | out, value 0 = off, 1 = receive (2 = transmit: never sent here) |
| 6 | Sample rate | out, 8 bytes: uint32 freq_hz, uint32 divider (rate = freq / divider; whole rates use divider 1) |
| 7 | Baseband filter | out, value = bandwidth & 0xffff, index = bandwidth >> 16 (Hz) |
| 14 | Board ID | in, 1 byte |
| 15 | Version string | in, up to 255 bytes |
| 16 | Frequency | out, 8 bytes: uint32 MHz, uint32 Hz (98.7 MHz = 98, 700000) |
| 17 | RF amp | out, value 0 or 1 (+14 dB in front of everything) |
| 19 | LNA gain | in, 1 byte back (1 = accepted); index = 0–40 dB in steps of 8 |
| 20 | VGA gain | in, 1 byte back; index = 0–62 dB in steps of 2 |
Compare the RTL-SDR: setting a frequency there means computing the R820T's PLL dividers and writing them over I²C, through the RTL2832U's register bridge. Here it's one request: the firmware does the PLL math.
Sample rate and the baseband filter
The rate is 2–20 MS/s. In front of the ADC sits a low-pass baseband filter with fixed steps (1.75, 2.5, 3.5, 5, 5.5, 6, 7, 8, 9, 10, 12, 14, 15, 20, 24, 28 MHz, total width). It has to be narrower than the rate, or what lies beyond ±rate/2 folds back into the picture (aliasing). libhackrf picks the widest step under ¾ of the rate; we ask for 8 MHz at 9.6 MS/s, so the usable window is the filter's ±4 MHz.
Why 9.6 and not 10 or 8? Our server cuts each listener a slice of the window (240 kHz for one station, 1.2 MHz for a wider look) by mixing and decimating, and decimating by a whole number is what keeps that cheap and exact: 9.6 MHz ÷ 240 kHz = 40 = 5 × 4 × 2. Try a few rates:
The minimum, 2 MS/s, matters too: our APRS decoder wants 1.024 MS/s, below what the HackRF can do, so the server asks for twice that (2.048) and the decoder decimates one step more.
Three gain stages
RF amp, 0 or 14 dB
At the antenna, before the mixer. For weak signals, and the stage strong signals overload first.LNA, 0–40 dB, steps of 8
The low-noise amp in the receive chain. Raise it first for weak signals: gain early sets the noise figure.VGA, 0–62 dB, steps of 2
Baseband gain, after the mixer, just before the ADC: it scales what reaches the 8-bit converter.The rule of thumb: enough gain that the noise floor rises a few dB when you add more (then the receiver's own noise isn't the limit), and not so much that the ADC hits its rails. Spectrum Lab shows the ADC level in dBFS and the share of samples at full scale: raise the LNA until the noise floor lifts, back off if clipping appears. On NYC's FM band LNA 16–24 with VGA 20 works; a weak 2 m repeater wants LNA 40. The server remembers a setting per band, because what suits broadcast FM is far too little for a 2 m repeater.
What can and can't hurt it
LNA and VGA can't damage the radio. Too much of either only clips the ADC: a distorted, useless picture, but no harm. What can: a very strong signal into the antenna port (a transmitter close by), made worse by the RF amp; switching on antenna power (a bias tee) into an antenna that shorts DC; and transmitting into the wrong thing. So:
- No transmit, no antenna power. Our driver doesn't implement either request: there's nothing to misuse.
- The RF amp is refused near strong transmitters. The server won't turn it on while the window covers FM broadcast, VHF or UHF TV, cellular downlinks or paging, and switches it off itself if you tune there with it on.
- The amp switches itself off when the signal clips. Every half second the server measures the ADC; if more than 0.2% of samples sit at full scale while the RF amp is on, the amp goes off. LNA and VGA stay where you set them: too much of them only distorts. (An earlier version also walked LNA and VGA down on clipping, and it kept muting weak 2 m repeaters: protection that costs reception isn't worth it where nothing can break.) For reference, LNA 40 + VGA 62 on the FM band clips about 0.5% of samples at −6.7 dBFS.
The limits live in the server (radio/server.ts), not the page, so no visitor, page or API call can get around them.
Signed samples
Both radios send interleaved I, Q bytes, but the RTL-SDR's are unsigned (0–255, 127.5 is zero, the .cu8 format) and the HackRF's are signed (−128–127, .cs8). Everything downstream of our drivers expects .cu8, so the HackRF driver converts, and the conversion is one XOR: flipping the top bit of a two's-complement byte adds 128.
// signed → unsigned: s + 128 === s ^ 0x80 for every byte; four at a time
const w = new Uint32Array(bytes.buffer, 0, bytes.length >> 2);
for (let i = 0; i < w.length; i++) w[i] ^= 0x80808080;
Streaming at 19 MB/s
Switch the transceiver to receive (request 1, value 1) and read bulk endpoint 1 (0x81) in 256 KiB transfers, four in flight, oldest first, exactly as in the RTL-SDR guide. At 9.6 MS/s that's 19.2 MB/s, comfortable for USB 2.0's ~40 MB/s. One lesson cost us a crash: when a read fails, the other three still in flight fail too ("Cancelled"), and in Node an unawaited rejected promise ends the process. So the loop always settles the queue on its way out:
try { while (streaming) { const r = await queue.shift(); queue.push(dev.transferIn(1, 262144)); onData(r.data); } }
finally { streaming = false; await Promise.allSettled(queue); } // in-flight reads settle, nobody crashes
Sharing 8 MHz
The server opens whichever radio is plugged in. Unplug the HackRF, plug the Nooelec back in, and it's a 2.4 MHz window again; nothing else changes. With the HackRF, each listener's 240 kHz slice is mixed down and decimated ÷5 ÷4 ÷2 from the 9.6 MS/s stream: one listener costs about half a CPU core on a Mac mini, in plain TypeScript. Spectrum Lab's strip above the waterfall shows the whole window live, with the US band plan over it if you tick Band plan.
Read the real code
The driver, exactly as it runs on this site (loaded from the source when the page is built). Click a highlighted part for what it does.
radio/src/hackrf.ts: the driver (requests, gains, streaming, the signed-to-unsigned XOR).radio/scripts/node-usb.ts: picks the HackRF or the RTL-SDR, whichever is connected.radio/server.ts: the window per radio, the amp blocklist, the clipping watchdog, gain per band.radio/scripts/hw-test.ts: plug in, run it, and it decodes an FM station's stereo pilot to prove the chain works (it did: 48 dB on 98.7 MHz).
Troubleshooting
| Symptom | Fix |
|---|---|
| Linux: access denied | A udev rule: SUBSYSTEM=="usb", ATTR{idVendor}=="1d50", ATTR{idProduct}=="6089", MODE="0660", GROUP="plugdev" (the hackrf package ships one), then replug. |
| Everything is one smear, ADC near 0 dBFS | Clipping: lower VGA, then LNA. Is the RF amp on near a strong transmitter? |
| Weak, noisy signal | Raise the LNA before the VGA; the RF amp only away from broadcast bands; a better antenna beats all of it. |
| Rate refused | 2–20 MS/s only; for slices on our server, a rate that divides into 240 kHz and 1.2 MHz (the calculator above). |
| Busy / can't claim | Another program (GQRX, SDR++, hackrf_transfer) has it open; one owner at a time. |
Back to Build for your own dongle (RTL-SDR) · Spectrum Lab
Last updated