Conductivity Sensor Arduino Code

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

//LIBRARIES

//=============================================================================
#include <SoftwareSerial.h> //Importing a library for Bluetooth communication

//PROGRAMMING VARIABLES
//=============================================================================
SoftwareSerial BTserial(10, 11); // Arduino Reciever (RX) | Arduino Transmitter
(TX)

int sensorPin = A1; //conductivity sensor


int relayPin = 2; //electronic switch that controls power to the pump

int sensorValue = 0;
float voltage_threshold = 3.5; //Minimum voltage level required to activate pump

//PROGRAM SETUP
//============================================================================
void setup()
{
BTserial.begin(9600); //setting up the bluetooth serial monitor
Serial.begin(9600); //setting up the normal serial monitor
pinMode(relayPin, OUTPUT); //defning the relayPin as an output.

//When the relayPin is given power, a switch connects the pump and its power
supply.
//When the relayPin is not given power, the switch is left open and the pump is
turned off
}
//MAIN PROGRAM
//============================================================================
void loop() //the code written below iterates continously
{
//The Arduino can supply an output voltage of 5V.
//When reading a voltage from a sensor pin, the voltage (0-5V) is read as digital
value (0-1023)

sensorValue = analogRead(sensorPin); //reading the voltage from the conductivity


sensor as a digital value
float voltage = sensorValue*(5.0/1023.0); //converting the digital value to its
real voltage value

//Printing the voltage value on the normal serial monitor


Serial.println(sensorValue);
Serial.print(voltage);
Serial.println("V");

//Sending the voltage value to the bluetooth serial monitor (which is observed by
the "ArduTooth" mobile app)
//Note: the ";" printed at the end is required for formatting by the app
BTserial.print(voltage);
BTserial.print(" V");
BTserial.print(";");

//If the conductivity sensor voltage is greater than or equal to (symbol: >=) the
predefined threshold voltage, turn on the pump.
//Otherwise, turn off the pump.
if(voltage >= voltage_threshold){
digitalWrite(relayPin, HIGH);
}
else{
digitalWrite(relayPin, LOW);
}

delay(100); //delay the next iteration of the code by 100 milliseconds


}

You might also like