Adding Remote Control

Using the Brickstuff 4-channel long-distance RF remote transmitter and receiver pack, you can easily add wireless remote control to your Arduino projects with the Brickstuff Arduino Shield.

As shown below, there are two plugs on the Brickstuff Arduino Shield for connecting the RF receiver.  One is a smaller plug with four wires and the other is a standard large Brickstuff power connector:

To connect the transmitter to the shield, carefully insert both plugs, and press with your fingernail until you feel the plugs "click" into place.

The remote receiver takes its power from the same power source as the Arduino shield, so if you are powering your Arduino from USB or its circular power input connector, the remote receiver will be powered.  If you are powering your Arduino using a Brickstuff power supply via the "Brickstuff Power In" plug, your receiver will get its power that way.

When connected to power, the small LED light on the receiver should turn on.  If you press any button on your remote transmitter, you should see the small LED light on the receiver flash in response to your button presses.

If for some reason the small LED light on the receiver does not flash when you press the buttons on the remote, complete the following steps:

  1. Make sure the small LED light on the transmitter turns on when you press a button.  If not, replace the battery inside the transmitter.
  2. Press the small white button on the receiver for one second, then release.
  3. Press any button on the remote transmitter, and the small LED light on the receiver should flash.  This indicates that the receiver is now "bound" to the remote transmitter and will only listen to commands from that unit.  Note that we also sell remote transmitters separately, so you can bind multiple transmitters (up to 10) to a single receiver.

Using the Remote to Control your Code

The important thing to remember about how the remote receiver works is that it has four inputs (one corresponding to each button on the remote transmitter).  The remote receiver's input plug is connected to the following pins on your Arduino Uno:

  • The "A" button on the remote is connected to pin 2 on your Arduino Uno
  • The "B" button on the remote is connected to pin 4 on your Arduino Uno
  • The "C" button on the remote is connected to pin A2 on your Arduino Uno
  • The "D" button on the remote is connected to pin A3 on your Arduino Uno

Each input is normally pulled LOW (to zero volts) by resistors onboard the Brickstuff Arduino Shield.

When a button is pressed on the remote transmitter, the input corresponding to that button becomes HIGH for as long as the button is pressed and held.  You should write your code so that it looks for HIGH readings on any of the button inputs.

Here is some sample code you can cut and paste into your Arduino sketches to read and process input from the remote receiver:

////////////////////////////
// Brickstuff Arduino Shield
//
// Sample code for adding RF remote control functionality
// (c)2020, Enthusiast Enterprises, LLC
//
// THIS CODE IS PROVIDED AS-IS WITH NO WARRANTY OR GUARANTEE OF ANY KIND
//
// HISTORY
// 11.27.2020 Version 1.0
////////////////////////////

// USAGE
//
// This code uses the notion of a "short press" and "long press" of the buttons on the RF remote transmitter to control 8 functions with 4 buttons.
// A "short press" is defined as a button that is pressed for less than 4x the rfPoll delay (defined below in the variables section)-- the default is 400ms or less for a short press
// A "long press" is defined a a button that is pressed for longer than the duration of a short press
//
// You can change the default durations by changing the rfPoll number below-- change at your own risk, however, as making this too long or too short can confuse your users
//
// Make sure to always include the checkRF() subroutine at the end of your code-- this is the section that reads the button inputs.
// This code is written in a non-blocking fashion, meaning you can add other code to run and it will not be interrupted by button presses on the remote.
// A value is only returned from the subroutine when a button has been released after first being pressed.

// RF-related variables
uint8_t rfPins[4] = {2,4,A2,A3};
#define rfPoll 100 // ms delay between reading of the RF ports (for debouncing)
uint8_t rfActive[4] = {0,0,0,0};  // Used in the debounce routine
uint8_t rfCommand[4] = {0,0,0,0}; // Used to deliver debounced (clean) RF commands received from the remote
uint8_t rfStat;
unsigned long rfTick[4];

void setup()
{
  for(uint8_t i=0; i<4; i++)  // Set the RF pins as inputs
  {
    pinMode(rfPins[i], INPUT);
  }
}

