NRF24L01 transmitting issue

Hello

I am transmitting one digital and analog data from transmitter(arduino mini pro) to receiver (arduino uno).

digital data is when sensor gets activate it will send message to receiver and din4 pin at receiver will get high, In analog if A0 volatge is less that 2V it will send message to receiver and din3 will get high.

The problem is if remove power supply to transmitter and connects again it is not transmitting data then if i reset the transmitter device by removing analog input pin then it is working.

What will be the issue? i tried with another new board again the same issue.

please help me solve the issue.

Here is the code

transmitter

#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
int msg[1];
const float referenceVolts = 5; 
RF24 radio(7,8);
const uint64_t pipe = 0xE8E8F0F0E1LL;
int SW1 = 3;
const float Volt = 2; 

void setup(void){
 Serial.begin(9600);
 radio.begin();
 radio.openWritingPipe(pipe);}

void loop(void){{
  int val = analogRead(A0);
 // read the value from the sensor 
 float volts = (val / 1023.0) * referenceVolts; 
 Serial.println(volts);
 if ((volts) < (Volt)){
  delay(3000);
 msg[0] = 121;
 radio.write(msg, 1);}
 } delay(100);
 if (digitalRead(SW1) == HIGH){
 msg[0] = 222;
 radio.write(msg, 1);}}

receiver code

#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
int msg[1];
RF24 radio(9,10);
const uint64_t pipe = 0xE8E8F0F0E1LL;
int LED1 = 3;
int LED2 = 4;
void setup(void){
 Serial.begin(9600);
 radio.begin();
 radio.openReadingPipe(1,pipe);
 radio.startListening();
 pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);}
void loop(void){
if (radio.available()){
bool done = false;
while (!done){
done = radio.read(msg, 1);
Serial.println(msg[0]);
{if (msg[0] == 121){delay(10);digitalWrite(LED1, HIGH);}
 {if (msg[0] == 222){digitalWrite(LED2, HIGH);}
 
delay(2000);}}}}
else
{digitalWrite(LED1, LOW);}
{digitalWrite(LED2, LOW);}}

Does the trandmitter loop() Serial.println(volts) show anything?
Please attach the entire code.

This is not correct, but may not be the only problem:

int msg[1];
. . .
. . .
radio.write(msg, 1);

The second parameter to the write() method is the size in bytes. An int (on say a Uno) is 2 bytes.
The corresponding read must also be changes.

Railroader:
Does the trandmitter loop() Serial.println(volts) show anything?
Please attach the entire code.

it is showing some strange message. when remove input A0 and reset the device then it is working fine.

By chance do i need to make any extra connections to arduino prpo min?

entire code i have posed already transmitter and receiver code

6v6gt:
This is not correct, but may not be the only problem:

int msg[1];

. . .
. . .
radio.write(msg, 1);




The second parameter to the write() method is the size in bytes. An int (on say a Uno) is 2 bytes.
The corresponding read must also be changes.

So what is the solution? I guess there is problem with the code. Because when i do reset it works fine.

bmg1234:
So what is the solution? I guess there is problem with the code. Because when i do reset it works fine.

radio.write(msg, 2) ; // you are sending 2 bytes if you are using a Uno etc.

done = radio.read(msg, 2 ) ; // you are receiving 2 bytes if you are using a Uno etc.

do i need to change for both transmitter and receiver?

Here is new changed code

But still the same issue. can you please tell if there is any mistake in the code

transmitter

#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
int msg[1];
const float referenceVolts = 5; 
RF24 radio(7,8);
const uint64_t pipe = 0xE8E8F0F0E1LL;
int SW1 = 3;
const float Volt = 2; 

void setup(void){
 Serial.begin(9600);
 radio.begin();
 radio.openWritingPipe(pipe);}

void loop(void){{
  int val = analogRead(A0);
 // read the value from the sensor 
 float volts = (val / 1023.0) * referenceVolts; 
 Serial.println(volts);
 if ((volts) < (Volt)){
  delay(3000);
 msg[0] = 121;
 radio.write(msg, 2);}
 } delay(100);
 if (digitalRead(SW1) == HIGH){
 msg[0] = 222;
 radio.write(msg, 2);}}

Receiver

#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
int msg[1];
RF24 radio(9,10);
const uint64_t pipe = 0xE8E8F0F0E1LL;
int LED1 = 3;
int LED2 = 4;
void setup(void){
 Serial.begin(9600);
 radio.begin();
 radio.openReadingPipe(1,pipe);
 radio.startListening();
 pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);}
