Arduino send values wirelessly using RF module
This method can be used to send and receive numeric values or integers over a short distance from one Arduino microcontroller to another.
Refer: RF ASK wireless communication; includes – Radiohead library installations, Tx and Rx circuit & code to send data as a character array.
Here the variable that the value is assigned is an unsigned 16-bit integer, this value can be easily used for further calculations or to apply in any functions as the data type is an integer.
Refer to the page of Radiohead library which details about sending sensor data using ASK RF module.
In the given transmitter circuit, the variable terminal of the potentiometer is connected to the Analog read pin A0 of the Arduino. The transmitter sends the instantaneous analog read values as serial binary data by the method of amplitude shift keying. It is then received and displayed in the serial monitor of the receiver Arduino. This is an useful option to wirelessly send values like sensor readings, measurements, output values, etc. Also, multiple receivers in the transmission range can simultaneously receive these values.
Transmitter code
#include <RH_ASK.h> #include <SPI.h> int state = 0; RH_ASK driver; void setup() { driver.init(); } void loop() { uint16_t data = analogRead(A0); driver.send((uint8_t *)&data, sizeof(data)); driver.waitPacketSent(); delay(200); }
In the above code, just assign the variable named “data”with any value or integer that needs to be sent.
Receiver code
#include <RH_ASK.h> #include <SPI.h> RH_ASK driver; void setup() { Serial.begin(9600); driver.init(); } void loop() { uint16_t data; uint8_t datalen = sizeof(data); if (driver.recv((uint8_t*)&data, &datalen)) { Serial.println(data); } }
Here the transmitter and receiver continuously send and receive the data. The code can be simply modified to different functions, like changing the time delay to get reading between fixed intervals of time, adding conditions to send readings only when reaching a threshold value, etc.
Hi,
at receiver side , i need analog voltage, could you help me in this. i want to transmit analog value and need analog voltage at receiver side
Reply
I have done this and it’s working fine, thanks.
RH_ASK.h causes compiling error in my codes
What is the error message? Have you installed the RadioHead library properly?
Can I send or receive negative numbers?
Not as a number but you can send as `-1` char array. At the transmitter and receiver end you could convert between int and char.
how to send 2 analog values from 2 potentiometers
1. Add another analog input pin declaration.
2. Read the values from both analog pins.
3. Send both values as part of the data packet.
4. In this modified receiver code, define an array ‘data’ to store the two analog values received from the transmitter.
Replace the transmitter and receiver void loop with the below codes,
//Transmitter
void loop()
{
// Read analog values from two different pins
uint16_t data1 = analogRead(A0);
uint16_t data2 = analogRead(A1);
// Send both analog values as part of the data packet
// Concatenate the two values into one array
uint16_t dataToSend[2] = {data1, data2};
// Send the data packet
driver.send((uint8_t *)&dataToSend, sizeof(dataToSend));
driver.waitPacketSent();
// Add a suitable delay before reading and sending the next values
delay(200);
}
//Receiver
void loop()
{
uint16_t data[2];
uint8_t datalen = sizeof(data);
// Check if there is data available to receive
if (driver.recv((uint8_t *)&data, &datalen))
{
if (datalen == sizeof(data))
{
// Data received successfully, extract the two analog values
uint16_t data1 = data[0];
uint16_t data2 = data[1];
// Print the received values
Serial.print(“Analog Value 1: “);
Serial.print(data1);
Serial.print(“, Analog Value 2: “);
Serial.println(data2);
}
else
{
// Data length mismatch, discard the packet and print an error message
Serial.println(“Received data length mismatch!”);
}
}
}