BLYNK
BLYNK.IO       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Turn off light with Blynk after PIR detects motion

Hello. I am making a home automation project using the HC-05 bluetooth module and HC-SR501 PIR sensor. Is it possible to turn off the lights through Blynk after PIR sensor turns it on when it detects movement? When I try it, I can turn the lights on and off with Blynk but the sensor would not work (lights won’t turn on when it detects motion, only Blynk button works). Below is the code

#define BLYNK_PRINT Serial
#include <BlynkSimpleSerialBLE.h>
#include <SoftwareSerial.h>

char auth[] = "***********";

SoftwareSerial SerialBLE(2, 3); // RX, TX

//define the pins
int LED = 4;
int PIR = 7;
int relay1 = 5;

void setup() {
  pinMode(LED, OUTPUT);
  pinMode(PIR, INPUT);
  digitalWrite(LED, LOW);

  digitalWrite(relay1, HIGH);
  pinMode(relay1, OUTPUT);
  Serial.begin(9600);
  SerialBLE.begin(9600);  // BT module
  Serial.println("Waiting for connections...");
  Blynk.begin(SerialBLE, auth);
  Serial.println("Connected");
}


void loop() {
  Blynk.run();
  //using the digitalRead function it will read the signal of the sensor
  int value = digitalRead(PIR);
  //if its high or if an any object is detected it will activate the LED
  if (value == HIGH) {
    digitalWrite(LED, HIGH);
    digitalWrite(relay1, LOW);
  }
  else {
    digitalWrite(LED, LOW);
    digitalWrite(relay1, HIGH);
  }

}

BLYNK_WRITE(V0) {
  digitalWrite(5, param.asInt());
}

Hey there,

first of all you must keep your void loop clean, the ideal blynk void loop should look like this

void loop() {
  Blynk.run();
}

Check this out
https://docs.blynk.io/en/legacy-platform/legacy-articles/keep-your-void-loop-clean

second,

Use this in a separate void function and call it using a blynk timer.
For more details, read this topic

1 Like

Once you’ve made the changes that @John93 has suggested, I’d do a bit of tidying-up too…

You are crating an alias for GPIO5, called relay1

and using it here…

but not here…

Also, you are defining relay1 as an OUTPUT, but doing that after you are attempting to write to it…

I’d also add some serial print statements into your sketch, so that you are 100% certain that your PIR sensor is actually changing the value of GPIO7 when it is being activated.

You haven’t actually stated what type of board you are using, which would be useful to know.

Pete.

2 Likes

Hello again everyone. Thank you for willing to guide me. I have made some modifications to my sketch following everyone’s suggestion and the documentations that has been linked. The new sketch is as shown below :-

#define BLYNK_PRINT Serial
#include <BlynkSimpleSerialBLE.h>
#include <SoftwareSerial.h>

char auth[] = "***************";

BlynkTimer timerPIR; //Announcing the timer

SoftwareSerial SerialBLE(2, 3); // RX, TX

//define the pins
int LED = 4;
int PIR = 7;
int relay1 = 5;

//this function is for describing what happens every 5 seconds
void readingPIR()
{
  //using the digitalRead function it will read the signal of the sensor
  int value = digitalRead(PIR);

  //if its high or if any object is detected it will activate the LED
  if (value == HIGH)
  {
    digitalWrite(LED, HIGH);
    digitalWrite(relay1, LOW);
    Serial.println("Light is ON, PIR");
  }
  else
  {
    digitalWrite(LED, LOW);
    digitalWrite(relay1, HIGH);
    Serial.println("Light is OFF, PIR");
  }
}

void setup() //pinMode then only digital
{
  pinMode(PIR, INPUT);
  pinMode(LED, OUTPUT);
  pinMode(relay1, OUTPUT);

  digitalWrite(LED, LOW);
  digitalWrite(relay1, HIGH);

  //this one calls the function 'readingPIR'every 5 seconds
  timerPIR.setInterval(28000L, readingPIR);

  Serial.begin(9600);
  SerialBLE.begin(9600);  // BT module
  Serial.println("Waiting for connections...");
  Blynk.begin(SerialBLE, auth);
  Serial.println("Connected");
}


void loop() // must be kept clean for blynk or will cause problems
{
  Blynk.run();
  timerPIR.run();
}

BLYNK_WRITE(V0)
{
  digitalWrite(relay1, param.asInt());

  //to check in serial monitor
  if (param.asInt())
  {
    Serial.println("Light OFF, through button");
  }
  else
  {
    Serial.println("Light ON, through button");
  }


}

Now everything works as intended, but in my serial monitor I sometimes see the sentence “Packet too big”, as shown below:-
image

Is that normal and can be ignored or should it be fixed? Also I apologize for not mentioning the board used. I am currently using Arduino Uno R3.

1 Like

This might help

1 Like

Hi,

My post that you refer to is not applicable: it was a bug some time ago, and this was fixed.

When using bluetooth modules connected with softwareserial you can have errors in the communication between the Bluetooth module and the Arduino, but the Blynk protocol has difficulties handling missing bytes.
Some thing:

  • the optimal speed for the module us 38400. Lower you can have buffer overflow in the module. Higher will introduce communication errors when using SoftwareSerial
  • use a hardware serial port if possible (e.g. on Mega).
  • make sure the notification bit is set correctly on the module. This is for HM-10, I don´t remember if it is also on HC-05. See my post [SOLVED] Blynk and BLE using HM10 module (OK+CONN problem) - #4 by federicobusero
1 Like

@federicobusero thank you for your explanation🙂

Your comment says every 5 seconds, but the timer is set to run once every 28 seconds.
This means that it will be any period between 0 and 28 seconds after the PIR trigger is activated before the relay will be turned on, and any period between 0 and 28 seconds after the PIR stops being activated before it turns off again.

Having a turn-off delay isn’t likely to be a problem - in fact it’s probably a distinct advantage, but having a delay before the light is turned on isn’t likely to work well in most situations.

A better solution is probably ti run the timer much more frequently (maybe every 500ms or so) but have a non-blocking timeout timer which needs to complete it’s timeout period before the relay is de-energised.

However, I’m not sure that Bluetooth is the best connection method for this type of solution, especially if you want the process to work correctly when your phone is out of Bluetooth range.

Pete.