How to Program Attiny84 to Use nrf24l01 Module with Arduino IDE

I want to create a drumkit that transmits MIDI signals wirelessly, to some MIDI processing device, say, a Mac or an iPhone. Arduinos are powerful and easy enough to get started with, but I want my project to be more portable and task specific, so I decide to shrink it down to an attiny84 powered board.

Lucky as I am, I found this Optimized High Speed NRF24L01 library by TMRh20, which is powerful and easy to use, and also supports Attiny series microcontrollers.

As a start-with-Arduino electronic enthusiast, I don’t have much coding experience. I tried to write pure C code and compile it with Makefile, but to no avail. Then I put my eyes on Arduino IDE, leaving optimisation and such stuff for future consideration. In order to have any success, I have to:

  1. Install ATtiny support within Arduino IDE because it doesn’t have that built-in natively, following this tutorial;
  2. Install NRF24L01 library.

That seems like a breeze, but when I connected everything as NRF24L01 library suggests, and burned some test sketch to the Attiny84 board, nothing happened. Oops… I had no idea what failed or where to debug. I googled around for quite some time, and after some frustration and impatience, I found this ATtiny84 Pin Issue #236. Finally I got the problem: incompatible pin map in the ATtiny support library and NRF24L01 library.

The issue page basically provides two methods: 1) switch to another arduino-tiny support library hosted on google, or 2) edit pin definitions in RF24.cpp of the NRF24L01 library. Method 1) does not work for me. Because I want my project to have as low latency as possible, I chose an external crystal oscillator running at 20MHz, which is the maximum frequency Attiny84 supports, but the jeelabs’ arduino-tiny does not have that option. So I stick with damellis version, and change a few lines of RF24.cpp to

 

To test if it works, I have this setup:

-> An Attiny84(damellis’ library compatible pinout) board as an RF24 transmitter, running this sketch,
-> An Arduino UNO turned into an MIDI device as an RF24 receiver, connected to a Mac with a USB cable, running this sketch,
-> A Mac running GarageBand.

LED blinks. Voila!

4 thoughts on “How to Program Attiny84 to Use nrf24l01 Module with Arduino IDE

  1. L.S,

    I read your article and I wondered what the line RF24 radio(2, 3); in your script meant. I.E. I would like to know which hardware pins you used on the 84.

    Regards

    Ad Huiberts

    1. Hi,

      Thanks for the reply.

      If you would like to check the above mentioned RF library(http://tmrh20.github.io/RF24/classRF24.html) out, you’ll see the definition RF24 (uint8_t _cepin, uint8_t _cspin), this is just to initialize RF24 library, using arduino equivalent pin numbers, NO.2 pin as ce pin, NO.3 pin as cs pin. According to the pinout map(https://curisious.com/wp-content/uploads/2016/12/damellis_compatible_attiny_pinout.png), NO.2 pin is pin 11 on the 84 board, NO.3 pin is pin 10 on 84 board.

      FYI, I connect 84-board-number 7(bottom left pin) to MISO, 8(bottom right pin) to MOSI, 9 to SCK, 10 to CS, 10 to CE sockets of the NRF24L01 module.

      Ted

  2. Code works great. Thanks for the assist.
    Below is a barebones code, RF24.cpp fix, and pinout with the tiny84 receiving.
    The RF24.cpp is in the documents/arduino/libaries/RF24 folder.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    #include “RF24.h”
    /*
    Fix RF24.cpp
    # define DI 6 // PA6
    # define DO 5 // PA5
    # define USCK 4 // PA4
    # define SS 7 // PA7
    *
    * ATTINY84 Connections
    * physical logical Function-NRF24
    * 7 6 MISO
    * 8 5 MOSI
    * 9 4 SCK
    * 10 3 CS
    * 11 2 CE
    */

    RF24 radio(2, 3);
    const int led = 0;
    byte addresses[][6] = {“1Node”,”2Node”};
    void setup()
    {
    pinMode(led, OUTPUT);
    radio.begin();
    radio.setRetries(15, 15);
    radio.setPALevel(RF24_PA_LOW);
    radio.openWritingPipe(addresses[0]);
    radio.openReadingPipe(1,addresses[1]);
    radio.startListening();
    }

    void loop()
    {
    int remoteclick1;
    if(radio.available())
    {
    while(radio.available())
    {
    radio.read(&remoteclick1, sizeof(int));
    }
    if(remoteclick1==12345)
    {
    digitalWrite(led, HIGH);
    delay(50);
    digitalWrite(led, LOW);
    delay(50);
    digitalWrite(led, HIGH);
    delay(50);
    digitalWrite(led, LOW);
    delay(500);
    }
    }
    }

    ~~~~~~~~~~~~and Nano code xmit~~~~~~~~~~~~~~~~
    #include
    #include “RF24.h”
    RF24 radio(7,8);
    byte addresses[][6] = {“1Node”,”2Node”};
    void setup()
    {
    Serial.begin(9600);
    radio.begin();
    radio.setRetries(10, 15);
    radio.setPALevel(RF24_PA_LOW);
    radio.openWritingPipe(addresses[1]);
    radio.openReadingPipe(1,addresses[0]);
    radio.stopListening();
    }
    void loop()
    {
    int Button1 = 12345;
    if (radio.write( &Button1, sizeof(int) ))
    {
    Serial.println(“Ack”);//Acknowledge remote nrf received.
    }
    }

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.