Water and Temperature Sensor not running together on Arduino Mega

I have a water and temperature sensor connected to the Arduino Mega board and they both work separately on different files when I put the code in and run it. So I tried to get them to run together by uploading one code for the water sensor and then trying to upload the temp. sensor code to run together but only one works. Would I need to combine the code together and if so what would the code be. What do I need to do to get both the water and temp sensor running at the same time?

Code I used for the water sensor.

#define POWER_PIN  10
#define SIGNAL_PIN A5

int value = 0; // variable to store the sensor value

void setup() {
  Serial.begin(9600);
  pinMode(POWER_PIN, OUTPUT);   // configure D7 pin as an OUTPUT
  digitalWrite(POWER_PIN, LOW); // turn the sensor OFF
}

void loop() {
  digitalWrite(POWER_PIN, HIGH);  // turn the sensor ON
  delay(10);                      // wait 10 milliseconds
  value = analogRead(SIGNAL_PIN); // read the analog value from sensor
  digitalWrite(POWER_PIN, LOW);   // turn the sensor OFF

  Serial.print("Sensor value: ");
  Serial.println(value);

  delay(1000);
}

Code I used for the Temp. sensor

#include <dht.h>

dht DHT;

#define DHT11_PIN 8

void setup(){
  Serial.begin(9600);
}

void loop(){
  int chk = DHT.read11(DHT11_PIN);
  Serial.print("Temperature = ");
  Serial.println(DHT.temperature);
  Serial.print("Humidity = ");
  Serial.println(DHT.humidity);
  delay(1000);
}

Do you understand how each separate sketch works?

Also, you didn't post your attempt at combining them.

No I don't really understand how the code works separately, I just copy and pasted the codes into one file, but there were a lot of error messages when I tried uploading it.

Keep it simple and stupid firstly.
Run some tutorials for the hardware selected.
If you are happy with the results of the tutorials you can merge these to your project.

Have a nice day and enjoy coding in C++.

You are supposed to copy and paste such messages and post them with your request, according to the forum guide. Have you read it?

That's what I did. This is for a school project and I'm also wondering how to be able to see the serial monitor appearing together at the same time for both the water and temp. sensor.

Confused, then. Which one of the two you posted, is the final, desired sketch?

You labelled one as for water, one as for temps...

You also did not say, which one works and which one doesn't.

The code is in two different sketch files, I meant to say that they both work separately but I need them to work together. How would I be able to do that?

That takes us back to unanswered post #2.

1 Like

Seems you didn't use pinmode to set SIGNAL_PIN as input in the Water Sensor code....did it work anyways?.......

I think it can, input mode is the default. But it casts aspersions on the quality of the code.

Yes

1 Like

Hi
Just combining the codes this is what I think:

#include <dht.h>
#define POWER_PIN  10
#define SIGNAL_PIN A5
#define DHT11_PIN 8
dht DHT;
int value = 0; // variable to store the sensor value

void setup() {
  Serial.begin(9600);
  pinMode(POWER_PIN, OUTPUT);   // configure D7 pin as an OUTPUT
  digitalWrite(POWER_PIN, LOW); // turn the sensor OFF
}

void loop() {
  digitalWrite(POWER_PIN, HIGH);  // turn the sensor ON
  delay(10);                      // wait 10 milliseconds
  value = analogRead(SIGNAL_PIN); // read the analog value from sensor
  digitalWrite(POWER_PIN, LOW);   // turn the sensor OFF

  Serial.print("Sensor value: ");
  Serial.println(value);

  delay(1000);
  int chk = DHT.read11(DHT11_PIN);
  Serial.print("Temperature = ");
  Serial.println(DHT.temperature);
  Serial.print("Humidity = ");
  Serial.println(DHT.humidity);
  delay(1000);
}

I am not a big expert myself...is this the same code you tried when combining?..
Also here its running water sensor first and then the temperature sensor

Then, did you test it? You made a big forum blunder, too... no code tags.
Did you use chatGPT?

No I did'nt use ChatGPT ..and thanks for pointing out the code tags

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