0% found this document useful (0 votes)
325 views

Part-1 PIR Motion Sensor and Servo Code

The document describes three projects: 1. A PIR motion sensor and servo motor that opens and closes a gate when motion is detected. 2. A keypad lock using a password to open a servo motor. It displays the key presses and password entry on an LCD screen. 3. Controlling an Arduino's LED ports remotely using Bluetooth from an Android phone. It connects an HC-05 Bluetooth module to the Arduino and uses a voltage divider for the RX line. Characters sent open different LEDs or turn all off.

Uploaded by

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

Part-1 PIR Motion Sensor and Servo Code

The document describes three projects: 1. A PIR motion sensor and servo motor that opens and closes a gate when motion is detected. 2. A keypad lock using a password to open a servo motor. It displays the key presses and password entry on an LCD screen. 3. Controlling an Arduino's LED ports remotely using Bluetooth from an Android phone. It connects an HC-05 Bluetooth module to the Arduino and uses a voltage divider for the RX line. Characters sent open different LEDs or turn all off.

Uploaded by

M Salman Ryan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Part-1

PIR Motion Sensor and Servo


Code
#include<SoftwareSerial.h>

#include <Servo.h>

Servo myservo;

int pos = 0;

int calibrationTime = 10;

long unsigned int lowIn;

long unsigned int pause = 50;

boolean lockLow = true;

boolean takeLowTime;

char val;

int pirPin = 12;

void setup()

myservo.attach(9);

Serial.begin(9600);

mySerial.begin(38400);

pinMode(pirPin, INPUT);

pinMode(9,OUTPUT);

digitalWrite(pirPin, LOW);

