Part-1 PIR Motion Sensor and Servo Code
Part-1 PIR Motion Sensor and Servo Code
#include <Servo.h>
Servo myservo;
int pos = 0;
boolean takeLowTime;
char val;
void setup()
myservo.attach(9);
Serial.begin(9600);
mySerial.begin(38400);
pinMode(pirPin, INPUT);
pinMode(9,OUTPUT);
digitalWrite(pirPin, LOW);
Serial.print(".");
}
void loop(){
if(digitalRead(pirPin) == HIGH){
//Serial.print("Motion Detected");
// digitalWrite(9,HIGH);
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);
delay(50);
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,
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
char Data[Password_Lenght]; // 6 is the number of chars it can hold + the null char = 7
bool Pass_is_good;
char customKey;
char keys[ROWS][COLS] = {
};
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.setCursor(0, 1);
delay(3000);
lcd.clear();
void loop()
if (door == 0)
customKey = customKeypad.getKey();
if (customKey == '#')
{
lcd.clear();
ServoClose();
delay(3000);
door = 1;
else Open();
void clearData()
while (data_count != 0)
return;
void ServoOpen()
for (pos = 180; pos >= 0; pos -= 5) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
void ServoClose()
for (pos = 0; pos <= 180; pos += 5) { // goes from 180 degrees to 0 degrees
void Open()
lcd.setCursor(0, 0);
customKey = customKeypad.getKey();
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
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.
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.