4

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:

  1. 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.
  2. 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:

enter image description here

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);
    }  
  }
}
10
  • 1
    Crystal is an electronics component where you need to have certain knowledge about electronic design, you can't just plug in a crystal and hoping it will work. Different crystals have different load capacitances (this will involving reading the datasheet of the crystal) and need some calculation to get the right load capacitors in order for it to work properly.
    – hcheung
    Jun 24, 2023 at 7:36
  • 1
    I am not sure how did you find that I put crystal resonator (oscillator) in the circuit without knowing what I am doing! Each electronic component has datasheet that everyone read it before using them. I used crystal with 22pF capacitor as stated in its datasheet and as suggested in Atmega328PU datasheet.
    – Jon depoy
    Jun 24, 2023 at 19:01
  • 1
    Well, if you search for 8MHz crystal, you will noticed that different crystal has different load capacitance. You need to read the datasheet of your crystal, not the datasheet of the MCU, then you need to do some calculation based on load capacitance datasheet told you to come out the correct capacitors for the particular crystal. BTW, If I remember correctly ATmega328 datasheet give a range of 12-22pF, depend on the crystal you are using.
    – hcheung
    Jun 25, 2023 at 0:39
  • 1
    The bare ATmeaga328P Fritzing diagram has an error in that only one leg of the right hand crystal load capacitor is connected to anything.
    – 6v6gt
    Jun 25, 2023 at 8:35
  • I am assuming that your barebone Atmega328PU can run simple sketches e.g. LED blink?
    – Fahad
    Jun 25, 2023 at 9:11

1 Answer 1

0

Assuming a basic test of the MCU works, say the blink sketch works at normal speed (0.5 Hz), try this format of the constructor:

RF24 radio(7, 8, 4000000); // set SPI speed to 4MHz instead of default 10MHz

The default of 10MHz maybe too high for your MCU configuration.

From https://github.com/nRF24/RF24/blob/master/COMMON_ISSUES.md

Some more things to try:

EDIT

Most Nrf24L01 problems are related to an inadequate power supply. Some 3.3 volt power supplies, say those of a Nano where the 3.3 volts is derived from the USB/UART adaptor, don't work well here. You could try adding two capacitors across the power rails at the point on the breadboard where the power for the nrf24L01 is taken. Use a 100nF ceramic and a 10 to 100uF electrolytic. You barebones atmega328p should anyway have its own decoupling capacitor as close as possible to the chip's power pins. Use a 100nF ceramic.

The link above has other trouble shooting measures.

2
  • 1
    Tried didn't work.
    – Jon depoy
    Jun 26, 2023 at 19:56
  • @Jondepoy I've added some more suggestions in the answer.
    – 6v6gt
    Jun 27, 2023 at 4:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.