NRF24L01 Bluetooth System

by fraser_sand in Circuits > Arduino

12 Views, 0 Favorites, 0 Comments

NRF24L01 Bluetooth System

Screenshot 2025-04-15 101124.png

As a part of building an Arduino blimp, my project group created a Bluetooth system using two NRF24L01 modules. The pupose of this instructables, is to show you how to setup two bluetooth modules, how to make them communicate with each other, how to pair them with a potentiometer that rotates a servo, how to scale the project to add more potentiometers that move more servos, and how one could scale up the project with different devices.


Bluetooth Theory

Bluetooth modules are devices that facilitate wireless data transmission between two electronic devices within relatively close proximity. Bluetooth is a common application that's used in many fields from personal electronics to medical devices. Along with a module, the devices also need some form of code to tell the modules what to do. To get two modules to communicate, they need to be matched to the same address. If matched, the bluetooth devices will send out and/or receive data from one another on one of the 79 frequencies used on a 2.4 GHz band (a frequency also commonly used in wifi). To make signals hard to cross or be intercepted, bluetooth uses frequency hoping spread spectrum or FHSS, a method that essentially rapidly switches the transmission frequencies. Overall, bluetooth is a power efficient, secure, and versitile means of wireless data transfer and communication.

Supplies

In this project we used the following supplies.

  1. Male to Female Wires
  2. Male to Male Wires
  3. Female to Female Wires
  4. Arduinos x2 (Either 2 UNO's or 1 UNO and 1 NANO)
  5. USB to Mini USB Cable
  6. NRF24L01 Bluetooth Module x2
  7. Servo x2
  8. Potentiometer x2

NRF24L01 Bluetooth Module Wiring

The first step is to wire the two bluetooth devices to their respective arduino's. Although they're wired the same, it might be a good idea to figure out which one will be your transmitter and which will be your receiver. This step is relatively simple, however, make sure you have the device oriented the correct way when wiring.

Module Communication

Once the NRF24L01 modules are both wired, you are now ready to get them to communicate with one another. One module will now be designated as the transmitter and one will be designated as the receiver. Coding it yourself is optional, however there is code below.


Transmitter Code


// Include Libraries

#include <SPI.h>

#include <nRF24L01.h>

#include <RF24.h>


//create an RF24 object

RF24 radio(9, 8); // CE, CSN


//address through which two modules communicate.

const byte address[6] = "00001";


void setup()

{

radio.begin();

//set the address

radio.openWritingPipe(address);

//Set module as transmitter

radio.stopListening();

}

void loop()

{

//Send message to receiver

const char text[] = "Hello World";

radio.write(&text, sizeof(text));

delay(1000);

}

Receiver Code


//Include Libraries

#include <SPI.h>

#include <nRF24L01.h>

#include <RF24.h>


//create an RF24 object

RF24 radio(9, 8); // CE, CSN


//address through which two modules communicate.

const byte address[6] = "00001";


void setup()

{

while (!Serial);

Serial.begin(9600);

radio.begin();

//set the address

radio.openReadingPipe(0, address);

//Set module as receiver

radio.startListening();

}


void loop()

{

//Read the data if available in buffer

if (radio.available())

{

char text[32] = {0};

radio.read(&text, sizeof(text));

Serial.println(text);

}

}

For this previous section of code, make sure your receiver and transmitter radio addresses match each other as well as making sure all of the pin placements align with the code. Think about changing your address to something more complicated to avoid potential interference from other devices.


Single Servo and Potentiometer

Follow the wiring diagrams below to add on a servo and potentiometer onto the same system as your already wired bluetooth modules. Make sure your pin placements are correct and make sure your servo is attached to your receiver arduino and that your potentiometer is with your transmitter arduino.


  1. Transmitter: Potentiometer Wiring


  1. Receiver: Servo Wiring


This next section of code is to be used to make your potentiometer control your servo via the bluetooth modules. Again, make sure all pins are correctly placed and aligned with the code as well as making sure the receiver and transmitter addresses match.


  1. Receiver code for Servo control

//Include Libraries

#include <SPI.h>

#include <nRF24L01.h>

#include <RF24.h>

#include <Servo.h>


int servoPin = 7;


Servo myServo;


//create an RF24 object

RF24 radio(9, 8); // CE, CSN


//address through which two modules communicate.

const byte address[6] = "00027";


void setup()

{

myServo.attach(servoPin);


while (!Serial);

Serial.begin(9600);

radio.begin();

//set the address

radio.openReadingPipe(0, address);

//Set module as receiver

radio.startListening();

}


void loop()

{

//Read the data if available in buffer

if (radio.available())

{

char text[32] = {0};

int data = 0;

radio.read(&text, sizeof(text));

data = atoi(text); // converts text into an integer

Serial.println(data);

myServo.write(data);

delay(15);

}

}


  1. Transmitter Code for Potentiometer

// Include Libraries

#include <SPI.h>

#include <nRF24L01.h>

#include <RF24.h>


//create an RF24 object

RF24 radio(9, 8); // CE, CSN


//address through which two modules communicate.

const byte address[6] = "00027";


// declare a dummy variable for testing

int var;


// analog pin used to connect the potentiometer

int potPin = A0;


void setup()

{

radio.begin();

//set the address

radio.openWritingPipe(address);

//Set module as transmitter

radio.stopListening();

}

void loop()

{

char text[10];

int analogValue;

//var = (var + 10) % 180; // increments var by 10, resets at 180


analogValue = analogRead(potPin);

var = map(analogValue, 0, 1023, 0, 179);

//Send integer to receiver

itoa(var, text, 10); //for integer ASCII representation


radio.write((uint8_t *)text, strlen(text));

delay(1000);

}


Scaling Number of Potentiometers and Servos

If you want to add more potentiometers to control more servos, follow these wiring diagrams. Just as always be warry of your pin placements as they need to line up with the code.

  1. Receiver: Multiple Servos to Arduino



  1. Transmitter: Multiple Potentiometers to Arduino


This last section of code is to be used when scaling the amount of potentiometers and servos. Since this code uses an array, you can add even more potentiometers and servos if wanted, you just have add in more of the code.


  1. Transmitter Multi Code

#include <SPI.h>

#include <nRF24L01.h>

#include <RF24.h>

const int POT_PIN1 = A0;

int potentiometerReading1;

const int POT_PIN2 = A1;

int potentiometerReading2;

const int POT_PIN3 = A2;

int potentiometerReading3;

RF24 radio(9, 8);

const byte address[6] = "07332";

byte Array[4];

void setup() {

radio.begin();

radio.openWritingPipe(address);

radio.setPALevel(RF24_PA_MIN);

radio.stopListening();

Serial.begin(9600);

pinMode(POT_PIN1, INPUT);

pinMode(POT_PIN2, INPUT);

pinMode(POT_PIN3, INPUT);

}

void loop() {

potentiometerReading1 = map(analogRead(POT_PIN1), 0, 1023, 0, 180);

potentiometerReading2 = map(analogRead(POT_PIN2), 0, 1023, 0, 180);

potentiometerReading3 = map(analogRead(POT_PIN3), 0, 1023, 0, 180);

// Serial.println(potentiometerReading1);

// Serial.println(potentiometerReading2);

Serial.println(potentiometerReading3);

Array[0] = 0;

Array[1] = potentiometerReading1;

Array[2] = 4;

Array[3] = 6;

radio.write(&Array, sizeof(Array));

delay(20);

Array[0] = 1;

Array[1] = potentiometerReading2;

Array[2] = 4;

Array[3] = 6;

radio.write(&Array, sizeof(Array));

delay(20);

Array[0] = 2;

Array[1] = potentiometerReading3;

Array[2] = 4;

Array[3] = 6;

radio.write(&Array, sizeof(Array));

delay(20);

}


  1. Receiver Multi Code

#include <SPI.h>

#include <nRF24L01.h>

#include <RF24.h>

#include <Servo.h>




const int SERVO_PIN1 = A5;

Servo servo1;

const int SERVO_PIN2 = A4;

Servo servo2;

const int SERVO_PIN3 = A3;

Servo servo3;

const int THRUSTER_PIN = 4;



RF24 radio(9, 8);

const byte address[6] = "07332";

bool turbineIsOn = true;

void setup() {

radio.begin();

radio.openReadingPipe(0, address);

radio.setPALevel(RF24_PA_MIN);

radio.startListening();

Serial.begin(9600);

pinMode(THRUSTER_PIN, OUTPUT);

servo1.attach(SERVO_PIN1);

servo2.attach(SERVO_PIN2);

}

void loop() {

if (radio.available()) {

byte Array[4];

radio.read(&Array, sizeof(Array));

Serial.print(Array[0]);

Serial.print(" ");

Serial.print(Array[1]);

Serial.println();

if (Array[0] == 0) {

moveServo1(Array[1]);

}

if (Array[0] == 1) {

moveServo2(Array[1]);

}

if (Array[0] == 2) {

moveServo3(Array[1]);

// turnMotor(Array[1])

}

}

}

void moveServo1(int rotation) {

servo1.write(rotation);

}

void moveServo2(int rotation) {

servo2.write(rotation);

}

void moveServo3(int rotation) {

servo3.write(rotation);

}

void turnMotor(int number) {

if (number > 90) {

digitalWrite(THRUSTER_PIN, HIGH);

} else {

digitalWrite(THRUSTER_PIN, LOW);

Serial.println("LOW");

}

}


If you want to control other bluetooth devices, this final code leaves you with the option to do so. Simply add in your own code specific to your device in the array on both the receiver and transmitter systems.