Connect buzzer to temperature sensor arduino

I have two codes, one for temperature and humidity and another for a buzzer ; both work fine, but how can I combine them so that when the temperature rises, the buzzer activates?
i use :
esp8266
DHT11
buzzer passive and active

Read this
https://forum.arduino.cc/t/beginners-combining-two-codes-doesnt-work/599945
and this

1 Like

Hi,
Welcome
so that we can help you better, I suggest that, using the </> tags, post both codes that work and your attempt to put the codes together, even if it doesn't work.

1 Like

Please explain. What is a "buzzer sensor"? Unless, it will be obvious from the code that you post.

its a passive and active buzzer

this is buzzer code on arduino :

#define CAYENNE_DEBUG // Uncomment to show debug messages
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
#include <CayenneMQTTESP8266.h>

// WiFi network info.
char ssid[] = "---";
char wifiPassword[] = "---";

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "---";
char password[] = "---";
char clientID[] = "---";

const unsigned char Passive_buzzer = 2;
const unsigned char Active_buzzer = 14;
const unsigned char built_in_led = 16;

void setup () {
pinMode (Passive_buzzer,OUTPUT) ;
pinMode (Active_buzzer,OUTPUT) ;
pinMode (built_in_led,OUTPUT) ;
}

void loop () {
digitalWrite(built_in_led,LOW) ; //Turn on the built in led
digitalWrite(Active_buzzer,HIGH) ; //Turn on active buzzer
delay (1000);

digitalWrite(built_in_led,HIGH) ; //Turn off the built in led
digitalWrite(Active_buzzer,LOW) ; //Turn off active buzzer
delay (1000);

tone(Passive_buzzer, 523) ; //DO note 523 Hz
delay (1000);
tone(Passive_buzzer, 587) ; //RE note ...
delay (1000);
tone(Passive_buzzer, 659) ; //MI note ...
delay (1000);
tone(Passive_buzzer, 783) ; //FA note ...
delay (1000);
tone(Passive_buzzer, 880) ; //SOL note ...
delay (1000);
tone(Passive_buzzer, 987) ; //LA note ...
delay (1000);
tone(Passive_buzzer, 1046) ; // SI note ...
delay (1000);
noTone(Passive_buzzer) ; //Turn off the pin attached to the tone()
}

and this is DHT11 sensor code:

/*
Cayenne DHT11 Sensor Project
*/

#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
#define DHTPIN 0 // D3

// Uncomment whatever type you're using! In this project we used DHT11
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22, AM2302, AM2321
//#define DHTTYPE DHT21 // DHT 21, AM2301

#include <CayenneMQTTESP8266.h>
#include <DHT.h>

// WiFi network info.
char ssid[] = "---";
char wifiPassword[] = "---";

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "---";
char password[] = "---";
char clientID[] = "---";

DHT dht(DHTPIN, DHTTYPE);

//Variables for DHT11 values
float h;
float t;
bool humidity = false;
bool Temperature = false;

void setup()
{
Serial.begin(9600);
Serial.print("Setup");
Cayenne.begin(username, password, clientID, ssid, wifiPassword);

humidity = false;
Temperature = false;
}

void loop()
{
//Run Cayenne Functions
Cayenne.loop();
}

CAYENNE_OUT(V0){
//Serial.println("humidity");

//Check if read failed and try until success
do {
//Read humidity (percent)
h = dht.readHumidity();

delay(1000);

} while (isnan(h));

Serial.print("humidity: ");
Serial.println(h);
Serial.print(" %\t");

//Set Humidity to true so we know when to sleep
humidity = true;

//Write to Cayenne Dashboard`
Cayenne.virtualWrite(V0, h);
}

CAYENNE_OUT(V1){
//Serial.println("temperature");

//Check if read failed and try until success
do {
//Read temperature as Celsius

t = dht.readTemperature();


delay(1000);

} while (isnan(t));

Serial.print("temperature: ");
Serial.println(t);
Serial.println(" *C");

//Set Temperature to true so we know when to sleep
//Temperature = true;

//Write to Cayenne Dashboard
Cayenne.virtualWrite(V1, t);
}

