I am trying to fix the problem of communicating between two nRF24L01+ together, one connected to an Arduino Uno and another connected to an ATmega328PU with an 8 MHz external crystal.
The bootloader on the ATmega328PU is “Arduino Pro or Pro Mini” 3.3 V, 8 MHz.
The problem is that the nRF24L01+ on the ATmega328PU is not responding (working). I am using the RF24 library as shown in the code below.
There are several things that I have already tried:
- To make sure that both nRF24L01+ modules work, I connected one to an Arduino Uno and another to an Arduino Mega and they work perfectly. So, the problem is not with the nRF modules.
- I changed the external crystal of the ATmega328PU to 16 MHz and changed the bootloader to Arduino Uno; didn’t work again.
The circuit diagram is shown below:
Transmitter code:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00001";
const int btn = 4;
int buttonState = 0;
int programState = 0;
void setup() {
radio.begin();
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_MIN);
radio.stopListening();
pinMode(btn, INPUT);
}
void loop() {
buttonState = digitalRead(btn);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if ((buttonState == LOW) && (programState == 0)) {
programState = 1;
}
else if ((buttonState == HIGH)&& (programState == 1)) {
const char text[] ="Hello World"; // you can customize this text to your wish
radio.write(&text, sizeof(text));
programState = 0;
Serial.println("BUTTON");
delay(1000);
}
}
Receiver code:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00001";
const int output = 2;
void setup() {
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(0, address);
radio.setPALevel(RF24_PA_MIN);
radio.startListening();
pinMode(output,OUTPUT);
digitalWrite(output, LOW);
delay(1000);
}
void loop() {
if (radio.available()) {
char text[32] = "";
radio.read(&text, sizeof(text));
Serial.println(text);
if (strcmp(text,"Hello World")==0) {
Serial.println("CORRECT");
digitalWrite(output, HIGH);
delay(2000);
digitalWrite(output, LOW);
delay(2000);
}
}
}
