Go Down

Topic: Nrf2401 and arduino (Read 314 times) previous topic - next topic

raminaziz

Dear friends,

I have set up a transmiter and receiver to turn on and off one Led using nrf2401 module and its ok working
Now I want to extend this and control 6 Leds on the receiver side.
Can any body please help me on this?

Paul_KD7HB

Dear friends,

I have set up a transmiter and receiver to turn on and off one Led using nrf2401 module and its ok working
Now I want to extend this and control 6 Leds on the receiver side.
Can any body please help me on this?
I expect lots of people could help if you gave us any hint as to what you need help with. What have you tried and what was the result?

Paul

noweare

To control 6 LEDs your probably gonna have to send two pieces of information.  The led your addressing and the state.

say your leds are numbered 1 to 6 and you want to turn led 3  to on. The transmitter would send
the number of the led (3) and the state (0 , because a 0 will light up the led).

On the receive side the arduino would read the info and extract the address and state information and change the output pins that the led's are connected to.

so in this case

digitalwrite(LED3, LOW);

You don't have to use numbers you can use characters also.
normally the reciever would let the transmitter that the message was recieved.
so hopefully this gave you an idea how to go about it.

fnb111

Please post the code that you have by using tags.

Robin2

Have a look at this Simple nRF24L01+ Tutorial.

Wireless problems can be very difficult to debug so get the wireless part working on its own before you start adding any other features.

The examples are as simple as I could make them and they have worked for other Forum members. If you get stuck it will be easier to help with code that I am familiar with. Start by getting the first example to work

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

TomGeorge

Hi,
Can you post the code you have already and  schematic of your project please?
What model Arduinos are you using?

Thanks.. Tom.. :)
Everything runs on smoke, let the smoke out, it stops running....

raminaziz

#6
Aug 25, 2019, 02:30 pm Last Edit: Aug 25, 2019, 02:45 pm by raminaziz
Thank you all
I have done the simple example and it worked ok I am using two Arduino Uno at the moment but may also use Nano too.
I am sending my code for six leds but tried to work first by two leds that I got stuck with it.

here is the code for Transmitter:


Code: [Select]

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(9, 8); // CE, CSN        
const byte address[6] = "00001";     //Byte of array representing the address. This is the address where we will send the data. This should be same on the receiving side.
int button1_pin = 2;
int button2_pin = 3;
//int button3_pin = 4;
//int button4_pin = 5;
//int button5_pin = 6;
//int button6_pin = 7;

boolean button1_state = 0;
boolean button2_state = 0;
//boolean button3_state = 0;
//boolean button4_state = 0;
//boolean button5_state = 0;
//boolean button6_state = 0;
void setup() {
 Serial.begin(9600);
pinMode(button1_pin, INPUT);
pinMode(button2_pin, INPUT);
//pinMode(button3_pin, INPUT);
//pinMode(button4_pin, INPUT);
//pinMode(button5_pin, INPUT);
//pinMode(button6_pin, INPUT);
radio.begin();                  //Starting the Wireless communication
radio.openWritingPipe(address); //Setting the address where we will send the data
radio.setPALevel(RF24_PA_MIN);  //You can set it as minimum or maximum depending on the distance between the transmitter and receiver.
radio.stopListening();          //This sets the module as transmitter
}
void loop()
{
   button1_state = digitalRead(button1_pin);
   button2_state = digitalRead(button2_pin);
  // button3_state = digitalRead(button3_pin);
  // button4_state = digitalRead(button4_pin);
  // button5_state = digitalRead(button5_pin);
  // button6_state = digitalRead(button6_pin);

  if(button1_state == HIGH)
  {
   const char text[] = "Button1 State is HIGH";
   Serial.println( " Button1 State is HIGH");
   radio.write(&text, sizeof(text));                  //Sending the message to receiver
  }
  else if(button1_state == LOW)
  {
    const char text[] = " Button1 State is LOW";
    Serial.println( "Button1 State is LOW");
    radio.write(&text, sizeof(text));
   }
  
  if(button2_state == HIGH)
   {
     const char text1[] = "Button2 State is HIGH";
     Serial.println( " Button2 State is HIGH");
     radio.write(&text1, sizeof(text1));
   }
else if(button2_state ==LOW)
 {
   const char text1[] = "Button2 State is LOW";
   Serial.println( "Button2 State is LOW");
   radio.write(&text1, sizeof(text1));
  }
radio.write(&button1_state, sizeof(button1_state));  //Sending the message to receiver
 radio.write(&button2_state, sizeof(button2_state));
 delay(1000);
}


