Raspberry Pi - Arduino Uno - detect the aliens with a HC-SR04 !

When I received my Arduino Beginner package he had in a HC-SR04. Under this barbarous name hides a ultrasonic distance sensor. And I immediately thought of this detector used in the film Alien (and its sequels) !

motion_tracker

For those who don't know this device, Apart from a shame that will befall you during 18 generations (Yes 18 nothing that much), Here is the device in action :

(here if the video does not appear)

Now you see certainly where I am getting. Not detect the Aliens that lurking in the House but simply detecting a movement in a corridor for example. For fun I added a buzzer that goes at the same rate as the flashing of the LED.

And here is the famous sensor :

hc-sr04-s

For those who want to know everything about this HC-SR04 component, the datasheet is available on this page.

Hardware

So for our detector need us the following equipment :

  • An HC-SR04
  • A breadboard
  • A red LED for the area “unauthorized”
  • A green LED for the area “authorized”
  • Minimum resistance of 190Ω (I use a 1KΩ resistor supplied in the kit)
  • A buzzer
  • Wires

Mounting

The Assembly will look like that :

HC-SR04_bb

The program

For the program I put comments to help you read even if it is frankly not complicated :

// Pin control module HC-SR04 const int trigPin = 3;
const int echoPin = 2;
// PIN for LED green const int greenLEDPin = 10;
// Pine for the LED const int redLEDPin = 12;
// Pine for the buzzer const int buzzerPin =. 7;
// Minimum distance correct const int min_dist = 200;
// Distance to trigger l alert const int alerte_dist = 100;
// Variable for distance calculated long distance;

// Function for setting up sub setup()
{
  // Initialization of the pins pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(greenLEDPin, OUTPUT);
  pinMode(redLEDPin, OUTPUT);
  pinMode(buzzerPin,OUTPUT);
  // Definition of the speed of transmission for the Serial.begin console(9600);
}

// Loop sub loop function()
{
  // Duration of the signal emitted by the probe. There is a go and return int length;
  // Activating the sensor digitalWrite(trigPin, HIGH);
  // Waiting delayMicroseconds(1000);
  // Disabling the digitalWrite probe(trigPin, LOW);
  // Requested duration duration = pulseIn probe(echoPin, HIGH);
  // Calculation in centimeters (Division by 2 for one-way and then by 29 the distance travelled by micro seconds)
  distance = time/58;

  if (distance >= min_dist || distance <= 0) {     Serial.print(distance);     Serial.println(" off limits");   } else {     Serial.println(distance);     if(distance >0 && distance < alerte_dist)     {       blink(distance, redLEDPin, true);     }     ElseIf(distance >= alerte_dist)
    {
      blink(distance, greenLEDPin, false);
    }
  }
}

// Function to make Flash an LED the distance of detected
// The buzzer can be active sub blink(int pause, int ledPin, int buzzer)
{
  pause = distance * 10;
  digitalWrite(ledPin, HIGH);
  if (buzzer) digitalWrite(buzzerPin,HIGH);
  delay(10);
  digitalWrite(ledPin, LOW);
  if (buzzer) digitalWrite(buzzerPin,LOW);
  delay(pause);
}

Result

WP_20130810_002

Voilà. If something passes in front of the sensor and enters the area defined as “unauthorized”, then the red LED will light up and the buzzer beep !

If you want, You can use the library in your program Ultrasonic that simplifies initializing and calculating distance. More information on its use here.

[EDIT] If you want to test further on the sensor you have an article here.

Good weekend to all !

You may also like...