Skip to main content

From sketch to reality


Sid the garden protector


This project is a part of my university's prototype course. Please read my earlier posts about the topic:
Project plan: the garden protector and Building the prototype.

The components:
Piezo speaker, alligator clips, PIR motion sensor, servo, Arduino UNO, breadboard, 2 x yellow leds-

PIR motion sensor connected.
I built this project step by step. At first I wanted to make sure that all the components work individually before putting them work together. 

 


Why there is just one wing you may ask. The answer simple - I have hunting dogs who love feathers and they got poor Sid in their mouth. The wings were destroyed and so was the other Servo... Sid also lost half of his feathers. But at least he survived.

The circuit


The code

Originally I had a problem to get all these three actions to work at the same time (blinking of LED lights, alarm sound, flapping of wings.) The solution was rather simple though, I just changed the values of the delay codes and got it work smoothly.

This code is based on PIR Sensor Arduino Alarm tutorial and Arduino's own examples of blinking lights and rotation of Servo.

#include <Servo.h> 

Servo myservo;  // creating a servo object 

int ledPin = 13;          // Led is placed to the pin 13.
int inputPin = 7;         // Another led places in the pin 7.
int pirState = LOW;       // we start, assuming no motion detected
int val = 0;              // variable for reading the pin status
int pinSpeaker = 10;      //Set up a speaker on a PWM pin.
int pos = 0;              // variable to store the servo position 

void setup() {
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(inputPin, INPUT);     // declare sensor as input
  pinMode(pinSpeaker, OUTPUT);
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
  Serial.begin(9600);
}

void loop(){
  val = digitalRead(inputPin);  // read input value
  if (val == HIGH) {            // check if the input is HIGH
    digitalWrite(ledPin, HIGH);  // turn LED ON
  delay(100);               // wait for a second
  
  digitalWrite(ledPin, LOW);    // turn the LED off by making the voltage LOW
  delay(100);                   // wait for a second
    playTone(300, 160);         // play the alarm
    delay(15);
    
     for(pos = 0; pos < 180; pos += 1)  // 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(1);                       // waits 1ms for the servo to reach the position 
  } 
  for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(1);                       // waits 1ms for the servo to reach the position 
  } 

    //for the serial monitor
    if (pirState == LOW) {
      // we have just turned on
      Serial.println("Motion detected!");
      // We only want to print on the output change, not state
      pirState = HIGH;
    }
  } else {
      digitalWrite(ledPin, LOW); // turn LED OFF
      playTone(0, 0);
      delay(300);    
      if (pirState == HIGH){
      // we have just turned of
      Serial.println("Motion ended!");
      // We only want to print on the output change, not state
      pirState = LOW;
    }
  }
}
// duration in mSecs, frequency in hertz
void playTone(long duration, int freq) {
    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);
    }
}

The result



Based on the prototype course by Tero Karvinen and the book Karvinen 2011: Make Arduino Bots and Gadgets. 
Sources: http://makezine.com/projects/pir-sensor-arduino-alarm/ and Arduino's own example codes.

Comments

Popular posts from this blog

Building Love-O-Meter by using a temperature sensor

This "Love-O-Meter" is based on the tutorial by Arduino and it comes with the starter kit. It uses a temperature sensor to measure the warmth of your skin and then starts to turn on (or off) the LEDs  indicated by the temperature.

The components Arduino UNOBreadboardJumper wiresLEDs220 ohm resistorsTMP36 temperature sensor

Building the Circuit
At first I ran the "Hello World" for Arduino to make sure the environment would work as expected. Now I could start to connect the jumper wires between Arduino UNO and the breadboard.

As usually I connected the breadboard to power (5V) and to the ground (GND). I inserted the TMP36 on the breadboard so the rounded part of the sensor would face away from Arduino.

I attached 3 LED lights and the resistors and connected them with Arduino. The lights should react to the heat of the finger and if the temperature would get hot enough all the lights would be on and would also tell you if you are a hot lover or not...

The result and the …

Using a button to control the LED light

This time my project was to configure how to build a button that would turn on and off depending if the user is pushing it or not. While holding the button down the LED should stay on until removing the finger.



For this assignment I used the fallowing components:
Arduino UNO and USBJumper wiresLED lightBreadboardButton10k ohm resistor
At first I run the "Hello World" for Arduino and made sure that the LED I was using worked properly (read my previous post).

I attached the button in the middle of the breadboard so the legs were touching the both "sides" of it.


Now I started to connect the jumper wires between the Arduino UNO and the breadboard. In order to get it working like in the sample code (Ardoino > Examples > 02.Digital > Button)
I linked the wires with Arduino's 5V (red wire, positive) and ground (black wire, negative) to the equal holes on the very corner of the breadboard (+ and -). I put the white jumper wire to connect the positive circuit to t…

Creating a Bootable USB for Kali Linux

If you are interested to see my documentation about making a bootable USB for Xubuntu please click here!

This USB was made by using the latest version of Xubuntu (the 14.04 release).

Downloading Kali Linux 
23.13. After navigating to https://www.kali.org/ I chose the version that would suit me the best and started downloading the ISO image for 64-Bit system.


USB imaging
22.40. I followed the guide offered by kali.org and used the commandsudo fdisk -lwithout plugging the USB yet. I needed to pay attention to the path of Device Boot (/dev/sdb1). After this I inserted USB and ran the same command again. Now I could see how the name of Device Boot had changed to sdc1. This would be my USB to use.

22.50 It only took a while to format the dd code correctly as the base was already given on Kali's web page. After running the correct command for the first time I got an error message "'/dev/sdc1': Permission denied". In these kind of situations I tend to ask sudo for help an…