Communication


Arduino Uno + nRF24L01

This week I used the nRF24L01 wireless transceiver. A pair is about $7. Easily compatible with Arduino. Just had to install the RF24 Library to make it work.

The arduino uno on the right was used to transmit data. While a button was pressed, a red LED on the breadboard lit up, and the arduino looped from 0-255 continuously and sent each number to the receiving module.

#include < nRF24L01.h>
#include < RF24.h>
#include < RF24_config.h>
#include < SPI.h>

int msg[1];
RF24 radio(9,10);
const uint64_t pipe = 0xE8E8F0F0E1LL;
const int button2Pin = 3; 
const int ledPin =  2;   
 
void setup(void){
  pinMode(button2Pin, INPUT);

  pinMode(ledPin, OUTPUT);     
  
  Serial.begin(9600);
  radio.begin();
  radio.openWritingPipe(pipe);
}
 
void loop(void){
  int button2State;  
  button2State = digitalRead(button2Pin);
  
  if (button2State == LOW)
  {
    digitalWrite(ledPin, HIGH);  // turn the LED on
    for (int x=0;x<255;x++){
    msg[0] = x;
    radio.write(msg, 1);
    }
  }
  else
  {
    digitalWrite(ledPin, LOW);  // turn the LED off
  }
}

The receiving module listened for the numbers sent from the transmitting module. If it received data, a yellow LED lit up, else, it would just turn on a red LED.

#include < nRF24L01.h>
#include < RF24.h>
#include < RF24_config.h>
#include < SPI.h>

int msg[1];
RF24 radio(9,10);
const uint64_t pipe = 0xE8E8F0F0E1LL;
int red = 3;
int green = 5;
int redNeg = 4;
int greenNeg = 6;
int lastmsg = 1;
 
void setup(void){
  Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(1,pipe);
  radio.startListening();
  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(redNeg, OUTPUT);
  pinMode(greenNeg, OUTPUT);
  digitalWrite(greenNeg, LOW);
  digitalWrite(redNeg, LOW);
}
 
void loop(void){
  if (radio.available()){
    bool done = false;  
      
    while (!done){
      done = radio.read(msg, 2); 
      if (msg[0] != lastmsg +1){
        digitalWrite(red, HIGH);
        digitalWrite(green,LOW);
        }    
      else {
       digitalWrite(red,LOW);
       digitalWrite(green,HIGH); 
        }
      lastmsg = msg[0];
      Serial.println(msg[0]);
     }
    }
    else {
      digitalWrite(red, HIGH);
      digitalWrite(green,LOW);
    }
}