Radio messenger: 2 nodes with RadioOS (Zephyr)
Two nRF52840 nodes talk by radio with RadioOS.
- nRF52840
- ST7735 TFT
- Loop antenna
Open in the simulatorNo account needed: press Simulate and it runs.

How it works
Two devices with nRF52840, ST7735 TFT and a 50 kHz radio link (loop antennas, OOK 2 kbit/s), 20 cm apart. Firmware RadioOS (Zephyr 4.4, source firmware/radioos): A greets, B acknowledges and replies. The ADC measures signal strength at the envelope detector: mV and dB above the switching threshold, plus a bar on the TFT. Key SW1A/SW1B = send PING. "Scene" view: pull the nodes apart: 20 cm ≈ 650 mV (+16 dB), 27 cm ≈ 210 mV (+6 dB), at 30 cm the link drops (field ∝ 1/r³, plus the diode curve). Shell: send <text>, rssi, ps, info.
Wiring
Each line is one connection (net) and the pins it joins.
| Net | Pins |
|---|---|
| GND | BT1A −BT1B −C1A 2C1B 2C2A 2C2B 2C3A 2C3B 2C5A 2C5B 2C6A 2C6B 2D1A KD1B KD2A KD2B KDS1A GNDDS1B GNDL1A 2L1B 2L2A 2L2B 2Q2A SQ2B SR4A 2R4B 2R5A 2R5B 2R7A 2R7B 2R11A 2R11B 2SW1A 2SW1B 2U1A GNDU1B GNDU1B P0.31U2A GNDU2B GNDU3A V−U3B V− |
| A_3V3 | C2A 1C3A 1D4A KDS1A LEDDS1A VCCR3A 1R6A 1R8A 1U1A VDDU1A P0.31U2A OUTU3A V+ |
| A_BTN | SW1A 1U1A P0.11 |
| A_CARRIER | R10A 1U1A P0.26 |
| A_DET | C6A 1D3A KR5A 1U1A P0.02U3A + |
| A_DRV | C4A 1D4A AQ1A DR3A 2 |
| A_G_CAR | Q2A GR10A 2 |
| A_G_TX | Q1A GR9A 2R11A 1 |
| A_LED_RX | R2A 1U1A P0.14 |
| A_LED_RX_K | D2A AR2A 2 |
| A_LED_TX | R1A 1U1A P0.13 |
| A_LED_TX_K | D1A AR1A 2 |
| A_MOSI | DS1A SDA/MOSIU1A P0.20 |
| A_Q_MID | Q1A SQ2A D |
| A_REF | R6A 2R7A 1U3A − |
| A_RX_ANT | C5A 1D3A AL2A 1R4A 1 |
| A_RX_DATA | R8A 2U1A P0.28U3A OUT |
| A_SCK | DS1A SCKU1A P0.19 |
| A_TFT_CS | DS1A CSU1A P0.21 |
| A_TFT_DC | DS1A DCU1A P0.22 |
| A_TFT_RES | DS1A RESU1A P0.23 |
| A_TX_ANT | C4A 2L1A 1 |
| A_TX_EN | R9A 1U1A P0.27 |
| A_VBAT | BT1A +C1A 1U2A ENU2A IN |
| B_3V3 | C2B 1C3B 1D4B KDS1B LEDDS1B VCCR3B 1R6B 1R8B 1U1B VDDU2B OUTU3B V+ |
| B_BTN | SW1B 1U1B P0.11 |
| B_CARRIER | R10B 1U1B P0.26 |
| B_DET | C6B 1D3B KR5B 1U1B P0.02U3B + |
| B_DRV | C4B 1D4B AQ1B DR3B 2 |
| B_G_CAR | Q2B GR10B 2 |
| B_G_TX | Q1B GR9B 2R11B 1 |
| B_LED_RX | R2B 1U1B P0.14 |
| B_LED_RX_K | D2B AR2B 2 |
| B_LED_TX | R1B 1U1B P0.13 |
| B_LED_TX_K | D1B AR1B 2 |
| B_MOSI | DS1B SDA/MOSIU1B P0.20 |
| B_Q_MID | Q1B SQ2B D |
| B_REF | R6B 2R7B 1U3B − |
| B_RX_ANT | C5B 1D3B AL2B 1R4B 1 |
| B_RX_DATA | R8B 2U1B P0.28U3B OUT |
7 more connections in the editor.
- C1A
- Capacitor
- C1B
- Capacitor
- C2A
- Capacitor
- C2B
- Capacitor
- C3A
- Capacitor
- C3B
- Capacitor
- C4A
- Capacitor
- C4B
- Capacitor
- C5A
- Capacitor
- C5B
- Capacitor
- C6A
- Capacitor
- C6B
- Capacitor
- D1A
- LED
- D1B
- LED
- D2A
- LED
- D2B
- LED
- D3A
- Diode
- D3B
- Diode
- D4A
- Diode
- D4B
- Diode
- Q1A
- N-MOSFET
- Q1B
- N-MOSFET
- Q2A
- N-MOSFET
- Q2B
- N-MOSFET
- R1A
- Resistor
- R1B
- Resistor
- R2A
- Resistor
- R2B
- Resistor
- R3A
- Resistor
- R3B
- Resistor
- R4A
- Resistor
- R4B
- Resistor
- R5A
- Resistor
- R5B
- Resistor
- R6A
- Resistor
- R6B
- Resistor
- R7A
- Resistor
- R7B
- Resistor
- R8A
- Resistor
- R8B
- Resistor
- R9A
- Resistor
- R9B
- Resistor
- R10A
- Resistor
- R10B
- Resistor
- R11A
- Resistor
- R11B
- Resistor
- SW1A
- Push button
- SW1B
- Push button
Parts
Firmware
src/main.c · 540 lines · the first 120 here, all of it in the editor
/*
* RadioOS - Zephyr-based firmware for a two-node text radio link (Circuit Lab).
*
* Hardware per node (nRF52840):
* ST7735S 128x160 TFT on SPIM2: SCK P0.19, MOSI P0.20, CS P0.21, D/C P0.22, RES P0.23
* Carrier 50 kHz square on PWM0 -> P0.26 (enabled while a frame is sent)
* TX_EN P0.27: keys the carrier into the transmit loop (two NMOS in series = AND)
* RX_IN P0.28: comparator output, high = carrier detected
* RSSI P0.02 (AIN0): envelope detector voltage (the comparator's + input)
* Button P0.11 (to GND, internal pull-up), LED TX P0.13, LED RX P0.14 (active high)
* Node ID strap P0.31: open = node A, tied to GND = node B
* Console/shell: UART0 TX P0.06, RX P0.08
*
* Radio link: on-off keying of the 50 kHz carrier, 2000 bit/s, 8N1 framing
* (bit 0 / start bit = carrier on, bit 1 / idle = carrier off).
* Frame: STX(0x02) src len payload[len] xor-checksum
*
* Signal strength: while a frame comes in, the detector voltage is sampled every
* millisecond; its peak is the carrier level. Shown as millivolts and as the margin
* above the comparator threshold (R6/R7 divider: VDD * 3.3k / 103.3k = 105 mV),
* i.e. how many dB the signal may still drop before the link fails.
*/
#include <zephyr/kernel.h>
#include <zephyr/version.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/drivers/spi.h>
#include <zephyr/drivers/pwm.h>
#include <zephyr/drivers/uart.h>
#include <zephyr/drivers/adc.h>
#include <math.h>
#include <zephyr/sys/printk.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
extern const uint8_t font5x7[95][5];
#define PIN_BTN 11
#define PIN_LED_TX 13
#define PIN_LED_RX 14
#define PIN_DC 22
#define PIN_RES 23
#define PIN_TXEN 27
#define PIN_RXIN 28
#define PIN_ID 31
#define BIT_US 500 /* 2000 bit/s */
#define CARRIER_NS 20000 /* 50 kHz */
#define MAXMSG 40
static const struct device *const gpio0 = DEVICE_DT_GET(DT_NODELABEL(gpio0));
static const struct device *const spi = DEVICE_DT_GET(DT_NODELABEL(spi2));
static const struct device *const pwm = DEVICE_DT_GET(DT_NODELABEL(pwm0));
static const struct device *const con = DEVICE_DT_GET(DT_CHOSEN(zephyr_console));
static const struct spi_config cfg = {
.frequency = 8000000,
.operation = SPI_OP_MODE_MASTER | SPI_TRANSFER_MSB | SPI_WORD_SET(8),
.cs = { .gpio = { .port = DEVICE_DT_GET(DT_NODELABEL(gpio0)), .pin = 21, .dt_flags = GPIO_ACTIVE_LOW }, .delay = 0, .cs_is_gpio = true },
};
static const struct adc_dt_spec rssi_adc = ADC_DT_SPEC_GET_BY_IDX(DT_PATH(zephyr_user), 0);
#define THRESH_MV 105 /* comparator reference: 3300 mV * 3.3k / 103.3k */
static char node = 'A';
static uint32_t n_tx, n_rx, n_err;
/* ------------------------------------------------------------------ display */
#define W 128
#define H 160
#define COLS 21
#define ROWS 20
#define RGB(r, g, b) ((((r) & 0xF8) << 8) | (((g) & 0xFC) << 3) | ((b) >> 3))
#define C_BLACK 0x0000
#define C_WHITE 0xFFFF
#define C_GREY RGB(150, 150, 150)
#define C_BLUE RGB(20, 60, 160)
#define C_DKBLUE RGB(10, 20, 50)
#define C_YELLOW RGB(255, 220, 0)
#define C_GREEN RGB(60, 230, 90)
#define C_CYAN RGB(80, 220, 255)
#define C_RED RGB(255, 70, 70)
#define C_GREY_DK RGB(60, 60, 60)
/* bars: -1 = plain text row, 0..5 = signal bars drawn at the right edge */
struct row { char t[COLS + 1]; uint16_t fg, bg; int8_t bars; bool dirty; };
static struct row rows[ROWS];
static K_MUTEX_DEFINE(ui_lock);
static K_SEM_DEFINE(ui_wake, 0, 1);
#define LOG_FIRST 5
#define LOG_LAST 17
static void spi_out(const uint8_t *b, size_t n)
{
const struct spi_buf sb = {.buf = (void *)b, .len = n};
const struct spi_buf_set ss = {&sb, 1};
spi_write(spi, &cfg, &ss);
}
static void cmd(uint8_t c, const uint8_t *p, size_t n)
{
gpio_pin_set_raw(gpio0, PIN_DC, 0);
spi_out(&c, 1);
if (n) { gpio_pin_set_raw(gpio0, PIN_DC, 1); spi_out(p, n); }
}
static void window(int x, int y, int w, int h)
{
uint8_t ca[4] = {0, x, 0, x + w - 1}, ra[4] = {0, y, 0, y + h - 1};
cmd(0x2A, ca, 4);
cmd(0x2B, ra, 4);
cmd(0x2C, NULL, 0);
gpio_pin_set_raw(gpio0, PIN_DC, 1);
}
static uint8_t line_px[W * 8 * 2];
static void draw_row(int r, const struct row *rw)
{
size_t len = strlen(rw->t);
for (int y = 0; y < 8; y++) {
for (int x = 0; x < W; x++) {
int cx = (x - 1) / 6, col = (x - 1) % 6;Open in the simulator