Worksheet 1.2.
2
Student Name: Priyanka Gami UID: 23MCA20134
Branch: MCA Section/Group: 6/B
Semester: 3rd Date of Performance: 05/08/2024
Subject Name: Internet of Things Subject Code: 23CAH-702
1. Aim/Overview of the practical:
Design a circuit and code using an Arduino Uno where two pushbuttons control the behavior of two LEDs. Button 1
should make LED L1 blink every second and LED L2 blink every 2 seconds. Button 2, when pressed, should make
both LEDs blink simultaneously with the same intervals as in the previous case. Provide the necessary schematic and
code, ensuring that the Arduino does not use the delay function for timing."
2. Apparatus:
Hardware Requirements
a) Arduino Uno
b) 2 x Pushbuttons
c) 2 x LEDs
d) 2 x 220Ω Resistors (for LEDs)
e) Breadboard
f) Jumper wires
Software requirements
a) Tinkercad
3. Circuit Diagram(TinkerCad):
1. Coding:
const int led1 = 4;
const int led2 = 5;
const int button1 = 2;
const int button2 = 3;
unsigned long previousMillis1 = 0;
unsigned long previousMillis2 = 0;
const long interval1 = 1000; // Interval for LED1
const long interval2 = 2000; // Interval for LED2
void setup() {
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
}
void loop() {
unsigned long currentMillis = millis();
if (digitalRead(button1) == LOW) {
if (currentMillis - previousMillis1 >= interval1) {
previousMillis1 = currentMillis;
digitalWrite(led1, !digitalRead(led1));
}
if (currentMillis - previousMillis2 >= interval2) {
previousMillis2 = currentMillis;
digitalWrite(led2, !digitalRead(led2));
}
}
else if (digitalRead(button2) == LOW) {
if (currentMillis - previousMillis1 >= interval1) {
previousMillis1 = currentMillis;
digitalWrite(led1, !digitalRead(led1));
}
if (currentMillis - previousMillis2 >= interval2) {
previousMillis2 = currentMillis;
digitalWrite(led2, !digitalRead(led2));
}
}
else {
// Turn off LEDs when no button is pressed
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
}
}
5. Learning outcomes (What I have learnt):
1. Learn how to connect and interface basic electronic components (LEDs and pushbuttons) with an
Arduino.
2. Learn to understand the use of resistors for current limiting and pull-down purposes.
3. Learn to use the millis() function for non-blocking timing, allowing for multiple tasks to run
simultaneously.