Course Content
📘 MODULE 9 – Automatic Dustbin
0/1
📘 MODULE 10 – Obstacle Avoiding Robot
0/1
📘 MODULE 11 – Edge Avoiding Robot
0/1
Arduino Hands-On Programming and Robotics Course

📘 MODULE 7 – Automatic Water Dispenser

Lesson 7.3 – Relay + Pump Control Code


Learning Objectives

By the end of this lesson, students will be able to:

  • Write Arduino code to control a relay using an IR sensor.
  • Understand how sensor input controls a water pump.
  • Learn how digitalRead() and digitalWrite() functions are used.
  • Control a relay module through Arduino.
  • Automate water dispensing using object detection.
  • Test and verify pump activation and deactivation.
  • Troubleshoot common programming mistakes.

Main Content

Introduction

In the previous lesson, we completed the hardware connections for the Automatic Water Dispenser.

The circuit contains:

  • Arduino UNO
  • IR Sensor
  • Relay Module
  • Water Pump

Now we need a program that can:

  1. Read the IR sensor.
  2. Detect a hand or bottle.
  3. Turn ON the relay.
  4. Start the water pump.
  5. Turn OFF the relay when the object is removed.

This creates a completely automatic and touchless water dispensing system.


Project Working Logic

The system follows a simple logic:

 
Object Detected



IR Sensor Activated



Arduino Reads Sensor



Relay ON



Pump ON



Water Dispensing



Object Removed



Relay OFF



Pump OFF
 

Pin Configuration

For this lesson, we will use:

Component Arduino Pin
IR Sensor OUT D2
Relay IN D8

Complete Arduino Program

const int irSensor = 2;
const int relayPin = 8;

void setup()
{
  pinMode(irSensor, INPUT);
  pinMode(relayPin, OUTPUT);

  digitalWrite(relayPin, LOW);

  Serial.begin(9600);
  Serial.println("IR Sensor Relay Control Started");
}

void loop()
{
  int sensorState = digitalRead(irSensor);

  Serial.print("IR Sensor Value: ");
  Serial.println(sensorState);

  if(sensorState == LOW)
  {
    digitalWrite(relayPin, HIGH);
    Serial.println("Object Detected -> Relay ON");
  }
  else
  {
    digitalWrite(relayPin, LOW);
    Serial.println("No Object Detected -> Relay OFF");
  }

  Serial.println("--------------------");
  delay(500);
}

Understanding the Code

Let us understand the program section by section.


Step 1: Declare Pin Numbers

 
const int irSensor = 2;
const int relayPin = 8;
 

These variables store the pin numbers.

IR Sensor

 
const int irSensor = 2;
 

The IR sensor output is connected to Arduino Digital Pin 2.


Relay Module

 
const int relayPin = 8;
 

The relay control pin is connected to Arduino Digital Pin 8.


Step 2: Setup Function

 
void setup()
{
pinMode(irSensor, INPUT);
pinMode(relayPin, OUTPUT);

digitalWrite(relayPin, LOW);
}
 

The setup() function runs only once when Arduino starts.


Configure IR Sensor Pin

 
pinMode(irSensor, INPUT);
 

The IR sensor sends signals to Arduino.

Therefore, it is configured as INPUT.


Configure Relay Pin

 
pinMode(relayPin, OUTPUT);
 

Arduino controls the relay.

Therefore, the relay pin is configured as OUTPUT.


Keep Relay OFF Initially

 
digitalWrite(relayPin, LOW);
 

This ensures:

 
Relay OFF
Pump OFF
 

when the system starts.


Step 3: Read Sensor Value

 
int sensorState = digitalRead(irSensor);
 

The digitalRead() function reads the sensor output.

Possible values:

 
HIGH
LOW
 

The value is stored in:

 
sensorState
 

Understanding IR Sensor Output

Most IR obstacle sensors behave like this:

Condition Output
Object Detected LOW
No Object HIGH

Always verify your sensor because some models may behave differently.


Step 4: Object Detection Logic

 
if(sensorState == LOW)
 

This condition checks whether an object is detected.

If TRUE:

 
Hand Detected
Bottle Detected
Glass Detected
 

then Arduino executes the code inside the if block.


Step 5: Turn ON Relay

 
digitalWrite(relayPin, HIGH);
 

When an object is detected:

 
Relay ON
 

The relay closes its switching contacts.

Power reaches the pump.


Result

 
Pump ON



Water Flow Starts
 

Step 6: Turn OFF Relay

 
else
{
digitalWrite(relayPin, LOW);
}
 

When no object is detected:

 
Relay OFF
 

Power is removed from the pump.


Result

 
Pump OFF



Water Flow Stops
 

Program Flowchart

 
Start



Read IR Sensor



Object Detected?

┌─────Yes─────┐
│ │
↓ │

