NRF24L01 module

I am using nRF24L01 module to transmit data of MQ-2 (Gas sensor) and DHT22 (Humidity and temp sensor) . I have written the code but the problem arising is that the radio.write() returns value 0 i.e. it is not sending any information. What should i do for this project?

Start by getting the examples in this topic working

Simple nRF24L01+ 2.4GHz transceiver demo

Hello yashodhan_45

Welcome to the world's best Arduino forum ever.

I have been following the topic of using this type of wireless module and the associated software functions for some time now.

For wireless projects, I recommend using the HC12 module.

What are the advantages:

  1. no external functions from a library are needed.
  2. the development of a communication protocol is in your hands
  3. the necessary development steps can be programmed and tested without using the wireless module
  4. the radio module can be easily configured for the project requirements
  5. both transmitter and receiver are contained in one radio module.

hth

Have a nice day and enjoy coding in C++.

Which library are you using?

Have a look at the return value of write(); it may not mean what you think it is. E.g.:

     * - `true` if the payload was delivered successfully and an acknowledgement
     *   (ACK packet) was received. If auto-ack is disabled, then any attempt
     *   to transmit will also return true (even if the payload was not
     *   received).

From here: RF24/RF24.h at master · nRF24/RF24 · GitHub

This means that the function will only return 'true' if either auto-ack is disabled, or if auto-ack is enabled and the message was delivered successfully and the receiver returned an ACK. In other words, the one thing you can never tell from a 'true' return value is whether the message was successfully sent.

Hello, I am using nRF24L01 module to transmit sensor data of DHT22 and MQ-2 sensor from Arduino Uno to Arduino Mega. I have also checked individually the functionality of the nRF Module and both are working fine. But, when I implement in project ,the radio.write() function returns 0 value. What should I do now?

Transmitter Code:

`#include <SPI.h>
#include <RF24.h>
#include <DHT.h>

// Define the pin configurations
#define MQ2_PIN A0 // Analog pin for MQ-2 sensor
#define DHT_PIN 2 // Digital pin for DHT22 sensor
#define DHT_TYPE DHT22

// Create instances for DHT and nRF24L01
DHT dht(DHT_PIN, DHT_TYPE);
RF24 radio(9, 10); // CE, CSN pins for nRF24L01 module

void setup() {
Serial.begin(9600);
dht.begin();
radio.begin();
radio.openWritingPipe(0xF0F0F0F0E1LL); // Set the address for communication
radio.stopListening();
}

void loop() {
// Read data from sensors
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
int gasValue = analogRead(MQ2_PIN);

// Print sensor data on Serial Monitor
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println(" %");
Serial.print("Gas Value: ");
Serial.println(gasValue);

// Prepare data to be sent
String dataToSend = String(temperature) + "," + String(humidity) + "," + String(gasValue);
Serial.println(dataToSend);
// Convert String to char array for nRF24L01 transmission
char charData[dataToSend.length() + 1];
Serial.print("Send:");
Serial.println(dataToSend.length() + 1);

dataToSend.toCharArray(charData, sizeof(charData));
Serial.print("Char Data:");
Serial.println(charData);
// Send data through nRF24L01 module
// Send data through nRF24L01 module
size_t bytesSent = radio.write(&charData, sizeof(charData));
Serial.print("Bytes Sent: ");
Serial.println(bytesSent);

Serial.print("Address of charData: ");
Serial.println((unsigned long)&charData, HEX);
delay(1000); // Delay between readings
}
`
Receiver Code:

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

RF24 radio(48, 49); // Create instance for nRF24L01 module

void setup() {
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(1, 0xF0F0F0F0E1LL); // Set the address for communication
radio.startListening(); // Start listening for incoming data
}

void loop() {
if (radio.available()) {
char receivedData[16]; // Assuming data length is 32 characters or less
radio.read(&receivedData, sizeof(receivedData));

// Display received data on Serial Monitor
Serial.print("Received Data:");
Serial.println(receivedData);
delay(1000);

}
}

First, reread the rules on posting code.

Then read and carefullly follow Robin2's simple rf24 tutorial to get the modules talking before adding complexity. See post #30 for a sketch to test module to processor connectivity.

1 Like

You know tha. t the SPI pins are in a different locstion?

Mega Name. Pin
MISO. 50
MOSI. 51
SCK. 52
CS (defaut) 53

Your two or more topics on the same or similar subject have been merged.

Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.

Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum

It will help you get the best out of the forum in the future.

Thank you.

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