NRF2401 Freezing code

Hi all

Hope someone can help as this is doing my nut in.

So I decided on a humidity/temp sensor for my garden along with a pool temperature probe.

That all works great, I can even get it into a very low power mode, which again is great as I wanted to be able to leave this in the garden for months/years on a set of batteries.

The problem comes when I add in the NRF2401 Wireless Transmitter.

Now I have been playing with these before and it all worked grand, I`m loading up a similar sketch and it just does not work, I think I isolated the problem but cant work out why its causing an issue as its actually needed.

I have the code turn on pins 3,4 and 5 and they supply the power to the sensors and RF TX, and switch off again when the readings have been read and sent over the TX.

At least that`s the theory, if you look down near the bottom of the code I have the transmit data

//transmit.write(&data, sizeof(data));

If I un-comment it, nothing is sent obviously but more importantly the sensors stop reading and nothing is updated, its like this line of code is freezing the rest, but I have no idea why.

Any help in identiying the issue greatfully appreciated.

Cheers Alan

#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>                                // RF Library
#include <RF24_config.h>#include <OneWire.h>
#include <DallasTemperature.h>                 // Pool Sensor
#include "Seeed_BME280.h"                        // BME280 Library
#include "LowPower.h"
#include <avr/power.h>
#include <avr/sleep.h>

#define DATA_PIN 2
#define SENSOR_RESOLUTION 12
#define SENSOR_INDEX 0

OneWire oneWire(DATA_PIN);
DallasTemperature sensors(&oneWire);
DeviceAddress sensorDeviceAddress;
BME280 bme280;

RF24 transmit (7, 10);                           //create RF24 object called transmit
byte address [5] = "00001";                      //set address to 00001

struct package
{

  float temperature = 0;
  float humidity = 0;
  float pool = 0;
  
};

typedef struct package Package;
Package data;

void setup() {

  pinMode(3, OUTPUT); // pool sensor
  pinMode(4, OUTPUT); // NRF TX
  pinMode(5, OUTPUT); // BME280
  digitalWrite(3, HIGH);
  digitalWrite(4, HIGH);
  digitalWrite(5, HIGH);


  Serial.begin(9600);
  bme280.init();
  sensors.begin();
  sensors.getAddress(sensorDeviceAddress, 0);
  sensors.setResolution(sensorDeviceAddress, SENSOR_RESOLUTION);

  transmit.begin();
  transmit.openWritingPipe(address);            //open writing pipe to address 00001
  transmit.setPALevel(RF24_PA_MIN);             //set RF power output to maximum
  transmit.setDataRate(RF24_250KBPS);           //set datarate to 250kbps
  transmit.setChannel(100);                     //set frequency to cdata.data.data.humidityumidityumidityannel 100
  transmit.stopListening();
}


void loop() {
 
  pinMode(3, OUTPUT); // pool sensor
  pinMode(4, OUTPUT); // NRF TX
  pinMode(5, OUTPUT); // BME280
  digitalWrite(3, HIGH);
  digitalWrite(4, HIGH);
  digitalWrite(5, HIGH);

  sensors.requestTemperatures();


  float temperatureInCelsius = sensors.getTempCByIndex(SENSOR_INDEX);

  data.temperature = bme280.getTemperature();
  data.humidity = bme280.getHumidity();
  data.pool = sensors.getTempCByIndex(SENSOR_INDEX); 


  Serial.print("Pool: ");
  Serial.print(temperatureInCelsius, 2);
  Serial.print("C ");
  Serial.println();

  Serial.println();
  Serial.print("Temp: ");
  Serial.print(bme280.getTemperature());
  Serial.println("C");
  Serial.println();

  Serial.print("Humidity: ");
  Serial.print(bme280.getHumidity());
  Serial.println("%");
  Serial.println();

  //transmit.write(&data, sizeof(data));

  delay(2000);

  pinMode(3, INPUT); // pool sensor
  pinMode(4, INPUT); // NRF TX
  pinMode(5, INPUT); // BME280
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);

  delay(2000);

  LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
  LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
}
void loop() {
 
  pinMode(3, OUTPUT); // pool sensor
  pinMode(4, OUTPUT); // NRF TX
  pinMode(5, OUTPUT); // BME280
  digitalWrite(3, HIGH);
  digitalWrite(4, HIGH);
  digitalWrite(5, HIGH);

Why are you setting the mode of the pins on every pass through loop()? What, exactly, are you using to detect a pool?

  float temperatureInCelsius = sensors.getTempCByIndex(SENSOR_INDEX);

  data.temperature = bme280.getTemperature();
  data.humidity = bme280.getHumidity();
  data.pool = sensors.getTempCByIndex(SENSOR_INDEX);

Do you REALLY need to call getTempCByIndex() twice?

  pinMode(3, INPUT); // pool sensor
  pinMode(4, INPUT); // NRF TX
  pinMode(5, INPUT); // BME280
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);

