433Mhz无线电收发模块是非常便宜而且是最简单的无线收发装置,你可以把它想象成一根虚拟的导线,可以传递信号。

本文为爱鱼客原创内容,未经作者及本站允许请勿转载至任何其他平台!

相比于蓝牙和WiFi收发装置(如ESP8266)相比,433Mhz无线电收发模块,如上图,是由两个模块组成的,分别是发射器(Transmitter)和接收器(Receiver),它只能单向发送信号。但是它的优点就是相比于蓝牙而言,发射的距离非常远,且穿墙能力也很好;相比于WiFi模块相比,它更加省电和高效。因此无线电收发非常适合单向的信息传送以及对耗能有要求的设备,比如设备使用电池当做电源,那么WiFi可能让你的设备用不到一天就能把电池耗尽。

433mhz-rf-wireless

Library库

VirtualWire库下载:http://yuns.co/f.php?k=dffaf

电路连接

上图左边是发射器(Transmitter),右边是接收器(Receiver);两个板上各有一个孔可以连接天线,在发射器上有标出“ANT”。

发射器:

  • DATA → D12
  • VCC → 5V
  • GND → GND

接收器:

  • GND → GND
  • DATA(中间两个都是DATA,完全相同) → D12
  • VCC → 5V

Arduino 代码:

发射器(Transmitter)

// transmitter.pde
//
// Simple example of how to use VirtualWire to transmit messages
// Implements a simplex (one-way) transmitter with an TX-C1 module
//
// See VirtualWire.h for detailed API docs
// Author: Mike McCauley (mikem@airspayce.com)
// Copyright (C) 2008 Mike McCauley
// $Id: transmitter.pde,v 1.3 2009/03/30 00:07:24 mikem Exp $

#include <VirtualWire.h>

void setup()
{
    Serial.begin(9600);	  // Debugging only
    Serial.println("setup"); // Prints "Setup to the serial monitor"

    vw_set_tx_pin(12);          // Sets pin D12 as the TX pin
    vw_set_ptt_inverted(true);  // Required for DR3100
    vw_setup(4000);	        // Bits per sec
}

void loop()
{
    const char *msg = "hello";   // Message to be sent
    digitalWrite(13, true);      // Flash a light to show transmitting
    vw_send((uint8_t *)msg, strlen(msg)); //Sending the message
    vw_wait_tx(); // Wait until the whole message is gone
    digitalWrite(13, false);   // Turn the LED off.
    delay(50);                 // A short gap.
}

接收器(Receiver)

// receiver.pde
//
// Simple example of how to use VirtualWire to receive messages
// Implements a simplex (one-way) receiver with an Rx-B1 module
//
// See VirtualWire.h for detailed API docs
// Author: Mike McCauley (mikem@airspayce.com)
// Copyright (C) 2008 Mike McCauley
// $Id: receiver.pde,v 1.3 2009/03/30 00:07:24 mikem Exp $

#include <VirtualWire.h>
int count;
void setup()
{
    Serial.begin(9600);	// Debugging only
    Serial.println("setup"); //Prints "Setup" to the serial monitor
    vw_set_rx_pin(12);       //Sets pin D12 as the RX Pin
    vw_set_ptt_inverted(true); // Required for DR3100
    vw_setup(4000);	     // Bits per sec
    vw_rx_start();       // Start the receiver PLL running
}

void loop()
{
    uint8_t buf[VW_MAX_MESSAGE_LEN];
    uint8_t buflen = VW_MAX_MESSAGE_LEN;
    if (vw_get_message(buf, &buflen)) // Non-blocking
    {
	int i;
        digitalWrite(13, true);  // Flash a light to show received good message
	// Message with a good checksum received, dump it.
	Serial.print("Got: ");

	for (i = 0; i < buflen; i++)
	{
            char c = (buf[i]);
            Serial.print(c);
	    Serial.print(" ");
	}
        count++;
       // Serial.print(count);
	Serial.println("");
        digitalWrite(13, false);
    }
}