Introduction:
Water sensors are valuable tools for measuring and monitoring the water level in various applications, including environmental monitoring, hydroponics, and home automation systems. To ensure accurate readings, it's essential to calibrate the water sensor properly. In this tutorial, we will guide you through the process of calibrating a water sensor using an Arduino microcontroller. By following these steps, you'll be able to obtain reliable water level measurements for your projects.
Materials Required:
- Arduino Uno : https://amzn.to/43RlPXv
- Water sensor : https://amzn.to/3CrLz0n
- Jumper wires : https://amzn.to/43TSwD9
- Breadboard
- USB cable for Arduino
- Computer with Arduino IDE installed
- Connect the VCC pin of the water sensor to the 5V pin on the Arduino board.
- Connect the GND pin of the water sensor to the GND pin on the Arduino board.
- Connect the signal pin of the water sensor to one of the analog input pins on the Arduino board (e.g., A0). Use a jumper wire for each connection and the breadboard to make the connections easier.
/* Analog input, analog output, serial output Reads an analog input pin, maps the result to a range from 0 to 255 and uses the result to set the pulse width modulation (PWM) of an output pin. Also prints the results to the Serial Monitor. The circuit: - potentiometer connected to analog pin 0. Center pin of the potentiometer goes to the analog pin. side pins of the potentiometer go to +5V and ground - LED connected from digital pin 9 to ground through 220 ohm resistor created 29 Dec. 2008 modified 9 Apr 2012 by Tom Igoe This example code is in the public domain. https://www.arduino.cc/en/Tutorial/BuiltInExamples/AnalogInOutSerial */ // These constants won't change. They're used to give names to the pins used: const int analogInPin = A0; // Analog input pin that the potentiometer is attached to const int analogOutPin = 9; // Analog output pin that the LED is attached to int sensorValue = 0; // value read from the pot int outputValue = 0; // value output to the PWM (analog out) void setup() { // initialize serial communications at 9600 bps: Serial.begin(9600); } void loop() { // read the analog in value: sensorValue = analogRead(analogInPin); // map it to the range of the analog out: outputValue = map(sensorValue, 0, 1023, 0, 255); // change the analog out value: analogWrite(analogOutPin, outputValue); // print the results to the Serial Monitor: Serial.print("sensor = "); Serial.print(sensorValue); Serial.print("\t output = "); Serial.println(outputValue); // wait 2 milliseconds before the next loop for the analog-to-digital // converter to settle after the last reading: delay(2); }
Step 3: Upload the Code to the Arduino Board
Connect your Arduino board to the computer if you haven't already. Then, select the appropriate board and port from the "Tools" menu in the Arduino IDE. Finally, click on the "Upload" button (right-facing arrow) to upload the code to your Arduino board.
Step 4: Monitor Serial Output and Determine Calibration Points
Once the code is uploaded successfully, open the Serial Monitor by clicking on the magnifying glass icon or navigating to "Tools" > "Serial Monitor" in the Arduino IDE. Set the baud rate to 9600 (same as in the code).
Immerse the water sensor in a container filled with water, ensuring that the sensor is completely submerged. Observe the values printed in the Serial Monitor. Note down the analog readings.
You will see that Analog Reading starts from 0 and ends till 700. As shown on Serial Monitor.
Step 5: Calibrate the Sensor
Now that you have gathered the calibration data, it's time to calibrate the sensor in the code.
I have calibrated my Water sensor and code is below:
int sensorval = 0; // holds the value int sensorpin = A0; // sensor pin used void setup() { Serial.begin(9600); pinMode(sensorpin, INPUT); } void loop() { sensorval = analogRead(sensorpin); if (sensorval<=200) { Serial.println("Water Level: Empty"); } else if (sensorval>=200 && sensorval<=450) { Serial.println("Water Level: Low"); } else if (sensorval>=450 && sensorval<=620) { Serial.println("Water Level: Medium"); } else if (sensorval>620) { Serial.println("Water Level: High"); } delay(1000); }
Step 6: Upload the Updated Code and Test
Upload the modified code to the Arduino board once again. Open the Serial Monitor and observe the readings. The water level should now be displayed accurately.
Watch the video:
Conclusion:
Calibrating a water sensor is crucial for obtaining accurate water level measurements in your projects. By following this step-by-step guide, you have learned how to calibrate a water sensor using an Arduino board. Remember to repeat the calibration process if you change water sensors or alter the setup. With calibrated water sensors, you can now confidently incorporate water level measurements into your projects, whether it's for irrigation systems, aquariums, or other water-related applications.
0 Comments