Buzzer activated by motion sensor

I want my buzzer pin to turn high when my motion sensor(connected to a digital pin) turns high. I did a code without blynk and it is working but when I add blynk it doesnt work. I can see the motion sensor going high and low in the app but the buzzer pin remains in low/high state and it never makes a sound in the hardware.


#define BLYNK_PRINT Serial
int buzzerPin = D6;
int sensorPin = D7;
int sensorState = 0;

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = " uEv7YIPD4*******FABHqS89vaG9gjro";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "King Mokoena";
char pass[] = "17071015";

BlynkTimer timer;

// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void myTimerEvent()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, millis() / 1000);
}

void setup()
{ pinMode (buzzerPin, OUTPUT);
  pinMode (sensorPin, INPUT);
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);

  // Setup a function to be called every second
  timer.setInterval(1000L, myTimerEvent);
}

void callMacroSensor(){

  digitalRead(sensorPin); 
  if (sensorPin == HIGH){
    digitalWrite(buzzerPin, HIGH);
  
  }
  else{
    digitalWrite(buzzerPin, LOW);
   
  }
  
  
  }
 
void loop()
{
  Blynk.run();
  timer.run(); // Initiates BlynkTimer
  callMacroSensor();
}

Having this in your void loop is the wrong thing to do.
Call it with a timer, in the same way that the myTimerEvent function is called.

Pete.

1 Like

I used a timer, it still doent work



#define BLYNK_PRINT Serial
int buzzerPin = D6;
int sensorPin = D7;
int sensorState = 0;

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = " uEv7YIPD4ytrrx9zFABHqS89vaG9gjro";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "King Mokoena";
char pass[] = "17071015";

BlynkTimer timer;

// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void myTimerEvent()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, millis() / 1000);
}

BlynkTimer timer2;

void TimerEvent()
{
 //OWN LOOP
 
   digitalRead(sensorPin); 
  if (sensorPin == HIGH){
    digitalWrite(buzzerPin, HIGH);
  
  }
  else{
    digitalWrite(buzzerPin, HIGH);
  }


}
void setup()
{ pinMode (buzzerPin, OUTPUT);
  pinMode (sensorPin, INPUT);
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);

  // Setup a function to be called every second
  timer.setInterval(1000L, myTimerEvent);  
  timer.setInterval(1000L, TimerEvent); 
  

}


 
void loop()
{

  Blynk.run();
  timer.run(); // Initiates BlynkTimer
  timer2.run();

   
  }
  
  

These two lines of code are redundant…

Both your timers are set to be called at 1 second intervals, so will clash every time. Search for topics about staggering timers.

Your if…else statement in TimerEvent does the same thing [digitalWrite(buzzerPin, HIGH)] regardless of the state of the sensor pin.

Pete.

1 Like

I fixed it, but it doesnt trigger the buzzer using the IF condition in arduino code, but when I use an event in the blynk app it works but the buzzer stays high

#define BLYNK_PRINT Serial
int buzzerPin = D6;
int sensorPin = D7;
int sensorState = 0;

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = " uEv7YIPD4ytrrx9zFABHqS89vaG9gjro";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "King Mokoena";
char pass[] = "17071015";

BlynkTimer timer;

// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void myTimerEvent()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, millis() / 1010);
}

BlynkTimer timer2;

void TimerEvent()
{
 //OWN LOOP
 
   sensorState=digitalRead(sensorPin); 
  if (sensorState == HIGH){
    digitalWrite(buzzerPin, HIGH);
  
  }
  else{
    digitalWrite(buzzerPin,LOW);
  }

Blynk.virtualWrite(V5,sensorState / 2028);
}
void setup()
{ pinMode (buzzerPin, OUTPUT);
  pinMode (sensorPin, INPUT);
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);

  // Setup a function to be called every second
  timer.setInterval(1000L, myTimerEvent);  
  timer.setInterval(1000L, TimerEvent); 
  

}


 
void loop()
{

  Blynk.run();
  timer.run(); // Initiates BlynkTimer
  timer2.run();

   
  }
  
  

Try adding some serial.print statements to see what values are being returned by the digitalRead and assigned to the sensorState variiable.

Pete.