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.