OVERVIEW
There are many ways to add wireless capability to your Arduino projects.
WiFi modules like the ESP8266 makes that possible, but you need to be somewhat close to a WiFi signal, and the code will need to be reconfigured if you want to connect to a different WiFi router, since the password would probably be different.
Bluetooth modules will kinda achieve the same thing, but using Bluetooth communication instead.
But sometimes you might just want to be able to send or receive without having to use WiFi or Bluetooth, well then the NRF24L01 would be a good choice.
Unlike the others it uses radio wave to communicate between modules, the same type used in cordless phones in your house.
In this tutorial we will keep it simple and use a regular cherry switch, that when clicked will send a value that will then be picked up by another module and will light up a WS2812 LED stick different colours based on that value received.
PARTS USED
Arduino UNO
Arduino NANO
Arduino NRF24L01
These are Amazon affiliate links...
They don't cost you anything and it helps me keep the lights on
if you buy something on Amazon. Thank you!
TRANSMITTER CONNECTIONS
The transmitter portion of this tutorial, which is the one the Cherry Switch is connected to is based on the Arduino NANO board.
As you can see we are connecting a NRF24L01 module and a Cherry Switch.
Here are the connections:
The Switch is connected to the GND and Pin 8 on the NANO
The NRF24L01 pins:
MISO connects to pin 12 of the NANO
MOSI connects to pin 11 of the NANO
SCK connects to pin 13 of the NANO
CE connects to pin 9 of the NANO
CSN connects to pin 10 of the NANO
GND and VCC of the NRF24L01 are connected to GND and 3.3V of the NANO
The NRF24L01 uses the SPI communication protocol, so you need to make sure that you are using the SPI pins of the version of the Arduino board you will want to use.
The NANO and UNO boards have the same ones:
Pin 11 = MOSI (Master Out Slave In) Pin 12 = MISO (Master In Slave Out) Pin 13 = SCK (Serial Clock)
But if you plan to use let’s say a MEGA 2560 then those pins will be different.
Pin 51 = MOSI (Master Out Slave In)
Pin 50 = MISO (Master In Slave Out)
Pin 52 = SCK (Serial Clock)
RECEIVER CONNECTIONS
The Receiver portion of this tutorial, which is the one that has the WS2812 LED stick and will receive the information sent by the Transmitter uses the Arduino UNO board.
Here are the connections:
The WS2812 RGB Stick DI (digital In) pin is connected to Pin 8 on the UNO
GND and VCC are connected to the GND and 5V of the UNO
The NRF24L01 pins:
MISO connects to pin 12 of the UNO
MOSI connects to pin 11 of the UNO
SCK connects to pin 13 of the UNO
CE connects to pin 9 of the UNO
CSN connects to pin 10 of the UNO
GND and VCC of the NRF24L01 are connected to GND and 3.3V of the UNO
THE CODE - TRANSMITTER
For this tutorial we will have 2 separate codes: 1 for the Transmitter and one for the Receiver.
The Transmitter or Client will be the one with the Switch connected that will send information to the Server which has the RGB stick connected.
In this tutorial we only have 1 switch connected but we could connect more switches or sensor if we wanted and just change the information sent based on those, which the Server will do an action based on that.
As always, Don’t forget to watch our Tutorial videos on the left side of this page for more information.
/*
Created by Yvan / https://Brainy-Bits.com
This code is in the public domain...
You can: copy it, use it, modify it, share it or just plain ignore it!
Thx!
*/
// NRF24L01 Module Tutorial - Code for Transmitter using Arduino NANO
//Include needed Libraries at beginning
#include "nRF24L01.h" //NRF24L01 library created by TMRh20 https://github.com/TMRh20/RF24
#include "RF24.h"
#include "SPI.h"
#define SwitchPin 8 // Arcade switch is connected to Pin 8 on NANO
int SentMessage[1] = {000}; // Used to store value before being sent through the NRF24L01
RF24 radio(9,10); // NRF24L01 used SPI pins + Pin 9 and 10 on the NANO
const uint64_t pipe = 0xE6E6E6E6E6E6; // Needs to be the same for communicating between 2 NRF24L01
void setup(void){
pinMode(SwitchPin, INPUT_PULLUP); // Define the arcade switch NANO pin as an Input using Internal Pullups
digitalWrite(SwitchPin,HIGH); // Set Pin to HIGH at beginning
radio.begin(); // Start the NRF24L01
radio.openWritingPipe(pipe); // Get NRF24L01 ready to transmit
}
void loop(void){
if (digitalRead(SwitchPin) == LOW){ // If Switch is Activated
SentMessage[0] = 111;
radio.write(SentMessage, 1); // Send value through NRF24L01
}
else {
SentMessage[0] = 000;
radio.write(SentMessage, 1);
}
}
THE CODE - RECEIVER
/*
Created by Yvan / https://Brainy-Bits.com
This code is in the public domain...
You can: copy it, use it, modify it, share it or just plain ignore it!
Thx!
*/
// NRF24L01 Module Tutorial - Code for Receiver using Arduino UNO
//Include needed Libraries at beginning
#include "nRF24L01.h" // NRF24L01 library created by TMRh20 https://github.com/TMRh20/RF24
#include "RF24.h"
#include "SPI.h"
#include "FastLED.h" // FastLED library for WS2812 RGB Stick http://fastled.io/
#define NUM_LEDS 8 // Number of leds on stick
#define LED_PIN 8 // Digital In (DI) of RGB Stick connected to pin 8 of the UNO
CRGB leds[NUM_LEDS]; // FastLED Library Init
int ReceivedMessage[1] = {000}; // Used to store value received by the NRF24L01
RF24 radio(9,10); // NRF24L01 used SPI pins + Pin 9 and 10 on the UNO
const uint64_t pipe = 0xE6E6E6E6E6E6; // Needs to be the same for communicating between 2 NRF24L01
void setup(void){
FastLED.addLeds<NEOPIXEL,LED_PIN>(leds, NUM_LEDS); // Setup FastLED Library
FastLED.clear(); // Clear the RGB Stick LEDs
// Light up starting LED's
for(int x = 0; x != NUM_LEDS; x++) {
leds[x] = CRGB::Red;
}
FastLED.setBrightness(50);
FastLED.show();
radio.begin(); // Start the NRF24L01
radio.openReadingPipe(1,pipe); // Get NRF24L01 ready to receive
radio.startListening(); // Listen to see if information received
pinMode(LED_PIN, OUTPUT); // Set RGB Stick UNO pin to an OUTPUT
}
void loop(void){
while (radio.available())
{
radio.read(ReceivedMessage, 1); // Read information from the NRF24L01
if (ReceivedMessage[0] == 111) // Indicates switch is pressed
{
for(int x = 0; x != NUM_LEDS; x++)
{
leds[x] = CRGB::Green;
FastLED.show();
}
}
else
{
for(int x = 0; x != NUM_LEDS; x++)
{
leds[x] = CRGB::Red;
FastLED.show();
}
}
delay(10);
}
}
TUTORIAL VIDEO
DOWNLOAD
Copy the above Sketch code in your Arduino IDE software to program your Arduino.
Used Libraries:
Download The NRF24L01 Library created by TMRh20 here: https://github.com/TMRh20/RF24
Download The FastLED Library created by focalintent here: https://github.com/FastLED/FastLED/releases
Once downloaded, just extract the content of the zip files inside your “arduino/libraries” folder.