Jump to content

IDPL - 15: IoT-Based Small Project

From cognifront
Revision as of 03:35, 27 August 2026 by CogWikiEditor (talk | contribs) (→Testing the System)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

IDPL - 9A: Distance Measurement Using Arduino Uno and Ultrasonic Sensor

Objective

To interface an ultrasonic sensor with an Arduino Uno and measure the distance of an object from the sensor.

Required Components

  • Arduino Uno
  • HC-SR04 ultrasonic sensor
  • USB cable
  • Jumper wires

Circuit Diagram

Board Connections

Pin Connections

Component Component Pin Arduino Uno Connection
HC-SR04 Ultrasonic Sensor VCC 5V
GND GND
TRIG Digital Pin 9
ECHO Digital Pin 10

Working Principle

The HC-SR04 ultrasonic sensor measures distance using ultrasonic sound waves.

The Arduino sends a short trigger pulse to the TRIG pin. The ultrasonic sensor then transmits an ultrasonic sound wave.

When the sound wave strikes an object, it is reflected back towards the sensor. The sensor measures the time required for the reflected wave to return.

The distance is calculated using:

Distance = (Time × Speed of Sound) / 2

The division by 2 is required because the sound wave travels from the sensor to the object and then back to the sensor.

Experiment Code

// © 2026, Cognifront. Member of The IoT Kits Family.

// Experiment: Distance Measurement Using Arduino Uno
// HC-SR04 Ultrasonic Sensor

#define TRIG_PIN 9
#define ECHO_PIN 10

long duration;
float distance;

void setup() {

  Serial.begin(9600);

  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);

  Serial.println("Ultrasonic Sensor Ready");
}

void loop() {

  // Generate trigger pulse
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);

  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);

  digitalWrite(TRIG_PIN, LOW);

  // Measure echo pulse duration
  duration = pulseIn(ECHO_PIN, HIGH);

  // Calculate distance in centimetres
  distance = duration * 0.0343 / 2;

  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");

  delay(500);
}

Testing the System

  1. Connect the HC-SR04 ultrasonic sensor according to the connection table.
  2. Upload the program to the Arduino Uno.
  3. Open the Serial Monitor.
  4. Set the baud rate to 9600.
  5. Place an object in front of the ultrasonic sensor.
  6. Observe the distance displayed in centimetres.
  7. Move the object closer or farther away.
  8. Observe the change in the displayed distance.

Observation

  • The Arduino transmits a trigger signal to the ultrasonic sensor.
  • The sensor measures the time taken by the ultrasonic wave to return.
  • The distance is displayed in the Serial Monitor.
  • Moving the object changes the measured distance.

Result

The distance of an object from the HC-SR04 ultrasonic sensor was successfully measured using Arduino Uno.

Conclusion

This experiment demonstrates distance measurement using an HC-SR04 ultrasonic sensor and Arduino Uno. The sensor uses the reflection of ultrasonic sound waves to determine the distance of an object.


IDPL - 9B: Soil Moisture Monitoring Using Arduino Uno

Objective

To interface a soil moisture sensor with an Arduino Uno and monitor the moisture level of soil.

Required Components

  • Arduino Uno
  • Soil moisture sensor module
  • USB cable
  • Jumper wires

Circuit Diagram

Board Connections

Pin Connections

Component Component Pin Arduino Uno Connection
Soil Moisture Sensor VCC 5V
GND GND
AO Analog Pin A0

Working Principle

The soil moisture sensor measures the moisture level in soil by detecting changes in the electrical properties around the sensing probe.

The sensor provides an analog output that is read by the Arduino using Analog Pin A0.

The Arduino converts the analog signal into a value between:

0 to 1023

The measured value can be used to indicate whether the soil is relatively dry or wet.

Note: The actual analog values depend on the sensor type, soil type and moisture conditions. The threshold used in the program may need to be adjusted after observing the sensor values.

Experiment Code

// © 2026, Cognifront. Member of The IoT Kits Family.

// Experiment: Soil Moisture Monitoring Using Arduino Uno

#define SOIL_SENSOR A0

int moistureValue;

void setup() {

  Serial.begin(9600);

  Serial.println("Soil Moisture Sensor Ready");
}

void loop() {

  // Read analog value from soil moisture sensor
  moistureValue = analogRead(SOIL_SENSOR);

  Serial.print("Soil Moisture Value: ");
  Serial.println(moistureValue);

  // Example threshold
  if (moistureValue > 700) {

    Serial.println("Soil Status: DRY");
  }

  else {

    Serial.println("Soil Status: MOIST");
  }

  Serial.println();

  delay(1000);
}

