HELP! Arduino + NRF24L01 (duplicate data in receiver))

i want to make wireless smoke & temp detector using nrf24l01. it fine at transmitter but not fine in receiver. Sometimes it displays duplicate readings at LCD receiver making the wrong position for the reading value.

here is the video for LCD Display:

this is the code we have so far:

//Transmitter
#include <SPI.h>      
#include <Mirf.h>    
#include <nRF24L01.h> 
#include <MirfHardwareSpiDriver.h> 
#include <LiquidCrystal_I2C.h>

int gasvalue;
float tempvalue;  
float vout;

LiquidCrystal_I2C lcd(0x27 ,20, 4);

void setup() 
{
  pinMode(A0,INPUT);
  pinMode(A1,INPUT);
  pinMode(2,OUTPUT);
  digitalWrite(2,HIGH);
  Serial.begin(9600);
  Serial.println("Library initialization; EC/CSN pin and channel definition and module transmission and reception address definition");
  delay(2000);
  Mirf.cePin = 9; 
  Mirf.csnPin = 10; 
  Mirf.spi = &MirfHardwareSpi; 
  Mirf.init();  
  Mirf.channel = 1; 
  Mirf.payload = sizeof(float); 
  Mirf.config(); 
 
  Mirf.setRADDR((byte *) "nrf01");
}
 
void loop() 
{
  //TEMP SENSOR
  vout=analogRead(A1);
  vout=(vout*500)/1023;
  tempvalue=vout; 
  Serial.println(tempvalue);
  
  //GAS SENSOR
  int analogSensor =analogRead(A0);
  float gasvalue = (analogSensor-50)/10; 
  Serial.println(gasvalue);
  
if (gasvalue >= 10)
  {
    digitalWrite(2,LOW);
  }
  else
  {
    digitalWrite(2,HIGH);
  }
  Mirf.setTADDR((byte *) "nrf02");
  Mirf.send((byte *) &tempvalue); //TEMP SENSOR
  while(Mirf.isSending());
  Mirf.send((byte *) &gasvalue); //GAS SENSOR
  while(Mirf.isSending());
  delay(500);
}  

.
.

//Receiver
#include <SPI.h>     
#include <Mirf.h>     
#include <nRF24L01.h> 
#include <MirfHardwareSpiDriver.h> 
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);

int buzzer = 7;
float gasvalue;
float tempvalue;

void setup() 
{
  pinMode(buzzer, OUTPUT); 
  Serial.begin(9600);
  lcd.init();
  lcd.backlight(); 
  delay(5000);
  lcd.clear();
  Mirf.cePin = 9; 
  Mirf.csnPin = 10; 
  Mirf.spi = &MirfHardwareSpi; 
  Mirf.init();  
  Mirf.channel = 1; 
  Mirf.setRADDR((byte *) "nrf02");
  Mirf.payload = sizeof(float); 
  Mirf.config();

  Serial.println("Reception of the value :"); 
}

void loop() 
{
 while(!Mirf.dataReady()){
  }
    //GAS SENSOR
    Mirf.getData((byte *) &gasvalue); 
    Serial.println(gasvalue); 
    lcd.setCursor(0,1);
    lcd.print("GAS LEVEL: ");
    lcd.print(gasvalue);
    lcd.print("%");
  while(!Mirf.dataReady()){
  }
    //TEMP SENSOR
    Mirf.getData((byte *) &tempvalue); 
    Serial.println(tempvalue); 
    lcd.setCursor(0,0);
    lcd.print("TEMPERATURE: ");
    lcd.print(tempvalue);
    lcd.print((char)223);
    lcd.print("C ");
    
if (gasvalue >= 10)
  {
    lcd.setCursor(0,3);
    lcd.print("DANGER");
    lcd.setCursor(0,2);
    lcd.print("-------FAN ON-------");
    tone(buzzer, 1000, 10000);
  }
  else
  {
    lcd.setCursor(0,3);
    lcd.print("NORMAL");
    lcd.setCursor(0,2);
    lcd.print("------FAN OFF-------");
    noTone(buzzer); 
  }
  delay(1000);
  lcd.clear(); 
}

Welcome to the forum

The receiver has no way of knowing what data is being received. Why not put all of the data to be transmitted into a struct and send it all at once then at the receiver end you will know what is being received

so, where i need to change for coding? at transmitter or receiver?

Both

In the Tx put the data into a struct and send it

In the Rx read the struct and use the data

it is correct? because it still display duplicate reading value at start only every time i on off, after that it ok. That make some noise at start because i connect with buzzer.

//Transmitter
#include <SPI.h>      
#include <Mirf.h>    
#include <nRF24L01.h> 
#include <MirfHardwareSpiDriver.h> 
#include <LiquidCrystal_I2C.h>

int gasvalue;
float tempvalue;  
float vout;

LiquidCrystal_I2C lcd(0x27 ,20, 4);

void setup() 
{
  pinMode(A0,INPUT);
  pinMode(A1,INPUT);
  pinMode(2,OUTPUT);
  digitalWrite(2,HIGH);
  Serial.begin(9600);
  Serial.println("Library initialization; EC/CSN pin and channel definition and module transmission and reception address definition");
  delay(2000);
  Mirf.cePin = 9; 
  Mirf.csnPin = 10; 
  Mirf.spi = &MirfHardwareSpi; 
  Mirf.init();  
  Mirf.channel = 1; 
  Mirf.payload = sizeof(float); 
  Mirf.config(); 
 
  Mirf.setRADDR((byte *) "nrf01");
}
 
