Friday, February 15, 2019

Arduino Relay Control with 433 MHz RF Remote and Receiver Connection and Coding

This is post is about the arduino 4 relay control module which control using the 433 MHz remote control module and receiver. Or how to connect the RF 433mHZ with arduino for remote controlling or debounce system. In this post I am going to share the complete sketch code is used for controlling debounce led or relay.






The below is complete sketch code for debounce. Just copy and paste. Verify the code and click on upload. You can change the setting or do changing in code according your needs.

// set pin numbers:
const int buttonPin = 2;    // the number of the pushbutton pin
const int ledPin = 12;      // the number of the LED pin
const int buttonPin2 = 3;    // the number of the pushbutton pin
const int ledPin2 = 11;      // the number of the LED pin
const int buttonPin3 = 4;
const int ledPin3 = 10;
const int buttonPin4 = 5;
const int ledPin4 = 9;
// Variables will change:
int ledState = HIGH;         // the current state of the output pin
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin
int ledState2 = HIGH;         // the current state of the output pin
int buttonState2;             // the current reading from the input pin
int lastButtonState2 = LOW;   // the previous reading from the input pin
int ledState3 = HIGH;
int buttonState3;
int lastButtonState3 = LOW;
int ledState4 = HIGH;
int buttonState4;
int lastButtonState4 = LOW;


// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers
long lastDebounceTime2 = 0;  // the last time the output pin was toggled
long debounceDelay2 = 50; // the debounce time; increase if the output flickers
long lastDebounceTime3 = 0;
long debounceDelay3 = 50;
long lastDebounceTime4 = 0;
long debounceDelay4 = 50;

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(ledPin3, OUTPUT);
 pinMode(buttonPin4, INPUT);
  pinMode(ledPin4, OUTPUT);


  // set initial LED state
  digitalWrite(ledPin, ledState);
  digitalWrite(ledPin2, ledState2);
  digitalWrite(ledPin3, ledState3);
  digitalWrite(ledPin4, ledState4);

}

void loop() {
  // read the state of the switch into a local variable:
  int reading = digitalRead(buttonPin);
  int reading2 = digitalRead(buttonPin2);
  int reading3 = digitalRead(buttonPin3);
  int reading4 = digitalRead(buttonPin4);



  // check to see if you just pressed the button
  // (i.e. the input went from LOW to HIGH),  and you've waited
  // long enough since the last press to ignore any noise:

  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

  if (reading2 != lastButtonState2) {
    // reset the debouncing timer
    lastDebounceTime2 = millis();
  }
  if (reading3 != lastButtonState3) {
    // reset the debouncing timer
    lastDebounceTime3 = millis();
  }
if (reading4 != lastButtonState4) {
    // reset the debouncing timer
    lastDebounceTime4 = millis();
  }
  
  if ((millis() - lastDebounceTime) > debounceDelay) {
  if ((millis() - lastDebounceTime2) > debounceDelay2) {
  if ((millis() - lastDebounceTime3) > debounceDelay3) {
  if ((millis() - lastDebounceTime4) > debounceDelay4) {
   
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading != buttonState) {
      buttonState = reading;

      // only toggle the LED if the new button state is HIGH
      if (buttonState == HIGH) {
        ledState = !ledState;
      }
    }
  }
      if (reading2 != buttonState2) {
      buttonState2 = reading2;

      // only toggle the LED if the new button state is HIGH
      if (buttonState2 == HIGH) {
        ledState2 = !ledState2;
      }
    }
  }

 if (reading3 != buttonState3) {
      buttonState3 = reading3;

      // only toggle the LED if the new button state is HIGH
      if (buttonState3 == HIGH) {
        ledState3 = !ledState3;
      }
    }
  }  

if (reading4 != buttonState4) {
      buttonState4 = reading4;

      // only toggle the LED if the new button state is HIGH
      if (buttonState4 == HIGH) {
        ledState4 = !ledState4;
      }
    }
  }    


  // set the LED:
  digitalWrite(ledPin, ledState);
  digitalWrite(ledPin2, ledState2);
  digitalWrite(ledPin3, ledState3);
  digitalWrite(ledPin4, ledState4);


  // save the reading.  Next time through the loop,
  // it'll be the lastButtonState:
  lastButtonState = reading;
  lastButtonState2 = reading2;
  lastButtonState3 = reading3;
  lastButtonState4 = reading4;


}
The above arduino code is for switch on off the leds using button. However we use the 433MHz Wireless Remote Control Switch Module Transceiver Receiver Transmitter DC 2.5V-5.0V with arduino to switch on/off the relay and relay control the light bulb, ceiling fan, socket etc in room wiring.

Watch the video for complete understanding.



I hope after watch the video, now you able to connect the wireless remote control (433MHZ) to arduino. Now if you have any question then use the below comment section.

1 comment:

Electrical Urdu & Hindi Tutorials