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.