Introduction: Ultrasonic Sensor Detects Someone in Front of Your Laptop

As a newcomer to Arduino’s world I want to share with you my first project with Ultrasonic device.

After reading this you’ll know how to get the exact time when somebody sits in front of your computer and record it into a file (and by sits I mean literally :) )

Of course this project only makes sense in these 2 scenarios:

  1. If you let your laptop without supervision (and password unprotected :P) for like 10 minutes and someone decides to read your facebook massages. With this implementation you will know how to detect it and make some home-drama ... or …
  2. You've just bought your Arduino kit and wanna make cool unuseful project on your own, like me :)

The hardware stuff you will need:

  • Arduino Uno
  • Breadboard
  • 2 x LED and Resistors (optional)
  • 8 x jumper cables
  • Ultrasonic detector for distance

Step 1: Hardware Part

Many thanks to Fitzing for creating this wonderful tool for drawing Arduino sketches.

First of all we have to connect our Ultrasonic Sensor Detector to the Arduino:

* VCC connection of the sensor attached to +5V

* GND connection of the sensor attached to ground

* TRIG connection of the sensor attached to digital pin 12

* ECHO connection of the sensor attached to digital pin 13

Even though the LEDs don’t contribute much to our project (I’m a big fan of the colorful stuff :) ) we can use the red LED for showing if there’s somebody standing in front and the green one otherwise.

Step 2: Arduino IDE Part

Now we can move on to the coding part of our project.

We have Arduino IDE side and Processing side. The latter part help us read information from our microcontroller and write it into a file.

So I assume you already know how to setup your Arduino IDE. You just have to be sure to select the right board and port (From the menu Tools -> Board -> Arduino Uno (in my case) and Tools -> Port -> COM4 (you should choose the port your Arduino is connected to))

Then create a new sketch start coding :)

#define trigPin 12
#define echoPin 13
#define blue 11
#define red 10

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(red, OUTPUT);
  pinMode(blue, OUTPUT);

void loop() {
  long duration, distance;
  digitalWrite(trigPin, LOW); 
  digitalWrite(trigPin, HIGH);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH); 

/* PulseIn waits for the pin to go HIGH, starts timing, then waits for the pin to go LOW and stops timing. Returns the length of the pulse in microseconds or 0 if no complete pulse was received within the timeout.
The speed of sound is 340 m/s or 29 microseconds per centimeter. The ping travels out and back, so to find the distance of the object we take half of the distance traveled. */
  distance = (duration/2) / 29;  
  if (distance < 100) {  
  } else {

Step 3: Processing Part

We are going to use a processing sketch to save serial data from our Arduino and to save it on our computer.

If you have some issues understanding how Processing works you should check this Great tutorial.

import processing.serial.*;
Serial myPort; String dataReading = ""; String [] timeFile = {}; String [] distanceFile = {};

void setup() {


// Open the port you are using ([1] in my case) at the rate you want. You can use println(Serial.list())to print the list your Serial ports.

myPort = new Serial(this, Serial.list()[1], 9600);



void draw() { } void serialEvent(Serial myPort) { dataReading = myPort.readString();

// Here we are creating good looking timestamp :)

String timestamp = nf(day(),2) + "." + nf(month(),2) + "." + year() + "-" + nf(hour(),2) + ":" + nf(minute(),2) + ":" + nf(second(),2); int distance = parseInt(dataReading.trim());

// The trim function removes all whitespaces (it craches otherwise) if(distance < 100){ String bla = timestamp + " Distance is " + distance + " cm"; println(bla); timeFile = append(timeFile, bla); saveStrings("timestamp/timestamp.txt", timeFile); } }