# Arduino LDR Speedometer

7,990

25

4

## Introduction: Arduino LDR Speedometer

Maybe you have got a mini cable car or any thing else that you want to know it's speed.

There are many ways on HOW-TO mesure speed, but today we will be working with basic speed measuring. I was just building my model train when this strange idea came into my head. I HAVE TO KNOW THE SPEED OF THE TRAIN... Since trains are close to ground and make shadow underneath it will be easy to measure approximate SPEED.

This really works and outputs real speed. It is not the exact speed but close to exact. Let's stop talking and do some DIYing.

What Can This Be Used For?

- It can be used for measuring speed of objects close to surfate that make shadow.

What Does This Guide Include?

- Arduino code

- Processing 2 code

- How to make speed sensor

What Do I Need For This Project

- ARDUINO (I'm using Arduino Mega 2560, but works fine on Arduino Uno ,...)

- LDR (Light Dependent Resistor)

- 500 Ohms Resistor

You will need some expirience with Arduino and electronics. You don't need exactly 500 Ohms resistor, but if you use resistor with different resistance you will have to change some values in Arduino code.

## Step 1: Connecting LDR to Arduino

Connect 5V to one leg of resistor. Connect the other leg (of resistor ) to one leg of LDR . Connect A0 (analog input) to that same leg of LDR. Connect GND to other pin of the LDR.

That is all about the connections. It's not rocket science! :)

## Step 2: Programing Arduino

Connect your Arduino to computer and look up for the port (COM port on windows) on which Arduino is connected to. Remember it. You'll need it later. Then just upload this code to Arduino. Just fill in object length you can play with other variables too if you want.

```int ldr = 0;
int if_val = 1;
int ldr_value = 0;
unsigned long time;
unsigned long time2;
float time3;
float sped = 0;
int val;
int start_val = 0;  //analog pin to which LDR is connected
float object_length = 5.5; //object length in cm
int sensitivity = 40;//Less more sensitive, more less sensitive

void setup()
{
Serial.begin(9600);   //start serial monitor
object_length = object_length * 1000;
start_val = val + sensitivity;
}void loop()
{
if (ldr_value > start_val){
if (if_val == 1){
if_val = 0;
time = millis();
} else { }
}else{
if(if_val == 0){
if_val = 1;
time2 = millis();
time3 = (time2 - time);
sped = (object_length / time3) / 100;
Serial.println(sped);
}

}
}```

## Step 3: Programing Processing

We won't stop at just reading the speed. We want an app that will read the output and display it in different units. There is no easier way than a simple program in PROCESSING. You can download it here.

Just copy and paste this code into Processing, connect Arduino to computer, run code on Arduino and press start button in Processing. And enter your port .

```import processing.serial.*;
PFont f;
float val = 0;
Serial port; // The serial port object
String Ardport = ""; //Enter the port on which Arduino is connected
void setup() {
size(200,200);
f = createFont("Arial",16,true); // Arial, 16 point, anti-aliasing on
// In case you want to see the list of available ports
// println(Serial.list());
port = new Serial(this, Ardport, 9600);
}
void draw() { }
// Called whenever there is something available to read
void serialEvent(Serial port) {
if (inString != null) {
// trim off any whitespace:
inString = trim(inString);
// convert to an float println(inString);
float val = float(inString);
float val1 = val * 3.6;
background(255);
textFont(f,16);
fill(0);
text("Speed : " + val + "M/s",10,50);
text("Speed : " + val1 + "Km/h",10,75);
println( "Raw Input:" + val);
}
}```

## Step 4: How Does It Work

The program waits for a shadow and then it measures time between the start of the shadow and the end of the shadow. So enter exact length of your object because if you don't it won't show real speed.

As you know speed is (length of ) PATH / TIME. So PATH is length of object and TIME is time between the start of the shadow and the end of the shadow.

I will make another Instructabe with IR sensor to mesure speed.

WARNING:

This speed meter is not reliable and needs same brightness through whole process. 