## Monday, 6 February 2012

### Speed measurement with Arduino

I will begin with my first experience working with Arduino and Arduino IDE. I made a speed detection "device" using 2 laser pointers and 2 LDR sensors connected to an Arduino UNO.

Building the schematics is very easy.

The resistors are used as pull-down resistors and I wired the sensors and put them in a case, to avoid them detecting surrounding light. For each case, a hole was drilled so that the laser beam can light the sensor while the ambient light does not affect the sensor.
The working principle is easy: an object that passes by will "cut" the laser beams, this means the LDR sensor will detect this sudden drop of light intensity. First I defined a threshold value under which the sensor is considered triggered, once the value is under threshold for the first sensor then Arduino waits for the second one to be triggered. During this waiting time it counts the elapsed time between the two events. When the second beam is interrupted, the timer stops and now is just simple math. The distance between the 2 sensors is known, the time between the two events is known, and speed can be computed as speed = distance/time.

Below you can find the Arduino code:

```/*
by Claudiu Cristian
*/

unsigned long time1;
int photocellPin_1 = 0;     // 1st sensor is connected to a0
int photocellPin_2 = 1;     // 2nd sensor is connected to a1
int threshold = 700;        //value below sensors are trigerd
float Speed;              // declaration of Speed variable
float timing;
unsigned long int calcTimeout = 0; // initialisation of timeout variable

void setup(void) {
// We'll send debugging information via the Serial monitor
Serial.begin(9600);
}

void loop(void) {
// if reading of first sensor is smaller than threshold starts time count and moves to calculation function
time1 = millis();
startCalculation();
}
}

// calculation function
void startCalculation() {
calcTimeout = millis(); // asign time to timeout variable
//we wait for trigger of sensor 2 to start calculation - otherwise timeout
if (millis() - calcTimeout > 5000) return;
}
timing = ((float) millis() - (float) time1) / 1000.0; //computes time in seconds
Speed = 0.115 / timing;  //speed in m/s given a separation distance of 11.5 cm
delay(100);
Serial.print(Speed);
Serial.print("\n");
}
```

I think the code is more than well commented and needs no further explanation.

Avihs said...

hey pal, i just need full program code for "Arduino speed measuring using single ultra sonic sensor (HC SR-04)"its very urgent for me
shivay59@gmail.com

K said...

Hi,

The ping sensor or ultrasonic sensor is not very reliable. You can find here how to get the distance to an object http://www.instructables.com/id/Easy-ultrasonic-4-pin-sensor-monitoring-hc-sr04/

After you got the distance once you measure it for the second time and from here is just a little math as speed is distance/time, you know the time between the two measurements, you have the distance difference so you can have the speed.

But be warned, the ultrasonic sensor is not very reliable and is kind of prone to faulty measurements since is a low quality sonar.

Have fun

Carl Attrill said...
This comment has been removed by the author.
Carl Attrill said...

Hi, thanks for posting this, you post gave me the method of building my own Pulse counter. http://retrotext.blogspot.co.uk/2014/03/a-basic-arduino-solar-pv-monitor.html

without it I would still be wondering on how to do it.

Thanks Again, keep up the good work!

K said...

Hi Carl,
Very nice project you have there! Quite some work for that. I like it.

Lieya Nordin said...

can i change LDR SENSOR with IR sensor

K said...

Hi, sorry for the late reply. In theory yes as long as your light source emits sufficient IR light. If you use IR LED as light source then IR sensor would be the best match. But if you use a regular LED then the results will be so and so. Actually you can use any light sensitive component even an reversed LED, the only trick is to check the readings offered by that device and modify the code accordingly.

MANISH MAHALWAL said...

Hi! Great work! Keep going. I was wondering how would I do the same thing with a Raspberry Pi 3.

MANISH MAHALWAL said...

Thanks!

Mansi Tripathy said...

hey,
i just need your help for this project and full program code for "Arduino speed detection using single ultra sonic sensor (HC SR-04)"its very urgent for me.