I currently have a few modules that read motion data and motor input on an RC boat. I am using it to create a neural network in python that will predict motion given a motor input and eventually produce its' own input. The only issue I am running into is that I cannot get my nRF24L01 Transceiver modules to transmit or receive data. I have tried a multitude of simple tutorials that have yielded no results and am at my wits end. The code I am attaching is the one that got the closest but still nothing. Yes, I have checked the wiring many times and tried multiple transceivers. Right now, the only possible issue I know about it the capacitor I have arcs between vcc and gnd is from a motor capacitor kit, but everywhere I read tells me to use an electrolytic capacitor; which I have ordered and will be here in two days. The problem is that I need to collect data before then... If you have suggestions on what to do with the modules or have other ideas for data transmission, I am open to anything I can get my hands on within the next few days.
Receiver:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
//----------------------------------------
// Defines CE and CSN PINs.
#define CE_PIN 9
#define CSN_PIN 10
// Address through which two modules communicate.
const byte Pipe_Address[6] = "00001";
// Variables to hold the data to be received.
// this must match "dataToSend" in the sender.
char dataReceived[32] = "";
// Variables to detect new incoming data.
bool newData = false;
// Create an RF24 object as a radio, while also setting the CE and CSN PINs.
RF24 radio(CE_PIN, CSN_PIN);
//________________________________________________________________________________VOID SETUP()
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println();
delay(2000);
Serial.println("This is the Receiver.");
// initialize the radio object.
radio.begin();
// set the Power Amplifier level.
// Use "RF24_PA_MIN" or "RF24_PA_LOW" if the distance between nRF24L01 modules is close to each other.
// Use "RF24_PA_HIGH" or "RF24_PA_MAX" if the distance between nRF24L01 modules is far from each other.
// For more details, see here : https://github.com/nRF24/RF24/blob/master/RF24.h#L26C1-L26C1
radio.setPALevel(RF24_PA_LOW);
// Set the transmission datarate.
radio.setDataRate(RF24_250KBPS);
// Set the address to receive data from the sender. The sender must use the same address.
radio.openReadingPipe(0, Pipe_Address);
// Set the nRF24L01 module as the receiver.
radio.startListening();
}
//________________________________________________________________________________
//________________________________________________________________________________VOID LOOP()
void loop() {
// put your main code here, to run repeatedly:
// Calling the receive_Data() subroutine.
receive_Data();
// Calling the show_Data() subroutine.
show_Data();
}
//________________________________________________________________________________
//________________________________________________________________________________receive_Data()
// Subroutine for receiving data.
void receive_Data() {
if (radio.available()) {
radio.read(&dataReceived, sizeof(dataReceived));
newData = true;
}
}
//________________________________________________________________________________
//________________________________________________________________________________show_Data()
// Subroutine for printing the received data to the serial monitor (serial communication).
void show_Data() {
if (newData == true) {
Serial.print("Data received : ");
Serial.println(dataReceived);
newData = false;
}
}
Transmitter:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
//----------------------------------------
// Defines CE and CSN PINs.
#define CE_PIN 9
#define CSN_PIN 10
// Address through which two modules communicate.
const byte Pipe_Address[6] = "00001";
// Variable for the data sending timer.
unsigned long previousMillis = 0;
unsigned long sendIntervalMillis = 1000; // Interval of sending data (1000 ms = 1 second).
// Variables to hold the data to be sent.
// The maximum data length that can be transmitted by the nRF24L01 module is 32 bytes.
char dataToSend[32] = "";
byte count = 0;
// Create an RF24 object as a radio, while also setting the CE and CSN PINs.
RF24 radio(CE_PIN, CSN_PIN); // CE, CSN
//________________________________________________________________________________VOID SETUP()
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println();
delay(2000);
Serial.println("This is the Sender.");
// initialize the radio object.
radio.begin();
// set the Power Amplifier level.
// Use "RF24_PA_MIN" or "RF24_PA_LOW" if the distance between nRF24L01 modules is close to each other.
// Use "RF24_PA_HIGH" or "RF24_PA_MAX" if the distance between nRF24L01 modules is far from each other.
// For more details, see here : https://github.com/nRF24/RF24/blob/master/RF24.h#L26C1-L26C1
radio.setPALevel(RF24_PA_LOW);
// Set the transmission datarate.
radio.setDataRate(RF24_250KBPS); // (RF24_250KBPS|RF24_1MBPS|RF24_2MBPS).
// Set the number and delay of retries upon failed submit.
radio.setRetries(5,5); // setRetries(delay,count).
// Set the nRF24L01 module as the sender.
radio.stopListening();
// Set the address to send data to the receiver. The receiver must use the same address.
radio.openWritingPipe(Pipe_Address);
// Prepare data to be sent.
sprintf(dataToSend, "Message %d", count);
}
//________________________________________________________________________________
//________________________________________________________________________________VOID LOOP()
void loop() {
// put your main code here, to run repeatedly:
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= sendIntervalMillis) {
previousMillis = currentMillis;
// Calls the send() subroutine.
send_Data();
}
}
//________________________________________________________________________________
//________________________________________________________________________________send_Data()
// Subroutine for sending data.
void send_Data() {
bool rslt;
// Always use sizeof() as it gives the size as the number of bytes.
// For example if dataToSend was an int sizeof() would correctly return 2
rslt = radio.write(&dataToSend, sizeof(dataToSend));
Serial.print("Data Sent : ");
Serial.print(dataToSend);
if (rslt) {
Serial.println(" | Acknowledge received");
update_Message();
}
else {
Serial.println(" | Failed to send data !");
}
}
//________________________________________________________________________________
//________________________________________________________________________________update_Message()
void update_Message() {
count++;
if (count > 9) count = 0;
sprintf(dataToSend, "Message %d", count);
}
Another close one, Receiver:
//Receiver
#include <SPI.h>
#include <RF24.h>
#include <nRF24L01.h>
RF24 radio(9,8); //CE, CSN
const byte address[10] = "ADDRESS01";
void setup() {
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(0, address);
radio.setPALevel(RF24_PA_MIN);
radio.startListening();
}
void loop() {
if(radio.available()) {
char txt[11] = "";
radio.read(&txt, sizeof(txt));
Serial.println(txt);
}
else {
Serial.println("No Data");
}
}
Transmitter:
//Transmitter
#include <SPI.h>
#include <RF24.h>
#include <nRF24L01.h>
RF24 radio(9,8); //CE,CSN
const byte address[10] = "ADDRESS01";
void setup() {
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_MIN);
radio.stopListening();
}
void loop() {
const char txt[] = "Hello World";
radio.write(&txt, sizeof(txt));
Serial.println(txt);
delay(1000);
}
My wiring is the same as what everyone uses.