Speed sensor

Yes, my idea is better for small scale. I am using lasers and LDRs on a current project of mine...
US is good but often not an option due to the costs involved, the OP obviously is concerned with that. Is there another solution that we can think of then? :wink:

Mowcius

Well, he could always rob a bank...

Well, what could be more stable is if you set up a laser on one side of the road - one of those small lasers would do, just has to be powerful enough...

on the other side you place a solar panel (one of those small ones, a few inches wide/long) inside of a case with an opening (so that the only light that can reach the panel is if the laser is aimed at it)... make sure both setups are stable and wont move when a car moves by (vibration is ok, as the solar panel should be large enough to catch the laser if it shifts a few millimeters)... and that's basically it... make two setups like that, then you basically have yourself a timegate system

Yeah but there is still the issue of:

Lasers shining across road where people are driving = illegal?

Mowcius

Not if it's low enough on the ground to be below the windshield level, just low enough to hit the body (as in, just above the bottom of the car's body), then there is no way that would interfere with the driver... plus, the laser's ray would be invisible to anyone driving by unless it directly hits them in the eye (which it won't), so no one would probably notice the fact that their speed is being checked unless they take the time to mess around with the four suspicious boxes placed beside the road (you should disguise them as mailboxes ;D)

Nice to see your dialogue on my case.
I've looked at the sensors from Maxbotix earlier and ruled them out on count of the price. Nice to see that we think the same way. This is just supposed to be a cheap simple project in my spare time.
Lasers legal or not don't bother me that much so I'll go with that option, but then the issue still is the small light resistor. The solar panel should be a much better idea then. The time gate system is what I'm aiming for, just didn't knew the name for it.
Any hints for a suitable solar panel and how that would work?

Not if it's low enough on the ground to be below the windshield level, just low enough to hit the body (as in, just above the bottom of the car's body), then there is no way that would interfere with the driver... plus, the laser's ray would be invisible to anyone driving by unless it directly hits them in the eye (which it won't), so no one would probably notice the fact that their speed is being checked unless they take the time to mess around with the four suspicious boxes placed beside the road (you should disguise them as mailboxes )

It still may not be legal, use infa-red ones if you can, then you wouldn't see anything (they are more expensive though).
Low enough to the ground = blind dogs?

Mowcius

Any hints for a suitable solar panel and how that would work?

I wouldn't have thought that a solar panel would work, I don't know how you'd interface with it, a small dot on it wouldn't produce much current...

You can get larger LDRs:
http://www.rapidonline.com/Electronic-Components/Optoelectronics/Photodetectors/Light-dependent-resistor-NORPS12/34793/kw/LDR
About 10mm sensing dia. I am using them for my project...

Mowcius

You can indeed use a solar panel... don't forget to enclose it though!

and to be safe, as I mentioned, use one of about a few inches wide

Nice, I will have to try that some time...

Mowcius