for the receiver side:

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(9, 8); // CE, CSN
const byte address[6] = "00001";
boolean button1_state = 0;
boolean button2_state = 0;
int led1_pin = 3;
int led2_pin = 4;
void setup() {
pinMode(4, OUTPUT);
pinMode(3, OUTPUT);
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(0, address);   //Setting the address at which we will receive the data
radio.setPALevel(RF24_PA_MIN);       //You can set this as minimum or maximum depending on the distance between the transmitter and receiver.
radio.startListening();              //This sets the module as receiver
}
void loop()
{
if (radio.available())              //Looking for the data.
{
 char text[32] = "";                 //Saving the incoming data
 char text1[32] = "";
   radio.read(&text, sizeof(text));    //Reading the data
   radio.read(&button1_state, sizeof(button1_state));    //Reading the data
  
  
 if(button1_state == HIGH)
   {
     digitalWrite(3, HIGH);
     Serial.println(text);
   }
   else
   {
     digitalWrite(3, LOW);
     Serial.println("b 1 is Low");
    }
    radio.read(&text1, sizeof(text1));
    radio.read(&button2_state, sizeof(button2_state));
 if(button2_state == HIGH)
   {
     digitalWrite(4, HIGH);
     Serial.println(text1);
   }  
 else
  {
    digitalWrite(4, LOW);
    Serial.println("b 2 is low");
    }
}

delay(1000);
}


THe result of the above codes is the that on receiver side one led comes on and off by itself without pushing any button)and the second led dose not come on at all .independent of pushing any button.

TomGeorge

Hi,
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?


How have you got your buttons wired?


Tom... :)
Everything runs on smoke, let the smoke out, it stops running....

raminaziz

#8
Aug 25, 2019, 05:03 pm Last Edit: Aug 25, 2019, 05:07 pm by raminaziz
Dear Tom
This is the connection push buttons .( not exactly the same pin numbers)




Robin2

I have done the simple example and it worked ok
If that means that you tried the first example in my Tutorial why don't you stick with that and just modify it a little to send the switch states.

You could record the state of each switch in an array
Code: [Select]
byte switchStates[6];

for (byte n = 0; n < 6; n++) {
    switchStates[n] = digitalRead(switchPin[n];
}
radio.write(&switchStates, sizeof(switchStates));


In the receiver have an equivalent 6 byte array

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

raminaziz

Thank you Robin2.
I have put your codes in my transmitter and shows no error so far and I am working on the receiver side now will you please see if I have done it correctly ?


Code: [Select]
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(9, 8); // CE, CSN         
const byte address[6] = "00001";     //Byte of array representing the address. This is the address where we will send the data. This should be same on the receiving side.

//boolean switchStates[6] = 0;

byte switchStates[6];
int switchPin[6]={2,3,4,5,6,7};

void setup() {
  Serial.begin(9600);
pinMode(switchPin[6], INPUT);

radio.begin();                  //Starting the Wireless communication
radio.openWritingPipe(address); //Setting the address where we will send the data
radio.setPALevel(RF24_PA_MIN);  //You can set it as minimum or maximum depending on the distance between the transmitter and receiver.
radio.stopListening();          //This sets the module as transmitter
}
void loop()
{
 
   byte switchStates[6];

for (byte n = 0; n < 6; n++)
{
    switchStates[n] = digitalRead(switchPin[n]);
}
radio.write(&switchStates, sizeof(switchStates));
if(switchStates == HIGH)
  {
   const char text[] = "Button1 State is HIGH";
   Serial.println( " Button1 State is HIGH");
   radio.write(&text, sizeof(text));                  //Sending the message to receiver
  }
  else if(switchStates == LOW)
  {
    const char text[] = " Button1 State is LOW";
    Serial.println( "Button1 State is LOW");
    radio.write(&text, sizeof(text));
   }
   
 
 radio.write(&switchStates, sizeof(switchStates));  //Sending the message to receiver
 
  delay(1000);}

Robin2

#11
Aug 25, 2019, 10:53 pm Last Edit: Aug 25, 2019, 10:56 pm by Robin2
You don't need any of this
Code: [Select]
if(switchStates == HIGH)
  {
   const char text[] = "Button1 State is HIGH";
   Serial.println( " Button1 State is HIGH");
   radio.write(&text, sizeof(text));                  //Sending the message to receiver
  }
  else if(switchStates == LOW)
  {
    const char text[] = " Button1 State is LOW";
    Serial.println( "Button1 State is LOW");
    radio.write(&text, sizeof(text));
   }
  
  
 radio.write(&switchStates, sizeof(switchStates));  //Sending the message to receiver




And when working on a wireless problem always post the Tx and the Rx code because they must work as a pair.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

TomGeorge

Dear Tom
This is the connection push buttons .( not exactly the same pin numbers)
Sorry, no picture.
Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html
Please attach the picture.
Thanks...  Tom... :)
Everything runs on smoke, let the smoke out, it stops running....

