All post of ElecFreaksGPRS/GPS/RF Wireless

nRF24L01 with Arduinio’s SPI Library

By June 11, 2011 11 Comments

Hi Guys, We have received some mails for asking if we will provide the nRF24L01‘s hardware SPI demo or library, which we just provided the software SPI before. So we modified the code used SPI library which you can get from Arduino’s standard library.

The frist change is the connection between pins and Arduino. The old mode is:

GND – GND, VCC – 3.3V, CS – D8, CSN – D9, SCK – D10, MOSI – D11, MISO – D12, IRQ – D13

If you want to use Arduino’s SPI library, you should connect them as below:

GND – GND, VCC – 3.3V, CS – D8, CSN – D9, SCK – D13,  MOSI – D11, MISO – D12, IRQ – D10

Define about CS, IRQ and CSN could be changed by yourself, the other pins you can not change because of the Arduino’s board provide.

The second, let us show you where the code was changed, it was very easy.

Main modefied is :


unsigned char SPI_RW(unsigned char Byte)
{
  unsigned char i;
  for(i=0;i<8;i++)                      // output 8-bit
  {
    if(Byte&0x80)
    {
      SPI_PORT |=MOSI;                  // output 'unsigned char', MSB to MOSI
    }
    else
    {
      SPI_PORT &=~MOSI;
    }
    SPI_PORT|=SCK;                      // Set SCK high..
    Byte <<= 1;                         // shift next bit into MSB..
    if(SPI_IN & MISO)
    {
      Byte |= 1;                        // capture current MISO bit
    }
    SPI_PORT&=~SCK;                     // ..then set SCK low again
  }
  return(Byte);                         // return read unsigned char
}

to


unsigned char SPI_RW(unsigned char Byte)
{
  return SPI.transfer(Byte);
}

Oh, So clear.

At last, maybe you like this code mode, however there are some defects also,  it’s just for Arduino but other microprocessors.

The softwore SPI is more in common use other development platform.

OK, this just is a sample code for your reference.

Download the Full Code..  (include SPI_rf24L01_TX and  SPI_rf24L01_RX)

Link to nRF24L01_Demo_For_Arduino..  (software SPI mode)

VN:F [1.9.13_1145]
Rating: 7.3/10 (6 votes cast)
VN:F [1.9.13_1145]
Rating: 0 (from 2 votes)
nRF24L01 with Arduinio's SPI Library, 7.3 out of 10 based on 6 ratings
ElecFreaks

Author ElecFreaks

More posts by ElecFreaks

Join the discussion 11 Comments

  • weitz says:

    Hi,

    Is there anyone can help me? I got a lot of compiling error as below (SPI.h, API.h and nRF24L01.h are already included) :

    Arduino:1.6.7 (Windows 7), “Arduino/Genuino Uno”

    nRF24L01Send:9: error: ‘SPI_DIR’ was not declared in this scope
    nRF24L01Send:13: error: ‘init_io’ was not declared in this scope
    nRF24L01Send:14: error: ‘SPI_Read’ was not declared in this scope
    nRF24L01Send:18: error: ‘TX_Mode’ was not declared in this scope
    nRF24L01Send:26: error: ‘tx_buf’ was not declared in this scope
    nRF24L01Send:27: error: ‘SPI_Read’ was not declared in this scope
    nRF24L01Send:30: error: ‘SPI_RW_Reg’ was not declared in this scope
    nRF24L01Send:31: error: ‘tx_buf’ was not declared in this scope
    nRF24L01Send:31: error: ‘SPI_Write_Buf’ was not declared in this scope
    nRF24L01Send:35: error: ‘SPI_RW_Reg’ was not declared in this scope
    nRF24L01Send:36: error: ‘tx_buf’ was not declared in this scope
    nRF24L01Send:36: error: ‘SPI_Write_Buf’ was not declared in this scope
    nRF24L01Send:38: error: ‘SPI_RW_Reg’ was not declared in this scope

    thank you!

    [Reply]

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: +1 (from 1 vote)
  • NRF24L01 ,NRF24L01 Module Demo For Arduino says:

    […] nRF24L01 with Arduinio’s SPI Library This entry was posted in User Guide and tagged Arduino, NRF24L01 by ietchenxiaotao. Bookmark the permalink. […]

  • NRF24L01 says:

    […] nRF24L01 with Arduinio’s SPI Library This entry was posted in HOT SALE, New Products, Specials Products. Bookmark the permalink. […]

  • […] Todos os meus módulos são testados com este demo (versão software SPI) ou com este (versão hardware SPI). Antes de testar com outros, teste com eles. Notar que as duas versões tem […]

  • dandrade says:

    Here code result! SOLVE! ERROR COMPILING:

    sketch_mar29a.cpp: In function ‘void setup()’:
    sketch_mar29a.cpp:10:3: error: ‘SPI_DIR’ was not declared in this scope
    sketch_mar29a.cpp:10:15: error: ‘CE’ was not declared in this scope
    sketch_mar29a.cpp:10:26: error: ‘CSN’ was not declared in this scope
    sketch_mar29a.cpp:11:17: error: ‘IRQ’ was not declared in this scope
    sketch_mar29a.cpp:14:11: error: ‘init_io’ was not declared in this scope
    sketch_mar29a.cpp:15:39: error: ‘SPI_Read’ was not declared in this scope
    sketch_mar29a.cpp:19:11: error: ‘TX_Mode’ was not declared in this scope
    sketch_mar29a.cpp: In function ‘void loop()’:
    sketch_mar29a.cpp:27:9: error: ‘tx_buf’ was not declared in this scope
    sketch_mar29a.cpp:28:43: error: ‘SPI_Read’ was not declared in this scope
    sketch_mar29a.cpp:31:28: error: ‘SPI_RW_Reg’ was not declared in this scope
    sketch_mar29a.cpp:32:21: error: ‘WR_TX_PLOAD’ was not declared in this scope
    sketch_mar29a.cpp:32:33: error: ‘tx_buf’ was not declared in this scope
    sketch_mar29a.cpp:32:40: error: ‘TX_PLOAD_WIDTH’ was not declared in this scope
    sketch_mar29a.cpp:32:54: error: ‘SPI_Write_Buf’ was not declared in this scope
    sketch_mar29a.cpp:36:28: error: ‘SPI_RW_Reg’ was not declared in this scope
    sketch_mar29a.cpp:37:21: error: ‘WR_TX_PLOAD’ was not declared in this scope
    sketch_mar29a.cpp:37:33: error: ‘tx_buf’ was not declared in this scope
    sketch_mar29a.cpp:37:40: error: ‘TX_PLOAD_WIDTH’ was not declared in this scope
    sketch_mar29a.cpp:37:54: error: ‘SPI_Write_Buf’ was not declared in this scope
    sketch_mar29a.cpp:39:16: error: ‘WRITE_REG’ was not declared in this scope
    sketch_mar29a.cpp:39:39: error: ‘SPI_RW_Reg’ was not declared in this scope

    [Reply]

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  • Pina says:

    The code’s gone! Don’t bother clicking or using anything on this page unfortunately. Khunts!

    [Reply]

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: -1 (from 1 vote)
  • Chubby says:

    BION I’m ipmressed! Cool post!

    [Reply]

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  • Alexavier says:

    That’s more than snelsibe! That’s a great post!

    [Reply]

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  • Gump says:

    Hey, that’s the gretaset! So with ll this brain power AWHFY?

    [Reply]

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  • […] nRF24L01 with Arduinio’s SPI Library […]

Leave a Reply