0

enter image description hereI am using dht humidity and temperature sensor on arduino and i would like to add relay and buzzer on the system. What I would like to do is to close it via buzzer and on 65% humidity run buzzer. The code i use is:

/* DHT-22 sensor with 12c 16x2 LCD with Arduino uno
   Temperature and humidity sensor displayed in LCD
   based on: http://www.ardumotive.com/how-to-use-dht-22-sensor-en.html and
   https://www.ardumotive.com/i2clcden.html for the i2c LCD library by Michalis Vasilakis
   Recompile by adhitadhitadhit
   Notes: use LCD i2c Library from link above, i'm not sure why but new Liquidcristal library from Francisco Malpartida  isn't working for me
   other thing, check your */

//Libraries
#include <dht.h> // sensor library using lib from https://www.ardumotive.com/how-to-use-dht-22-sensor-en.html
#include <LiquidCrystal_I2C.h> // LCD library using from  https://www.ardumotive.com/i2clcden.html for the i2c LCD library 
#include <Wire.h> 
dht DHT;

//Constants
#define DHT22_PIN 2     // DHT 22  (AM2302) - pin used for DHT22 
LiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address to 0x27 after finding it from serial monitor (see comment above) for a 16 chars and 2 line display

//Variables
float hum;  //Stores humidity value
float temp; //Stores temperature value

void setup()
{
    Serial.begin(9600);
    lcd.init();                      // initialize the lcd 
  // Print a message to the LCD.
  lcd.backlight();
  lcd.setBacklight(HIGH);
}

void loop()
{
    int chk = DHT.read22(DHT22_PIN);
    //Read data and store it to variables hum and temp
    hum = DHT.humidity;
    temp= DHT.temperature;
    //Print temp and humidity values to LCD
    lcd.setCursor(0,0);
    lcd.print("Humidity: ");
    lcd.print(hum);
    lcd.print("%");
    if (humidity <= 50) 
{ 
   digitalWrite(ledPin, HIGH); 
   digitalWrite(buzzer, HIGH); 

 } 

 else 

 { 

 digitalWrite(ledPin, LOW); digitalWrite(buzzer, LOW);
    
    lcd.setCursor(0,1);
    lcd.print("Temp: "); 
    lcd.print(temp);
    lcd.println("Celcius");
    delay(2000); //Delay 2 sec between temperature/humidity check.
}

How to add relay and buzzer to close it with buzzer run on 65% humidity value?

20
  • Do you know how to implement the required subtasks (control relay/buzzer, compare values)? If yes, where exactly do you have problems?
    – Sim Son
    Feb 19 at 19:21
  • Yes i did it once but this time i changed my LCD connection with socket. Defining buzzer and relay it gives error on ide. I have follewed arduino official code. So i need to adapt it to above code which provides humidity and temperature on LCD
    – krank s
    Feb 19 at 19:33
  • Hmm, we need to see the code that doesn't work and which you have problems with... how do you actually "define" a relay/buzzer? You just need to set the desired pins to outputs and control them with digitalWrite() and tone() corresponding to some logic you have to implement. Without seeing what you do we can't know what you're doing wrong.
    – Sim Son
    Feb 19 at 19:38
  • you don't have any code that says if humidity is equal to 65%, then sound a buzzer
    – jsotola
    Feb 19 at 20:10
  • 1
    I have uploaded the infos you asked for. I couldn't decide the relay connection so I need your help on relay connection and code.
    – krank s
    Feb 20 at 17:50
0

In this code:

void loop()
{
   sample temp and humidity;
   display temp;
   display humidity;

   if humidity <= 50
      LED on;
      buzzer on;
   else
      display temp
      wait 2 sec
   end
  • There is a missing '}' to to close the if/else.
  • Why do you display temp twice within the loop?
  • Where is the logic for the relay and the buzzer at 65%?
  • Pin numbers for the LED and the buzzer are undefined.
  • The LED and buzzer pins need to be set to OUTPUT mode in your setup() function.

If I understand your description, you probably need somethng like the following, in your loop function:

   if humidity <= 50
      LED on;
      buzzer on;
   else if hum >= 65%m
      close relay;
      buzzer on;
   end

When I have a problem with program logic that isn't obvious to me, I write it out in pseudo-code as I did above. Once I figure out the logic, then I'll type in C++ with proper syntax. If I haven't understood your intentions in the above pseudo-code, I'd suggest you do the same thing, until you believe what you've written. Only then try to make it into working code.

1
  • What I missed was seems to be added humidity function into void main function. By checking c# I solved it. Now the system works fine thank you.
    – krank s
    Mar 11 at 9:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.