void loop() 
{
  //TEMP SENSOR
  vout=analogRead(A1);
  vout=(vout*500)/1023;
  tempvalue=vout; 
  Serial.println(tempvalue);
  
  //GAS SENSOR
  int analogSensor =analogRead(A0);
  float gasvalue = (analogSensor-50)/10; 
  Serial.println(gasvalue);
  
if (gasvalue >= 10)
  {
    digitalWrite(2,LOW);
  }
  else
  {
    digitalWrite(2,HIGH);
  }
  Mirf.setTADDR((byte *) "nrf02");
  Mirf.send((byte *) &tempvalue); //TEMP SENSOR
  Mirf.send((byte *) &gasvalue); //GAS SENSOR
  while(Mirf.isSending());
  delay(500);
}  
//Receiver
#include <SPI.h>     
#include <Mirf.h>     
#include <nRF24L01.h> 
#include <MirfHardwareSpiDriver.h> 
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);

int buzzer = 7;
float gasvalue;
float tempvalue;

void setup() 
{
  pinMode(buzzer, OUTPUT);
  noTone(buzzer);
  Serial.begin(9600);
  lcd.init();
  lcd.backlight(); 
  delay(5000);
  lcd.clear();
  Mirf.cePin = 9; 
  Mirf.csnPin = 10; 
  Mirf.spi = &MirfHardwareSpi; 
  Mirf.init();  
  Mirf.channel = 1; 
  Mirf.setRADDR((byte *) "nrf02");
  Mirf.payload = sizeof(float); 
  Mirf.config();

  Serial.println("Reception of the value :"); 
}

void loop() 
{
 while(!Mirf.dataReady()){
  }
    
    //GAS SENSOR
    Mirf.getData((byte *) &gasvalue);
    Mirf.getData((byte *) &tempvalue);
    noTone(buzzer); 
    Serial.println(gasvalue);
    Serial.println(tempvalue);
    lcd.setCursor(0,1);
    lcd.print("GAS LEVEL: ");
    lcd.print(gasvalue);
    lcd.print("%");  
    lcd.setCursor(0,0);
    lcd.print("TEMPERATURE: ");
    lcd.print(tempvalue);
    lcd.print((char)223);
    lcd.print("C ");
    
if (gasvalue >= 10)
  {
    lcd.setCursor(0,3);
    lcd.print("DANGER");
    lcd.setCursor(0,2);
    lcd.print("-------FAN ON-------");
    tone(buzzer, 1000, 10000);
  }
  else
  {
    lcd.setCursor(0,3);
    lcd.print("NORMAL");
    lcd.setCursor(0,2);
    lcd.print("------FAN OFF-------");
    noTone(buzzer); 
  }
  delay(1000);
  lcd.clear(); 
}

I don't see any structs

can u show me which part need to struct?

Example sketches to send and receive a struct

Tx

// SimpleTx - the master or the transmitter

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

//not all #defines used but here for documentation
#define CE_PIN   9
#define CSN_PIN 10
#define MOSI_PIN 11
#define MISO_PIN 12
#define SCK_PIN 13

const byte slaveAddress[5] = {'R', 'x', 'A', 'A', 'A'};

RF24 radio(CE_PIN, CSN_PIN); // Create a Radio

char txNum = '0';

struct data
{
  byte pinState;
  byte temperature;
} sentData;

unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 1000;

void setup()
{
  Serial.begin(115200);
  Serial.println("SimpleTx Starting");
  radio.begin();
  radio.setDataRate( RF24_250KBPS );
  radio.setRetries(3, 5); // delay, count
  radio.openWritingPipe(slaveAddress);
}

void loop()
{
  currentMillis = millis();
  if (currentMillis - prevMillis >= txIntervalMillis)
  {
    sentData.pinState = !sentData.pinState;
    sentData.temperature = sentData.temperature + 1;
    send();
    prevMillis = millis();
  }
}

void send()
{
  bool rslt;
  rslt = radio.write( &sentData, sizeof(sentData) );
  if (rslt)
  {
    Serial.println("  Acknowledge received");
  }
  else
  {
    Serial.println("  Tx failed");
  }
}

Rx

// SimpleRx - the slave or the receiver

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

//not all #defines used but here for documentation
#define CE_PIN   9
#define CSN_PIN 10
#define MOSI_PIN 11
#define MISO_PIN 12
#define SCK_PIN 13

const byte thisSlaveAddress[5] = {'R', 'x', 'A', 'A', 'A'};

RF24 radio(CE_PIN, CSN_PIN);

struct data
{
  byte pinState;
  int temperature;
} receivedData;

bool newData = false;

void setup()
{
  Serial.begin(115200);
  Serial.println("SimpleRx Starting");
  radio.begin();
  radio.setDataRate( RF24_250KBPS );
  radio.openReadingPipe(1, thisSlaveAddress);
  radio.startListening();
}

void loop()
{
  getData();
  showData();
}

void getData()
{
  if ( radio.available() )
  {
    radio.read( &receivedData, sizeof(receivedData) );
    newData = true;
  }
}

void showData()
{
  if (newData == true)
  {
    Serial.print("Data received\tpinState : ");
    Serial.print(receivedData.pinState);
    Serial.print("\t");
    Serial.print("temperature : ");
    Serial.println(receivedData.temperature);
    newData = false;
  }
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.