void loop(void){
if (radio.available()){
bool done = false;
while (!done){
done = radio.read(msg, 2);
Serial.println(msg[0]);
{if (msg[0] == 121){delay(10);digitalWrite(LED1, HIGH);}
 {if (msg[0] == 222){digitalWrite(LED2, HIGH);}
 
delay(2000);}}}}
else
{digitalWrite(LED1, LOW);}
{digitalWrite(LED2, LOW);}}

it is showing some strange message

What is that message?

Also, your code is nearly unreadable. Assuming you're using the Arduino IDE, please autoformat it so you don't have multiple curly brackets on one line (among other things).

wildbill:
What is that message?

Also, your code is nearly unreadable. Assuming you're using the Arduino IDE, please autoformat it so you don't have multiple curly brackets on one line (among other things).

Yes I am using Arduino IDE and did auto format

Here is the code for transmitter after auto format.

#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
int msg[2];
const float referenceVolts = 5;
RF24 radio(7, 8);
const uint64_t pipe = 0xE8E8F0F0E1LL;
int SW1 = 3;
const float Volt = 2;

void setup(void) {
  Serial.begin(9600);
  radio.begin();
  radio.openWritingPipe(pipe);
}

void loop(void) {
  {
    int val = analogRead(A0);
    // read the value from the sensor
    float volts = (val / 1023.0) * referenceVolts;
    Serial.println(volts);
    if (volts < Volt) {
      delay(3000);
      msg[0] = 121;
      radio.write(msg, 2);
    }
  } delay(100);
  if (digitalRead(SW1) == HIGH) {
    msg[0] = 222;
    radio.write(msg, 2);
  }
}

receiver code

#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
int msg[2];
RF24 radio(9, 10);
const uint64_t pipe = 0xE8E8F0F0E1LL;
int LED1 = 3;
int LED2 = 4;
void setup(void) {
  Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(1, pipe);
  radio.startListening();
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
}
void loop(void) {
  if (radio.available()) {
    bool done = false;
    while (!done) {
      done = radio.read(msg, 2);
      Serial.println(msg[0]);
    { if (msg[0] == 121) {
          delay(10);
          digitalWrite(LED1, HIGH);
        }
      { if (msg[0] == 222) {
            digitalWrite(LED2, HIGH);
          }

          delay(2000);
        }
      }
    }
  }
  else
  {
    digitalWrite(LED1, LOW);
  }
  {
    digitalWrite(LED2, LOW);
  }
}

What does the receiver show it's getting in the Serial print statement?

Hello

please find attachment.

I think this part of code is wrong.

{
    int val = analogRead(A0);
    // read the value from the sensor
    float volts = (val / 1023.0) * referenceVolts;
    Serial.println(volts);
    if (volts < Volt) {
      delay(3000);
      msg[0] = 121;
      radio.write(msg, 2);
    }

if i delete this part and try to send digital data it is working fine.

my task is to send digital dat when tilt sensor is activated and transmitter is working with battery if battery goes less than 2v it should send message to the receiver and make digital pin high at receiver.

i am stuck with analog part sending message when it goes less than 2v. when i unplug power supply an d plug in again i need make sensor active and reset device all the time.

can you please check the code to send message to transmitter when voltage goes less than 2

serial monitor attachment

Message should still be defined as: int msg[1]
You have it as this in your latest code: int msg[2];

first it was mgs[1] only but some one suggested to change it to mgs[2] as it is sending 2 bytes

I don't think it matters - you're just wasting a couple of bytes.

I suggest that you make a much more minimal pair of programs that do nothing but send msg[0] every few seconds and see if you can receive and print the value. Something's broken and the rest of your code is just making it harder to see what.

Yes when battery is removed voltage goes 0 that is less than 2v in that case i think it is trying to send message and stuck.

please correct me if i am wrong

Also in the transmitter part, you have a number of surplus and incorrectly placed brackets '}' and '{' that the logic is wrong in that msg[0] has no value assigned to it when SW1 is LOW. This could be the cause of the odd results in the serial monitor.
The indentation when you format the code should give you a clue.

6v6gt:
Also in the transmitter part, you have a number of surplus and incorrectly placed brackets '}' and '{' that the logic is wrong in that msg[0] has no value assigned to it when SW1 is LOW. This could be the cause of the odd results in the serial monitor.
The indentation when you format the code should give you a clue.

I did auto format.