Remote Control Power Sockets: Part 4: Arduino Proof of Concept

In Parts 1 to 3 we examined the remote control: in Part 1 on paper, in Part 2 with the oscilloscope and in Part 3 with an SDR stick. The result is the codeword that the remote control sends for each socket and each button. Before the Linux boards come into play, we take the shortest path from the codeword to a switching socket in this part: an Arduino Uno and an FS1000A transmitter switch a REV 008342 socket (group 3, socket 0) on and off every five seconds. The Arduino sketch is a proof of concept and short enough to read in one go.

Wiring

  • DATA to D10

  • VCC to 5V

  • GND to GND

A wire of about 17 cm on the antenna pad ANT, a quarter of the wavelength at 433 MHz, improves the range considerably. A spiral antenna like the one in the photo works too. We describe the module itself in the Wiring section of Part 9.

Wiring sketch: pins D10, 5V and GND of the Arduino Uno go to DATA, VCC and GND of the FS1000A transmitter, plus an antenna wire of 17 cmThe test setup: an Arduino Uno connected to the FS1000A transmitter, which has a spiral antenna soldered to it

Toolchain

We use arduino-cli, the command-line version of the Arduino tools, and build with it in our preferred environment: the terminal. We download a fixed version and check it against the published checksum before unpacking it. The last command installs the core for AVR boards such as the Uno:

V=1.5.1
wget https://downloads.arduino.cc/arduino-cli/${V}-checksums.txt https://downloads.arduino.cc/arduino-cli/arduino-cli_${V}_Linux_64bit.tar.gz
sha256sum -c --ignore-missing ${V}-checksums.txt
tar -xzf arduino-cli_${V}_Linux_64bit.tar.gz arduino-cli
sudo install arduino-cli /usr/local/bin/
arduino-cli core install arduino:avr

The sketch

The Arduino sketch goes into the file rev_toggle/rev_toggle.ino, because the folder and the file name must match:

/*
 * Switches one REV 008342 socket (group 3, socket 0) on and off every
 * 5 s. Same codewords and timing as librsswitch (PT2260 encoder,
 * 350 us base pulse, 10 repetitions each followed by a sync bit).
 *
 * Wiring (Arduino Uno, FS1000A): DATA to D10, VCC to 5V, GND to GND.
 */

const uint8_t TX_PIN = 10;
const unsigned long SERIAL_BAUD = 9600;
const unsigned int PULSE_US = 350;
const uint8_t REPEAT = 10;
const unsigned long PAUSE_MS = 5000;

/* pulse widths and sync length in units of PULSE_US */
const uint8_t SHORT = 1;
const uint8_t LONG = 3;
const uint8_t SYNC_LOW = 31;

/* delayMicroseconds() is exact only up to 16383 us */
const unsigned int DELAY_US_MAX = 16383;
static_assert(SHORT <= DELAY_US_MAX / PULSE_US &&
              LONG <= DELAY_US_MAX / PULSE_US &&
              SYNC_LOW <= DELAY_US_MAX / PULSE_US,
              "pulse too long for delayMicroseconds()");

/* group "FFF1" + socket "1FF0" + data ("0010" on, "0001" off) */
const char CODE_ON[] = "FFF11FF00010";
const char CODE_OFF[] = "FFF11FF00001";

static void pulse(uint8_t high, uint8_t low)
{
        digitalWrite(TX_PIN, HIGH);
        delayMicroseconds(PULSE_US * high);
        digitalWrite(TX_PIN, LOW);
        delayMicroseconds(PULSE_US * low);
}

static void send_codeword(const char *code)
{
        for (uint8_t n = 0; n < REPEAT; n++) {
                for (const char *c = code; *c; c++) {
                        switch (*c) {
                        case '0':
                                pulse(SHORT, LONG);
                                pulse(SHORT, LONG);
                                break;
                        case '1':
                                pulse(LONG, SHORT);
                                pulse(LONG, SHORT);
                                break;
                        case 'F':
                                pulse(SHORT, LONG);
                                pulse(LONG, SHORT);
                                break;
                        }
                }
                pulse(SHORT, SYNC_LOW); /* sync */
        }
}

void setup()
{
        pinMode(TX_PIN, OUTPUT);
        digitalWrite(TX_PIN, LOW);
        Serial.begin(SERIAL_BAUD);
}

void loop()
{
        Serial.println("on");
        send_codeword(CODE_ON);
        delay(PAUSE_MS);
        Serial.println("off");
        send_codeword(CODE_OFF);
        delay(PAUSE_MS);
}

The codeword

The REV 008342 listens for a codeword of twelve tri-state characters, which the sketch splits 4+4+4 into group, socket and data. Group 3 is FFF1, socket 0 is 1FF0 and the data part is 0010 for on and 0001 for off. Together that gives FFF11FF00010 and FFF11FF00001, the same words that librsswitch assembles, see Part 9. The Pollin set from Parts 1 to 3 splits its word 5+5+2.

The sketch sends each character as two pulse pairs with a base pulse of 350 µs, which is four of the 87.5 µs periods from Part 2:

  • 0: short high, long low, twice.

  • 1: long high, short low, twice.

  • F: short high, long low, then long high, short low.

After the twelve characters comes the sync pulse, a short high followed by 31 short lows. In the sketch the constants SHORT (one base pulse), LONG (three base pulses) and SYNC_LOW (31 base pulses) stand for these times. A static_assert stops the build if one of these times is longer than the 16383 µs that delayMicroseconds() can wait exactly. The sketch sends the whole word ten times, which takes about half a second.

For this setup to switch the socket, we set it to group 3, socket 0.

For this, the REV socket has a rotary dial on its housing, with the letters A to D for the group and the numbers 1 to 3 for the socket. A to D stand for groups 0 to 3, 1 to 3 for sockets 0 to 2. Group 3, socket 0 is therefore position D 1.

The rotary dial on the housing of the REV socket with the letters A to D and the numbers 1 to 3, below it the type plate REV 008342

Compiling and uploading

We run both commands in the folder that contains rev_toggle/:

arduino-cli compile --fqbn arduino:avr:uno rev_toggle
arduino-cli upload -p /dev/ttyACM0 --fqbn arduino:avr:uno rev_toggle

We tested this with an Arduino Uno, arduino-cli 1.5.1 and the arduino:avr core 1.8.8 on /dev/ttyACM0: the socket switched on and off every five seconds.

What comes next

In Part 5 we switch the same socket from an STM32F0DISCOVERY, with libopencm3 instead of the Arduino libraries. In Part 6 we do the same with Zephyr on an STM32F429I-DISCO. Then come the Linux boards: in Part 7 we set up a Raspberry Pi, in Part 8 a BeagleBone Black, and in Part 9 we switch the sockets with librsswitch.