RF433 MHZ and Arduino
Library:
Receiver:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
/* https://forbiddenbit.com/arduino-tutorial/rf433-mhz-and-arduino/ Receiver: Arduino: Module 433Mhz Rx: - GND GND - 5V VCC - D3 Data */ #define D4 4 #define D5 5 #include <VirtualWire.h> byte message[VW_MAX_MESSAGE_LEN]; // a buffer to store the incoming messages byte messageLength = VW_MAX_MESSAGE_LEN; // the size of the message int value = 0; void setup() { Serial.begin(9600); pinMode(D4, OUTPUT); pinMode(D5, OUTPUT); Serial.println("Ready..."); vw_set_rx_pin(3); // pin vw_setup(2000); // bps vw_rx_start(); } void loop() { if (vw_get_message(message, &messageLength)) // { for (int i = 0; i < messageLength; i++) { value = message[i]; } Serial.println(value); if (value == 0) { digitalWrite(D4, HIGH); digitalWrite(D5, LOW); } else if (value == 2) { digitalWrite(D4, LOW); digitalWrite(D5, HIGH); } else if (value == 4) { digitalWrite(D4, HIGH); digitalWrite(D5, HIGH); } Serial.println(value); } } |
Transmitter:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
/* https://forbiddenbit.com/arduino-tutorial/rf433-mhz-and-arduino/ Transmitter: Arduino: Module 433Mhz Tx: - GND GND - 5V VCC - D3 Data */ #include <VirtualWire.h> #define size 1 int pot = A3; byte TX_buffer[size]={0}; byte i; int value; void setup() { Serial.begin(9600); pinMode(4, INPUT_PULLUP); pinMode(5, INPUT_PULLUP); // virtual wire vw_set_tx_pin(3); // pin vw_setup(2000); // bps for(i=0;i<size;i++) { TX_buffer[i]=i; } } void loop() { if (digitalRead(4) == LOW){value=2;} else if (digitalRead(5) == LOW){value=4;} else {value=0;} Serial.println(value); int val = value; TX_buffer[0] = val; vw_send(TX_buffer, size); vw_wait_tx(); delay(10); } |