raminaziz

#13
Aug 25, 2019, 11:45 pm Last Edit: Aug 25, 2019, 11:48 pm by raminaziz
You don't need any of this

...R
Hi Robin,
Now I am sending both Tx and RX codes for your considerations.

TX Code:
Code: [Select]

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(9, 8); // CE, CSN        
const byte address[6] = "00001";     //Byte of array representing the address. This is the address where we will send the data. This should be same on the receiving side.

//boolean switchStates[6] = 0;

byte switchStates[6];
int switchPin[6]={2,3,4,5,6,7};

void setup() {
Serial.begin(9600);
pinMode(switchPin[6], INPUT);

radio.begin();                  //Starting the Wireless communication
radio.openWritingPipe(address); //Setting the address where we will send the data
radio.setPALevel(RF24_PA_MIN);  //You can set it as minimum or maximum depending on the distance between the transmitter and receiver.
radio.stopListening();          //This sets the module as transmitter
}
void loop()
{

 byte switchStates[6];
 for (byte n = 0; n < 6; n++)
    {
      switchStates[n] = digitalRead(switchPin[n]);
      radio.write(&switchStates[n], sizeof(switchStates[n]));
       Serial.println( switchStates[n]);
  delay(10);
  }
}  

[code]

RX Code:

[code]
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(9, 8); // CE, CSN
const byte address[6] = "00001";
byte switchStates[6];
int LED_Pin[6]={2,3,4,5,6,7};
int switchPin[6]={2,3,4,5,6,7};

char text[32] = "";

void setup() {
pinMode(LED_Pin[6], OUTPUT);
;
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(0, address);   //Setting the address at which we will receive the data
radio.setPALevel(RF24_PA_MIN);       //You can set this as minimum or maximum depending on the distance between the transmitter and receiver.
radio.startListening();              //This sets the module as receiver
}
void loop()
{
 byte switchStates[6];

for (byte n = 0; n < 6; n++)
{
   switchStates[n] = digitalRead(switchPin[n]);
   radio.read(&switchStates[n], sizeof(switchStates[n]));
if (radio.available())              //Looking for the data.
{
  radio.read(&text, sizeof(text));    //Reading the data
  radio.read(&switchStates, sizeof(switchStates));    //Reading the data
  
  
 if(switchStates[n] == HIGH)
   {
     digitalWrite(LED_Pin[n], HIGH);
     Serial.println(text);
   }
   else
   {
     digitalWrite(LED_Pin[n], LOW);
     Serial.println("Leds are Low");
    }
}  
}
 
   // radio.read(&text, sizeof(text));
  //  radio.read(switchStates[n], sizeof(switchStates[n]));
 
delay(10);
}
[code]


Unfortunately no Led will come on.

[/code]

Whandall

Unfortunately no Led will come on.
Fortunately no important devices are controlled by this unsensible try.  ;)

Put all your data in a struct/array and send one packet.
Your approach wastes a lot of bandwidth and makes it impossible to identify the sent data.

Don't read from the radio without checking available first.
Ah, this is obviously some strange usage of the word 'safe' that I wasn't previously aware of. (D.Adams)

Go Up