I am trying to build a wireless switch. At one end, I have Arduino Uno connected to Nrf24l01+pa+lna module. While at the other end there is Arduino Nano connected to Nrf24l01+pa+lna and a relay. I send char '1' or '0' through Nrf24l01 attached to Arduino Uno. Nrf24l01 attached to Arduino Nano receives this char and turns on or off the relay connected to Pin D3 of Arduino Nano. The my Arduino Nano replies back as '1' or '0'. Uno receives it and displays it on serial monitor. At start the communication sometimes worked and sometimes didn't. Then I connected a 10uF capacitor to the both Nrf24l01 modules. Now I am receiving 00 Hex and relay is also not responding. Can any one help. Code is attached.
// Arduino Uno Code
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8); // CE, CSN
const byte addresses[][6] = {"00001", "00002"};
char serialChar;
char motorChar;
void setup() {
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(addresses[1]); // 00002
radio.openReadingPipe(1, addresses[0]); // 00001
radio.setPALevel(RF24_PA_MIN);
}
void loop() {
radio.stopListening();
if (Serial.available() > 0) {
serialChar = Serial.read();
radio.write(&serialChar, sizeof(serialChar));
delay(5);
radio.startListening();
while (!radio.available());
radio.read(&motorChar, sizeof(motorChar));
Serial.print(motorChar);
}
}
//Arduino Nano Code
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8); // CE, CSN
const byte addresses[][6] = {"00001", "00002"};
char command;
int relayPin = 3;
void setup() {
radio.begin();
radio.openWritingPipe(addresses[0]); // 00001
radio.openReadingPipe(1, addresses[1]); // 00002
radio.setPALevel(RF24_PA_LOW);
pinMode(relayPin,OUTPUT);
digitalWrite(relayPin, HIGH); // Relay is Now Off
}
void loop() {
delay(5);
char state = '0'; // switch is off
radio.startListening();
if (radio.available()) {
radio.read(&command, sizeof(command));
if(command == '1'){
digitalWrite(relayPin,LOW); // Relay is Now On
state = command;
delay(5);
radio.stopListening();
radio.write(&state, sizeof(state));
}
else if (command == '0'){
digitalWrite(relayPin, HIGH); // Relay is Now Off
state = command;
delay(5);
radio.stopListening();
radio.write(&state, sizeof(state));
}
else{
delay(5);
radio.stopListening();
radio.write(&state, sizeof(state));
}
}
}
00 hex
and0
are the same numberradio.stopListening();