INDIAN INSTITUTE OF INFORMATION
TECHNOLOGY, BHOPAL
Experiment 3
Subject: Internet of Things (IoT) (IT-322)
Branch: Information Technology
6th Semester
Submitted by: Submitted to:
Manish kumar Ms. Pradnya Gajbhiye
Scholar no.: 23U03065 Assistant Professor, IIIT Bhopal
Embedded Systems Lab | Arduino Interfacing Page 1
1. Aim / Objectives
The aim of this experiment is to understand digital input and output interfacing using Arduino by
designing and implementing a circuit that uses a push button to control an LED and a relay
module.
Specific Objectives:
• Understand digital input and output interfacing using Arduino.
• Interface a push button as an input device.
• Control an LED and a relay module as output devices.
• Develop an Arduino program to control LED and relay using a push button.
• Learn basic concepts of hardware control, switching, and automation.
• Modify the program to control only LED on single press and relay on long press.
• Explain relay working principle in own words.
2. Theory
2.1 Arduino Uno
Arduino Uno is an open-source microcontroller board based on the ATmega328P microcontroller.
It has 14 digital input/output pins (of which 6 can be used as PWM outputs), 6 analog inputs, a 16
MHz quartz crystal, a USB connection, a power jack, an ICSP header, and a reset button. It is
widely used for learning embedded systems and prototyping electronic projects. Digital pins can
be configured as INPUT or OUTPUT using the pinMode() function in the Arduino IDE.
2.2 Push Button
A push button is a simple momentary switch that completes or breaks a circuit when pressed. In
Arduino projects, push buttons are commonly used as digital input devices. When a push button is
connected between a digital pin and GND with an internal pull-up resistor enabled
(INPUT_PULLUP), the pin reads HIGH when the button is not pressed and LOW when the button
is pressed. Debouncing techniques are used to eliminate false triggering due to mechanical
bouncing of the switch contacts.
2.3 LED (Light Emitting Diode)
An LED is a semiconductor device that emits light when current flows through it. In Arduino
interfacing, an LED is connected to a digital output pin through a current-limiting resistor (typically
220Ω to 330Ω). When the digital pin is set HIGH (5V), current flows through the LED and it lights
up. When the pin is set LOW (0V), the LED turns off. LEDs are used as indicator lights and for
visual output in embedded systems.
2.4 Relay Module
A relay is an electrically operated switch that uses an electromagnet to mechanically operate a
switch mechanism. It allows a low-power signal from a microcontroller (like Arduino, which
operates at 5V) to control a high-power circuit (like AC appliances operating at 220V). The relay
module consists of:
• Coil: Electromagnet energized by the Arduino control signal.
• Armature: A movable metal strip attracted by the electromagnet.
• Contacts: Normally Open (NO), Normally Closed (NC), and Common (COM) terminals.
When the Arduino sends a HIGH or LOW signal (depending on the relay type – active HIGH or
active LOW), the coil is energized, the armature moves, and the NO contact closes (or NC contact
opens), switching the high-power circuit. A flyback diode is included in relay modules to protect
the microcontroller from voltage spikes generated by the collapsing magnetic field of the coil.
2.5 Relay Working Principle (In Own Words)
A relay works like a remote-controlled switch. When I give a small electrical signal from Arduino to
the relay's control pin, it energizes a coil inside the relay. This coil acts like a magnet and pulls a
Embedded Systems Lab | Arduino Interfacing Page 2
metal arm (armature) towards it. This mechanical movement causes the main switch contacts to
either connect or disconnect a completely separate high-voltage circuit. When the signal is
removed, a spring pushes the armature back to its original position. This way, a small Arduino
signal can safely switch ON or OFF a high-voltage device like a bulb or fan without any direct
electrical connection between the two circuits. The relay essentially acts as an electrical isolator
and a power amplifier for the control signal.
2.6 Single Press vs Long Press Logic
To distinguish between a single press and a long press, the Arduino records the time when the
button is first pressed using millis(). When the button is released, it checks the elapsed time. If the
duration is less than a defined threshold (e.g., 1000 ms), it is treated as a single press and only
the LED toggles. If the duration is greater than or equal to the threshold, it is treated as a long
press and the relay toggles. This technique avoids the use of delay() and keeps the system
responsive.
3. Apparatus / Software Required
3.1 Hardware Components
Sr. Component Specification Quantity
1 Arduino Uno ATmega328P, 5V 1
2 USB Cable Type A to Type B 1
3 Push Button Tactile Switch, 1
momentary
4 LED Red/Green, 5mm 1
5 Relay Module 5V Single Channel 1
6 Resistor 220Ω (for LED) 1
7 Resistor 10kΩ (pull-down, 1
optional)
8 Breadboard 830 tie-point 1
9 Jumper Wires Male-to-male As required
3.2 Software Required
• Arduino IDE (Version 2.x or later) – for writing and uploading Arduino code.
• Tinkercad (online) – for circuit design, simulation and screenshot capture.
• Web Browser (Chrome / Firefox) – for accessing Tinkercad.
4. Circuit Diagram
The circuit was designed and simulated using Tinkercad. The following connections were made:
4.1 Circuit Connections
Sr. Component Component Pin Arduino Pin
1 Push Button One terminal Digital Pin 2
(INPUT_PULLUP)
2 Push Button Other terminal GND
3 LED (Anode) +ve leg (via 220Ω) Digital Pin 13
4 LED (Cathode) –ve leg GND
Embedded Systems Lab | Arduino Interfacing Page 3
5 Relay Module VCC 5V
6 Relay Module GND GND
7 Relay Module IN (Signal) Digital Pin 7
5. Arduino Source Code
5.1 Basic Program – Push Button Controls LED and Relay
The following program toggles the LED and relay together on every button press:
// Basic Program – LED and Relay control with Push Button
const int buttonPin = 2; // Push button connected to pin 2
const int ledPin = 13; // LED connected to pin 13
const int relayPin = 7; // Relay module control pin
bool ledState = false;
bool relayState = false;
bool lastButtonState = HIGH;
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, HIGH); // Relay OFF (active LOW module)
[Link](9600);
}
void loop() {
bool currentButton = digitalRead(buttonPin);
if (lastButtonState == HIGH && currentButton == LOW) {
delay(50); // debounce
ledState = !ledState;
relayState = !relayState;
digitalWrite(ledPin, ledState ? HIGH : LOW);
digitalWrite(relayPin, relayState ? LOW : HIGH); // Active LOW
[Link]("LED: ");
[Link](ledState ? "ON" : "OFF");
[Link](" | Relay: ");
[Link](relayState ? "ON" : "OFF");
}
lastButtonState = currentButton;
}
5.2 Modified Program – Single Press = LED Only, Long Press = Relay
The following modified program uses press duration to distinguish between controlling only the
LED (short press < 1000ms) and controlling only the relay (long press ≥ 1000ms):
// Modified Program – Single Press: LED | Long Press: Relay
const int buttonPin = 2;
const int ledPin = 13;
const int relayPin = 7;
const long longPressTime = 1000; // 1 second threshold
bool ledState = false;
bool relayState = false;
bool buttonPressed = false;
unsigned long pressStartTime = 0;
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
Embedded Systems Lab | Arduino Interfacing Page 4
pinMode(ledPin, OUTPUT);
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, HIGH); // Relay OFF initially
[Link](9600);
}
void loop() {
bool currentButton = digitalRead(buttonPin);
// Button just pressed (falling edge)
if (currentButton == LOW && !buttonPressed) {
buttonPressed = true;
pressStartTime = millis();
}
// Button just released (rising edge)
if (currentButton == HIGH && buttonPressed) {
buttonPressed = false;
unsigned long pressDuration = millis() - pressStartTime;
if (pressDuration < longPressTime) {
// SHORT PRESS: Toggle LED only
ledState = !ledState;
digitalWrite(ledPin, ledState ? HIGH : LOW);
[Link]("Short Press → LED: ");
[Link](ledState ? "ON" : "OFF");
} else {
// LONG PRESS: Toggle Relay only
relayState = !relayState;
digitalWrite(relayPin, relayState ? LOW : HIGH);
[Link]("Long Press → Relay: ");
[Link](relayState ? "ON" : "OFF");
}
}
}
6. Simulation Output
The circuit was simulated in Tinkercad. The following observations were recorded:
6.1 Observation Table
Sr. Input (Button) Press Type LED State Relay State
1 Not Pressed – OFF OFF
2 Pressed (< 1 sec) Single / Short Press ON (Toggle) No Change
3 Pressed (≥ 1 sec) Long Press No Change ON (Toggle)
4 Pressed Again (Short) Single Press OFF (Toggle) No Change
5 Pressed Again (Long) Long Press No Change OFF (Toggle)
7. Result
The experiment was successfully completed. The following results were achieved:
• A push button was successfully interfaced as a digital input to the Arduino Uno.
• An LED was controlled as a digital output, toggling ON and OFF on each short press of the
push button.
• A 5V relay module was successfully interfaced and controlled using the Arduino output pin.
• The modified program accurately distinguished between a single (short) press and a long
press using the millis() function, eliminating the use of delay().
Embedded Systems Lab | Arduino Interfacing Page 5
• Single press (< 1000 ms): Only the LED toggles its state. The relay remains unchanged.
• Long press (≥ 1000 ms): Only the relay toggles its state. The LED remains unchanged.
• The simulation in Tinkercad confirmed correct LED and relay behavior as per the program
logic.
• Serial Monitor output confirmed the detection of press type and corresponding state
changes.
8. Conclusion
This experiment provided a comprehensive understanding of digital input/output interfacing using
Arduino Uno. By interfacing a push button, LED, and relay module, practical knowledge of
hardware control and switching was gained. The basic program demonstrated how a
microcontroller can simultaneously control multiple output devices using a single input. The
modified program introduced the concept of event-driven programming by using press duration
(millis() function) to perform different actions without blocking the microcontroller with delay() calls.
The relay working principle was clearly understood — it acts as an electrically isolated switch
where a small microcontroller signal controls a large power circuit through electromagnetic action.
This concept is fundamental in industrial automation, home automation (IoT), and embedded
systems design. The use of Tinkercad for simulation proved effective in validating circuit designs
before physical implementation, saving time and reducing hardware errors.
Overall, the objectives of the experiment were fully met, and the knowledge gained forms a strong
foundation for more advanced embedded systems projects.
Embedded Systems Lab | Arduino Interfacing Page 6