Hi, I'm a mechanical engineer who is noob in electronics.
I decided to improve my electronic knowledge and started with arduino, leds, lcds, potentiometers so on. My next challenge is building an RC device, so I bought some NRF24L01 modules.
I have been trying loads of stuff last 3 weeks without any success. First I tried with antenna, after hundred of trials I decided my arduino is broken bought new one still no success changed stuff again no success and so on....
I am trying desperately with robin2's tutorials since it is known working..
I bought NRF modules without external antenna, tried again. Still no success....
And today I cut my jumper wires to make wires shorter as adviced in many topics. Tadaa, I am able to create communication between tx and rx. The real reason I keep being unsuccessful looks like the noise on the cables. Finally it works. BUT... The rx sometimes updates message number and sometimes doesn't.
I am using uno as rx and pro mini as tx, both clones.
I use NRF24L01 modules with adapters.
I tried MISO and MOSI twisted and not twisted as adviced on some video on youtube. Nothing changes..
tx: arduino pro mini, powered with 2 18650 batteries. when I measure voltage it is 8V. Arduino powered via raw pin, nrf24l01 is connected to 8V, when I measure voltage on nrf24l01 it is exactly 3.3V.
added pin 10 as output as adviced in various places.
my setup;
tx code:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN 8
#define CSN_PIN 9
const byte slaveAddress[5] = {'R','x','A','A','A'};
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
char dataToSend[10] = "Message 0";
char txNum = '0';
unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 1000; // send once per second
void setup() {
Serial.begin(9600);
Serial.println("SimpleTx Starting");
radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.setRetries(3,5); // delay, count
radio.openWritingPipe(slaveAddress);
pinMode(10, OUTPUT);
}
//====================
void loop() {
currentMillis = millis();
if (currentMillis - prevMillis >= txIntervalMillis) {
send();
prevMillis = millis();
}
}
//====================
void send() {
bool rslt;
rslt = radio.write( &dataToSend, sizeof(dataToSend) );
// 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
Serial.print("Data Sent ");
Serial.print(dataToSend);
if (rslt) {
Serial.println(" Acknowledge received");
updateMessage();
}
else {
Serial.println(" Tx failed");
}
}
//================
void updateMessage() {
// so you can see that new data is being sent
txNum += 1;
if (txNum > '9') {
txNum = '0';
}
dataToSend[8] = txNum;
}
rx: arduino uno connected to computer via usb. nrf powered via arduino 5V pin, also tried to power via batteries, it got even worse, I don't know why because I use adapter it shouldn't change I guess.
rx code:
// SimpleRx - the slave or the receiver
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN 8
#define CSN_PIN 9
const byte thisSlaveAddress[5] = {'R','x','A','A','A'};
RF24 radio(CE_PIN, CSN_PIN);
char dataReceived[10]; // this must match dataToSend in the TX
bool newData = false;
//===========
void setup() {
Serial.begin(9600);
Serial.println("SimpleRx Starting");
radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.openReadingPipe(1, thisSlaveAddress);
radio.startListening();
pinMode(10, OUTPUT);
}
//=============
void loop() {
getData();
showData();
}
//==============
void getData() {
if ( radio.available() ) {
radio.read( &dataReceived, sizeof(dataReceived) );
newData = true;
}
}
void showData() {
if (newData == true) {
Serial.print("Data received ");
Serial.println(dataReceived);
newData = false;
}
}
I am only able to get output from rx serial monitor, because when I connect tx to usb I can't power rx. (told you I am noob)
Serial monitor output;
13:02:25.664 -> Data received Message 0
13:02:26.662 -> Data received Message 0
13:02:27.690 -> Data received Message 1
13:02:28.689 -> Data received Message 1
13:02:30.734 -> Data received Message 1
13:02:31.744 -> Data received Message 1
13:02:32.747 -> Data received Message 1
13:02:33.760 -> Data received Message 2
13:02:34.757 -> Data received Message 3
13:02:35.791 -> Data received Message 3
...
...
...
Your advices are most appreciated. Please forgive me if I posted anything wrong against the rules, I tried to be open and according to the rules.