looks great, cant miss that even if i tie the laser to a lamp post. Now I'll just need to find solar panels. (Right after these messages... Implies that I'm taking a break now to go to sleep.)
Thanks a lot for all the help! If, (I'd like to say when), this idea becomes real I'll be sure to post it here.

/Ulf

You didn't say what type of reflex (reflector) you have across the lane from you. If it is the usual safety reflector, then it should be forgiving of vibration. Most safety reflectors use the "corner cube" principle. This sends light almost directly back to the source, regardless of the incoming direction of the light. I just tried this with my pocket laser and the reflector on my bicycle. The light does not come back in a pencil beam, it is spread out a few cm, but does come back very close to the laser pointer. Maybe a better quality reflector would have a tighter return beam?

I think you could make this work with the laser aimed at the reflector across the lane, and something like a 5 cm by 5 cm solar cell directly behind the laser pointer, maybe with both inside a 10 cm dark plastic tube to block stray light. The solar cell should mostly ignore (integrate) flickering in the return beam.

Here is another idea, what if the time it takes a car to pass a single sensor is long compared to the time of the flickering? You could program to ignore the quick on and off events and only record data when the blocking was over a certain minimum time interval. This would reduce your timing accuracy (jitter) and would set an upper limit on the speed you could measure.

I've just ordered the solar panels, they're 2,5cm square. So in a week or two they should arrive and by then i hope to have some code to test them with. Stay tuned...

/Ulf

Cool, I look forward to hearing about it...

Mowcius

I think you could make this work with the laser aimed at the reflector across the lane, and something like a 5 cm by 5 cm solar cell directly behind the laser pointer, maybe with both inside a 10 cm dark plastic tube to block stray light. The solar cell should mostly ignore (integrate) flickering in the return beam.

Don't go with reflectance... it's best you have the "receiving" solar panel on the other side of the street and then just send the data by wireless means...

Don't go with reflectance... it's best you have the "receiving" solar panel on the other side of the street and then just send the data by wireless means...

That still only works if you have access to somewhere at ground level on both sides of the street. Interesting anyway. Unfortunately I live on a dead end road with no traffic or I would try somethig similar!

Mowcius

OK, christmas, free time, hardware and arduino at hand. Got everything hooked up and my code works after some modifications. I realized the hard way that when iside a interrupt routine millis() isn't ticking along. Well, modified and working in my test setup. Here is the code. One problem to solve is that if something breaks one laser beam and not the other the code assumes a car is moving inside the speedgate and calculates a very low speed when the second beem eventually is broken. Have tried several ways to reset this but no good idea works for me.

int upSpeed, downSpeed; //Speed calculated in each direction
int upCarCounter=0, downCarCounter=0, totalCars=0; //Number of cars in each direction and total
int upMaxSpeed=0, downMaxSpeed=0; //Max speed for both directions
int upMinSpeed=999, downMinSpeed=999; //Minimum speed for both directions
long upAverageSpeed=0, downAverageSpeed=0, totalAverageSpeed=0; //Sum of speeds to calculate average from
int upCarCounterHour=0, downCarCounterHour=0, totalCarsHour=0; //Number of cars in each direction and total
int upMaxSpeedHour=0, downMaxSpeedHour=0; //Max speed for both directions
int upMinSpeedHour=999, downMinSpeedHour=999; //Minimum speed for both directions
long upAverageSpeedHour=0, downAverageSpeedHour=0, totalAverageSpeedHour=0; //Sum of speeds to calculate average from
long upGateTime=0, downGateTime=0;  //When interrupt happened, car passing time gate
long lastUpGateTime=0, lastDownGateTime=0;  //Previous time a car passed
long hourTimer=0;  //Timer to control hourly output
int hourCounter=0;  //Count hours
int upTime, downTime; //Time for car to pass gate in each direction
int distance=10000; // Distance betveen upwards and downwards gate in mm
int upGate=2;  //Pin for up gate
int downGate=3;  //Pin for down gate

void setup()
{
  pinMode(upGate, INPUT);  //Setup pins for input
  pinMode(downGate, INPUT);
  Serial.begin(9600);
  attachInterrupt(0, upwardsTimer, FALLING);  //Set up interrupts to call when change happens, car is passing gate
  attachInterrupt(1, downwardsTimer, FALLING);
}


void loop()
{
  if (millis()-hourTimer>10000) //Check if 60 minutes has passed
  {
    hourTimer=millis(); //Reset hour timer for new measurement period 
    hourlyPrintout();  //Print the total and this hours stats
    resetHourlyStats();  //Reset hourly stats for new period
    hourCounter++;  //Increase hour counter
  }


  if (upGateTime!=lastUpGateTime && downGateTime!=lastDownGateTime)  // Both gates have changed so a car has passed
  {
    if (upGateTime<downGateTime)  //Did it pass downwards or upwards?
      downwardsRun();
    else
      upwardsRun();

    lastUpGateTime=upGateTime;  //Reset timers
    lastDownGateTime=downGateTime;
  }

}


void upwardsRun()  //Prints stats for car going upwards
{


  upTime=upGateTime-downGateTime;  //How long for the car to pass the gate
  upSpeed=distance/upTime*36/10;  //Calculate speed in Km/h

  if(upSpeed<upMinSpeed)  //update fastest and slowest speed
      upMinSpeed=upSpeed;
  if(upSpeed>upMaxSpeed)
    upMaxSpeed=upSpeed;

  if(upSpeed<upMinSpeedHour)  //update fastest and slowest speed this hour
      upMinSpeedHour=upSpeed;
  if(upSpeed>upMaxSpeedHour)
    upMaxSpeedHour=upSpeed;

  upAverageSpeed=upAverageSpeed+upSpeed;    //Update average speeds and car counters
  totalAverageSpeed=totalAverageSpeed+upSpeed;
  upCarCounter++;
  totalCars++;
  upAverageSpeedHour=upAverageSpeedHour+upSpeed;    //Update average speeds and car counters this hour
  totalAverageSpeedHour=totalAverageSpeedHour+upSpeed;
  upCarCounterHour++;
  totalCarsHour++;


  Serial.print("Car number ");
  Serial.print(totalCars);
  Serial.print(" upwards speed: ");  //Print the speed
  Serial.println(upSpeed);
  Serial.println("");

}


void downwardsRun()  //Prints stats for car going downwards
{


  downTime=downGateTime-upGateTime;  //How long for the car to pass the gate
  downSpeed=distance/downTime*36/10;  //Calculate speed in Km/h

  if(downSpeed<downMinSpeed)  //update fastest and slowest speed
      downMinSpeed=downSpeed;
  if(downSpeed>downMaxSpeed)
    downMaxSpeed=downSpeed;

  if(downSpeed<downMinSpeedHour)  //update fastest and slowest speed this hour
      downMinSpeedHour=downSpeed;
  if(downSpeed>downMaxSpeedHour)
    downMaxSpeedHour=downSpeed;

  downAverageSpeed=downAverageSpeed+downSpeed;    //Update average speeds and car counters
  totalAverageSpeed=totalAverageSpeed+downSpeed;
  downCarCounter++;
  totalCars++;

  downAverageSpeedHour=downAverageSpeedHour+downSpeed;    //Update average speeds and car counters this hour
  totalAverageSpeedHour=totalAverageSpeedHour+downSpeed;
  downCarCounterHour++;
  totalCarsHour++;

  Serial.print("Car number ");
  Serial.print(totalCars);
  Serial.print(" downwards speed: ");  //Print the speed
  Serial.println(downSpeed);
  Serial.println("");

}


void hourlyPrintout()  //Prints hourly stats
{
  Serial.println("----------------------------------------------------------");  //Print all info about the cars
  Serial.println("");
  Serial.print("          Hour interval: ");
  Serial.print(hourCounter);
  Serial.print("-");
  Serial.println(hourCounter+1);

  Serial.print("      Total cars passed: ");
  Serial.print(totalCars);
  Serial.print("\t      Total cars passed this hour: ");
  Serial.println(totalCarsHour);
  Serial.print("    Total Average speed: ");
  Serial.print(totalAverageSpeed/totalCars);
  Serial.print("\t    Total Average speed this hour: ");
  Serial.println(totalAverageSpeedHour/totalCarsHour);
  Serial.println("");

  Serial.print("    Cars passed upwards: ");
  Serial.print(upCarCounter);
  Serial.print("\t    Cars passed upwards this hour: ");
  Serial.println(upCarCounterHour);
  Serial.print("  Upwards Average speed: ");
  Serial.print(upAverageSpeed/upCarCounter);
  Serial.print("\t  Upwards Average speed this hour: ");
  Serial.println(upAverageSpeedHour/upCarCounterHour);
  Serial.print("      Upwards Max speed: ");
  Serial.print(upMaxSpeed);
  Serial.print("\t      Upwards Max speed this hour: ");
  Serial.println(upMaxSpeedHour);
  Serial.print("      Upwards Min speed: ");
  Serial.print(upMinSpeed);
  Serial.print("\t      Upwards Min speed this hour: ");
  Serial.println(upMinSpeedHour);
  Serial.println("");

  Serial.print("  Cars passed downwards: ");
  Serial.print(downCarCounter);
  Serial.print("\t  Cars passed downwards this hour: ");
  Serial.println(downCarCounterHour);
  Serial.print("Downwards Average speed: ");
  Serial.print(downAverageSpeed/downCarCounter);
  Serial.print("\tDownwards Average speed this hour: ");
  Serial.println(downAverageSpeedHour/downCarCounterHour);
  Serial.print("    Downwards Max speed: ");
  Serial.print(downMaxSpeed);
  Serial.print("\t    Downwards Max speed this hour: ");
  Serial.println(downMaxSpeedHour);
  Serial.print("    Downwards Min speed: ");
  Serial.print(downMinSpeed);
  Serial.print("\t    Downwards Min speed this hour: ");
  Serial.println(downMinSpeedHour);
  Serial.println("");
  Serial.println("");


}


void resetHourlyStats()
{
  totalCarsHour=0;  //Reset hourly stats for new measurement period
  upCarCounterHour=0;
  downCarCounterHour=0;
  upMaxSpeedHour=0;
  downMaxSpeedHour=0; 
  upMinSpeedHour=999;
  downMinSpeedHour=999;
  upAverageSpeedHour=0;
  downAverageSpeedHour=0;
  totalAverageSpeedHour=0;
}


void upwardsTimer()  //Car has passed upper gate
{
  upGateTime=millis();  //Time when car passed gate
  /*
  Serial.print("Upper gate passed at: ");
   Serial.println(upGateTime);
   Serial.println("");
   */
}


void downwardsTimer()  //Car has passed lower gate
{
  downGateTime=millis();  //Time when car passed gate
  /*
  Serial.print("Lower gate passed at: ");
   Serial.println(downGateTime);
   Serial.println("");
   */
}

/Ulf

Looking good!

I can't really think of a good way to stop the errors you are having though.

Becuase there are two lanes of traffic it is not going to be easy. Have you tried it on the road yet then or just as a protype test?

Mowcius

Still on the test bench. Have tested laser pointer module and solar cell about 6 meter apart so this should work. Now I need lots of cable...
I still want to use a reflector so I can have all electronics on one side of the road and just a reflex, mirror or something on the other side.

Now one laser pointer is more of a laser lamp. The point is about 5x5 cm at 6m distance. May need to change it.

/Ulf

If you cannot alter the laser focus then I have a lot of laser modules and I would be happy to sell one or two to you (PM me if you want). They are good to about 600m and have focusing on them (with flying leads, apparently 2.3-3v but I have run them off 9v and no issues for a short time - probably should have a resistor off 5v or 3.3v to get them to 3v or lower)