i need to put if else condition on buzzer code
I want to the buzzer to alarm when temp hits a min and a max.
It doesn't appear to be reading the temp sensor. all it does is alarm..

this is buzzer code\ :point_down:




#define CAYENNE_DEBUG       // Uncomment to show debug messages
#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#include <CayenneMQTTESP8266.h>


// WiFi network info.
char ssid[] = "----";
char wifiPassword[] = "---";

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "--";
char password[] = "---";
char clientID[] = "--";

const unsigned char Passive_buzzer = 2;
const unsigned char Active_buzzer = 14;
const unsigned char built_in_led = 16;

void setup () {
pinMode (Passive_buzzer,OUTPUT) ;
pinMode (Active_buzzer,OUTPUT) ;
pinMode (built_in_led,OUTPUT) ;
}

void loop () {
digitalWrite(built_in_led,LOW) ; //Turn on the built in led
digitalWrite(Active_buzzer,HIGH) ; //Turn on active buzzer
delay (1000);
 
digitalWrite(built_in_led,HIGH) ; //Turn off the built in led
digitalWrite(Active_buzzer,LOW) ; //Turn off active buzzer
delay (1000); 

tone(Passive_buzzer, 523) ; //DO note 523 Hz
delay (1000); 
tone(Passive_buzzer, 587) ; //RE note ...
delay (1000); 
tone(Passive_buzzer, 659) ; //MI note ...
delay (1000); 
tone(Passive_buzzer, 783) ; //FA note ...
delay (1000); 
tone(Passive_buzzer, 880) ; //SOL note ...
delay (1000); 
tone(Passive_buzzer, 987) ; //LA note ...
delay (1000); 
tone(Passive_buzzer, 1046) ; // SI note ...
delay (1000); 
noTone(Passive_buzzer) ; //Turn off the pin attached to the tone()
}

and this is DHT11 sensor code :point_down:

/*
Cayenne DHT11 Sensor Project
*/

#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#define DHTPIN 0          // D3

// Uncomment whatever type you're using! In this project we used DHT11
#define DHTTYPE DHT11     // DHT 11
//#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321
//#define DHTTYPE DHT21   // DHT 21, AM2301

#include <CayenneMQTTESP8266.h>
#include <DHT.h>

// WiFi network info.
char ssid[] = "--";
char wifiPassword[] = "--";

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "---";
char password[] = "---";
char clientID[] = "---";

DHT dht(DHTPIN, DHTTYPE);

//Variables for DHT11 values
float h;
float t;
bool humidity = false;
bool Temperature = false;


void setup()
{
  Serial.begin(9600);
  Serial.print("Setup");
  Cayenne.begin(username, password, clientID, ssid, wifiPassword);

  humidity = false;
  Temperature = false;
}

void loop()
{
  //Run Cayenne Functions
  Cayenne.loop();
}

CAYENNE_OUT(V0){
  //Serial.println("humidity");

  //Check if read failed and try until success
  do {
    //Read humidity (percent)
    h = dht.readHumidity();

    delay(1000);
  } while  (isnan(h));

  Serial.print("humidity: ");
  Serial.println(h);
  Serial.print(" %\t");

  //Set Humidity to true so we know when to sleep
  humidity = true;

  //Write to Cayenne Dashboard`
  Cayenne.virtualWrite(V0, h);
}

CAYENNE_OUT(V1){
  //Serial.println("temperature");
  
  //Check if read failed and try until success
  do {
    //Read temperature as Celsius
   
    t = dht.readTemperature();
    

    delay(1000);
  } while  (isnan(t));

  Serial.print("temperature: ");
  Serial.println(t);
  Serial.println(" *C");

  //Set Temperature to true so we know when to sleep
  //Temperature = true;

  //Write to Cayenne Dashboard
  Cayenne.virtualWrite(V1, t);
}

Then make an attempt and post it in a new message. Here is the reference for the 'if' statement:
https://www.arduino.cc/reference/en/language/structure/control-structure/if/

why not something as simple as this

#include <DHT.h>

#define DHTPIN 0          // D3
#define DHTTYPE DHT11     // DHT 11

DHT dht (DHTPIN, DHTTYPE);

