Internet of Things (IoT) - Lecture 6 Arduino Projects
Internet of Things (IoT) - Lecture 6 Arduino Projects
IT 8311
Credit rating: 10
LECTURE VI.
Eng.Malissa
[email protected]
Arduino Projects
01/17/2025 2
OUTLINES
• Arduino Projects No-1: Pushbutton- Controlled LED
• Arduino Projects No-2: Light Dimmer
• Arduino Projects No-3: Bar Graph
• Arduino Projects No-4: Disco Strobe Light
• Arduino Projects No-5: LCD Screen Writer
• Arduino Projects No-6: Weather Station
• Arduino Projects No-7: Intruder Sensor
• Arduino Projects No-8: Motion Sensor Alarm
• Arduino Projects No-9: Keypad Entry System
Arduino Projects
No-1
Pushbutton- Controlled
LED
01/17/2025 4
Project 1:Pushbutton- Controlled LED
• In this project, you’ll add a pushbutton switch
to an LED circuit to control when the L ED is
lit.
• Parts Required
Arduino board Momentary tactile four-pin
Breadboard pushbutton
Jumper wires 10k-ohm resistor
LED 220-ohm resistor
01/17/2025 5
Project 1:Pushbutton- Controlled LED
01/17/2025 6
How It Works
• When pressed, a pushbutton completes a circuit,
turning it on. As soon as the button is released,
the connection will spring back and break that
circuit, turning it off. The pushbutton switch is
also known as a momentary or normally open
switch, and is used in, for example, computer
keyboards.
• This is in contrast to a toggle switch, which stays
either on or off until you toggle it to the other
position, like a light switch.
01/17/2025 7
How It Works
• This type of pushbutton has four pins, but you generally use
only two at a time for connection. You’ll use the top
connections in this project, although the two unused pins at the
bottom would do the same job. As shown below , the pins
work in a circuit.
• Pins A and C are always connected, as are pins B and D. When
the button is pressed, the circuit is complete.
01/17/2025 8
The Build
• Place your pushbutton in a breadboard, as
shown in Figure 1-3.
01/17/2025 9
The Build
• Connect pin A to one leg of a 10k-ohm resistor,
and connect that same resistor leg to Arduino pin
2. Connect the other resistor leg to the GND rail,
and connect the GND rail to the Arduino’s GND.
Connect pin B on the switch to the +5V rail, and
connect this rail to +5V on the Arduino
01/17/2025 10
The Build
• Add the LED to your breadboard, connecting the longer,
positive leg to Arduino pin 13 via a 220-ohm resistor and
the shorter leg to GND.
Figure 1-4
01/17/2025 12
The Sketch
• In this sketch, you assign a pin for the pushbutton
and set it as INPUT, and a pin for the LED and set
it as OUTPUT.
• The code tells the Arduino to turn the LED on as
long as the button is being pressed (completing
the circuit), and to keep the LED off when the
button is not being pressed.
• When the button is released, the circuit breaks and
the LED will turn off again.
01/17/2025 13
const int buttonPin = 2; // Pin connected to pushbutton
const int ledPin = 13; // Pin connected to LED
int buttonState = 0; // Give pushbutton a value
void setup() {
pinMode(ledPin, OUTPUT); // Set LED pin as output
pinMode(buttonPin, INPUT); // Set pushbutton pin as input
}
void loop() {
buttonState = digitalRead(buttonPin); // Read input from pin 2
if (buttonState == HIGH) { // If pushbutton is pressed, set as HIGH
digitalWrite(ledPin, HIGH); // Turn on LED
}
else {
digitalWrite(ledPin, LOW); // Otherwise, turn off LED
}
}
01/17/2025 14
Arduino Projects
No-2
Light Dimmer
01/17/2025 15
Project 2: Light Dimmer
• In this project, you’ll create a dimmer
switch by adding a potentiometer to
control the brightness of an LED .
Parts Required
• Arduino board, Breadboard, Jumper
wires, LED, 50k-ohm potential meter and
470 ohm resistor
01/17/2025 16
Potentiometer.
• A potentiometer is a variable resistor with a
knob that allows you to alter the resistance of
the potentiometer as you turn it. It is
commonly used in electrical devices such as
volume controls on audio equipment. This
project uses a 50k-ohm potentiometer.
01/17/2025 17
The build
01/17/2025 18
The build
01/17/2025 19
The Build
01/17/2025 20
The Sketch
• This sketch works by setting pin A0 as your
potentiometer and pin 9 as an OUTPUT to
power the LED.
• You then run a loop that continually reads the
value from the potentiometer and sends that
value as voltage to the LED.
• The voltage value is between 0–5 volts, and
the brightness of the LED will vary
accordingly.
01/17/2025 21
int potPin = A0; // Analog input pin connected to the potentiometer
int potValue = 0; // Value that will be read from the potentiometer
int led = 9; // Pin 9 (connected to the LED) is capable of PWM
// Runs once at beginning of the program
void setup() {
pinMode(led, OUTPUT); // Set pin 9 to output
}
// Loops continuously
void loop() {
potValue = analogRead(potPin); // Read potentiometer value
// from A0 pin
analogWrite(led, potValue/4); // Send potentiometer value to LED
// to control brightness with PWM
delay(10); // Wait for 10 ms
}
01/17/2025 22
Arduino Projects
No-3
Bar Graph
01/17/2025 23
Project 3:Bar Graph
• In this project, you’ll combine what you’ve
learned in the previous LED projects to create
an LED bar graph that you can control with a
potentiometer.
• Parts Required
• Adruino board, Breadboard, Jumper wires, 9-
LEDs, 50k-ohm potentiometer and 9 220-ohm
resistor
01/17/2025 24
How It Works
• A bar graph is a series of LEDs in a line, similar to
what you might see on an audio display. It’s made up
of a row of LEDs with an analog input, like a
potentiometer or microphone.
• In this project, you use the analog signal from the
potentiometer to control which LEDs are lit.
• When you turn the potentiometer one way, the LEDs
light up one at a time in sequence, as labeled LED 1-
9, until they are all on. When you turn it the other
way, they turn off in sequence, as labeled LED 1-9
01/17/2025 25
How It Works
01/17/2025 26
The Build
01/17/2025 27
The Build
01/17/2025 28
The Sketch
• The sketch first reads the input from the
potentiometer. It maps the input value to the output
range, in this case nine LEDs.
• Then it sets up a for loop over the outputs. If the
output number of the LED in the series is lower
than the mapped input range, the LED turns on; if
not, it turns off. See?
• Simple! If you turn the potentiometer to the right,
the LEDs light up in sequence. Turn it to the left,
and they turn off in sequence.
01/17/2025 29
const int analogPin = A0; // Pin connected to the potentiometer
const int ledCount = 9; // Number of LEDs
int ledPins[] = {2,3,4,5,6,7,8,9,10}; // Pins connected to the LEDs
void setup() {
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
pinMode(ledPins[thisLed], OUTPUT); // Set the LED pins as output
}
}
// Start a loop
void loop() {
int sensorReading = analogRead(analogPin); // Analog input
int ledLevel = map(sensorReading, 0, 1023, 0, ledCount);
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
if (thisLed < ledLevel) { // Turn on LEDs in sequence
digitalWrite(ledPins[thisLed], HIGH);
}
else { // Turn off LEDs in sequence
digitalWrite(ledPins[thisLed], LOW);
}}
}01/17/2025 30
Arduino Projects
No-4
Disco Strobe Light
01/17/2025 31
Project 4: Disco Strobe Light
• In this project, you'll apply the skills you
learned in Project 3 to make a strobe light
with adjustable speed settings.
• Parts Required
• Adruino board, Breadboard, Jumper
wires, 2-blue LEDs, 2-red LEDs, 50k-
ohm potentiometer and 4 220-ohm
resistor
01/17/2025 32
How It Works
• Turning the potentiometer up or down changes the
speed of the flashing lights, creating a strobe effect.
You can use red and blue LEDs for a flashing police
light effect .
• Connect the LEDs of the same color to the same
Arduino pin so they’ll always light together.
• If you build a casing to house your LEDs, you’ll have
your own mobile strobe unit. You can add up to 10
LEDs; just update the sketch to include your output
pins and the new number of LEDs.
01/17/2025 33
The Build
01/17/2025 34
The Build
01/17/2025 35
The Sketch
• The sketch works by setting the analog signal from the
potentiometer to the Arduino as an input and the pins
connected to the LEDs as outputs.
• The Arduino reads the analog input from the
potentiometer and uses this value as the delay value—the
amount of time that passes before the LEDs change state
(either on or off).
• This means that the LEDs are on and off for the duration
of the potentiometer value, so changing this value alters
the speed of the flashing. The sketch cycles through the
LEDs to produce a strobe effect.
01/17/2025 36
const int analogInPin = A0; // Analog input pin connected to the
// potentiometer
int sensorValue = 0; // Value read from the potentiometer
int timer = 0; // Delay value
// Set digital pins 12 and 11 as outputs
void setup() {
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
}
// Start a loop to turn LEDs on and off with a delay in between
void loop() {
sensorValue = analogRead(analogInPin); // Read value from the
// potentiometer
timer = map(sensorValue, 0, 1023, 10, 500); // Delay 10 to 500 ms
01/17/2025 37
digitalWrite(12, HIGH); // LED turns on
delay(timer); // Delay depending on potentiometer value
digitalWrite(12, LOW); // LED turns off
delay(timer);
digitalWrite(12, HIGH);
delay(timer);
digitalWrite(12, LOW);
digitalWrite(11, HIGH);
delay(timer);
digitalWrite(11, LOW);
delay(timer);
digitalWrite(11, HIGH);
delay(timer);
digitalWrite(11, LOW);
}
01/17/2025 38
Arduino Projects
No-5
LCD Screen Writer
01/17/2025 39
Project 5: LCD Screen Writer
• Not only is there something very satisfy ing
about having an LCD screen display your own
messages, but it’s also very useful.
• Parts Required
Arduino board, Breadboard. Jumper wires,
16x2 LCD screen, 50k-ohm potentiometer
• Libraries Required
Liquid Crystal
01/17/2025 40
How It Works
• An LCD screen is made of two sheets of polarizing
material with a liquid crystal solution between them.
• Current passing through the solution creates an image or,
in this case, characters. For this project, you’ll need an
LCD screen that’s compatible with the Hitachi HD44780
driver for it to work with the Arduino—there are lots of
them out there and you can usually identify them by their
16-pin interface.
• We’ll use the LiquidCrystal library to send characters to
the LCD screen. The LiquidCrystal library maps the
characters and uses the print.lcd commands to copy the
message from the sketch to the screen.
• Before you start, you need to prepare your LCD screen.
01/17/2025 41
The Build
01/17/2025 42
LCD
01/17/2025 43
SETUP
01/17/2025 44
01/17/2025 45
The Sketch
• This sketch is included in your IDE examples. Load it from the
IDE by going to File- Examples4-LiquidCrystal and then
clicking Scroll.
• The sketch uses the LiquidCrystal library that’s built into the
Arduino IDE to send messages from the Arduino to the LCD
screen.
• You can change the message by replacing "Arduino Sketch" at
2.
• To use this circuit setup with the example sketches in the
Arduino IDE, we also change the LCD pins in the sketch (12,
11, 5, 4, 3, 2) at 1 to 7, 8, 9, 10, 11, 12, as these are the pins
we’ve assigned. I’ve re-created the sketch here as you’ll see it
in the IDE, but with those changes made.
01/17/2025 46
// Include the library code
#include <LiquidCrystal.h>
// Initialize the library with the numbers of the interface pins
1
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
void setup() {
// Set up the LCD's number of columns and rows
lcd.begin(16, 2);
2 // Print a message to the LCD
lcd.print("Arduino Sketch");
delay(1000);
}
void loop() {
// Scroll 13 positions (string length) to the left
// to move it offscreen left
for (int positionCounter = 0; positionCounter < 13;
positionCounter++) {
// Scroll one position left
lcd.scrollDisplayLeft();
// Wait a bit
01/17/2025
delay(150); 47
// Scroll 29 positions (string length + display length) to the right
// to move it offscreen right
for (int positionCounter = 0; positionCounter < 29;
positionCounter++) {
// Scroll one position right
lcd.scrollDisplayRight();
delay(150); // Wait a bit
}
01/17/2025 48
// Scroll 16 positions (display length + string length) to the left
// to move it back to center
for (int positionCounter = 0; positionCounter < 16;
positionCounter++) {
// Scroll one position left
lcd.scrollDisplayLeft();
delay(150); // Wait a bit
}
// Delay at the end of the full loop
delay(1000);
}
01/17/2025 49
Arduino Projects
No-6
Weather Station
01/17/2025 50
Project 6:Weather Station
• In this project you’ll set up a weather station to
measure temperature and humidity, and display
the values on an LCD screen.
• Parts Required
Arduino board, Breadboard, Jumper wires, 50k-
ohm potentiometer, 16x2 LCD screen, DHT11
humidity sensor
• Libraries Required
Liquid Crystal and DHT
01/17/2025 51
How it works
• The humidity sensor used in this project is the relatively cheap
DHT11, which measures both humidity and temperature.
• It uses a capacitive humidity sensor and resistive-type
temperature sensor to take a reading from its environment.
• It sends this reading to the Arduino as voltage, and the
Arduino converts this to readable values displayed on the
screen.
• For best results, you should mount your sensor on an outside
wall with a decent amount of open space. You’ll want to
mount your LCD screen indoors or seal it carefully in a clear,
waterproof bag or casing to keep it protected from the
elements. The DHT11 comes with four pins
01/17/2025 52
The Build
01/17/2025 53
The Build
01/17/2025 54
01/17/2025 55
The Sketch
• This sketch uses the LiquidCrystal library,
which comes with the Arduino IDE, and the
DHT library, which you will need to download
and install from
http://nostarch.com/arduinohandbook/ (see
“Libraries” on page 7).
• The DHT library controls the function of the
sensor, and the LCD library displays the
readings on the screen.
01/17/2025 56
#include <LiquidCrystal.h>
#include "DHT.h" // Call the DHT library
#define DHTPIN 8 // Pin connected to DHT
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
#define DHTTYPE DHT11 // Define the type of DHT module
DHT dht(DHTPIN, DHTTYPE); // Command to the DHT.h library
void setup() {
dht.begin(); // Start the sensor
lcd.begin(16, 2); // LCD screen is 16 characters by 2 lines
}
void loop() {
float h = dht.readHumidity(); // Value for humidity
float t = dht.readTemperature(); // Value for temperature
t = t * 9 / 5 + 32; // Change reading from Celsius to Fahrenheit
if (isnan(t) || isnan(h)) { // Check that DHT sensor is working
01/17/2025 57
lcd.setCursor(0, 0);
lcd.print("Failed to read from DHT"); // If DHT is not working,
// display this
} else { // Otherwise show the readings on the screen
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Humidity: ");
lcd.print(h);
lcd.print("%");
lcd.setCursor(0, 1);
lcd.print("Temp: ");
lcd.print(t);
lcd.print("f");
}
}
01/17/2025 58
Arduino Projects
No-7
Intruder Sensor
01/17/2025 59
Project 7: Intruder Sensor
• In this project, we’ll use an ultrasonic sensor
to detect an intruder.
• Parts Required
• Arduino board, Breadboard, Jumper wires,
Four-pin HC-SR04 ultrasonic sensor,
Servomotor, Red LED, Green LED and 2 220-
ohm resistors
01/17/2025 60
How It Works
• This project is versatile and can be used and adapted in
various ways. Because the ultrasonic sensor can define
distance, you could, for example, use it to define an area
and trigger an alarm when that perimeter is breached.
The sensor works similarly to a radar: it sends out an
ultrasonic signal, or ping. When this signal hits an object,
it bounces back like an echo, and the time between the
ping and the echo is used to calculate distance.
• The Arduino can use this calculation
to trigger an event, depending on the value received.
01/17/2025 61
How It Works
In this project, when the sensor detects an intruder
within a predefined vicinity, the red LED will light
and the servo arm will move. You
can adapt this project to trigger a different event
when the intruder is detected, like pressing a
security system button or locking a door.
For a friendlier scenario, you could set the distance
really close so that when you wave your hand in
front of the sensor, the servo presses a button to
release a treat, like candy.
01/17/2025 62
The Build
01/17/2025 63
01/17/2025 64
The Sketch
• When an object is within the trigger distance, the red LED will light and
the servo will move 45 degrees. You can change the distance of the
sensor field in the following line of the sketch:
if (distance <= 15)
The time difference between the two signals gives us our distance
reading. If the distance is more than our set minimum, the green LED
stays on; if not, the red LED lights and the servo moves.
01/17/2025 65
#include <NewPing.h> // Call NewPing library
#include <Servo.h> // Call Servo library
#define trigPin 12 // Trig pin connected to Arduino 12
#define echoPin 13 // Echo pin connected to Arduino 13
#define MAX_DISTANCE 500
NewPing sonar(trigPin, echoPin, MAX_DISTANCE); // Library setting
int greenLed = 3, redLed = 2; // Set green LED to pin 3, red to pin 2
int pos = 20;
Servo myservo;
void setup() {
Serial.begin (115200);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(greenLed, OUTPUT);
pinMode(redLed, OUTPUT);
myservo.attach(9); // Servo attached to pin 9
}
void loop() {
int duration, distance, pos = 0, i;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH); // Trig pin sends a ping
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH); // Echo receives the ping
distance = (duration / 2) / 29.1;
01/17/2025 66
Serial.print(distance);
Serial.println(" cm");
// If sensor detects object within 15 cm
if (distance <= 15) {
digitalWrite(greenLed, LOW); // Turn off green LED
digitalWrite(redLed, HIGH); // Turn on red LED
myservo.write(180); // Move servo arm 180 degrees
delay(450);
digitalWrite(redLed, LOW); // Light the red LED
myservo.write(90);
delay(450);
digitalWrite(redLed, HIGH);
myservo.write(0);
delay(450);
digitalWrite(redLed, LOW);
myservo.write(90);
}
// Otherwise
else {
digitalWrite(redLed, LOW); // Turn off red LED
digitalWrite(greenLed, HIGH); // Turn on green LED
myservo.write(90);
}
delay(450);
}
01/17/2025 67
Arduino Projects
No-8
Motion Sensor Alarm
01/17/2025 68
Arduino Projects 8: Motion Sensor Alarm
01/17/2025 69
How it works
• This project is based on the HC SR501 PIR
sensor, which is widely available online for a
few dollars. We’re going to set it up so that
when someone passes in front of the PIR
sensor, the LED will light up and the piezo
buzzer will sound , but you can adapt it for
various other output.
01/17/2025 70
The Build
01/17/2025 71
01/17/2025 72
The Sketch
• The sketch works by setting Arduino pin 13 as
output for the LED, pin 2 as input for the PIR
sensor, and pin 10 as output for the piezo
buzzer.
• When the PIR sensor is triggered, a HIGH
signal is sent to the Arduino, which will in turn
light the LED and play a tone on the piezo
buzzer.
01/17/2025 73
• int ledPin = 13; // Pin connected to LED
int inputPin = 2; // Pin connected to PIR sensor
int pirState = LOW; // Start PIR state LOW with no motion
int val = 0; // Variable for reading the pin status
int pinSpeaker = 10; // Pin connected to piezo
void setup() {
pinMode(ledPin, OUTPUT); // Set LED as output
pinMode(inputPin, INPUT); // Set sensor as input
pinMode(pinSpeaker, OUTPUT);
Serial.begin(9600);
}
void loop() {
val = digitalRead(inputPin); // Read PIR input value
if (val == HIGH) { // Check if input is HIGH
digitalWrite(ledPin, HIGH); // If it is, turn ON LED
playTone(300, 160);
delay(150);
if (pirState == LOW) {
// Print to the Serial Monitor if motion detected
Serial.println("Motion detected!");
pirState = HIGH;
}
01/17/2025 74
} else {
digitalWrite(ledPin, LOW); // If input is not HIGH,
// turn OFF LED
playTone(0, 0);
delay(300);
if (pirState == HIGH) {
Serial.println("Motion ended!");
pirState = LOW;
}
}
}
void playTone(long duration, int freq) { // Duration in ms,
// frequency in Hz
duration *= 1000;
int period = (1.0 / freq) * 1000000;
long elapsed_time = 0;
while (elapsed_time < duration) {
digitalWrite(pinSpeaker, HIGH);
delayMicroseconds(period / 2);
digitalWrite(pinSpeaker, LOW);
delayMicroseconds(period / 2);
elapsed_time += (period);
}
}
01/17/2025 75
Arduino Projects
No-9
Keypad Entry System
01/17/2025 76
01/17/2025 77
Keypad Entry System
• It’s time to introduce a keypad to your Arduino
by building a keypad entry system.
• Parts Required
Arduino board, Breadboard, Jumper wires,
Tower Pro SG90 9g ,servomotor, Green LED,
Red LED. 4.4 membrane keypad, 2 220-ohm
resistors
• Libraries Required
Keypad, Servo and Password
01/17/2025 78
How It Works
• A keypad is basically a series of buttons that output
a number or character depending on which button
is pressed. With the keypad face up, the wires are
numbered 1–8 from left to right.
• The first four wires correspond to the rows, and the
latter four to the columns.
• You’ll need to download the library for the keypad
from the http://nostarch.com/arduinohandbook/
and save it in your IDE’s. Arduino libraries folder.
01/17/2025 79
How It Works Cont.…
• We’ll connect this keypad to a servo and some LEDs to create
a lock system like the secret knock lock project
• To use the lock, enter your code and press the asterisk (*) to
confirm. If the code matches the password defined in the
sketch, the green LED will flash and the servo will move 90
degrees.
• If the code is incorrect, the red LED will flash. Use the hash
key (#) to reset between code inputs. You could swap this
servo for a more substantial one capable of unlocking a
heavier deadbolt on a door, or locking and unlocking a box
from the inside with the keypad and LEDs mounted
externally
01/17/2025 80
Testing The Keypad
First we’ll test the keypad with the following code:
#include <Keypad.h>
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {2,3,4,5};
byte colPins[COLS] = {6,7,8,9};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins,
ROWS, COLS);
01/17/2025 81
Testing The Keypad
void setup() {
Serial.begin(9600);
}
void loop() {
char key = keypad.getKey();
if (key != NO_KEY){
Serial.println(key);
}
}
01/17/2025 82
The Build
01/17/2025 83
The Build
01/17/2025 84
01/17/2025 85
The Sketch
• First, the sketch calls on the Keypad, Servo, and Password
• libraries.
• The Servo library is included in the IDE, but you’ll have to
• download the Keypad and Password libraries (http://nostarch.com/arduinohandbook/).
• We then set the eight pins that will determine the
• input from the keypad, and set Arduino pins 11 and 12 to control
• the LEDs and pin 13 to control the servomotor. The Arduino waits
• for your code input from the keypad and for you to confirm your
• input with *. Once you’ve pressed the asterisk key, the sketch will
• check the entry against the password in the code. If the entry doesn’t
• match the password, the red LED will be set to HIGH and light; if
• the entry does match the password,
• the green LED will be set to
• HIGH and light, and the servomotor will turn. Pressing # will reset the
• sketch so it’s ready for another entry.
• To alter the password, change the number in quotation marks in
• the following line.
• Password password = Password("2468");
• The default password in the sketch is 2468.
01/17/2025 86
#include <Password.h>
#include <Keypad.h>
#include <Servo.h>
Servo myservo;
Password password = Password("2468"); // Set password
const byte ROWS = 4; // Set four rows
const byte COLS = 4; // Set four columns
char keys[ROWS][COLS] = { // Define the keymap
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = { 9,8,7,6 }; // Pins connected to keypad
// ROW0, ROW1, ROW2 and ROW3
byte colPins[COLS] = { 5,4,3,2, }; // Pins connected to keypad
// COL0, COL1 and COL2
01/17/2025 87
// Create the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins,
ROWS, COLS);
void setup() {
Serial.begin(9600);
delay(200);
pinMode(11, OUTPUT); // Set green LED as output
pinMode(12, OUTPUT); // Set red LED as output
myservo.attach(13); // Pin connected to servo
keypad.addEventListener(keypadEvent); // Add an event listener to
// detect keypresses
}
void loop() {
keypad.getKey();
myservo.write(0);
}
void keypadEvent(KeypadEvent eKey) {
switch (keypad.getState()) {
case PRESSED:
Serial.print("Pressed: ");
Serial.println(eKey);
switch (eKey) {
case '*': checkPassword(); break;
case '#': password.reset(); break;
default: password.append(eKey);
}
}
} 01/17/2025 88
void checkPassword() {
if (password.evaluate() ){
Serial.println("Success"); // If the password is correct...
myservo.write(90); // Move servo arm 90 degrees
digitalWrite(11, HIGH); // Turn on green LED
delay(500); // Wait 5 seconds
digitalWrite(11, LOW); // Turn off green LED
} else {
Serial.println("Wrong"); // If the password is incorrect...
myservo.write(0);
digitalWrite(12, HIGH); // Turn on red LED
delay(500); // Wait 5 seconds
digitalWrite(12, LOW); // Turn off red LED
}
}
01/17/2025 89
END.
Thanks for Listening.