enter image description hereHi again? I saw my last question did not come out well, so let me rephrase it. Ideally, my project is composed of 3 sensors: 2 dht-22 sensors and an mq-7 sensor. For the dht22 sensors, I am using data pin connections 9 and 10 PWM on the Arduino mega 2560 board with their respective 10 kilo-ohm resistors, with 5v supply and ground connections. The issue is that I want to set limits for both temperature and humidity for both sensors to be: (16

#include "DHT.h"

#define DHT1PIN 9     // what pin we're connected to
#define DHT2PIN 10

#define DHT1TYPE DHT22    
#define DHT2TYPE DHT22  

// Connect pin 1 (on the left) of the sensor to +5V
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor


void setup() {
  Serial.println("DHTxx test!");


void loop() {
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h1 = dht1.readHumidity();
  float t1 = dht1.readTemperature();
  float h2 = dht2.readHumidity();
  float t2 = dht2.readTemperature();

  // check if returns are valid, if they are NaN (not a number) then something went wrong!
  if (isnan(t1) || isnan(h1)) {
    Serial.println("Failed to read from DHT #1");
  } else {
    Serial.print("Humidity 1: "); 
    Serial.print(" %\t");
    Serial.print("Temperature 1: "); 
    Serial.println(" *C");
  if (isnan(t2) || isnan(h2)) {
    Serial.println("Failed to read from DHT #2");
  } else {
    Serial.print("Humidity 2: "); 
    Serial.print(" %\t");
    Serial.print("Temperature 2: "); 
    Serial.println(" *C");

Ps: LEDs used are 10mm. For the MQ-7 sensor, this is it's code.

/* MQ-7 Carbon Monoxide Sensor Circuit with Arduino */

 const int AOUTpin=0;//the AOUT pin of the CO sensor goes into analog pin A0 of the arduino
 const int DOUTpin=8;//the DOUT pin of the CO sensor goes into digital pin D8 of the arduino
 const int ledPin=13;//the anode of the LED connects to digital pin D13 of the arduino

 int limit;
 int value;

 void setup() {
 Serial.begin(115200);//sets the baud rate
 pinMode(DOUTpin, INPUT);//sets the pin as an input to the arduino
 pinMode(ledPin, OUTPUT);//sets the pin as an output of the arduino

 void loop()
 value= analogRead(AOUTpin);//reads the analaog value from the CO sensor's AOUT pin
 limit= digitalRead(DOUTpin);//reads the digital value from the CO sensor's DOUT pin
 Serial.print("CO value: ");
 Serial.println(value);//prints the CO value
 Serial.print("Limit: ");
 Serial.print(limit);//prints the limit reached as either LOW or HIGH (above or underneath)
 if (limit == HIGH){
 digitalWrite(ledPin, HIGH);//if limit has been reached, LED turns on as status indicator
 digitalWrite(ledPin, LOW);//if threshold not reached, LED remains off

I am yet to buy a relay circuit to be able to connect it with the circuit using a , but for now I would appreciate it the scenarios highlighted for the sensors could be addressed and also, how the two codes could be combined to be one single code. If there is any question, feel free to ask. The project is for silo monitoring and control.


1 Answer 1


Only thing that gives people problems when combining two simple sketches are "delay" commands. There is just one "delay" so it's perfect.

  1. Create new sketch
  2. Paste everything that's not in a method from the two sketches.
  3. Combine setup methods together by pasting first one and second one below.
  4. Remove duplicated commands like Serial.begin(115200); from the method
  5. Combine loop methods together by pasting first one and second one below.
  6. Move code in loop to make the code visually pleasing (it's called the "smell" of code)

This should not be a big problem with those two particular sketches. Please ask more precise questions as soon as you will have problems.

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.