Jump to content

IPDL - 15: IoT-Based Small Project

From cognifront

IPDL - 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 4
ECHO Digital Pin 6

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

int trigPin = 4;
int echoPin = 6;

void setup()
{
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  Serial.begin(9600);
  delay(5000);
}

void loop()
{
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);            //set the trigger pin high to provide a bus;
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  int duration = pulseIn(echoPin, HIGH);  //used to check duration of wave (tx & rx);
  float distance = duration * 0.034 / 2;
  if(distance >= 0)
  {
    Serial.print("Distance in cm is: ");
    Serial.println(distance);
  }
  
  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.


IPDL - 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
/* Change these values based on your observations */
#define wetSoil 277   // Define max value we consider soil 'wet'
#define drySoil 380   // Define min value we consider soil 'dry'

// Define analog input
#define sensorPin A0

void setup() {  
  Serial.begin(9600);
  pinMode(sensorPin, INPUT);
}

void loop() {
  // Read the Analog Input and print it
  int moisture = analogRead(sensorPin);
  Serial.print("Analog output: ");
  Serial.println(moisture);
  
  // Determine status of our soil
  if (moisture < wetSoil) {
    Serial.println("Status: Soil is too wet");
  } else if (moisture >= wetSoil && moisture < drySoil) {
    Serial.println("Status: Soil moisture is perfect");
  } else {
    Serial.println("Status: Soil is too dry - time to water!");
  }
  Serial.println();
  
  // Take a reading every second
  delay(1000);
}

Testing the System

  1. Connect the soil moisture 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. Observe the moisture value when the sensor probe is dry.
  6. Insert the sensor probe into moist soil.
  7. Observe the change in the moisture value.
  8. 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.


IPDL - 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.
  2. Connect the pump to its suitable external power supply through the relay.
  3. Ensure that all voltage and current ratings are correct.
  4. Upload the program to the Arduino Uno.
  5. Open the Serial Monitor at 9600 baud.
  6. Power the pump using its appropriate power supply.
  7. Observe that the pump turns ON for approximately 5 seconds.
  8. Observe that the pump turns OFF for approximately 5 seconds.
  9. 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.

IPDL - 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

Working Principle

  1. Install Python 3.11
  2. Verify Python 3.11
  3. Create cv_project folder
  4. Enter ~/cv_project
  5. Create cv_env using Python 3.11
  6. Activate cv_env
  7. Upgrade pip
  8. Install PyTorch CPU version
  9. Install Ultralytics
  10. Install remaining dependencies
  11. Verify YOLO installation
  12. Download/load yolov8n.pt
  13. Run image object detection test
  14. Check /dev/video0 camera
  15. Run live YOLO camera detection
  16. Later, reactivate the environment and run your programs

Set Up and running

Basic Setup for Object Detection. Run the commands below one by one: Install Python 3.11

sudo apt update
sudo apt install -y python3.11 python3.11-venv

Check Python 3.11

python3.11 --version

Go to home directory

cd ~

Create a project folder

mkdir -p cv_project

Enter the project folder

cd ~/cv_project

Create virtual environment

python3.11 -m venv cv_env

Activate virtual environment

source cv_env/bin/activate

Upgrade pip

python -m pip install --upgrade pip

After this, you should see: (cv_env) pi@raspberrypi:~/cv_project $

Install PyTorch Library from the URL

pip install torch torchvision --index-url https://download.pytorch.org/whl/cpu

Install Ultralytics

pip install ultralytics --no-deps

Install other dependencies

pip install matplotlib pyyaml requests scipy tqdm psutil pandas ultralytics-thop
pip install polars ultralytics-platform
pip install "opencv-python>=4.7.0"

Check if YOLO is installed successfully

python -c "from ultralytics import YOLO; print('YOLO OK')"

Load YOLOv8n

python -c "from ultralytics import YOLO; model=YOLO('yolov8n.pt'); print('YOLOv8 Nano model loaded successfully')"

Next: run actual object detection Create a file using the console itself

nano yolo_test.py

Paste the complete code, copy and paste from below

from ultralytics import YOLO
model = YOLO("yolov8n.pt")
results = model("https://ultralytics.com/images/bus.jpg", save=True)
print("Detection completed!")

After pasting the code, to save, Press: Ctrl + O → Enter → Ctrl + X

Run Bash:

python yolo_test.py

After successful test 1. Check the camera device Run:

ls /dev/video*

If you see: /dev/video0

2. Test live YOLOv8n detection Create the program: Create a file using the console itself

nano yolo_camera.py

Paste the complete code, copy and paste from below

from ultralytics import YOLO
model = YOLO("yolov8n.pt")
model.predict(
    source=0,
    show=True,
    conf=0.5
)

To Save, Press: Ctrl + O → Enter → Ctrl + X

Run Bash:

python yolo_camera.py

Press q to stop the live detection.

Next time you want to activate it:

cd ~/cv_project
source ~/cv_project/cv_env/bin/activate

And the run the bash files as usual

To exit the environment:

deactivate

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 YOLO 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 YOLO model process the video frames and identify supported objects.