So, after some trial and error I managed to interface the MRF24J40 modules with two Arduinos and have them exchange data.
This looks more complicated than it is. On the breadboard are two wireless transceiver modules, each one connected to an Arduino and an LED as a rudimentary status display. The Arduino communicates with the module over three SPI pins and three pins for reset, chip select and interrupt.
Pin mapping:
MRF | Arduino | ||
---|---|---|---|
GND | 1 | GND | |
RESET | 2 | 6 | |
INTR | 4 | 2 | Interrupt 0 |
SDI | 5 | 11 | MOSI |
SCK | 6 | 13 | SCK |
SDO | 7 | 12 | MISO |
CS | 8 | 7 | |
VIN | 10 | 3V3 |
This represents the pin layout from the Arduino Nano, on the Mega the SPI pins are a little different. MOSI, MISO and SCK are on pins 51, 50 and 52 instead of 11, 12 and 13.
As a bonus I connected pin 9 (NC) to a LED, figuring that “not connected” in the data sheet means that this can be used as an indicator if the module has a connection. It seems that I was right, the LED lights up when I connect the Arduino to power and turns off shortly after initializing, when the other module is available. Shortly after turning one device off the LED on the other device turns on again, so I guess it detected the connection loss.
Software
I used the Basic_TwoWay example from the library and changed just a few things:
const int pin_reset = 6; const int pin_cs = 7; const int pin_interrupt = 2;
I kept the default values for pin_reset and pin_cs but changed the pin_interrupt to 2 (which is the default interrupt 0 on both of my Arduinos.
mrf.set_pan(0xcafe); // This is _our_ address mrf.address16_write(0x4201);
I kept the PAN name 0xCAFE and just gave the devices different addresses. One stayed with the address 0x4201, the other got 0x4202. I don’t have much experience with these devices yet, but I guess you can define those address pretty freely, you only have to make sure every one is unique and they all use the same PAN id.
mrf.send16(0x4202, "abcd");
This is the last line I changed, making sure that 0x4201 sends to 0x4202 and vice versa.
Here is my working code, but I’d check the github repository for updated versions: MRF24J40MA_test001.ino
Here is the output from the serial console:
txxxing... TX went ok, got ack received a packet 15 bytes long ASCII data (relevant data): abcd LQI/RSSI=115/255
“abcd” is the transmitted data, LQI is a link quality indicator, RSSI is a signal strength indicator. I found a pretty good explanation about LQI and RSSI on the Texas Instruments support site.
Helpful resources
- MRF24J40 Datasheet
- mrf24j40ma Arduino library on github
- Comments in this post from the author of the library
- Hardware descriptions of Arduino Nano and Arduino Mega
Whee! it worked! Always glad when it works for people 🙂
As i know, arduino Mega works in 5V voltage and MRF24J40 module in 3.3V.
But here the two modules are directly connected !!
this can’t burn our MRF24j40 module ???
The Arduino Mega (actually most bigger versions) have an 5V and an 3.3V pin. I’m using the 3.3V pin.
aha !! and that’s enough ? great!
because i saw on net, they were always talking about the need to use some 5v / 3.3v converter (not only with Vin but mainly with INT, MOSI, MISO) otherwise the module burns…
You still have 5V on the data lines (SCK, MOSI, CS).
Hi !
why it doesn’t work on arduino due ??
I used SPI pins since it isn’t like mega on 50, 51, 52 for MOSI, MISO and SCK. and i thought it’s the same for everything else.
But the program is uploaded on arduino and nothing happens.
have you an idea how can i solve this ?
D50, D51, D52 on board Arduino Mega. Not pin 50,51,52 on ATmega. Ok
I have Arduino Pro Mini. It work with Arduino Pro Mini ?
I don’t have an Arduino Pro Mini to try it, but I don’t see why not (at least with the 3.3V version, I don’t know if the MRF module can handle 5V, I wouldn’t try it).
you try it. Communication is how far?
I didn’t do any range tests.
Ohm, tks, I’ll doing
Hello You
– I have a question. SPI not select by ss (pin 7 on board), INT0(pin 2) active or deactive ?
– Because I need connect Arduino with mrf24j40ma and RFID RC522 . But all used SPI.
SDI – > MISO , SDO -> MOSI or SDI -> MOSI , SDO -> MISO
You have to connect the Input of one to the Output of the other. It’s listed in the table in the article.
Where connect pin Wake (mrf24j40ma) ?
I didn’t need it so I didn’t connect it.
Please , You can post schematic. I made it not worked
Everything I have is already in the blog post.
In your pin-out you connect MRF pin 4 to Arduino Nano pin 2.
Looking at the Arduino Nano pin 2 it is the RX pin. Is this correct?
Pin 2 and 3 are external interrupts, RX and TX are on pins 0 and 1 respectively. Take a look at the documentation of the Arduino Nano.
Sorry but this is unclear to me. I did look at the documentation. If I look at the Atmega168 pin 2 is D4 and pin 3 is GND.
If I look at the Arduino board they show two rows of pins numbered 1 to 15, there are no pin 0, pin 2 on the left is D0/RX and pin 2 on the right is GND (looking at Arduino manual page 5)
Looking at page 2 of the same manual I now see the Arduino with pins numbered 1 – 30 and pin 2 is D0/RX
Can you send other kind of data, like a reading of a sensor?
It’s a serial connection, you can transfer everything you want.
How can i send a value, i still not get it.
mrf.send16(0x4202,”abcd”);
it’s only send char.
How is all the sketch for the Arduino Mega 2560?. Thanks
and, is the same for the two Arduino, or we need some other command in the receiver for get the data?
hi, I am using this library for MRF24J40MC, i turned PA/LNA on. But the range just about 2 meters. What wrong?
Are you using the antena in receptor and transmissor? If yes don’t do that. Use the antena only in the receptor. Than you get more range, about 10 meters. I’m using the antena of 5dbi in the receptor module, but i will try use one of 15dbi to see if i can get a better range.
I encounter the same problem when using MRF24j40MC and the range is around 50cm. How to solve it? The coding problem?
I try use code complie MRF24J40 + arduino nano same U.but it not work.
#include
#include
const int pin_reset = 6;
const int pin_cs = 10; // default CS pin on ATMEGA8
const int pin_interrupt = 2; // default interrupt pin on ATMEGA8
Mrf24j mrf(pin_reset, pin_cs, pin_interrupt);
long last_time;
long tx_interval = 1000;
void setup() {
Serial.begin(9600);
mrf.set_pan(0xcafe);
// This is _our_ address
mrf.address16_write(0x6001);
attachInterrupt(0, interrupt_routine, CHANGE); // interrupt 0 equivalent to pin 2 on ATMEGA8
last_time = millis();
interrupts();
}
void interrupt_routine() {
mrf.interrupt_handler(); // mrf24 object interrupt routine
}
void loop() {
mrf.check_flags(&handle_rx, &handle_tx);
unsigned long current_time = millis();
if (current_time – last_time > tx_interval) {
last_time = current_time;
Serial.println(“txxxing…”);
mrf.send16(0x4202, “abcd”);
}
}
void handle_rx() {
// data to receive, nothing to do
}
void handle_tx() {
if (mrf.get_txinfo()->tx_ok) {
Serial.println(“TX went ok, got ack”);
} else {
Serial.print(“TX failed after “);Serial.print(mrf.get_txinfo()->retries);Serial.println(” retries\n”);
}
}
output
txxxing…
txxxing…
txxxing…
txxxing…
but led in tx arduino nano will blink everytime.
Please you can help me.
Thank you.
Hi Gerald,
Could you tell us what kind of connection it is? I see that you have used the Karl Pallson library ?
Could you tell us a short description about the library, about the connection and how the TX and RX communicate ? I know that is 802.15.4 standard, but i don’t understand how the communication is implemented. I asked Karl and he said that is definitely not zigbee.
If this connection is not zigbee, what kind of connection is ? Have you used coordinator, end device, pan coordinator according to the Karl’s library?
Sorry, no idea. I just used the library, I didn’t care for the details.
hello, i follow the steps for communication between mega2560 and uno, but the TX always failed, do you know why?
Hello I have problem with the rx buffer… I not understand how flush it. Thanks