DHT12 dht12;



#include <Wire.h>

#include <DHT12.h>


DHT12 dht12;


float IRpin = A0;  // analog pin for IR sensor

float DHT12pin = A1; // analog pin for DHT12 sensor


void setup() {

  pinMode(IRpin, INPUT);

  Serial.begin(9600);

  dht12.begin();

}


void loop() {

  float irValue = analogRead(IRpin);

  float dht12Humidity = dht12.getHumidity();

  float dht12Temperature = dht12.getTemperature();


  Serial.print("IR sensor value: ");

  Serial.println(irValue);


  Serial.print("DHT12 humidity: ");

  Serial.print(dht12Humidity);

  Serial.print("%, Temperature: ");

  Serial.print(dht12Temperature);

  Serial.println("°C");


  delay(2000);

}



This code initializes the DHT12 sensor object and sets the IR sensor pin (A0) and DHT12 sensor pin (A1) as inputs. Inside the loop() function, it reads the analog value from the IR sensor using analogRead() and the humidity and temperature values from the DHT12 sensor using the readHumidity() and readTemperature() methods of the DHT12 object.

The obtained sensor values are then printed to the serial monitor using the Serial.println() function. Note that the DHT12 library requires the Wire library, which is included at the top of the code. Also, there is a delay of 2 seconds between each loop iteration to avoid reading the sensors too frequently.

Post a Comment

Previous Post Next Post