{"id":184,"date":"2010-04-05T12:37:50","date_gmt":"2010-04-05T17:37:50","guid":{"rendered":"http:\/\/nootropicdesign.com\/projectlab\/?p=184"},"modified":"2018-11-02T09:20:00","modified_gmt":"2018-11-02T14:20:00","slug":"speed-trap","status":"publish","type":"post","link":"https:\/\/nootropicdesign.com\/projectlab\/2010\/04\/05\/speed-trap\/","title":{"rendered":"Speed Trap!  A GPS-Based Speeding Alert"},"content":{"rendered":"<p><strong><em>Difficulty Level = 9<\/em><\/strong>  <a href=\"\/projectlab\/difficulty-levels\/\">[What&#8217;s this?]<\/a><\/p>\n<p>This was a fun project I built using a perf-board Arduino with GPS receiver, some LEDs, some long wire, some clever code, and a car!  <\/p>\n<p>I have this <em>friend<\/em> that has a speeding problem.  So I built this device for my <em>friend<\/em> so he&#8217;ll know when he&#8217;s speeding.  How will he know?  Because when the small perf-board Arduino device with a GPS module detects that his vehicle&#8217;s speed is over the speed limit, it turns on a police lights display that is mounted on the inside of his car&#8217;s rear windshield.  When he sees those flashing police lights in the rear-view mirror, he&#8217;ll know he needs to slow down!<\/p>\n<div id=\"attachment_189\" style=\"width: 650px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/nootropicdesign.com\/projectlab\/wp-content\/uploads\/2010\/03\/speedTrap-rearview.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-189\" src=\"https:\/\/nootropicdesign.com\/projectlab\/wp-content\/uploads\/2010\/03\/speedTrap-rearview.jpg\" alt=\"\" title=\"speedTrap-rearview\" width=\"640\" height=\"480\" class=\"size-full wp-image-189\" srcset=\"https:\/\/nootropicdesign.com\/projectlab\/wp-content\/uploads\/2010\/03\/speedTrap-rearview.jpg 640w, https:\/\/nootropicdesign.com\/projectlab\/wp-content\/uploads\/2010\/03\/speedTrap-rearview-300x225.jpg 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/a><p id=\"caption-attachment-189\" class=\"wp-caption-text\">Police in the rear view mirror!<\/p><\/div>\n<p\/>\n<p\/>\nOK, it&#8217;s me, but you already figured that out.  If you weren&#8217;t smart, you wouldn&#8217;t be here.  Here&#8217;s a rundown of this project&#8217;s features:<\/p>\n<ul>\n<li>A simple perf-board Arduino circuit with an <a href=\"http:\/\/www.sparkfun.com\/commerce\/product_info.php?products_id=465\">EM-406a GPS Receiver<\/a> connected to the microcontroller<\/li>\n<li>A pair of CAT-5 cables running from the circuit to the back of my Honda Civic<\/li>\n<li>A small perf-board with red and blue LEDs and two white LEDs representing the headlights of a police car.  This small board is mounted on the inside of the rear windshield using suction cups.<\/li>\n<li>The software running on the microcontroller is programmed to know the speed limit in different locations near where I live and drive.  I did this by specifying &#8220;speed zones&#8221; which are polygons and a speed limit.  The polygons are defined as a list of latitude\/longitude vertices.<\/li>\n<li>Whenever the GPS module reports the current position and speed (every second) the code determines which bounding polygon or &#8220;speed zone&#8221; the car is located in.  If the GPS receiver reports that the current speed is greater than the speed zone&#8217;s limit, the police lights are activated.  If below the speed limit, the lights will be turned off.\n<\/ul>\n<h3>The Hardware<\/h3>\n<p>Look at this nice clean circuit&#8230;<\/p>\n<p\/>\n<a href=\"https:\/\/nootropicdesign.com\/projectlab\/wp-content\/uploads\/2010\/03\/gpsArduino-top.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/nootropicdesign.com\/projectlab\/wp-content\/uploads\/2010\/03\/gpsArduino-top.jpg\" alt=\"\" title=\"gpsArduino-top\" width=\"640\" height=\"480\" class=\"alignnone size-full wp-image-181\" srcset=\"https:\/\/nootropicdesign.com\/projectlab\/wp-content\/uploads\/2010\/03\/gpsArduino-top.jpg 640w, https:\/\/nootropicdesign.com\/projectlab\/wp-content\/uploads\/2010\/03\/gpsArduino-top-300x225.jpg 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/a><\/p>\n<p\/>\nOMG don&#8217;t look at the bottom!<\/p>\n<p\/>\n<a href=\"https:\/\/nootropicdesign.com\/projectlab\/wp-content\/uploads\/2010\/03\/gpsArduino-bottom.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/nootropicdesign.com\/projectlab\/wp-content\/uploads\/2010\/03\/gpsArduino-bottom.jpg\" alt=\"\" title=\"gpsArduino-bottom\" width=\"640\" height=\"480\" class=\"alignnone size-full wp-image-180\" srcset=\"https:\/\/nootropicdesign.com\/projectlab\/wp-content\/uploads\/2010\/03\/gpsArduino-bottom.jpg 640w, https:\/\/nootropicdesign.com\/projectlab\/wp-content\/uploads\/2010\/03\/gpsArduino-bottom-300x225.jpg 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/a><\/p>\n<p><!--more--><br \/>\nThis is the schematic.  This is just a perf-board version of an Arduino with a few extra things.  Ten of the digital outputs are connected to a header for the cable to connect to.  The GPS receiver is connected to power and ground, and tied to the TX and RX pins on the ATMEGA328 microcontroller.  There is a test switch connected to Arduino pin 9 (ATMEGA328 pin 15).  Lastly, there&#8217;s a 5 pin header for interfacing with a USB to serial board so I can program the chip.  The 5 pins are 5V, GND, TX, RX, and the DTR signal for resetting the ATMEGA328 processor when the chip is programmed.<\/p>\n<p\/>\n<a href=\"https:\/\/nootropicdesign.com\/projectlab\/wp-content\/uploads\/2010\/03\/speedTrapSchematic.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/nootropicdesign.com\/projectlab\/wp-content\/uploads\/2010\/03\/speedTrapSchematic-sm.png\" alt=\"\" title=\"speedTrapSchematic-sm\" width=\"640\" height=\"375\" class=\"alignnone size-full wp-image-199\" \/><\/a><\/p>\n<p\/>\n<p\/>\nHere&#8217;s the circuit board connected to the messy CAT-5 wiring harness.  There&#8217;s about 6 feet of wire running to the LED circuit board.<br \/>\n<div id=\"attachment_182\" style=\"width: 650px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/nootropicdesign.com\/projectlab\/wp-content\/uploads\/2010\/03\/speedTrap-connected.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-182\" src=\"https:\/\/nootropicdesign.com\/projectlab\/wp-content\/uploads\/2010\/03\/speedTrap-connected.jpg\" alt=\"\" title=\"speedTrap-connected\" width=\"640\" height=\"480\" class=\"size-full wp-image-182\" srcset=\"https:\/\/nootropicdesign.com\/projectlab\/wp-content\/uploads\/2010\/03\/speedTrap-connected.jpg 640w, https:\/\/nootropicdesign.com\/projectlab\/wp-content\/uploads\/2010\/03\/speedTrap-connected-300x225.jpg 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/a><p id=\"caption-attachment-182\" class=\"wp-caption-text\">The simple GPS Arduino conntected to a battery and the wires that run to the police lights.<\/p><\/div><\/p>\n<p\/>\n<p\/>\nThe test button can be used to verify that everything is connected.<br \/>\n<div id=\"attachment_183\" style=\"width: 650px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/nootropicdesign.com\/projectlab\/wp-content\/uploads\/2010\/03\/speedTrap-lights.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-183\" src=\"https:\/\/nootropicdesign.com\/projectlab\/wp-content\/uploads\/2010\/03\/speedTrap-lights.jpg\" alt=\"\" title=\"speedTrap-lights\" width=\"640\" height=\"480\" class=\"size-full wp-image-183\" srcset=\"https:\/\/nootropicdesign.com\/projectlab\/wp-content\/uploads\/2010\/03\/speedTrap-lights.jpg 640w, https:\/\/nootropicdesign.com\/projectlab\/wp-content\/uploads\/2010\/03\/speedTrap-lights-300x225.jpg 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/a><p id=\"caption-attachment-183\" class=\"wp-caption-text\">Blinky police lights that mount on the rear windshield of my car.<\/p><\/div><\/p>\n<p\/>\n<p\/>\nHere&#8217;s how the LED circuit board looks mounted in the rear windshield.  I put a small square of black paper above the circuit board to reduce reflection on the glass.<br \/>\n<div id=\"attachment_208\" style=\"width: 650px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/nootropicdesign.com\/projectlab\/wp-content\/uploads\/2010\/03\/speedTrap-incar.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-208\" src=\"https:\/\/nootropicdesign.com\/projectlab\/wp-content\/uploads\/2010\/03\/speedTrap-incar.jpg\" alt=\"\" title=\"speedTrap-incar\" width=\"640\" height=\"480\" class=\"size-full wp-image-208\" srcset=\"https:\/\/nootropicdesign.com\/projectlab\/wp-content\/uploads\/2010\/03\/speedTrap-incar.jpg 640w, https:\/\/nootropicdesign.com\/projectlab\/wp-content\/uploads\/2010\/03\/speedTrap-incar-300x225.jpg 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/a><p id=\"caption-attachment-208\" class=\"wp-caption-text\">LED circuit board mounted on inside of rear windshield<\/p><\/div><\/p>\n<p\/>\n<p\/>\nThis is what it looks like when the police lights are activated.  First a video shot to the rear of the car, then what it looks like in the rear view mirror.  It was difficult to capture this on video, and it really doesn&#8217;t do it justice.  I can tell you that when driving around at night, the illusion is fantastic.  It really does look like there is a police car behind you!<\/p>\n<p><iframe loading=\"lazy\" width=\"640\" height=\"360\" src=\"https:\/\/www.youtube.com\/embed\/nh-PDEUle0Y\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\n<h3>The Code<\/h3>\n<p><a href=\"https:\/\/nootropicdesign.com\/projectlab\/downloads\/SpeedTrap.pde\">Download the full Arduino code here.<\/a><\/p>\n<p>The software running on the Arduino ATMEGA328 chip utilizes the wonderfully robust <a href=\"http:\/\/arduiniana.org\/libraries\/NewSoftSerial\/\">NewSoftSerial<\/a> library for communicating with the EM-406a GPS module and the very convenient <a href=\"http:\/\/arduiniana.org\/libraries\/tinygps\/\">TinyGPS<\/a> library for parsing the NMEA sentences from the EM-406a.  Both of these awesome libraries are provided by Mikal Hart.<\/p>\n<p>The TinyGPS library makes it easy to obtain the speed over ground in miles per hour.  The method for determining whether my car is speeding or not depends not only on knowing how fast I&#8217;m going and where I am, but also knowing what the speed limit is where I am.  For this, I have to hard-code in the information about various speed zones in the area I&#8217;m driving.  This isn&#8217;t as tedious as it sounds.  The idea is to define polygons on a map that represent different speed zones.  Here&#8217;s a map I sketched in order to plan my code.  I just did a screen capture in Google Maps and then drew on it with a simple drawing program.  Click through for a larger version:<\/p>\n<div id=\"attachment_215\" style=\"width: 650px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/nootropicdesign.com\/projectlab\/wp-content\/uploads\/2010\/03\/polygonsMap-large.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-215\" src=\"https:\/\/nootropicdesign.com\/projectlab\/wp-content\/uploads\/2010\/03\/polygonsMap-640x480.png\" alt=\"Click for larger map\" title=\"polygonsMap-640x480\" width=\"640\" height=\"480\" class=\"size-full wp-image-215\" srcset=\"https:\/\/nootropicdesign.com\/projectlab\/wp-content\/uploads\/2010\/03\/polygonsMap-640x480.png 640w, https:\/\/nootropicdesign.com\/projectlab\/wp-content\/uploads\/2010\/03\/polygonsMap-640x480-300x225.png 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/a><p id=\"caption-attachment-215\" class=\"wp-caption-text\">Map showing polygons definition for my neighborhood - click through for larger map<\/p><\/div>\n<p\/>\n<p\/>\nThen I used a simple tool provided at Google Maps called the <a href=\"http:\/\/code.google.com\/apis\/maps\/documentation\/javascript\/v2\/examples\/tile-detector.html\">&#8220;tile detector&#8221;<\/a>.  By clicking on the tile detector map you can easily determine the latitude and longitude of any point.  Then I defined polygons in the setupSpeedZones().  Each zone has a speed limit and a list of vertices that define the polygon.<\/p>\n<pre class=\"code\">\r\n  \/\/ Rockford Road                                                                                          \r\n  speedZones[0] = &SpeedZone(45);\r\n  speedZones[0]->setVertices(6, (Vertex[6]){\r\n      Vertex(45.027162772967756, -93.48137855529785),\r\n      Vertex(45.02946790848425, -93.4742546081543),\r\n      Vertex(45.02955889877115, -93.46193790435791),\r\n      Vertex(45.02861865883124, -93.46172332763672),\r\n      Vertex(45.02861865883124, -93.47412586212158),\r\n      Vertex(45.02649547957147, -93.48133563995361)});\r\n\r\n\r\n  \/\/ Schmidt Lake Rd                                                                                        \r\n  speedZones[1] = &SpeedZone(45);\r\n  speedZones[1]->setVertices(10, (Vertex[10]){\r\n      Vertex(45.044176126280206, -93.48219394683838),\r\n      Vertex(45.04390322470628, -93.47322463989258),\r\n      Vertex(45.043387740403595, -93.46974849700928),\r\n      Vertex(45.0440548368525, -93.46498489379883),\r\n      Vertex(45.0440548368525, -93.46185207366943),\r\n      Vertex(45.04332709488612, -93.46185207366943),\r\n      Vertex(45.0433574176529, -93.46580028533936),\r\n      Vertex(45.04272063617576, -93.46983432769775),\r\n      Vertex(45.043266449304376, -93.47434043884277),\r\n      Vertex(45.04332709488612, -93.48215103149414)});\r\n\r\n      ...\r\n<\/pre>\n<p>The function getSpeedLimit() checks to see if the current GPS position is within each speed zone starting with the first one.  Note that I defined a &#8220;catch all&#8221; speed zone with a limit of 25mph around the whole area.  It&#8217;s ok if polygons overlap as long as you understand that the first match found will be the one that is used for the speed limit.  This means you have to order your speed zone definitions with care.  My catch all speed zone is defined last for this reason.<\/p>\n<p>The algorithm for determining if a point is within a polygon is actually quite simple (I was surprised).  I adapted my code from the <a href=\"http:\/\/www.ecse.rpi.edu\/Homepages\/wrf\/Research\/Short_Notes\/pnpoly.html\">algorithm provided here<\/a>.  I&#8217;m grateful that I found such a simple implementation to use.<\/p>\n<p>The rest of the code should be pretty self-explanatory.  If the car is speeding, turn on the police lights.  If not, turn them off.  <a href=\"https:\/\/nootropicdesign.com\/projectlab\/downloads\/SpeedTrap.pde\">Download the full Arduino code here.<\/a><\/p>\n<p><h3>Conclusions<\/h3>\n<p>This project worked amazingly well.  The GPS receiver can take a long time (maybe 3 minutes) to get a fix when it is powered on, but once it gets a fix, it is very accurate.  The only shortcoming is that the EM-406a receiver only outputs NMEA sentences once per second, so there&#8217;s a bit of lag, but for the most part this device would correctly react to speed limit changes almost immediately after crossing a speed zone polygon border.<\/p>\n<p>The speed over ground reported by the EM-406a is pretty accurate but seemed to be about 2mph lower than what my speedometer in my car reports.  So if I was in a 45mph speed zone, the police lights would not come on until I got up to about 47mph.  If this difference is consistent, I could easily compensate for it in the software.<\/p>\n<p>Overall, this was a really fun project with great results!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Difficulty Level = 9 [What&#8217;s this?] This was a fun project I built using a perf-board Arduino with GPS receiver, some LEDs, some long wire, some clever code, and a car! I have this friend that has a speeding problem. So I built this device for my friend so he&#8217;ll know when he&#8217;s speeding. How [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[3,8],"tags":[],"class_list":["post-184","post","type-post","status-publish","format-standard","hentry","category-arduino","category-gps"],"_links":{"self":[{"href":"https:\/\/nootropicdesign.com\/projectlab\/wp-json\/wp\/v2\/posts\/184","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nootropicdesign.com\/projectlab\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nootropicdesign.com\/projectlab\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nootropicdesign.com\/projectlab\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/nootropicdesign.com\/projectlab\/wp-json\/wp\/v2\/comments?post=184"}],"version-history":[{"count":39,"href":"https:\/\/nootropicdesign.com\/projectlab\/wp-json\/wp\/v2\/posts\/184\/revisions"}],"predecessor-version":[{"id":1971,"href":"https:\/\/nootropicdesign.com\/projectlab\/wp-json\/wp\/v2\/posts\/184\/revisions\/1971"}],"wp:attachment":[{"href":"https:\/\/nootropicdesign.com\/projectlab\/wp-json\/wp\/v2\/media?parent=184"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nootropicdesign.com\/projectlab\/wp-json\/wp\/v2\/categories?post=184"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nootropicdesign.com\/projectlab\/wp-json\/wp\/v2\/tags?post=184"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}