Testing the System

  1. Connect the soil moisture sensor according to the connection table.
  1. Upload the program to the Arduino Uno.
  1. Open the Serial Monitor.
  1. Set the baud rate to 9600.
  1. Observe the moisture value when the sensor probe is dry.
  1. Insert the sensor probe into moist soil.
  1. Observe the change in the moisture value.
  1. Compare the readings for dry and moist conditions.

Observation

  • The sensor provides an analog moisture value to the Arduino.
  • The value changes according to the moisture condition around the sensor probe.
  • The program displays the soil moisture value in the Serial Monitor.
  • The soil is classified as DRY or MOIST according to the programmed threshold.

Result

The soil moisture level was successfully monitored using a soil moisture sensor and Arduino Uno.

Conclusion

This experiment demonstrates the basic principle of soil moisture monitoring using Arduino Uno. The analog output from the sensor is read and processed by the Arduino to indicate the condition of the soil.

IDPL - 9C: Submersible Pump Control Using Arduino Uno

Objective

To control a submersible water pump using an Arduino Uno through a suitable relay module.

Required Components

  • Arduino Uno
  • Submersible water pump
  • Single-channel relay module
  • Suitable external power supply for the pump
  • USB cable
  • Jumper wires

Circuit Diagram

Board Connections

Pin Connections

Component Component Pin Arduino Uno Connection
Single-Channel Relay IN Digital Pin 7
VCC 5V
GND GND

Pump Power Connection

The submersible pump must be powered using a suitable power supply matching the pump voltage and current requirements.

The relay is used as an electrical switch:

Pump Power Supply → Relay COM / NO → Pump

The exact power-side wiring depends on the type and voltage rating of the pump and relay.

Important: Do not power a submersible pump directly from an Arduino output pin. The Arduino pin cannot supply the current required by the pump.

Safety Note: Always verify the voltage and current ratings of the pump, relay and power supply before making the connections.

Working Principle

The Arduino controls the relay through Digital Pin 7.

When the relay is activated, the relay completes the pump power circuit and the pump starts operating.

When the relay is deactivated, the pump power circuit is disconnected and the pump stops.

Note: This program assumes an active-LOW relay module.

  • `LOW` on Digital Pin 7 → Pump ON
  • `HIGH` on Digital Pin 7 → Pump OFF

Experiment Code

// © 2026, Cognifront. Member of The IoT Kits Family.

// Experiment: Submersible Pump Control Using Arduino Uno
// Active-LOW Relay Module

#define RELAY_PIN 7

void setup() {

  Serial.begin(9600);

  pinMode(RELAY_PIN, OUTPUT);

  // Start with pump OFF
  digitalWrite(RELAY_PIN, HIGH);

  Serial.println("Submersible Pump Control Ready");
}

void loop() {

  // Turn pump ON
  Serial.println("Pump ON");

  digitalWrite(RELAY_PIN, LOW);

  delay(5000);

  // Turn pump OFF
  Serial.println("Pump OFF");

  digitalWrite(RELAY_PIN, HIGH);

  delay(5000);
}

Testing the System

  1. Connect the relay module to the Arduino according to the connection table.
  1. Connect the pump to its suitable external power supply through the relay.
  1. Ensure that all voltage and current ratings are correct.
  1. Upload the program to the Arduino Uno.
  1. Open the Serial Monitor at 9600 baud.
  1. Power the pump using its appropriate power supply.
  1. Observe that the pump turns ON for approximately 5 seconds.
  1. Observe that the pump turns OFF for approximately 5 seconds.
  1. Verify that the ON/OFF cycle repeats continuously.

Observation

  • The relay activates when the Arduino sends the ON control signal.
  • The submersible pump starts operating when the relay connects the pump power circuit.
  • The pump stops when the relay disconnects the power circuit.
  • The Serial Monitor displays the current pump status.

Result

The submersible water pump was successfully controlled using Arduino Uno and a relay module.

Conclusion

This experiment demonstrates the basic control of a submersible water pump using Arduino Uno. The Arduino provides the control signal, while the relay safely switches the pump power circuit.

IDPL - 9D: Real-Time Object Detection Using Raspberry Pi, USB Camera and OpenCV

Objective

To perform real-time object detection using a Raspberry Pi, a USB camera and OpenCV.

Required Components

  • Raspberry Pi 4
  • USB camera
  • Raspberry Pi power supply
  • MicroSD card with Raspberry Pi OS
  • Monitor, keyboard and mouse for setup
  • Python 3 environment
  • OpenCV Python library
  • Pre-trained object detection model files

System Diagram

Board Connections

Connections

Component Connection
USB Camera Raspberry Pi USB Port
Raspberry Pi Power Supply Raspberry Pi Power Input
Monitor Raspberry Pi HDMI Output

Software Setup

Open the Raspberry Pi terminal.

Install OpenCV if it is not already available:

pip install opencv-python

