Issues using nrf24l01 and MAX7219 on same SPI bus

Hello,

I have been trying for a while now to get an nrf24l01 and MAX7219 to work together over the same SPI bus to drive a seven segment display. I've been able to get both halves of the system to work independently, but I am unable to get them to work together.

Working nrf24l01 example:
Transmitter

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

RF24 radio(7, 8);                   // Create an instance of the RF24 class, with CE and CSN pin numbers
const byte addresses[][6] = {"00001", "00002"};
const int buttonPin = 2;             // Button pin connected to digital pin 2
int numberToSend = 0;                // Number to be sent
int receivedNumber = 0;

void setup() {
  pinMode(buttonPin, INPUT);  // Set button pin as input with internal pull-up resistor
  radio.begin();                     // Initialize the radio module
  radio.openWritingPipe(addresses[0]);   // 00001 Set the address of the receiving module
  radio.openReadingPipe(1, addresses[1]);  // 00002
  radio.setPALevel(RF24_PA_LOW);      // Set the power level (optional)
  Serial.begin(9600);                 // Initialize Serial communication
}

void loop() {
  if (digitalRead(buttonPin) == HIGH) {       // Check if the button is pressed
    receivedNumber++;                          // Increment the number to be sent
    radio.stopListening();
    radio.write(&receivedNumber, sizeof(receivedNumber));  // Send the number
    Serial.print("Sent: ");
    Serial.println(receivedNumber);

    delay(500);                              // Debounce delay
  }

  radio.startListening();

  if (radio.available()) {
    Serial.print("Value Recieved: ");
    radio.read(&receivedNumber, sizeof(receivedNumber));  // Receive the number from the transmitting module
    Serial.println(receivedNumber);
  }

  
}

Reciever

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

RF24 radio(8, 10);                   // Create an instance of the RF24 class, with CE and CSN pin numbers
const byte addresses[][6] = {"00001", "00002"};
const int buttonPin = 7;             // Button pin connected to digital pin 2
int receivedNumber = 0;              // Received number

void setup() {
  pinMode(buttonPin, INPUT);  // Set button pin as input with internal pull-up resistor
  radio.begin();                     // Initialize the radio module
  radio.openWritingPipe(addresses[1]);   // 00002 Set the address of the transmitting module
  radio.openReadingPipe(1, addresses[0]);   // 00001
  radio.setPALevel(RF24_PA_LOW);      // Set the power level (optional)

  Serial.begin(9600);                 // Initialize Serial communication
}

void loop() {
  if (digitalRead(buttonPin) == HIGH) {       // Check if the button is pressed
    receivedNumber++;                        // Increment the received number
    radio.stopListening();
    radio.write(&receivedNumber, sizeof(receivedNumber));  // Send the number back
    Serial.print("Sent back: ");
    Serial.println(receivedNumber);

    delay(500);                              // Debounce delay
  }

  radio.startListening();

  if (radio.available()) {
    Serial.print("Value Recieved: ");
    radio.read(&receivedNumber, sizeof(receivedNumber));  // Receive the number from the transmitting module
    Serial.println(receivedNumber);
  }
}

Working MAX7219 example:

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

LedControl lc = LedControl(11, 13, 9, 1);   // Create an instance of the LedControl class, with DIN, CLK, and CS pin numbers
const int buttonPin = 7;             // Button pin connected to digital pin 7
int number = 0;

void setup() {
  pinMode(buttonPin, INPUT);  // Set button pin as input with internal pull-up resistor
  lc.shutdown(0, false);              // Enable the MAX7219 chip
  lc.setIntensity(0, 10);              // Set the brightness level (adjust as needed)
  lc.clearDisplay(0);                 // Clear the 7-segment display
  lc.setDigit(0,1, (byte)0, false);
  lc.setDigit(0,0, (byte)0, false);
  Serial.begin(9600);                 // Initialize Serial communication
}

void displayNumber(uint8_t number) {
  Serial.println("Displaying Number: ");
  Serial.println(number);
  lc.setDigit(0, 1, number / 10, false);     // Display tens digit
  lc.setDigit(0, 0, number % 10, false);     // Display ones digit
}

void loop() {
  if (digitalRead(buttonPin) == HIGH) {       // Check if the button is pressed
    number++;
    if (number > 99){
      number=0;
    }
    displayNumber(number);
    delay(250);
  }
}

Problem code using nrf24l01 and MAX7219 together:
Reciever

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

RF24 radio(8, 10);                   // Create an instance of the RF24 class, with CE and CSN pin numbers
const byte addresses[][6] = {"00001", "00002"};
LedControl lc = LedControl(11, 13, 9, 1);   // Create an instance of the LedControl class, with DIN, CLK, and CS pin numbers
const int buttonPin = 7;             // Button pin connected to digital pin 2
int receivedNumber = 0;              // Received number