Now, they are input pins? With the pullup resistors turned off?

  LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
  LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);

Are you sure you don't need to do this 6 or 7 more times?

I'm setting the pins to make sure that they go high and stay high till the very end of the script.

Then I can ensure the 2 sensors, 1, pool thermometer, 2, BME280 temp and humidity sensor, and 3, the NRF transmitter actually switch off power at the end, without the command for the pins to become input and go low, the pins remained high and none of the peripherals turned off.

And these questions are great, but not solving my issue of that one line of TX code freezing everything

//transmit.write(&data, sizeof(data));

Ozric13:
I have the code turn on pins 3,4 and 5 and they supply the power to the sensors and RF TX, and switch off again when the readings have been read and sent over the TX.

Are you using the IO pins to supply power to the sensors and the NRF module? Don't do that, it is a terrible idea.

I am, it's works perfectly, my only issue is the NRF code

Even if I remove the sensors from the pins, I still get the freeze with that line of code

Have you tried any of the simple getting started examples from the RF24 library? Do they work?
What happens when you try to printDetails()?

printDetails()?

What will that show me?

Yes looked at examples and got a little lost, this code is from another sketch and was working flawlessly, so I don't understand now why I'm getting these issues

Ozric13:
I am, it's works perfectly, my only issue is the NRF code

If I understand this response to Reply #3 then it reads as if you are using an Arduino I/O pin to provide power to the Vcc pin of the nRF24 module.

I see two things wrong with that

  • The I/O pin will be at 5v and the nRF24 needs a max of 3.6v
  • An I/O pin cannot provide enough current for an nRF24.

...R
Simple nRF24L01+ Tutorial

Ozric13:
printDetails()?

What will that show me?

That'll let you know that communication between the Arduino and the NRF module is working correctly.

Ozric13:
Yes looked at examples and got a little lost, this code is from another sketch and was working flawlessly, so I don't understand now why I'm getting these issues

My gut feeling is that the NRF module is miswired, or because it is powered so poorly powered it is browning out whenever you try to transmit with it.
Either way, it isn't communicating back to the Arduino correctly so when you try to have it transmit it doesn't respond. I've had a similar issue in the past when I had a loose connection on the MISO line between NRF module and Arduino.

EDIT: Or definitely this-

Robin2:

  • The I/O pin will be at 5v and the nRF24 needs a max of 3.6v

I've also just seen that you haven't included the SPI library in your sketch, that is likely to cause some problems.

without the command for the pins to become input and go low, the pins remained high and none of the peripherals turned off.

Leaving the pins as OUTPUT (set ONCE in setup), and turning the pins off WILL make the pins go LOW. If that doesn't happen, you've fubared the hardware.

Hi Robin, it's a 3.3v pro mini and can sink 40Ma per IO pin, so I don't see that's an issue.

Specs wise on transmit the NRF uses 10 to 15mA so again I see no reason why I can't power the external hardware on the pins directly, if I was shifting 1amp then of course would use a relay, mosfet or whatever.

Paul, I tested the script using LEDs instead of the sensors and NRF, no damage has occurred to the pins, they are switching on and off quite nicely with the script, I only changed the pins to input and off after the data was sent, just telling the pins to go low, nothing happened so the sensors and NRF were still powered on.

BJ I haven't used the SPI library as it was not in the original sketch, what is this used for?

Ozric13:
Specs wise on transmit the NRF uses 10 to 15mA so again I see no reason why I can't power the external hardware on the pins directly,

Is "it will not work when powered from pins" reason enough?

It’s much easier to start with a setup (and code) that has been proven to work by many people and modify it (step-by-step) into your custom project than it is to start with your non-working, custom configuration and make it work.

Ozric13:
Hi Robin, it's a 3.3v pro mini and can sink 40Ma per IO pin, so I don't see that's an issue.

Get the first example in my Tutorial working in the conventional way first.

When it works you can experiment with a different power source.

...R

Ozric13:
BJ I haven't used the SPI library as it was not in the original sketch, what is this used for?

It is required for the NRF24 library to work. Look at any of the example sketches that come with the library.