Verify the installation:

python3 -c "import cv2; print(cv2.__version__)"

The object detection program requires the following model files:

  • `MobileNetSSD_deploy.prototxt`
  • `MobileNetSSD_deploy.caffemodel`

Place both files in the same directory as the Python program.

Working Principle

The USB camera captures live video frames and sends them to the Raspberry Pi.

OpenCV reads each frame from the camera.

A pre-trained MobileNet SSD object detection model processes the video frame and identifies supported objects.

For every detected object:

  1. The object class is identified.
  1. A confidence value is calculated.
  1. A bounding box is drawn around the object.
  1. The object label and confidence percentage are displayed.

The processed video is then displayed in a real-time window.

Experiment Code

# © 2026, Cognifront. Member of The IoT Kits Family.

# Experiment: Real-Time Object Detection
# Raspberry Pi + USB Camera + OpenCV

import cv2
import numpy as np

# MobileNet SSD object classes
CLASSES = [
    "background", "aeroplane", "bicycle", "bird",
    "boat", "bottle", "bus", "car",
    "cat", "chair", "cow", "diningtable",
    "dog", "horse", "motorbike", "person",
    "pottedplant", "sheep", "sofa", "train",
    "tvmonitor"
]

PROTOTXT = "MobileNetSSD_deploy.prototxt"
MODEL = "MobileNetSSD_deploy.caffemodel"

# Load the pre-trained model
net = cv2.dnn.readNetFromCaffe(PROTOTXT, MODEL)

# Open USB camera
camera = cv2.VideoCapture(0)

if not camera.isOpened():
    print("Error: USB camera not detected")
    exit()

print("Object Detection Started")
print("Press Q to quit")

while True:

    # Capture frame
    ret, frame = camera.read()

    if not ret:
        print("Error: Unable to read camera frame")
        break

    # Get frame dimensions
    height, width = frame.shape[:2]

    # Prepare frame for MobileNet SSD
    blob = cv2.dnn.blobFromImage(
        cv2.resize(frame, (300, 300)),
        0.007843,
        (300, 300),
        127.5
    )

    # Run object detection
    net.setInput(blob)
    detections = net.forward()

    # Process detections
    for i in range(detections.shape[2]):

        confidence = detections[0, 0, i, 2]

        # Minimum confidence threshold
        if confidence > 0.5:

            class_id = int(detections[0, 0, i, 1])

            label = CLASSES[class_id]

            box = detections[0, 0, i, 3:7] * np.array(
                [width, height, width, height]
            )

            start_x, start_y, end_x, end_y = box.astype("int")

            # Draw bounding box
            cv2.rectangle(
                frame,
                (start_x, start_y),
                (end_x, end_y),
                (0, 255, 0),
                2
            )

            # Display object name and confidence
            text = f"{label}: {confidence * 100:.1f}%"

            cv2.putText(
                frame,
                text,
                (start_x, max(start_y - 10, 20)),
                cv2.FONT_HERSHEY_SIMPLEX,
                0.5,
                (0, 255, 0),
                2
            )

    # Display result
    cv2.imshow("Raspberry Pi Object Detection", frame)

    # Press Q to quit
    if cv2.waitKey(1) & 0xFF == ord("q"):
        break

# Release resources
camera.release()
cv2.destroyAllWindows()

Running the Program

Save the Python program in the same directory as the two model files.

Open the terminal in that directory and run:

python3 object_detection.py

The USB camera will start capturing live video.

A bounding box and label will appear around detected objects.

Press:

Q

to stop the program.

Testing the System

  1. Connect the USB camera directly to the Raspberry Pi.
  1. Power ON the Raspberry Pi.
  1. Open the terminal.
  1. Verify that the USB camera is detected.
  1. Ensure that OpenCV and NumPy are installed.
  1. Place the model files and Python program in the same directory.
  1. Run the Python program.
  1. Place supported objects in front of the camera.
  1. Observe the detected object name and confidence percentage.
  1. Verify that a bounding box is drawn around the detected object.
  1. Press Q to close the object detection window.

Observation

  • The USB camera captures live video.
  • The Raspberry Pi processes the captured frames using OpenCV.
  • Supported objects are detected by the pre-trained model.
  • A bounding box appears around detected objects.
  • The object name is displayed.
  • The confidence percentage is displayed.
  • Pressing Q stops the program.

Result

Real-time object detection was successfully performed using a Raspberry Pi, USB camera and OpenCV with a pre-trained MobileNet SSD model.

Conclusion

This experiment demonstrates real-time computer vision and object detection using Raspberry Pi. The USB camera captures live video, while OpenCV and the pre-trained MobileNet SSD model process the video frames and identify supported objects.

This is a completely independent Raspberry Pi-based experiment and does not require an Arduino connection.