I have an ultrasonic sensor and a button at the transmitter and a buzzer at the receiver. Can both modules be used to turn on the buzzer if the data is sent at the same time?
All nRT24 devices are TRANCEIVERS! They transmit and receive, but not both at the same time. Certainly, you can prepare the data fro the sensor and the button switch on one end and transmit that data to the second nRF24. The code you write for both ends will need to handle their part of the project.
What I mean is the instructors told us to put a button in our project in case the ultrasonic is not working. But when the ultrasonic and button are both transmitting, can the receiver handle the data or does the transmitter can only handle one module at a time? Btw thank you for replying
Yes.
You don't need to send both data. You just need to send a command to make the buzzer sound. The transmitting Arduino needs to check if the button or the ultrasonic sensor is triggered, and in either case send a command to trigger the buzzer.
If you did want to send both data, you can of course do that. The "payload" transmitted & received can be anything you want, including data from many sensors.
The Nrf24 use a 32 byte packet of data that is sent and received. You can put anything you want in that packet of data.
When we tested it the receiver received both the distance and the buttonState but the buzzer only activates when the ultrasonic detects it and it doesn't work when we push the button
A simple coding error, not a limitation of any parts of the hardware.
Okay thank you
You're welcome.
You marked the topic as solved. Did you find the coding error so quickly?
If you need help finding the coding error, post the code on this topic. Read the forum guide in the sticky post first, do you don't break forum rules.
Transmitter:
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include "printf.h"
#define trigPin 5
#define echoPin 6
RF24 radio(7, 8);
// Radio pipe addresses for the 2 nodes to communicate.
const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
struct sensor
{
int id = 0;
long distance = 0;
char text[50] = "Recieved";
};
typedef struct sensor Sensor;
Sensor data;
void setup(void)
{
Serial.begin(57600);
printf_begin();
printf("\n\rRF24/examples/pingpair/\n\r");
printf("ROLE: %s\n\r", "S");
radio.begin();
radio.setRetries(15, 15);
radio.setPayloadSize(32);
radio.openWritingPipe(pipes[0]);
radio.openReadingPipe(1, pipes[1]);
radio.startListening();
radio.printDetails();
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop(void)
{
// First, stop listening so we can talk.
radio.stopListening();
radio.write(&data, sizeof(data));
Serial.print("\nPackage:");
Serial.print(data.id);
Serial.print("\n");
Serial.println(data.distance);
// Serial.println(data.text);
data.id = data.id + 1;
// data.distance = data.distance + 0.1;
// delay(lat);
long duration;
digitalWrite(trigPin, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin, HIGH);
delayMicroseconds(10); // Added this line
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
data.distance = (duration / 2) / 29.1;
}
Receiver:
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include "printf.h"
int relayPin = 2;
int buzzerPin = 3;
int buttonState = 0;
RF24 radio(7, 8);
const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
struct sensor
{
int id = 0;
long distance = 0;
// char text[50] = "empty";
};
typedef struct sensor Sensor;
Sensor data;
void setup(void)
{
Serial.begin(57600);
printf_begin();
printf("\n\rRF24/examples/pingpair/\n\r");
printf("ROLE: %s\n\r", "R");
radio.begin();
radio.setRetries(15, 15);
radio.setPayloadSize(32);
radio.openWritingPipe(pipes[0]);
radio.openReadingPipe(1, pipes[1]);
radio.startListening();
radio.printDetails();
pinMode(relayPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
}
void loop(void)
{
if (radio.available())
{
radio.read(&data, sizeof(data));
Serial.print("\nPackage:");
Serial.print(data.id);
Serial.print("\n");
Serial.println(data.distance);
// Serial.println(data.text);
if (data.distance > 0 && data.distance < 10) {
digitalWrite(relayPin, LOW);
Serial.println("Warning");
digitalWrite(buzzerPin, HIGH);
}
else {
digitalWrite(relayPin, HIGH);
Serial.println("STOP");
digitalWrite(buzzerPin, LOW);
}
}
}
This is our code for the ultrasonic and buzzer + relayPin, we want to add a button to it with the same function as the ultrasonic
Post that code.
it's above
Are you saying that the buzzer is not triggered because you have not written any code yet to read the button, transmit it's value, receive that value, check the value and trigger the buzzer?
We already had several tutorials we had follow on putting a button but we have failed many times. The button was transmitting, we can see it having the value 1 on the serial monitor and when we press it, it turns 0 but the buzzer won't activate
You are mistaken. The code above does not transmit any value read from a button.
We already deleted that code
Write it again. If it still does not work, post it here.
Transmitter:
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include "printf.h"
#define trigPin 5
#define echoPin 6
#define button 4
RF24 radio(7, 8);
// Radio pipe addresses for the 2 nodes to communicate.
const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
boolean buttonState = 0;
struct sensor
{
int id = 0;
long distance = 0;
char text[50] = "Recieved";
};
typedef struct sensor Sensor;
Sensor data;
void setup(void)
{
Serial.begin(57600);
printf_begin();
printf("\n\rRF24/examples/pingpair/\n\r");
printf("ROLE: %s\n\r", "S");
radio.begin();
radio.setRetries(15, 15);
radio.setPayloadSize(32);
radio.openWritingPipe(pipes[0]);
radio.openReadingPipe(1, pipes[1]);
radio.startListening();
radio.printDetails();
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(button, INPUT);
}
void loop(void)
{
// First, stop listening so we can talk.
radio.stopListening();
radio.write(&data, sizeof(data));
Serial.print("\nPackage:");
Serial.print(data.id);
Serial.print("\n");
Serial.println(data.distance);
// Serial.println(data.text);
data.id = data.id + 1;
// data.distance = data.distance + 0.1;
// delay(lat);
long duration;
digitalWrite(trigPin, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin, HIGH);
delayMicroseconds(10); // Added this line
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
data.distance = (duration / 2) / 29.1;
buttonState = digitalRead(button);
radio.write(&buttonState, sizeof(buttonState));
}
Receiver:
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include "printf.h"
int relayPin = 2;
int buzzerPin = 3;
RF24 radio(7, 8);
const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
struct sensor
{
int id = 0;
long distance = 0;
// char text[50] = "empty";
};
typedef struct sensor Sensor;
Sensor data;
void setup(void)
{
Serial.begin(57600);
printf_begin();
printf("\n\rRF24/examples/pingpair/\n\r");
printf("ROLE: %s\n\r", "R");
radio.begin();
radio.setRetries(15, 15);
radio.setPayloadSize(32);
radio.openWritingPipe(pipes[0]);
radio.openReadingPipe(1, pipes[1]);
radio.startListening();
radio.printDetails();
pinMode(relayPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
}
void loop(void)
{
if (radio.available())
{
radio.read(&data, sizeof(data));
Serial.print("\nPackage:");
Serial.print(data.id);
Serial.print("\n");
Serial.println(data.distance);
// Serial.println(data.text);
if (data.distance > 0 && data.distance < 10) {
digitalWrite(relayPin, LOW);
Serial.println("Warning");
digitalWrite(buzzerPin, HIGH);
}
else {
digitalWrite(relayPin, HIGH);
Serial.println("STOP");
digitalWrite(buzzerPin, LOW);
}
radio.read(&buttonState, sizeof(buttonState));
if (buttonState == HIGH) {
digitalWrite(buzzerPin, HIGH);
}
else {
digitalWrite(buzzerPin, LOW);
}
}
}
I would add button state to the data struct and only send one message.
pinMode(button, INPUT);
Is there an external pull down or pull up to keep the button from floating. More typical is to use INPUT_PULLUP and wire the button from an input to ground.