Design and program a smart device that perform a certain function or solve a problem: program Arduino UNO to read signals from multiple input components. I decided to design a smart home using the Arduino for the lights, opening gate and alarm system.
More details about the idea:
When we press the correct password in the ketpad the servo motor will work to open\close the gate, if we intered the wrong password the Buzzer will work as alarm system,also we can open the Led in the front door and in the hole by Bush button.
I got inspired from a YouTube video.
Electric components which I used to build my circuit
Resistor
Resistor to protect led to get burn
Wires
Breadboard
Crocodile wires to join the component with Arduino/breadboard.
Buzzer for genarating music
Keypad
on\off switch
Arduino uno
Servo Motor
Other Materials which I used in crafting
Cardboard
Glue Gun
Softwares
Arduino IDE for flashing the code onto the Arduino board.
I used the tinkercad as a simulation program that used for simulating the electric circuits.
The circuit
The Main components of my circuit are :
on\off switch (Input)
Arduino Uno R3
Buzzer (output)
Breadboard
Keypad 3x4 (Input)
2 LEDs (Wight +red) (output)
Two 220 ohm Resistors
male-to-male jumper wires
Crocodiles
Servo Motors (output)
Procedures:
The Buzzer's positive wire is connected to Pin (12) and its GND is connected to the GND of the Breadboard.
The Keypad is connected to the Arduino as following:
pin_rows[ROW_NUM] = {9, 8, 7, 6}
pin_column[COLUMN_NUM] = {5, 4, 3}
The On/Off Switch is connected to the GND of the Breadboard and the other is connected to Pin 13.
I connect the Wight and Red LEDs Cathode with the 220-ohm Resistors on the same row of the Breadboard and the anode with Pin 10.
I connected the sero motors to the GND of the Breadboard and the othe is connected to pin 11 and the third wire connected on the positive in breadboard
#include <Keypad.h>
#include <Servo.h>
Servo lock;
const int ROW_NUM = 4; //four rows
const int COLUMN_NUM = 3; //three columns
char keys[ROW_NUM][COLUMN_NUM] = {
{ '1', '2', '3' },
{ '4', '5', '6' },
{ '7', '8', '9' },
{ '*', '0', '#' }
};
byte pin_rows[ROW_NUM] = { 9, 8, 7, 6 }; //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = { 5, 4, 3 }; //connect to the column pinouts of the keypad
char digit1;
char digit2;
char digit3;
char digit4;
char enter;
int counter = 1;
int state = 0;
Keypad keypad = Keypad(makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM);
void setup() {
Serial.begin(9600);
lock.attach(11);
pinMode(10, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(13, INPUT_PULLUP);
lock.write(0);
}
void loop(){
char key = keypad.getKey();
if (digitalRead(13)== LOW)
{
digitalWrite(10,HIGH);
}
else
{
digitalWrite(10,LOW);
delay(100);
if (key) {
if (counter == 1) { digit1 = key; }
if (counter == 2) { digit2 = key; }
if (counter == 3) { digit3 = key; }
if (counter == 4) { digit4 = key; }
if (counter == 5) { enter = key; }
counter++;
}
if (counter == 6) {
switch (enter) {
case '#':
if (digit1 == '1' && digit2 == '9' && digit3 == '8' && digit4 == '0') {
Serial.println("correct paass");
if (state == 0) {
lock.write(150);
state = 1;
} else {
lock.write(0);
state = 0;
}
digitalWrite(10, HIGH);
digitalWrite(12, HIGH);
delay(100);
digitalWrite(12, LOW);
} else {
Serial.println("wrong paass");
digitalWrite(12, HIGH);
delay(1000);
digitalWrite(12, LOW);
digitalWrite(10, LOW);
delay(1000);
}
break;
default:
Serial.print("wrong enter key('#')");
}
digit1 = NULL;
digit2 = NULL;
digit3 = NULL;
digit4 = NULL;
enter = NULL;
counter = 1;
}
Serial.print(digit1);
Serial.print(digit2);
Serial.print(digit3);
Serial.print(digit4);
Serial.println(enter);
}
}
In the part below I defined the libraries of Keypad 3x4 and LCD 16x2.
#include <Keypad.h>
#include <Servo.h>
Servo lock;
const int ROW_NUM = 4; //four rows
const int COLUMN_NUM = 3; //three columns
char keys[ROW_NUM][COLUMN_NUM] = {
{ '1', '2', '3' },
{ '4', '5', '6' },
{ '7', '8', '9' },
{ '*', '0', '#' }
};
byte pin_rows[ROW_NUM] = { 9, 8, 7, 6 }; //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = { 5, 4, 3 }; //connect to the column pinouts of the keypad
char digit1;
char digit2;
char digit3;
char digit4;
char enter;
int counter = 1;
int state = 0;
Keypad keypad = Keypad(makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM);
In void setup part I defined the Buzer ,2LEDs , Servo motors and the push button
void setup() {
Serial.begin(9600);
lock.attach(11);
pinMode(10, OUTPUT);\\for the LEDs
pinMode(12, OUTPUT);\\for the buzzer
pinMode(11, OUTPUT);\\for the servo motor
pinMode(13, INPUT_PULLUP);\\for the push button
In the Void Loop, I added if condition for the push button when it's on play the LEDs,
void loop(){
char key = keypad.getKey();
if (digitalRead(13)== LOW)
{
digitalWrite(10,HIGH);
}
else
{
digitalWrite(10,LOW);
delay(100);
also I added if condition for the Servo Motor to work in 150 degree as well as buzzer when the Keypad write the right serial ,If the serial is wrong the buzzer will work as alarm system and the servo motor will not work.
if (key) {
if (counter == 1) { digit1 = key; }
if (counter == 2) { digit2 = key; }
if (counter == 3) { digit3 = key; }
if (counter == 4) { digit4 = key; }
if (counter == 5) { enter = key; }
counter++;
}
if (counter == 6) {
switch (enter) {
case '#':
if (digit1 == '1' && digit2 == '9' && digit3 == '8' && digit4 == '0') {
Serial.println("correct paass");
if (state == 0) {
lock.write(150);
state = 1;
} else {
lock.write(0);
state = 0;
}
digitalWrite(10, HIGH);
digitalWrite(12, HIGH);
delay(100);
digitalWrite(12, LOW);
} else {
Serial.println("wrong paass");
digitalWrite(12, HIGH);
delay(1000);
digitalWrite(12, LOW);
digitalWrite(10, LOW);
delay(1000);
}
break;
default:
Serial.print("wrong enter key('#')");
}
digit1 = NULL;
digit2 = NULL;
digit3 = NULL;
digit4 = NULL;
enter = NULL;
counter = 1;
}
Serial.print(digit1);
Serial.print(digit2);
Serial.print(digit3);
Serial.print(digit4);
Serial.println(enter);
}
}
The encloser
I did the same wiring of the components on the Breadboard as I did on Tinkercad.
Arduino Uno was connected to my PC with the USB connector.
When press the on\off switch the LEDs lights up.
If I clicked the keypad with the right serial the servo motor turns on and made 150 angel to open the gate ,also the buzzer will make a peep sound .
If I clicked the keypad with the wrong serial the servo motor will not open the gate ,also the buzzer will make alarm sound .
I used Chopsticks to fix the servo motor after I paste the Chopsticks with the glue gun in the cardboard .
I made a simple enclosure with cardboard boxes.
I made holes for Arduino, on\off , servo motor and LEDs. In the front part, I fixed the keypad with paper clips
Final outcomes
After uploading the code to Arduino, the On/Off Switch wasn't working compatibly with the other components for unclear reasons.
I checked the code and the wiring with with my colleagues but unfortunately it didn't work .
I tryed to change the code several time ,until it works finally after I added this code before if key
void loop(){
char key = keypad.getKey();
if (digitalRead(13)== LOW)
{
digitalWrite(10,HIGH);
}
else
{
digitalWrite(10,LOW);
delay(100);
Each feature should be tested alone before adding all of them in the same circuit.
The Servo Motor has 3 different coloured wires in Tinercad and the real Servo. They don't change.
Each one for a certain function:
GND - Brown
Power - Red
Signal - Orange
That helped me a lot in connecting it to other components in the circuit.
Title of Media