Arduino to Arduino Radio frequency (RF) communication

本文介绍如何使用Arduino Nano和Arduino Mega实现两个Arduino之间的无线射频(RF)通信。通过上传RF接收器代码到Arduino Nano,并将RF发射器代码上传到Arduino Mega,可以建立稳定的无线数据传输。使用VirtualWire库来处理数据包的发送与接收。

Arduino to Arduino Radio frequency (RF) communication

To set up this example I uploaded the RF receiver code to the Arduino Nano, and the RF transmitter code to the Arduino Mega (1280).

RF Receiver code

#include <VirtualWire.h>    // you must download and install the VirtualWire.h to your hardware/libraries folder
#undef int
#undef abs
#undef double
#undef float
#undef round

void setup()
{
    Serial.begin(9600);    

// Initialise the IO and ISR
    vw_set_ptt_inverted(true);    // Required for RX Link Module
    vw_setup(2000);                   // Bits per sec
    vw_set_rx_pin(4);           // We will be receiving on pin 4 i.e the RX pin from the module connects to this pin. 
    vw_rx_start();                      // Start the receiver 
}

void loop()
{
    uint8_t buf[VW_MAX_MESSAGE_LEN];
    uint8_t buflen = VW_MAX_MESSAGE_LEN;

    if (vw_get_message(buf, &buflen)) // check to see if anything has been received
    {
    int i;
     // Message with a good checksum received.
        
    for (i = 0; i < buflen; i++)
    {
        Serial.print(buf[i]);                     // the received data is stored in buffer
        }
    Serial.println("");
     }
}

RF Transmitter code

#include

<VirtualWire.h> // you must download and install the VirtualWire.h to your hardware/libraries folder

#undef int
#undef abs
#undef double
#undef float
#undef round

void setup()
{
     // Initialise the IO and ISR
    vw_set_ptt_inverted(true); // Required for RF Link module
    vw_setup(2000);                 // Bits per sec
    vw_set_tx_pin(3);                // pin 3 is used as the transmit data out into the TX Link module, change this as per your needs  
}

void loop()
{
    const char *msg = "Community of Robots";       // this is your message to send

   vw_send((uint8_t *)msg, strlen(msg));
   vw_wait_tx();                                          // Wait for message to finish
   delay(200);
}

Notes:

  • The received serial text will be represented as ASCII code. To convert it to character codes simply type cast it as (char). e.g.Serial.print((char)buf[i]);
  • The transmitter has a pin to attach an antenna wire although in my experiment it worked fine without it.

Resources

Tags:
  • rf
  • link
  • wireless
  • communication
  • 433mhz
Source:
1849hrs.txt
Published:
28-04-2013 18:49
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值