Combining analog sensor codes with digital sensor code

I am designing a product for an A-level Design project and I wanted to try use an Arduino to measure the temperature of water and detect if water is present. Im not sure if I have put this is the right category but i'm not sure what it would go in to.

I have watched videos and tried to follow tutorials on how to combine analog and digital code but I haven't had any success and it hasn't seemed to make a difference for me. I'm new to working with Arduino's so i'm not sure what to do.

**I'd also like to use 2 buttons , one to turn the water sensor on/off , and another to turn the temperature sensor off/on but I'm not sure how to do that **

These are the 2 sketches of code I have found online and used :

temperature sensor ( DS18b20 ) with I2C module

#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS); 
LiquidCrystal_I2C lcd(0x27, 16, 2); 
 
DallasTemperature sensors(&oneWire);

 
void setup(void)
{

  sensors.begin();
  Serial.begin(9600);
  lcd.init();
  lcd.backlight();
}
void loop(void)
{ 

  sensors.requestTemperatures(); 
 

  
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Temperature:");
  lcd.setCursor(0,1);
  lcd.print(sensors.getTempCByIndex(0));
  lcd.setCursor(7,1);
  lcd.print("C");
  lcd.setCursor(9,1);
  lcd.print((sensors.getTempCByIndex(0) * 9.0) / 5.0 + 32.0);
  lcd.setCursor(15,1);
  lcd.print("F");
  

  delay(1000);
} 

Water sensor with buzzer

const int waterSens = A5; //define water sensor 
int waterVal; // define the water sensor value 

void setup() {
  pinMode(11, OUTPUT);
  pinMode(waterSens, INPUT);
  Serial.begin(9600);

 }

 void loop() {
   waterVal = analogRead(waterSens) ;

   Serial.println(waterVal) ;

   if (waterVal <=600) {
     noTone(11);

   }
else{
  tone(11,500);
 }
 }

This is the combined code

#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS); 
LiquidCrystal_I2C lcd(0x27, 16, 2); 
 
DallasTemperature sensors(&oneWire);

const int waterSens = A5; //define water sensor 
int waterVal; // define the water sensor value 
 
void setup(void)
{

  pinMode(11, OUTPUT);
  pinMode(4, INPUT);
  Serial.begin(9600);

  sensors.begin();
  Serial.begin(9600);
  lcd.init();
  lcd.backlight();
}
void loop(void)
{ 

  sensors.requestTemperatures(); 
 

  
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Temperature:");
  lcd.setCursor(0,1);
  lcd.print(sensors.getTempCByIndex(0));
  lcd.setCursor(7,1);
  lcd.print("C");
  lcd.setCursor(9,1);
  lcd.print((sensors.getTempCByIndex(0) * 9.0) / 5.0 + 32.0);
  lcd.setCursor(15,1);
  lcd.print("F");
  
     waterVal = analogRead(waterSens) ;

   Serial.println(waterVal) ;

   if (waterVal <=600) {
     noTone(11);

   }
else{
  tone(11,500);
 }
  
  delay(1000);
}

Both the codes work separately but together the :

  • buzzer goes off randomly ;

  • water sensor doesn't detect any water;

  • temperature sensor stops updating in the serial monitor and the LCD when the water sensor is plugged in but works when I take it out the analog input slot in the Arduino.

I also want to combine the code with the ESP8266 wifi module in the Blink app but i'm hoping all I have to do is copy and pate that with this code.

I hope this all is clear enough to find a solution.

Hi, @partc001
Welcome to the forum.

Thanks for using code tags. :+1: :+1:

What model Arduino are you using?
How have you got your buttons wired?

Can we please have a circuit diagram?
An image of a hand drawn schematic will be fine, include ALL power supplies, component names and pin labels.

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

1 Like

Use A0 for the water sensor

You cant use A4 and A5 the same time you are using the I2C bus

1 Like

I don't need a diagram.
Have you tried using A0 and NOT A5 as I suggested?

I was replying to Tom about the buttons , Im going to try putting the sensor in the A0 pinhole when I get access to the board again later today

I have changed the input to pin A0 and now the temperature sensor works whilst the water sensor is connected. However , the buzzer isn't always going off when the sensor is in water and goes off uncontrollably other times.

What kind of water sensor are you using?

its a water level sensor ,

the " Robodo SEN18 Water Level Sensor " is the closest match I can find.

That device can basically only detect the presence or absence of water. High humidity can even set it off

1 Like

You will need to play the 600 threshold value in order to get it to work right.
Try seing it lower maybe 480.

1 Like

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