void setup() {
  pinMode(buttonPin, INPUT);  // Set button pin as input with internal pull-up resistor
  lc.shutdown(0, false);              // Enable the MAX7219 chip
  lc.setIntensity(0, 4);              // Set the brightness level (adjust as needed)
  lc.clearDisplay(0);                 // Clear the 7-segment display
  lc.setDigit(0,1, (byte)0, false);
  lc.setDigit(0,0, (byte)0, false);
  radio.begin();                     // Initialize the radio module
  radio.openWritingPipe(addresses[1]);   // 00002 Set the address of the transmitting module
  radio.openReadingPipe(1, addresses[0]);   // 00001
  radio.setPALevel(RF24_PA_LOW);      // Set the power level (optional)
  Serial.begin(9600);                 // Initialize Serial communication
}

void displayNumber(uint8_t number) {
  Serial.println("Displaying Number");
  lc.setDigit(0, 1, number / 10, false);     // Display tens digit
  lc.setDigit(0, 0, number % 10, false);     // Display ones digit
}

void loop() {
 
  if (digitalRead(buttonPin) == HIGH) {       // Check if the button is pressed
    receivedNumber++;                        // Increment the received number
    displayNumber(receivedNumber);            // Display the number on the 7-segment display
    radio.stopListening();
    radio.write(&receivedNumber, sizeof(receivedNumber));  // Send the number back
    Serial.print("Sent back: ");
    Serial.println(receivedNumber);

    delay(500);                              // Debounce delay
  }

  radio.startListening();

  if (radio.available()) {
    Serial.print("Value Recieved: ");
    radio.read(&receivedNumber, sizeof(receivedNumber));  // Receive the number from the transmitting module
    Serial.println(receivedNumber);
    
    displayNumber(receivedNumber);                        // Display the received
  }
}

Any ideas what could be causing the modules to be working fine separately, but not together? The best I can tell is that the nrf24 is not letting go of the CLK pin and preventing use of the MAX7219.

Thanks in advance for your help.

That is definitely not the case, SPI CLK is provided by the master, which is the Arduino.

The MAX7219 can be driven via SPI, but is not a SPI device.

Can you post a schematic of how you have wired these two devices up to the SPI bus.

They need different pins for the chip enable lines of each device.

Hi Mike,

I don't have a good schematic readily available, but the connections are as follows:

nrf24l01 to Arduino Uno:

  • GND to GND
  • VCC to 3.3V
  • CE to DIG8
  • CSN to DIG10
  • SCK to DIG13
  • MOSI to DIG11
  • MISO to DIG12
  • IRQ to None
  • 47microF capacitor between GND and VCC

MAX7219* to Arduino Uno:

  • DIN to DIG11
  • GND to GND
  • Load (CS) to DIG9
  • DOUT to None
  • V+ to 5V
  • ISET to 5V over 10K Ohm resistor
  • CLK to DIG13
    *I left out the connections to the Digits and Segments here as they are independent of the connection to the Arduino

So, my main SPI connections are:

  • CLK as DIG13
  • MOSI as DIG11
  • MISO as DIG12
  • CS(nrf) as DIG10
  • CS(MAX) as DIG9

Please note the Transmitter code in my original post uses slightly different pins due to being connected to an Arduino Mega instead of an Uno.

The chip select pin on each device must be a different Arduino pin. You have them using the same pin. This means that they can never work in the same piece of code.

The chip select should normally be high, and is taken low to activate the device just before you send it data, and put back high when you have finished talking to it.

No wonder they will not work in the same sketch.

As to a schematic you must always draw one before you begin to construct anything. Because only then do you know what you are supposed to be doing. Will never get anywhere in electronics if you do not draw a schematic first.

Mike, thanks for the advice. I’ll make sure to work from a schematic going forward.

I’m still a bit confused though. I was under the impression that the CS pins were the “chip select”. Therefore I thought they were on separate pins (DIG10 for the nrf and DIG9 for the MAX). Is that incorrect?

Hi

I don't see a problem at all. MAX7219 is not a SPI device, so it not require to connect to SPI CLK and MOSI pins

the "LedControl" Library from Eberhart is not using SPI but a software bitbang.
I have an adopted MAX7219 which can be used with HW SPI.

Arduino Noiasca LED Control with print method (MAX7219/MAX7221) (rothschopf.net)

just follow the HW SPI example

#include <NoiascaLedControlSpi.h>
const uint8_t LEDCS_PIN = 8;           // 8 LED CS or LOAD -  8 CS
LedControlSpi lc = LedControlSpi (LEDCS_PIN, LED_MODULES); // Hardware SPI

It should work with other SPI devices on the same bus (I'm using a SPI Ethernet in several projects).

1 Like

It is very interesting would it works with NRF24 module at the same pins, because NRF seems not to be a "true" SPI device too.
I think it would be more easy use a separate pins for them.

well, I can only confirm tests with SPI devices like the wiznet w5100 and the Hoperf RFM95W.
So a test with an nrf might be interesting (depending what that library is really using...)

You are correct.

No that is correct, it is just the confusing way you labeled your wiring list. This would not have been confusing if you would have posted a schematic as requested from you.

Thank you all for your feedback. I tried b707's suggestion and simply ran the max7219 off separate pins from the nrf24 and everything is working perfectly.

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