0% found this document useful (0 votes)
52 views4 pages

NRF RX TX

The document contains code for a simple wireless communication system using RF24, consisting of a transmitter (TX) and a receiver (RX). The TX sends a message every second, while the RX listens for incoming messages and displays them. Additionally, there are examples of basic LED control in response to signal transmission on both TX and RX sides.

Uploaded by

conceptronix
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views4 pages

NRF RX TX

The document contains code for a simple wireless communication system using RF24, consisting of a transmitter (TX) and a receiver (RX). The TX sends a message every second, while the RX listens for incoming messages and displays them. Additionally, there are examples of basic LED control in response to signal transmission on both TX and RX sides.

Uploaded by

conceptronix
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

#define CE_PIN 9

#define CSN_PIN 10

const byte slaveAddress[5] = {'R','x','A','A','A'};

RF24 radio(CE_PIN, CSN_PIN); // Create a Radio

char dataToSend[10] = "Message 0";


char txNum = '0';

unsigned long currentMillis;


unsigned long prevMillis;
unsigned long txIntervalMillis = 1000; // send once per second

void setup() {

Serial.begin(9600);

Serial.println("SimpleTx Starting");

radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.setRetries(3,5); // delay, count
radio.openWritingPipe(slaveAddress);
}

//====================

void loop() {
currentMillis = millis();
if (currentMillis - prevMillis >= txIntervalMillis) {
send();
prevMillis = millis();
}
}

//====================

void send() {

bool rslt;
rslt = radio.write( &dataToSend, sizeof(dataToSend) );
// Always use sizeof() as it gives the size as the number of bytes.
// For example if dataToSend was an int sizeof() would correctly return 2

Serial.print("Data Sent ");


Serial.print(dataToSend);
if (rslt) {
Serial.println(" Acknowledge received");
updateMessage();
}
else {
Serial.println(" Tx failed");
}
}
//================

void updateMessage() {
// so you can see that new data is being sent
txNum += 1;
if (txNum > '9') {
txNum = '0';
}
dataToSend[8] = txNum;
}

RX

// SimpleRx - the slave or the receiver

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

#define CE_PIN 9
#define CSN_PIN 10

const byte thisSlaveAddress[5] = {'R','x','A','A','A'};

RF24 radio(CE_PIN, CSN_PIN);

char dataReceived[10]; // this must match dataToSend in the TX


bool newData = false;
TX
//===========

void setup() {

Serial.begin(9600);

Serial.println("SimpleRx Starting");
radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.openReadingPipe(1, thisSlaveAddress);
radio.startListening();
}

//=============

void loop() {
getData();
showData();
}

//==============
void getData() {
if ( radio.available() ) {
radio.read( &dataReceived, sizeof(dataReceived) );
newData = true;
}
}

void showData() {
if (newData == true) {
Serial.print("Data received ");
Serial.println(dataReceived);
newData = false;
}
}

1ST WORKING RX

const int ledPin = 3; // Pin for LED on RX


const int rxPin = 7; // Pin to receive simulated transmission from TX

void setup() {
pinMode(ledPin, OUTPUT); // LED output on RX
pinMode(rxPin, INPUT); // Pin to receive transmission signal

digitalWrite(ledPin, LOW); // RX LED is OFF by default


}

void loop() {
int receivedSignal = digitalRead(rxPin);

// If signal received from TX (HIGH), blink RX LED


if (receivedSignal == HIGH) {
digitalWrite(ledPin, HIGH); // Turn on LED
delay(100); // Keep LED on for a short time
digitalWrite(ledPin, LOW); // Turn off LED briefly
delay(100); // Delay for the blink effect
} else {
digitalWrite(ledPin, LOW); // Keep RX LED OFF when no signal
}

delay(100); // Delay for a stable reading


}

1ST WORKING TX

const int switchPin = 2; // Pin for SPST switch


const int ledPin = 3; // Pin for LED on TX
const int txPin = 7; // Pin to simulate transmission (connected to RX side)

void setup() {
pinMode(switchPin, INPUT); // Switch input
pinMode(ledPin, OUTPUT); // LED output on TX
pinMode(txPin, OUTPUT); // Pin to simulate transmission

digitalWrite(ledPin, LOW); // LED is OFF by default


digitalWrite(txPin, LOW); // No transmission signal by default
}

void loop() {
int switchState = digitalRead(switchPin);

// If switch is pressed, blink TX LED and send HIGH signal to simulate


transmission
if (switchState == HIGH) {
digitalWrite(ledPin, HIGH); // Turn on TX LED
digitalWrite(txPin, HIGH); // Send HIGH signal to RX
delay(100); // Keep LED on for a short time
digitalWrite(ledPin, LOW); // Turn off TX LED
delay(100); // Keep LED off for a short time
}
// If switch is not pressed, keep TX LED off
else {
digitalWrite(ledPin, LOW); // Turn off TX LED
digitalWrite(txPin, LOW); // Send LOW signal to RX
}

delay(100); // Debounce delay


}

You might also like