Teensy 3.1 nrf24l01+ issue

Status
Not open for further replies.

renatyv

New member
Code written for communication using nrf24l01+ works if I use two arduino UNOs.
But when I try to upload the same code for receiver to teensy3.1 using the same nrf24l01+ board, it does not work.
All I can see on serial monitor is "tick". No data is received.
"Sender" runs on Arduino UNO.
SPI library is standard arduino library.

Pin connection for arduino and teensy is the same:
controller ->nrf
gnd -> 1
vcc -> 2
9 -> 3
10 -> 4
11 -> 6
12 ->7
13 ->5
irq(8) is not connected(black wire)
All connections are tested.

Arduino IDE version 1.0.5
Teensy version 1.18rc2
Code and photos of connections are attached.
2014-01-21 23.00.11.jpg2014-01-21 23.03.32.jpg

Code for receiver:
Code:
#include <SPI.h>
#include "RF24.h"
/********** radio ************/

RF24 radio(9,10); // Set up nRF24L01 radio on SPI bus plus pins 9 & 10

const uint64_t reading_pipe = 0xF0F0F0F0E2LL;

void init_radio(void){
  radio.begin();
  radio.setRetries(1,0);
  radio.setCRCLength(RF24_CRC_16);
  radio.setAutoAck(false);
  radio.setPayloadSize(sizeof(int));
  radio.openReadingPipe(1,reading_pipe);
  radio.startListening();
}

#define RECEIVE_DELAY_MICROS 3300

void transfer_all_payloads(void){
  int data = 0;
  radio.read(& data,sizeof(int));
  Serial.println(data);
}

void setup(void)
{ 
  Serial.begin(115200);
  init_radio();
}

void loop(void)
{
  Serial.println("tick");
  if(radio.available()){
    transfer_all_payloads();
  }
  delay(1000);
}

Code for transmitter:
Code:
#include <SPI.h>
#include "RF24.h"

/************************* radio *****************/
RF24 radio(9,10); // Set up nRF24L01 radio on SPI bus plus pins 9 & 10
const uint64_t writing_pipe = 0xF0F0F0F0E2LL;

void init_radio(void){
  radio.begin();
  radio.setRetries(1,0);
  radio.setAutoAck(false);
  radio.setCRCLength(RF24_CRC_16);
  radio.setPayloadSize(sizeof(int));
  radio.openWritingPipe(writing_pipe);
}

void setup(void)
{
  Serial.begin(115200);
  init_radio();
}

void loop(void)
{ 
  for(int i=0;i<1000;i++){
    Serial.print("sending ");
    Serial.println(i);
    radio.startWrite(& i,sizeof(int));
    delay(1000);
  }
}
RF24 library:
View attachment nRF24L01.h
View attachment RF24_config.h
View attachment RF24.cpp
View attachment RF24.h
 
I tried to use libraries and sketches mentioned above.
Still does not work on Teensy 3.1.
I tried two nrf modules and two teensy 3.1, still no luck.
 
I've long wondered... those nF24L01's are low bit rate but very low power. At 2.4GHz, and with a crude on-PCB antenna, do these things achieve range such that a pair can deal with, say, 100 ft. range and 2 or so drywall-walls in the path?

The on-board PCB antennas can tend to be orientation sensitive due to their pattern.

This, versus a sub-GHz radio.
 
Hooray! I have finally found the error! :)
The problem is struct byte alignment is different on arduino and on teensy3. Consider structure
Code:
typedef struct{ //5 bytes
  unsigned long timestamp; //4 bytes
  uint8_t crc; //1 byte
} snapshot_t;
Since teensy is 32bit,
Code:
Serial.println(sizeof(snapshot_t));
returns 8 on teensy.
Arduinos are 8 bit, therefore
Code:
Serial.println(sizeof(snapshot_t));
returns 5 on arduino.
Solution:
Code:
#pragma pack(1)
typedef struct{ //5 bytes
  unsigned long timestamp; //4 bytes
  uint8_t crc; //1 byte
} snapshot_t;
 
Last edited:
Yes... common mistake when moving from 8 bit micros.

#pragma pack(1), on other C compilers, often has an equivalent but different syntax.

uint8_t long timestamp; //4 bytes
shouldn't that be: unsigned long timestamp;
 
Last edited:
You are wright. I fixed the code in the post.
I wonder why the compiler doesn't error on: uint8_t long timestamp;
It seems to ignore uint8_t and use long, making this a signed rather than unsigned. That can bite you later.
 
One gotcha I noticed with the #pragma pack(1) solution is that duff2013's enormously useful LowPower_Teensy3 lib defines a structure that works only because it isn't normally packed. Ye olde 32 bit processor and addressing word alignment complement to the structure packing ying-yang. You can fix it by adding padding 3 padding bytes in LowPower_Teensy3.h thusly:

