0% found this document useful (0 votes)
10 views6 pages

Exp3 LED Relay Arduino

The document outlines an experiment conducted at the Indian Institute of Information Technology, Bhopal, focusing on digital input and output interfacing using Arduino. It details the objectives, theory, apparatus, circuit design, and programming for controlling an LED and a relay module with a push button. The experiment successfully demonstrated the principles of hardware control and event-driven programming, confirming the functionality through simulation in Tinkercad.

Uploaded by

fords931580
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views6 pages

Exp3 LED Relay Arduino

The document outlines an experiment conducted at the Indian Institute of Information Technology, Bhopal, focusing on digital input and output interfacing using Arduino. It details the objectives, theory, apparatus, circuit design, and programming for controlling an LED and a relay module with a push button. The experiment successfully demonstrated the principles of hardware control and event-driven programming, confirming the functionality through simulation in Tinkercad.

Uploaded by

fords931580
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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

Common questions

Powered by AI

Interfacing a push button, LED, and relay with an Arduino Uno imparts practical knowledge in several areas: understanding digital input (push button) and output (LED and relay) interfacing, implementing control logic in Arduino code, and deploying non-blocking event-driven programming using millis(). It emphasizes the role of relays in safely controlling high-power circuits and enhances problem-solving skills in configuring and troubleshooting embedded systems. This knowledge is foundational for developing more complex automation projects .

The pinMode() function in Arduino programming is essential for configuring digital pins as either INPUT or OUTPUT. This setting determines the pin's function — whether it reads external signals or sends signals to control other devices. For instance, a pin set as INPUT with INPUT_PULLUP is used for reading states like those from a push button, while a pin set as OUTPUT can switch LEDs or control relays, enabling versatile hardware interfacing .

Using millis() improves the implementation of a responsive interface by allowing non-blocking code execution. Unlike delay(), which halts code execution entirely for a specified time, millis() provides timestamps to manage time-based events. This enables the microcontroller to perform other tasks continuously, such as reading other inputs or updating outputs, leading to more efficient and responsive program behavior, especially when distinguishing between short and long button presses .

A relay module comprises a coil, armature, and contacts (NO, NC, COM). The coil, energized by a low-power control signal, generates a magnetic field attracting the armature—a movable metal strip. This action switches the contacts: Normally Open (NO) closes, and Normally Closed (NC) opens (or vice versa depending on the relay configuration). The armature and contacts facilitate the switching of a high-power circuit, while isolating it electrically from the control circuitry, making relays suitable for switching different voltage levels safely .

The primary components required for interfacing with an Arduino Uno include a push button, an LED with a current-limiting resistor (220Ω to 330Ω), and a 5V single-channel relay module. The connections are as follows: Connect one terminal of the push button to digital pin 2 (configured as INPUT_PULLUP) and the other terminal to GND. The anode of the LED is connected to digital pin 13, and the cathode to GND. For the relay module, connect VCC to 5V, GND to GND, and the IN pin to digital pin 7 .

The distinction between a short press and a long press enhances functionality by allowing a single input device to perform multiple actions. For instance, a short press can toggle an LED, while a long press can control a relay. This differentiation reduces the need for additional buttons or controls, making the system more efficient. It utilizes the millis() function to measure press duration, enabling non-blocking, responsive interactions without relying on delay(), which could hinder real-time system responsiveness .

Using Tinkercad for simulating an Arduino interface circuit provides several advantages: it allows for visualization of circuit design, enabling detection and correction of potential errors before physical implementation. This approach saves time and reduces the risk of hardware damage or incorrect wiring. Tinkercad also facilitates iterative testing of program logic and its impact on connected components, ensuring correct behavior such as LED and relay operations according to planned logic .

A current-limiting resistor is essential when interfacing an LED with Arduino to prevent excessive current flow, which can damage the LED. The resistor limits the current to safe levels, ensuring the LED lights up without overheating or burning out. Typically, a 220Ω to 330Ω resistor is used. This precaution mitigates risks associated with electrical failures and protects both the LED and the microcontroller from over-current damage .

A relay acts as a remotely controlled switch. It receives a small electrical signal from a microcontroller, which energizes an internal coil to create a magnetic field. This field pulls a metal armature, causing the switch contacts to connect or disconnect a high-voltage circuit. The relay effectively isolates the control circuit (low power) from the controlled circuit (high power), allowing for safe switching of high-power devices without direct electrical connection. In industrial automation, relays are crucial for controlling large machinery and ensuring safety by isolating different power levels .

A flyback diode is used in a relay module to protect the microcontroller from voltage spikes caused by the collapsing magnetic field of the coil when the relay is de-energized. This diode is placed in parallel with the coil, providing a path for the back EMF (Electromotive Force) to dissipate safely, thus preventing potential damage to the control circuitry from high-voltage transients .

You might also like