void loop()
{
  // Read the RF remote inputs
  rfStat = checkRF(0);  // Button "A"
  if(rfStat == 1) // Short press detected on Button A
  {
    // Put your code here to control what happens with a short press on Button A
  }
  if(rfStat == 2) // Long press detected on Button A
  {
    // Put your code here to control what happens with a long press on Button A
  }
  rfStat = checkRF(1);  // Button "B"
  if(rfStat == 1) // Short press detected on Button B
  {
    // Put your code here to control what happens with a short press on Button B
  }
  if(rfStat == 2) // Long press detected on Button B
  {
    // Put your code here to control what happens with a long press on Button B
  }
  rfStat = checkRF(2);  // Button "C"
  if(rfStat == 1) // Short press detected on Button C
  {
    // Put your code here to control what happens with a short press on Button C
  }
  if(rfStat == 2) // Long press detected on Button C
  {
    // Put your code here to control what happens with a long press on Button C
  }
  rfStat = checkRF(3);  // Button "D"
  if(rfStat == 1) // Short press detected on Button D
  {
    // Put your code here to control what happens with a short press on Button D
  }
  if(rfStat == 2) // Long press detected on Button D
  {
    // Put your code here to control what happens with a long press on Button D
  }

  // Put the rest of your code here-- the RF reading routine is non-blocking, so the rest of your code will run even when a button is pressed.
}

// Make sure to include this subroutine at the end of any sketch you write that calls it.
uint8_t checkRF(uint8_t rfPort) // Returns the status of the given RF port (0-3)-- has built-in debounce and only returns a non-zero number after the button is released.  1 = short press, 2 = long press.
{
  if(rfActive[rfPort] == 0 && digitalRead(rfPins[rfPort]) == HIGH) // Start the "ON" timer
  {
    rfActive[rfPort] = 1;
    rfCommand[rfPort] = 0;
    rfTick[rfPort] = millis();
  }

  if(rfActive[rfPort] > 0 && digitalRead(rfPins[rfPort]) == LOW)
  {
    rfActive[rfPort] = 0;
    if((millis() - rfTick[rfPort]) > rfPoll && (millis() - rfTick[rfPort]) < (rfPoll * 4))
    {
      return 1;
    }
    if(millis() - rfTick[rfPort] >= (rfPoll * 4))
    {
      return 2;
    }
  }
  return 0;
}

This code is also attached as an Arduino .ino file.

If your code is not working as intended, make sure to check the small LED light on the receiver and verify that it blinks when a button is pressed on the remote transmitter.  If the light does not flash when you press a button on the transmitter, follow the numbered steps at the top of this article to bind your remote transmitter to its receiver.

