Skip to content

Instantly share code, notes, and snippets.

@rlogiacco
Last active December 15, 2022 23:36
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save rlogiacco/b50b2a4398029bd4eb3c to your computer and use it in GitHub Desktop.
Save rlogiacco/b50b2a4398029bd4eb3c to your computer and use it in GitHub Desktop.
nRF24 multiple transmitters
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
int senderId;
// Set up nRF24L01 radio on SPI bus plus pins 9 & 10
//Contacts from the radio to connect NRF24L01 pinamnam -> Arduino
//SCK -> 13
//MISO -> 12
//MOSI -> 11
//CSN -> 10
//CE -> 9
RF24 radio(9, 10);
// this is not the channel address, but the transmitter address
const uint64_t pipe = 0xE8E8F0F0E1LL;
//LEDs connected to these pins
// ENSURE YOU HAVE THE RIGHT DIGITAL PINS HERE
int LEDpins[5] = { 2, 3, 4, 5, 6 };
void setup(void) {
Serial.begin(9600);
radio.begin();
// the following statements improve transmission range
radio.setPayloadSize(2); // setting the payload size to the needed value
radio.setDataRate(RF24_250KBPS); // reducing bandwidth
radio.openReadingPipe(1, pipe); // Open one of the 6 pipes for reception
radio.startListening(); // begin to listen
// Enable all the LED pins as output
for (int i = 0; i < 5; i++) {
pinMode(LEDpins[i], OUTPUT);
digitalWrite(LEDpins[i], LOW); // this is unnecessary but good practice nonetheless
}
}
void loop(void) {
// Turns off all the LEDs
for (int i = 0; i < 5; i++) {
digitalWrite(LEDpins[i], LOW);
}
if (radio.available()) {
// this while is here to throw away all the packets but the last one
bool done = false;
while (!done) {
// read and write expect a reference to the payload (& symbol)
// second argument is the packet length in bytes (sizeof(int) == 2)
done = radio.read(&senderId, 2);
}
//Light up the correct LED for 50ms
digitalWrite(LEDpins[senderId], HIGH);
Serial.print("LED ");
Serial.print(senderId);
Serial.println(" On");
delay(50);
}
}
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
int transmitterId;
// Set up nRF24L01 radio on SPI bus plus pins 9 & 10
//Contacts from the radio to connect NRF24L01 pinamnam -> Arduino
//SCK -> 13
//MISO -> 12
//MOSI -> 11
//CSN -> 10
//CE -> 9
RF24 radio(9, 10);
// this is not the channel address, but the transmitter address
const uint64_t pipe = 0xE8E8F0F0E1LL;
//button connected to these pins
int buttonPin1 = 2;
void setup(void) {
// CHANGE THIS PER EACH TRANSMITTER, from 0 to 4
transmitterId = 0;
radio.begin();
// the following statements improve transmission range
radio.setPayloadSize(2); // setting the payload size to the needed value
radio.setDataRate(RF24_250KBPS); // reducing bandwidth
radio.openWritingPipe(pipe); // set the transmitter address
}
void loop(void) {
//until the button (buttonPin1) pressed send the package (id) to receiver Arduino
if (digitalRead(buttonPin1) == HIGH) {
// some implementations automatically shut down the radio after a transmission: this
// ensures the radio is powered up before sending data
radio.powerUp();
// read and write expect a reference to the payload (& symbol)
// second argument is the packet length in bytes (sizeof(int) == 2)
radio.write(&transmitterId, 2);
}
}
@ssengm
Copy link

ssengm commented Jan 29, 2016

hi, in your receiver code,

done = radio.read(&senderId, 2); shows error that radio.read is void type. how to handle this ? I would appreciate your response :)

@robogue
Copy link

robogue commented Mar 15, 2016

hi Salseng, try to replace your RF24 lib with the maniacbug's one. i've experienced like that and now success with that steps :)

@nomyt
Copy link

nomyt commented Dec 2, 2016

done is unused, so just do: radio.read(&senderId, 2);

@OgliariNatan
Copy link

Use the done like this:
bool done = 0;
while (!done) {
Serial.println("esta no WHILE");
// read and write expect a reference to the payload (& symbol)
// second argument is the packet length in bytes (sizeof(int) == 2)
radio.read(&senderId, 2); //done = radio.read(&senderId, 2);
done = 1;
}

@AbhayAmbekar
Copy link

AbhayAmbekar commented Mar 8, 2018

Hello,
I have been trrying to learn NRF24L01 but have some doubts. There are Two Transmitters(T1,T2) and Two Receivers(R1,R2) in same vicinity how to make sure the receiver receives only the intended transmitter data. i.e R1 should only pick/listen T1 transmitted data and not T2 transmitted data, similarly R2 should only pick/listen to T2 transmitted data and not T1 transmitted data. I heard something about Transmitter address can you please help me more on this ? And how many addresses can we assign or use ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment