Sending Messages Using Low-Cost Wireless Modules
You want to transmit data between two Arduino boards using simple, low-cost wireless modules.
This recipe uses simple transmit and receive modules such as the SparkFun 315 MHz: WRL-10535 and WRL-10533, or the 434 MHz: WRL-10534 and WRL-10532.
Wire the transmitter as shown in Figure 14-1 and the receiver as in Figure 14-2. Some modules have the power line labeled VDD instead of Vcc.
Figure 14-1 Simple wireless transmitter using VirtualWireFigure 14-2 Simple wireless receiver using VirtualWire
The transmit sketch sends a simple text message to the receive sketch, which echoes the text to the Serial Monitor. The transmit and receive sketches use the VirtualWire library written by Mike McCauley to provide the interface to the wireless hardware. The library can be downloaded fromhttp://www.open.com.au/mikem/arduino/VirtualWire-1.5.zip:
The receive sketch also uses the VirtualWire library:
The VirtualWire library defaults to pin 12 for transmit and pin 11 for receive, but see the documentation link at the end of this recipe if you want to use different pins.
Setup initializes the library. The
loop code simply calls a
send function that calls the library
vw_send and waits for the message to be transmitted.
The receive side initializes the library receive logic and then waits in
loop for the message.
vw_get_message will return
true if a message is available, and if so, each character in the message is printed to the Serial Monitor.
The VirtualWire library handles the assembly of multiple bytes into packets, so sending binary data consists of passing the address of the data and the number of bytes to send.
The sending sketch that follows is similar to the transmit sketch in this recipe’s Solution, but it fills the message buffer with binary values from reading the analog input ports using
analogRead. The size of the buffer is the number of integers to be sent multiplied by the number of bytes in an integer (the six analog integer values take 12 bytes because each
int is two bytes):
sizeof operator is used to determine the number of bytes in an
The receive side waits for messages, checks that they are the expected length, and converts the buffer back into the six integer values for display on the Serial Monitor:
The Serial Monitor will display the analog values on the sending Arduino:
Bear in mind that the maximum buffer size for VirtualWire is 30 bytes long (the constant
VW_MAX_MESSAGE_LEN is defined in the library header file).
Wireless range can be up to 100 meters or so depending on supply voltage and antenna and is reduced if there are obstacles between the transmitter and the receiver.
Also note that the messages are not guaranteed to be delivered, and if you get out of range or there is excessive radio interference some messages could get lost. If you need a guaranteed wireless delivery mechanism, the ZigBee API used in recipes that follow is a better choice, but these inexpensive modules work well for tasks such as displaying the status of Arduino sensors—each message contains the current sensor value to display and any lost messages get replaced by messages that follow.