1

I am trying to get the NRF24 Modules to work but all I get is short lived moments of when it works. I am using the transmitter to send signals to the receiver, which in turn sends a signal to the Motor Controller module to spin the motor.

Both the transmitter and receiver are connected on Nano Every's. Currently they both Arduinos are powered with 9v batteries with the NRF24 modules connected to the 3.3v pin. Both NRF24 modules have 10uf caps connected. I double checked for connections issues and shorts.

Sometimes the Rx and Tx connect and the motor spins correctly and everything is fine. This lasts only a few moments before it stops responding again. Usually it happen when I connect the USB to the transmitter to see what the Serial console is printing. Once I open up the Serial monitor it might, if I am lucky, connect and work for a bit.

I don't have any reason to believe that it is a connection issue since it sporadically works as intended. I can post a diagram if needed. If anyone has any clue I'd appreciate the help.

Here is my Tx code:

#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>
#include <SPI.h>

const byte ledPin = 5;

RF24 radio(7,8); //CE, CNS
const byte address[6] = "00001";
const int button = 3;
int buttonPressed;  


int joystick;


void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_MAX);
radio.setDataRate(RF24_250KBPS);
radio.stopListening(); //transmitter

pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
delay(300);
digitalWrite(ledPin, LOW);
delay(300);  
digitalWrite(ledPin, HIGH);
delay(300);  
digitalWrite(ledPin, LOW);
radio.printDetails();
  
}

void loop() {
  
  // put your main code here, to run repeatedly:
 
  
  joystick = analogRead(A1);
  Serial.println(joystick);

  if(joystick > 530)
  {
    motorForward(joystick); 
  }
  if(joystick < 480)
  {
    motorBackward(joystick);
  }
  if(joystick >= 480 && joystick <= 530)
  {
    motorStill();
  }

  
}


void motorBackward(int jsInput)
{
 //digitalWrite(motorPin1, LOW);
 //digitalWrite(motorPin2, HIGH);
 int motorSpeed = map(jsInput, 480, 0, 0, 255);
 int message[] = {2, motorSpeed};
 radio.write(&message, sizeof(message));
 //analogWrite(motorPWM, motorSpeed);

 
 delay(20);
 
}

void motorForward(int jsInput)
{

 int motorSpeed = map(jsInput, 530, 1023, 0, 255);
 int message[] = {1, motorSpeed};
 radio.write(&message, sizeof(message));

 delay(20);
 
 

 
}

void motorStill()
{
   
    int message[] = {0};
    radio.write(&message, sizeof(message));
}

Here is my Rx code:

#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>
#include <SPI.h>
#define motorPWM 6

RF24 radio(7,8); //CE, CNS
const byte address[6] = "00001"; 

byte motorPin1 = 4;
byte motorPin2 = 5;

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(0,address);
radio.setPALevel(RF24_PA_MAX);
radio.setDataRate(RF24_250KBPS);
radio.startListening();

 pinMode(motorPin1, OUTPUT);
 pinMode(motorPin2, OUTPUT);
 pinMode(motorPWM, OUTPUT);

 //test motor 
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPWM, 600);
Serial.print("Test motor started");
delay(2000);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin1, LOW);
Serial.print("Test motor complete");
radio.printDetails();


}

void loop() {
  
  // put your main code here, to run repeatedly:
  
if(radio.available())
{
   //0 = still, 1 is forward and 2 is backward
  int message[2];
  Serial.println("radio detected");
  radio.read(&message, sizeof(message));
  Serial.println(message[0]);
  Serial.println(message[1]);
  
  
   
  if(message[0] == 1)
  {
    
    digitalWrite(motorPin1, HIGH);
    digitalWrite(motorPin2, LOW);
    analogWrite(motorPWM, message[1]);
    delay(20);
    Serial.println("move forward");
  }

  if(message[0] == 2)
  {
    digitalWrite(motorPin1, LOW);
    digitalWrite(motorPin2, HIGH);
    analogWrite(motorPWM, message[1]);
    delay(20);
  }

  if(message[0] == 0)
  {
    digitalWrite(motorPin1, LOW);
    digitalWrite(motorPin2, LOW);
    delay(20);
  }

  Serial.println("radio available");
  
  
}
  
}
2

0

Your Answer

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

Browse other questions tagged or ask your own question.