Relay ON │

↓ │

Pump ON │

│ │
└────No───────┘



Relay OFF



Pump OFF



Repeat
 

Practical Example

Suppose a bottle is placed under the dispenser.

Sensor output:

 
LOW
 

Arduino executes:

 
digitalWrite(relayPin, HIGH);
 

Result:

 
Relay ON
Pump ON
Water Dispensing
 

When the bottle is removed:

Sensor output:

 
HIGH
 

Arduino executes:

 
digitalWrite(relayPin, LOW);
 

Result:

 
Relay OFF
Pump OFF
Water Stops
 

Adding Serial Monitor for Testing

During testing, it is useful to display the sensor status.

Example:

 
Serial.begin(9600);
 

Inside setup().


Inside loop():

 
Serial.println(sensorState);
 

This helps verify whether the sensor is detecting objects correctly.


Enhanced Testing Program

 
const int irSensor = 2;
const int relayPin = 8;

void setup()
{
pinMode(irSensor, INPUT);
pinMode(relayPin, OUTPUT);

Serial.begin(9600);

digitalWrite(relayPin, LOW);
}

void loop()
{
int sensorState = digitalRead(irSensor);

Serial.println(sensorState);

if(sensorState == LOW)
{
digitalWrite(relayPin, HIGH);
}
else
{
digitalWrite(relayPin, LOW);
}

delay(100);
}
 

Common Programming Mistakes

Mistake 1: Wrong Pin Number

Wrong:

 
const int relayPin = 7;
 

while wiring uses Pin 8.

Always match code and hardware.


Mistake 2: Missing pinMode()

Wrong:

 
void setup()
{
}
 

Without pinMode(), the program may behave unpredictably.


Mistake 3: Missing Brackets

Wrong:

 
if(sensorState == LOW)

digitalWrite(relayPin, HIGH);
 

Always use braces for clarity.

Correct:

 
if(sensorState == LOW)
{
digitalWrite(relayPin, HIGH);
}
 

Mistake 4: Reversed Logic

Some relay modules are Active LOW.

In such modules:

 
digitalWrite(relayPin, LOW);
 

turns ON the relay.

Always test your relay module.


Testing Procedure

Step 1

Upload the code.


Step 2

Power the system.


Step 3

Place a hand near the sensor.

Expected:

 
Relay Click
Pump ON
 

Step 4

Remove the hand.

Expected:

 
Relay OFF
Pump OFF
 

Step 5

Repeat several times.

Verify stable operation.


Real-World Examples

Office Water Dispenser

Automatically dispenses water when a bottle is placed under the nozzle.


School Drinking Water Station

Students receive water without touching taps.


Hospital Water System

Reduces contact and improves hygiene.


Smart Beverage Machine

Detects cups automatically before dispensing.


Public Water Stations

Provides touchless access to drinking water.


Best Practices

Test Relay Before Connecting Pump

Verify switching operation first.


Use Stable Power Supply

Pump performance depends on adequate power.


Protect Electronics from Water

Keep the Arduino and relay away from spills.


Organize Wiring

Neat wiring improves reliability.


Verify Sensor Detection Range

Adjust the sensor sensitivity if required.


Summary

In this lesson, we created the Arduino program for the Automatic Water Dispenser. The IR sensor detects a hand, bottle, or glass and sends a signal to Arduino. Arduino processes the signal and activates the relay. The relay supplies power to the water pump, allowing water to flow automatically. When the object is removed, the pump turns OFF. This simple logic creates a fully automated touchless dispensing system.


Key Terms

  • IR Sensor
  • Relay Module
  • Water Pump
  • digitalRead()
  • digitalWrite()
  • INPUT
  • OUTPUT
  • Automation Logic
  • Object Detection
  • Touchless System

Quiz

1. Which Arduino pin receives the IR sensor signal?

A. D8

B. D2

C. D13

D. A0

Answer: B


2. Which Arduino pin controls the relay?

A. D8

B. D2

C. A1

D. D12

Answer: A


3. Which function reads the sensor state?

A. analogWrite()

B. digitalWrite()

C. digitalRead()

D. Serial.print()

Answer: C


4. What happens when the relay turns ON?

A. Sensor stops

B. Pump receives power

C. Arduino restarts

D. Code stops

Answer: B


5. What is the purpose of the IR sensor?

A. Measure temperature

B. Detect objects

C. Store data

D. Increase voltage

Answer: B


Assignment

Practical Activity

Build the complete Automatic Water Dispenser circuit and upload the program.

Perform the following tests:

Test Expected Result
Hand Near Sensor Pump ON
Bottle Near Sensor Pump ON
Glass Near Sensor Pump ON
Object Removed Pump OFF

Write your observations and note whether the relay and pump responded correctly.

Scroll to Top