Code:
typedef struct sleep_block_struct {
    /* Structure wake source */
    volatile uint32_t wake_source;
    /* Structure RTC Config */
    unsigned long rtc_alarm;
    /* Module Wake Type */
    uint32_t modules;
    /* Structure GPIO Config */
    uint16_t gpio_pin;
    uint16_t gpio_mode;
    /* Structure LPTMR Config */
    uint16_t lptmr_timeout;
    /* Structure TSI Config */
    uint16_t tsi_threshold;
    uint8_t tsi_pin;
    uint8_t pad1;
    uint8_t pad2;
    uint8_t pad3;
    /* pointer to callback function */
    void (*callback)();
    sleep_block_struct() : wake_source(0), rtc_alarm(0), modules(0),
                           gpio_pin(0), gpio_mode(0), lptmr_timeout(0),
                           tsi_threshold(0), tsi_pin(0), callback(NULL) {};
} sleep_block_t;

Or by moving elements in the structure around to word-align. I didn't play around with that.

This got me up and running with my nrf24 hibernating, waking, flinging packets, hibernating and whatnot.
 
On the RadioHead forum, Mike said that he's re-tested the Nordic nrf24 radios with Teensy 3.1. All OK he says.
Is there a PCB for using this with Teensy 3?

I don't care for the nrf24 radios - too crude. And for low volume telemetry, sub-GHz like HopeRF radio modules provide much better range and fade margins, and don't have to compete for air time with WiFi.
 
humm Then I think its time and space problem with my teensy and nrf24 which seems to work great with pro mini's and radiohead but not teensy3.1.
while I have checked every thing and has been doing this for 1week. Just one thing is left is to put this stuff under logic analyzer and to try the init().
which aarduino and teensyduino does mike Use and radiohead version?
Can I get that info...
 
Is there a PCB for using this with Teensy 3?

Yup, see here.

At 2.4GHz, and with a crude on-PCB antenna, do these things achieve range such that a pair can deal with, say, 100 ft. range and 2 or so drywall-walls in the path?

With the above and its crude pcb antenna I can transmit/receive at 30 feet, or ~10 m through one dry wall.
 
Last edited:
thanks.
You may know that the subGHz radios, at modest bit rates, yield about 100m line of sight. Inherent advantage of 433 or 868/915 (902-928) MHz vs. 2400MHz - less free space loss, less loss from path obstructions. Plus, the subGHz radios often are used with an onboard power amp for 60-100mW vs. typical 2.4GHz radios with 3-30mW. Low transmit duty cycle makes TX power consumption a moot point.

The relatively new, low cost Long Range (LoRa) radio modules use spread spectrum rather than FM, and use unusually long spreading codes and very low bit rates (like 100's) to get their long range in non-line-of-sight conditions. Most of these are subGHz, used for suburban and rural telemetry/SCADA.

FYI: a standard two-layer drywall (wallboard) wall, at 2.4GHz, is about 5dB of attenuation. An exterior wall, non-masonry, is much more, e.g., 15dB. Masonry is more yet. There's a NIST report on attenuation vs. frequency for common building materials. A subfloor of plywood in a residence is like 10-20dB at 2.4GHz. A commercial building may have poured concrete over a steel pan subfloor and those are bad news for RF.
 
Last edited:
Appreciate the info on db losses. For my purposes of indoor use, small footprint and low power are the key benefits. And the longest distance in my house is ~40 feet, so it is just adequate for my usage. With the shorter wavelength, doesn't the antenna need to be muuuch longer?

Sorry, got the units wrong, the nRF24L01+ reception is about 10 m through dry walls, or about 30 feet.
 
Last edited:
I had tested with nrf24 PA +LNA and one with crude antenna in ground open space I got it to work at 45m approx. I hit a wall But I think I could go further with that range.
if I Just use two simple nrf24 I am just able to reach 3-5 meters and its fails multiple time when something. So I got it to work at 250kbps it seems to increase range but not too much. I have not tried to measure things with scales .. But it a good idea to do that once I setup everything again. nrf24 with PA +LNA increases range a lot.
Next test should be like
1 normal NRF24 and other nrf24 PA+LNA test
and both with nrf24 PA+LNA test
I think there is video on youtube for range test of nrf24.
 
Opinion alert:
As I recall the nrf24 w/o PA is about 1mW. too puny. Even w/PA it is what, 10 or 20mW?
2.4GHz is the land of broadband and wee little low speed devices are much better off in sub-GHz.
 
Last edited:
Status
Not open for further replies.
Back
Top