for(int i = 0; i < calibrationTime; i++) {

Serial.print(".");

}
void loop(){

//PIR and Gate_servo motor

if(digitalRead(pirPin) == HIGH){

//Serial.print("Motion Detected");

// digitalWrite(9,HIGH);

for (pos = 0; pos <= 120; pos += 2) {

myservo.write(pos);

delay(50);

if(lockLow){

//makes sure we wait for a transition to LOW before any further output is made:

lockLow = false;

//delay(50);

takeLowTime = true;

if(digitalRead(pirPin) == LOW){

// digitalWrite(9, LOW);

for (pos = 120; pos <= 0;pos -= 2) {


myservo.write(pos);

delay(50);

//the led visualizes the sensors output pin state

if(takeLowTime){

lowIn = millis(); //save the time of the transition from high to LOW

takeLowTime = false; //make sure this is only done at the start of a LOW phase

//if the sensor is low for more than the given pause,

//we assume that no more motion is going to happen

if(!lockLow && millis() - lowIn > pause){

//makes sure this block of code is only executed again after

//a new motion sequence has been detected

lockLow = true;
Serial.print("motion ended"); //output

//delay(50);

part-2
Keypad Control Servo
Code
#include <Keypad.h>

#include <LiquidCrystal.h>

#include <Servo.h>

Servo myservo;gg
LiquidCrystal lcd(A0, A1, A2, A3, A4, A5);

#define Password_Lenght 3 // Give enough room for six chars + NULL char

int pos = 0; // variable to store the servo position

char Data[Password_Lenght]; // 6 is the number of chars it can hold + the null char = 7

char Master[Password_Lenght] = "12";

byte data_count = 0, master_count = 0;

bool Pass_is_good;

char customKey;

const byte ROWS = 4;

const byte COLS = 3;

char keys[ROWS][COLS] = {

{'1', '2', '3'},

{'4', '5', '6'},

{'7', '8', '9'},

{'*', '0', '#'}

};

bool door = true;

byte rowPins[ROWS] = {1, 2, 3, 4}; //connect to the row pinouts of the keypad

byte colPins[COLS] = {5, 6, 7}; //connect to the column pinouts of the keypad
Keypad customKeypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS); //initialize an instance of
class NewKeypad

void setup()

myservo.attach(9);

ServoClose();

lcd.begin(16, 2);

lcd.print(" Smart Resturant");

lcd.setCursor(0, 1);

lcd.print("Place Your Order");

delay(3000);

lcd.clear();

void loop()

if (door == 0)

customKey = customKeypad.getKey();

if (customKey == '#')

{
lcd.clear();

ServoClose();

lcd.print(" Thank You");

delay(3000);

door = 1;

else Open();

void clearData()

while (data_count != 0)

{ // This can be used for any array size,

Data[data_count--] = 0; //clear array for new data

return;

void ServoOpen()

for (pos = 180; pos >= 0; pos -= 5) { // goes from 0 degrees to 180 degrees

// in steps of 1 degree

myservo.write(pos); // tell servo to go to position in variable 'pos'


delay(15); // waits 15ms for the servo to reach the position

void ServoClose()

for (pos = 0; pos <= 180; pos += 5) { // goes from 180 degrees to 0 degrees

myservo.write(pos); // tell servo to go to position in variable 'pos'

delay(15); // waits 15ms for the servo to reach the position

void Open()

lcd.setCursor(0, 0);

lcd.print(" Order Delievered");

customKey = customKeypad.getKey();

if (customKey) // makes sure a key is actually pressed, equal to (customKey != NO_KEY)

Data[data_count] = customKey; // store char into data array

lcd.setCursor(data_count, 1); // move cursor to show each new char

lcd.print(Data[data_count]); // print char at said cursor

data_count++; // increment data array by 1 to store new char, also keep track of the number of chars
entered

}
if (data_count == Password_Lenght - 1) // if the array index is equal to the number of expected chars,
compare data to master

if (!strcmp(Data, Master)) // equal to (strcmp(Data, Master) == 0)

lcd.clear();

ServoOpen();

lcd.print("Order Delievered");

door = 0;

else

lcd.clear();

lcd.print("Order Placed");

delay(1000);

door = 1;

clearData();

}
}

part-3
BLUETOOTH CONTROL LED
We are explaining how to control Arduino UNO Ports using Android’s Bluetooth. Bluetooth is a
type of wireless communication used to transmit voice and data at high speeds using waves of
radio. It’s widely used in mobile phones for making calls, headset and share data. This type of
communication is a cheap and easy way to control something remotely using arduino.
Figure 1 - LED Control Circuit and Android Phone

HC Bluetooth Module
HC Bluetooth module has 4 pins to be connected to arduino, they are:
 RXD : RXD will receive data from arduino
 TXD : TXD will send data to arduino
 VCC : VCC is the power supply (3.3V 6.6V)
 GND : GND is the ground
Figure 2 - HC 07 Bluetooth Module

On my module, as you can see from the photo above, the TX line is rated at 3.3V. This means
that even though we can power the module with 5 volts from the Arduino, the communication
lines from and to the module are supposed to be at 3.3 volts. Sending data from the Bluetooth
module (via the module’s TX pin) will not be an issue, as the Arduino’s RX line will interpret the
3.3V signal from the Bluetooth module correctly. Receiving data from the Arduino is where we
need to do more work. The RX line of the Arduino Uno is running at 5V, so we will be sending a
higher voltage on the Bluetooth’s module RX line, than suggested by the label. This may
damage the Bluetooth module permanently!

You have to pay attention about the RXD level, some modules work with 5V, but this one works
with 3.3V, and arduino TX will send a 5V signal, then it needs a voltage divider.
The formula above we have R1 = 1,2 K R2 = 2,2 K Vin = 5v.
Solving for Vout we get: Vout = 5 x 2,2 K / (2,2 K + 1,2 K ) = 5v x 2,2 K / 3,4 K = 3.24 V
Essentially, you can use any combination of resistors, as long as R2 is twice the value of R1.

If you do not have resistors handy, you can use a positive 3.3v voltage regulator. Connect the
Input pin of the voltage regulator to the Arduino TX line, the Ground pin to the Arduino Ground
and the output pin of the voltage regulator to the JY-MCU RX line.

Figure 3 - The connection circuit of Arduino UNO and HC 07 Bluetooth Module

ARDUINO UNO Program


const int LED [] = {8,9,10,11};// Array of Port Numbers
char x;
void setup() {
Serial.begin(9600);// Serial Port Baund Rate
for(int i=0; i<=3;i++)
{
pinMode(LED [i], OUTPUT);
}
}

void loop() {
if( Serial.available() ) // if data is available to read
{
x = Serial.read(); // read it and store it in 'x'
}
if( x == 'a' )// if 'a' was received
{
digitalWrite(LED[0], HIGH);
digitalWrite(LED[1], LOW);
digitalWrite(LED[2], LOW);
digitalWrite(LED[3], LOW);
}
else if(x=='b')// if 'b' was received
{
digitalWrite(LED[0], LOW);
digitalWrite(LED[1], HIGH);
digitalWrite(LED[2], LOW);
digitalWrite(LED[3], LOW);
}
else if(x=='c')// if 'c' was received
{
digitalWrite(LED[0], LOW);
digitalWrite(LED[1], LOW);
digitalWrite(LED[2], HIGH);
digitalWrite(LED[3], LOW);
}
else if(x=='d')// if 'd' was received
{
digitalWrite(LED[0], LOW);
digitalWrite(LED[1], LOW);
digitalWrite(LED[2], LOW);
digitalWrite(LED[3], HIGH);
}
else if(x=='e')// if 'e' was received
{
//Running LED
for(int i=0; i<=3; i++)
{
digitalWrite(LED[i],HIGH);
delay(200);
digitalWrite(LED[i],LOW);
}
for(int j=2;j>=1; j--)
{
digitalWrite(LED[j],HIGH);
delay(200);
digitalWrite(LED[j], LOW);
}
}
else if(x=='q')//Veri 'q' ise(if 'q' was received)
{
digitalWrite(LED[0], LOW);
digitalWrite(LED[1], LOW);
digitalWrite(LED[2], LOW);
digitalWrite(LED[3], LOW);
}
}

Arduino UNO program runs according to the character set from the Android program. The
characters is given in the table below.

Table 1 - Character Set

You might also like