const unsigned char Active_buzzer = 14;

const float TempMin = 20.0;
const float TempMax = 24.0;

void setup ()
{
    pinMode (Active_buzzer,OUTPUT) ;
}

void loop ()
{
    float t = dht.readTemperature ();

    if (t < TempMin || TempMax < t)
        digitalWrite(Active_buzzer, HIGH); // on
    else
        digitalWrite(Active_buzzer, LOW); // off
}

Hi,
try this code:

/*
  Cayenne DHT11 Sensor Project
*/

const unsigned char Passive_buzzer = 2;
const unsigned char Active_buzzer = 14;
const unsigned char built_in_led = 16;

#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#define DHTPIN 0          // D3

// Uncomment whatever type you're using! In this project we used DHT11
#define DHTTYPE DHT11     // DHT 11
//#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321
//#define DHTTYPE DHT21   // DHT 21, AM2301

#include <CayenneMQTTESP8266.h>
#include <DHT.h>

// WiFi network info.
char ssid[] = "--";
char wifiPassword[] = "--";

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "---";
char password[] = "---";
char clientID[] = "---";

DHT dht(DHTPIN, DHTTYPE);

//Variables for DHT11 values
float h;
float t;
float tOld;                 // Save initial or old read temp.
bool humidity = false;
bool Temperature = false;
//----------------------------------------------------------------------------
void setup()
{
  Serial.begin(9600);
  Serial.print("Setup");
  pinMode (Passive_buzzer, OUTPUT) ;
  pinMode (Active_buzzer, OUTPUT) ;
  pinMode (built_in_led, OUTPUT) ;

  Cayenne.begin(username, password, clientID, ssid, wifiPassword);

  humidity = false;
  Temperature = false;
  tOld = dht.readTemperature();
}
//----------------------------------------------------------------------------
void loop()
{
  //Run Cayenne Functions
  Cayenne.loop();
}

CAYENNE_OUT(V0) {
  //Serial.println("humidity");

  //Check if read failed and try until success
  do {
    //Read humidity (percent)
    h = dht.readHumidity();

    delay(1000);
  } while  (isnan(h));

  Serial.print("humidity: ");
  Serial.println(h);
  Serial.print(" %\t");

  //Set Humidity to true so we know when to sleep
  humidity = true;

  //Write to Cayenne Dashboard`
  Cayenne.virtualWrite(V0, h);
}

CAYENNE_OUT(V1) {
  //Serial.println("temperature");

  //Check if read failed and try until success
  do {
    //Read temperature as Celsius

    t = dht.readTemperature();   
    if ( tOld < t)      // If new ra is high then old call buzzer
    {
      sound();          // Sound buzzer
    }
    tOld = t;           // Save new read

    delay(1000);
  } while  (isnan(t));

  Serial.print("temperature: ");
  Serial.println(t);
  Serial.println(" *C");

  //Set Temperature to true so we know when to sleep
  //Temperature = true;

  //Write to Cayenne Dashboard
  Cayenne.virtualWrite(V1, t);
}
//----------------------------------------------------------------------------
void sound() {
  digitalWrite(built_in_led, LOW) ; //Turn on the built in led
  digitalWrite(Active_buzzer, HIGH) ; //Turn on active buzzer
  delay (1000);

  digitalWrite(built_in_led, HIGH) ; //Turn off the built in led
  digitalWrite(Active_buzzer, LOW) ; //Turn off active buzzer
  delay (1000);

  tone(Passive_buzzer, 523) ; //DO note 523 Hz
  delay (1000);
  tone(Passive_buzzer, 587) ; //RE note ...
  delay (1000);
  tone(Passive_buzzer, 659) ; //MI note ...
  delay (1000);
  tone(Passive_buzzer, 783) ; //FA note ...
  delay (1000);
  tone(Passive_buzzer, 880) ; //SOL note ...
  delay (1000);
  tone(Passive_buzzer, 987) ; //LA note ...
  delay (1000);
  tone(Passive_buzzer, 1046) ; // SI note ...
  delay (1000);
  noTone(Passive_buzzer) ; //Turn off the pin attached to the tone()
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.