In your projects, you might find cases in which you want transfer data wirelessly between two arduinos. You can do this via bluetooth, IR, LORA and many different ways.
In this article, we'll be exploring a pretty cheap option which is the FS1000A transmitter and the XY-MK-5V receiver. It's good for very short range wireless data communication.
We'll be working with two arduino nanos and transferring data from one to another.
So we know that it worked:
It's a pretty simple synchronization but it should be enough to walk you through the basics and go on to do much more fancy things with the module.
The operating range of this module is about 3 meters without an antenna so it's not well suited for long range applications. However, it can be well suited for creating a simple remote
This module uses a technique called Amplitude Shift Keying (ASK). When the transmitter receives:
It simply works on an ON / OFF basis and it's simplicity is one of the reasons it is a cheap module. However as a downside, it is susceptible to interference and background noise.
To setup the FS1000a transmitter:
To setup the XY-MK-5V receiver:
You'll notice that the XY-MK-5V has two data pins. These two data pins are the same so it doesn't matter which you connect to.
It might be worth mentioning that when the XY-MK-5V was driven by the Nano hooked to a USB source, it wasn't suffient enough so it requires a dedicated power source.
Both the transmitter and receiver will use the RadioHead library. You'll want to download the zip, unzip and place it into your Arduino libraries directory.
Upload the transimitter.ino code to your transmitter nano connected to the FS1000a.
At a highlevel, we use initialize the RadioHead Ask Library (RH_ASK) and set it to transmit at pin 11 and receive at pin 12 (although this Nano won't be receiving anything).
Every 300ms, the transmitter alternates between sending an on signal and off signal and further, turn the buildin LED on and off.
You'll notice that in each send call, in addition to the on / off state (represented a 1 or 0), we also send a pairKey which is another 8 bit value between 0 and 255. This isn't required but is used to as an identifier for this transmitter. In this case, we have set this transmitter to pairKey 0.
If you had 20 other transmittter and wanted to distinguish them on the receiver, you can change this pairKey for each transmitter and use it know which message came from which transmitter.
// RadioHead ASK library #include <RH_ASK.h> #include <SPI.h> // Set pin 12 as the receiver // Set pin 11 as the transmitter RH_ASK driver(2000, 12, 11); void setup() { digitalWrite(LED_BUILTIN, LOW); // Initialize RadioHead ASK driver.init(); } void loop() { // This is basically to distinguish other radio // signals. 8-bit value between (0-255). uint8_t pairKey = 0; uint8_t onSignal = 0; uint8_t offSignal = 1; uint8_t on[] = {pairKey, onSignal}; uint8_t off[] = {pairKey, offSignal}; // RadioHead requires the pointer to the array as a parameter, so obtain the pointer. const uint8_t *onData = on; const uint8_t *offData = off; // Alternate between on and off. driver.send( (uint8_t *)onData, 2); driver.waitPacketSent(); digitalWrite(LED_BUILTIN, HIGH); delay(300); driver.send( (uint8_t *)offData, 2); driver.waitPacketSent(); digitalWrite(LED_BUILTIN, LOW); delay(300); }
// RadioHead ASK library #include <RH_ASK.h> #include <SPI.h> // Set pin 12 as the receiver // Set pin 11 as the transmitter RH_ASK driver(2000, 12, 11); void setup() { digitalWrite(LED_BUILTIN, LOW); // Setup Serial Monitor // Serial.begin(9600); // Initialize RadioHead ASK driver.init(); } void loop() { // Create an empty array of 8-bit variables that is used to store the received data. // Should be the same size as the message. uint8_t data[2]; uint8_t dataLength = sizeof(data); if (driver.recv(data, &dataLength)) { // For debugging. // Serial.print("Message Received: "); // Serial.println(data[0]); // Serial.println(data[1]); uint8_t pairKey = 0; uint8_t onSignal = 0; uint8_t offSignal = 1; uint8_t receivedPairKey = data[0]; // On or off. uint8_t statusSignal = data[1]; // We the pair keys match. if (data[0] == pairKey) { if (statusSignal == offSignal) { digitalWrite(LED_BUILTIN, LOW); } if (statusSignal == onSignal) { digitalWrite(LED_BUILTIN, HIGH); } } } }
Upload the transimitter.ino code to your transmitter nano connected to the XY-MK-5V.
On the receiver end, we check for any signals received. If the pairKey matches, we respond to the on / off signal by turning the buildin LED on and off.
If successful, you should see your two nanos blinking their LED at the same time!
I'm a senior front-end engineer by day and electronics inventor by night. I setup this site to share my explorations, discoveries and learnings with you.
Feel free to mail me if you have any questions or feedback.