Arduino Nano

Arduino basic Tutorial

Blynk iot Tutorial

Visit all the Links

ArduinoTutorial by Manmohan Pal

Ultrasonic Water level using Blynk iot

Ultrasonic Water level using Blynk iot








Building an Ultrasonic Distance Meter using Blynk IoT is a fun project that involves using an ultrasonic sensor to measure distance and then sending the data to a Blynk app on your smartphone for real-time monitoring. You'll need an Arduino (or compatible board), an HC-SR04 Ultrasonic Sensor, and the Blynk App for the IoT integration.

Here’s a step-by-step tutorial to guide you through the process:

What You’ll Need:

  1. Arduino Board (Arduino Uno, Nano, or any compatible model)
  2. HC-SR04 Ultrasonic Sensor
  3. Jumper wires
  4. Breadboard (optional)
  5. Blynk Account (for mobile app interface)
  6. Wi-Fi Module (ESP8266/ESP32 if you're using a Wi-Fi-enabled Arduino or a standalone ESP module)
  7. Blynk App (installed on your smartphone)

Step 1: Setting Up Blynk

  1. Download the Blynk App: Go to your smartphone’s app store (Google Play or Apple App Store) and install the Blynk app.
  2. Create an Account: Open the app, create a new account, and log in.
  3. Create a New Project:
    • Tap on the "New Project" button.
    • Choose your board type (e.g., Arduino Uno, ESP32, ESP8266).
    • Select Wi-Fi as your connection type.
    • Tap Create Project. Blynk will generate an Auth Token, which you’ll receive in your email.
  4. Set Up Your Blynk Dashboard:
    • After creating your project, add a Value Display widget to show the distance readings.
    • Tap on the widget to configure it, set the unit to cm (centimeters).
    • You can also add a Button widget to control things like triggering the sensor, or setting thresholds for alerts.

Step 2: Wiring the Components

  1. Connect the Ultrasonic Sensor (HC-SR04) to your Arduino board:

    • VCC to 5V on Arduino.
    • GND to GND on Arduino.
    • Trig to Pin 9 (you can choose any digital pin).
    • Echo to Pin 10 (you can choose any digital pin).
  2. If using ESP8266/ESP32:

    • For ESP8266/ESP32, use the respective 3.3V pin for the sensor's VCC. The wiring otherwise remains the same.

Step 3: Installing Blynk Library and Code

  1. Install the Blynk Library:

    • Open the Arduino IDE.
    • Go to Sketch > Include Library > Manage Libraries.
    • Search for Blynk and install it.
  2. Install the ESP8266/ESP32 Library (if using these):

    • Go to Tools > Board > Board Manager, search for ESP8266 or ESP32, and install the necessary package.
  3. Code for the Ultrasonic Distance Meter:

    • Use the following code to read the distance from the ultrasonic sensor and send it to the Blynk app:
///////////////////////////////////////////////////////////////////////////////
#define BLYNK_TEMPLATE_ID "...................."
#define BLYNK_TEMPLATE_NAME "...................."
#define BLYNK_AUTH_TOKEN "....................."

// Comment this out to disable prints and save space
#define BLYNK_PRINT Serial


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#define trig D5   // Trig pin
#define echo D6
char auth[] = BLYNK_AUTH_TOKEN;

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Manmohan Pal";
char pass[] = "8989811397";
int depth =30;

BlynkTimer timer;
 
void waterlevel()
{
  digitalWrite(trig, LOW);
  delayMicroseconds(2);
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);
  long t = pulseIn(echo, HIGH);
  long cm = t / 29 / 2;
  Serial.println(cm);
  long level= depth-cm;
  if (level<0)
  level=0;
  level = map(level,0,depth-3,0,100);
  Blynk.virtualWrite(V0, level);
  Blynk.virtualWrite(V1, cm);
}

 
void setup()
{
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  timer.setInterval(10L, waterlevel);
 
}

void loop()
{
  Blynk.run();
  timer.run();
  }
///////////////////////////////////////////////////////////////////////////////

Step 4: Uploading the Code to the Arduino

  1. Select Your Board: Go to Tools > Board and select the appropriate board (Arduino Uno, ESP8266, ESP32, etc.).
  2. Select the Port: Go to Tools > Port and select the correct COM port for your board.
  3. Upload the Code: Click the Upload button in the Arduino IDE to upload the code to your board.

Step 5: Using the Blynk App

  1. Open the Blynk App: Launch the app on your smartphone.
  2. View the Distance: The Value Display widget will show the distance in real-time, updated by the ultrasonic sensor readings.
  3. Control the System (optional): If you’ve added any buttons for triggering or controlling the sensor, you can interact with the app to modify the behavior.

Step 6: Testing the Distance Meter

  1. Test the Ultrasonic Sensor: Place an object at a known distance from the sensor, and check if the Blynk app displays the correct value.
  2. Adjust the Sensor: If necessary, adjust the maxDistance or the triggering interval in the code.

Optional Enhancements:

  • Add Multiple Sensors: If you want to monitor multiple distances, use multiple ultrasonic sensors and corresponding virtual pins in Blynk.
  • Sound Alerts: You can also integrate a buzzer or LED that activates when the distance crosses a certain threshold.

Conclusion

Now, you’ve successfully created an ultrasonic distance meter that sends data to the Blynk IoT platform, enabling you to monitor the readings remotely on your smartphone!

No comments:

Post a Comment