4replies Oldest first
  • Oldest first
  • Newest first
  • Active threads
  • Popular
  • Hi....the distant transmitter and get give fantastic reach and security, since every far off transmitter is bound to its beneficiary utilizing a code. This permits numerous controllers to be utilized inside a similar space without causing impedance. You can likewise buy extra far off transmitters and tie them to a current collector on the off chance that you need to permit more than one distant to control a solitary recipient.

    Like
  • LoriePetty Absolutely true!

    Like
    • WRTyler
    • -the National Mall guy-
    • wrTyler
    • 3 yrs ago
    • Reported - view

    I have modified the code to include a double click.  So now each button has three options, single click, double click and click & hold (long click).  Guess I'm so used to turning something on with a single click and off with a double click that using the click & hold just felt weird.

    -wrtyler-

    ////////////////////////////
    // Brickstuff Arduino Shield
    //
    // Sample code for adding RF remote control functionality
    // (c)2020, Enthusiast Enterprises, LLC
    //
    // THIS CODE IS PROVIDED AS-IS WITH NO WARRANTY OR GUARANTEE OF ANY KIND
    //
    // HISTORY
    // 11.27.2020 Version 1.0
    // 05.23.2021 modified by wrtyler to include double click
    ////////////////////////////
    
    // USAGE
    //
    // This code uses the notion of a "short press" and "long press" of the buttons on the RF remote transmitter to control 8 functions with 4 buttons.
    // A "short press" is defined as a button that is pressed for less than 4x the rfPoll delay (defined below in the variables section)-- the default is 400ms or less for a short press
    // A "double short press" is defined as a button that is pressed twice before 4x the rfPoll delay (defined below in the variables section)-- the default is 400ms or less for a short press
    // A "long press" is defined a a button that is pressed for longer than the duration of a short press
    //
    // You can change the default durations by changing the rfPoll number below-- change at your own risk, however, as making this too long or too short can confuse your users
    //
    // Make sure to always include the checkRF() subroutine at the end of your code-- this is the section that reads the button inputs.
    // This code is written in a non-blocking fashion, meaning you can add other code to run and it will not be interrupted by button presses on the remote.
    // A value is only returned from the subroutine when a button has been released after first being pressed.
    
    // RF-related variables
    uint8_t rfPins[4] = {2,4,A2,A3};
    #define rfPoll 100 // ms delay between reading of the RF ports (for debouncing)
    uint8_t rfActive[4] = {0,0,0,0};  // Used in the debounce routine
    uint8_t rfCommand[4] = {0,0,0,0}; // Used to deliver debounced (clean) RF commands received from the remote
    uint8_t rfStat;
    unsigned long rfTick[4];
    
    void setup()
    {
      for(uint8_t i=0; i<4; i++)  // Set the RF pins as inputs
      {
        pinMode(rfPins[i], INPUT);
      }
    }
    
    void loop()
    {
      // Read the RF remote inputs
      rfStat = checkRF(0);  // Button "A"
      if(rfStat == 1) // Short press detected on Button A
      {
        // Put your code here to control what happens with a short press on Button A
      }
      if(rfStat == 2) // Double short press detected on Button A
      {
        // Put your code here to control what happens with a double short press on Button A
      }
      if(rfStat == 3) // Long press detected on Button A
      {
        // Put your code here to control what happens with a long press on Button A
      }
      rfStat = checkRF(1);  // Button "B"
      if(rfStat == 1) // Short press detected on Button B
      {
        // Put your code here to control what happens with a short press on Button B
      }
      if(rfStat == 2) // Double short press detected on Button B
      {
        // Put your code here to control what happens with a double short press on Button B
      }
      if(rfStat == 3) // Long press detected on Button B
      {
        // Put your code here to control what happens with a long press on Button B
      }
      rfStat = checkRF(2);  // Button "C"
      if(rfStat == 1) // Short press detected on Button C
      {
        // Put your code here to control what happens with a short press on Button C
      }
      if(rfStat == 2) // Double short press detected on Button C
      {
        // Put your code here to control what happens with a double short press on Button C
      }
      if(rfStat == 3) // Long press detected on Button C
      {
        // Put your code here to control what happens with a long press on Button C
      }
      rfStat = checkRF(3);  // Button "D"
      if(rfStat == 1) // Short press detected on Button D
      {
        // Put your code here to control what happens with a short press on Button D
      }
      if(rfStat == 2) // Double short press detected on Button D
      {
        // Put your code here to control what happens with a double short press on Button D
      }
      if(rfStat == 3) // Long press detected on Button D
      {
        // Put your code here to control what happens with a long press on Button D
      }
    
      // Put the rest of your code here-- the RF reading routine is non-blocking, so the rest of your code will run even when a button is pressed.
    }
    
    // Make sure to include this subroutine at the end of any sketch you write that calls it.
    // Returns the status of the given RF port (0-3)-- has built-in debounce and only returns
    // a non-zero number after the button is released.  1 = short press, 2 = double short press, 3 = long press.
    
    uint8_t checkRF(uint8_t rfPort) {
      // Butten is pressed, start the "ON" timer
      if(rfActive[rfPort] == 0 && digitalRead(rfPins[rfPort]) == HIGH)
      {
        rfActive[rfPort] = 1;
        rfCommand[rfPort] = 0;
        rfTick[rfPort] = millis();
      }
    
      // Button is released, determine what happened
      if(rfActive[rfPort] > 0 && digitalRead(rfPins[rfPort]) == LOW)
      {
        rfActive[rfPort] = 0;
        // Double click
        if((millis() - rfTick[rfPort]) > (rfPoll * 2) && (millis() - rfTick[rfPort]) < (rfPoll * 4))
        {
          return 2;
        }
        // Single click
        if((millis() - rfTick[rfPort]) > rfPoll && (millis() - rfTick[rfPort]) < (rfPoll * 4))
        {
          return 1;
        }
        // Click and hold (long click)
        if(millis() - rfTick[rfPort] >= (rfPoll * 4))
        {
          return 3;
        }
      }
      return 0;
    }
    Like 1
  • WRTyler Nice, thank you for sharing your improvements!

    Like
Like1 Follow
  • 1 Likes
  • 3 yrs agoLast active
  • 4Replies
  • 168Views
  • 3 Following