MRF24J40 and Arduino

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

Published by

Gerald

Diplom-Informatiker (DH) in Darmstadt. Ich blogge über Entwicklung, Internet, mobile Geräte und Virtualisierung. Meine Beiträge gibt es auch bei Google+ und Facebook.

37 thoughts on “MRF24J40 and Arduino”

  1. 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 ???

      1. 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…

  2. 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 ?

    1. 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).

  3. 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.

  4. 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?

      1. 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)

      1. How can i send a value, i still not get it.

        mrf.send16(0x4202,”abcd”);

        it’s only send char.

  5. hi, I am using this library for MRF24J40MC, i turned PA/LNA on. But the range just about 2 meters. What wrong?

    1. 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.

      1. I encounter the same problem when using MRF24j40MC and the range is around 50cm. How to solve it? The coding problem?

  6. 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.

  7. 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?

  8. hello, i follow the steps for communication between mega2560 and uno, but the TX always failed, do you know why?

Leave a Reply

Your email address will not be published. Required fields are marked *