








{"id":123,"date":"2020-10-12T21:34:31","date_gmt":"2020-10-12T21:34:31","guid":{"rendered":"https:\/\/osoyoo.info\/?p=123"},"modified":"2020-10-13T22:14:03","modified_gmt":"2020-10-13T22:14:03","slug":"arduino-basic-lesson-22-ir-track-sensor","status":"publish","type":"post","link":"https:\/\/osoyoo.info\/index.php\/2020\/10\/12\/arduino-basic-lesson-22-ir-track-sensor\/","title":{"rendered":"Hardware Programming with Arduino IDE &#8211; Lesson 22: IR Track Sensor"},"content":{"rendered":"<p><a href=\"https:\/\/osoyoo.info\/index.php\/2020\/10\/12\/arduino-basic-lesson-21-mini-fan\/\"><img loading=\"lazy\" class=\"alignnone\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/Previous.png\" alt=\"\" width=\"206\" height=\"59\" \/><\/a>\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0<a href=\"https:\/\/osoyoo.info\/index.php\/2020\/10\/11\/arduino-basic-tutorial-directory\/\"><img loading=\"lazy\" class=\"alignnone\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/Tutorial.png\" alt=\"\" width=\"206\" height=\"59\" \/><\/a>\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0<a href=\"https:\/\/osoyoo.info\/index.php\/2020\/10\/12\/arduino-basic-lesson-23-ultrasonic-sensor\/\"><img loading=\"lazy\" class=\"alignright\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/Next.png\" alt=\"\" width=\"206\" height=\"59\" \/><\/a><\/p>\n<div align=\"center\">\n<div align=\"center\"><img class=\"aligncenter\" src=\"https:\/\/osoyoo.com\/picture\/icon\/authorized_retailers.png\" \/><\/div>\n<div align=\"center\"><\/div>\n<table>\n<tbody>\n<tr>\n<td>Buy from US<\/td>\n<td>Buy from UK<\/td>\n<td>Buy from DE<\/td>\n<td>Buy from IT<\/td>\n<td>Buy from FR<\/td>\n<td>Buy from ES<\/td>\n<td>Buy from JP<\/td>\n<\/tr>\n<tr>\n<td><a href=\"https:\/\/www.amazon.com\/dp\/B08J7DKJK9?ref=myi_title_dp\"><img loading=\"lazy\" class=\"alignnone size-large\" src=\"https:\/\/osoyoo.com\/picture\/icon\/us_buy_now.png\" width=\"150\" height=\"30\" \/><\/a><\/td>\n<td><a href=\"https:\/\/www.amazon.co.uk\/dp\/B08J6BL2P7\"><img loading=\"lazy\" class=\"alignnone\" src=\"https:\/\/osoyoo.com\/picture\/icon\/uk_buy_now.png\" alt=\"\" width=\"150\" height=\"30\" \/><\/a><\/td>\n<td><a href=\"https:\/\/www.amazon.de\/dp\/B08J6BL2P7\"><img loading=\"lazy\" class=\"alignnone size-large\" src=\"https:\/\/osoyoo.com\/picture\/icon\/de_buy_now.png\" width=\"150\" height=\"30\" \/><\/a><\/td>\n<td><a href=\"https:\/\/www.amazon.it\/dp\/B08J6BL2P7\"><img loading=\"lazy\" class=\"alignnone size-large\" src=\"https:\/\/osoyoo.com\/picture\/icon\/it_buy_now.png\" width=\"150\" height=\"30\" \/><\/a><\/td>\n<td><a href=\"https:\/\/www.amazon.fr\/dp\/B08J6BL2P7\"><img loading=\"lazy\" class=\"alignnone size-large\" src=\"https:\/\/osoyoo.com\/picture\/icon\/fr_buy_now.png\" width=\"150\" height=\"30\" \/><\/a><\/td>\n<td><a href=\"https:\/\/www.amazon.es\/dp\/B08J6BL2P7\"><img loading=\"lazy\" class=\"alignnone size-large\" src=\"https:\/\/osoyoo.com\/picture\/icon\/es_buy_now.png\" width=\"150\" height=\"30\" \/><\/a><\/td>\n<td><a href=\"https:\/\/www.amazon.co.jp\/dp\/B08J7TLP48\"><img loading=\"lazy\" class=\"alignnone size-large\" src=\"https:\/\/osoyoo.com\/picture\/icon\/jp_buy_now.png\" width=\"150\" height=\"30\" \/><\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h2><strong>Content<\/strong><\/h2>\n<ol>\n<li><a href=\"#1\">Introduction<\/a><\/li>\n<li><a href=\"#2\">Preparations<\/a>\n<ul>\n<li><a href=\"#2.1\">Hardware<\/a><\/li>\n<li><a href=\"#2.2\">Software<\/a><\/li>\n<\/ul>\n<\/li>\n<li><a href=\"#3\">About the IR Track Sensor<\/a><\/li>\n<li><a href=\"#5\">Connection<\/a><\/li>\n<li><a href=\"#6\">Upload Sketch<\/a><\/li>\n<li><a href=\"#7\">Program Running Result<\/a><\/li>\n<\/ol>\n<h1 id=\"1\">Introduction<\/h1>\n<p>The IR Track Sensor is essentially an IR LED and an IR detector. It works by transmitting a beam of IR light downward toward the surface. In this lesson, we will show you how the IR Track Sensor works and how to use it with the Osoyoo Uno board.<\/p>\n<h1 id=\"2\">Preparations<\/h1>\n<h2 id=\"2.1\">Hardware<\/h2>\n<ul>\n<li>Osoyoo UNO Board (Fully compatible with Arduino UNO rev.3) x 1<\/li>\n<li>OSOYOO Magic I\/O Shield for Arduino x1<\/li>\n<li>OSOYOO IR Track Sensor x 1<\/li>\n<li>3 pin PnP cable x 1<\/li>\n<li>USB Cable x 1<\/li>\n<li>PC x 1<\/li>\n<\/ul>\n<h2 id=\"2.2\">Software<\/h2>\n<ul>\n<li>Arduino IDE (version 1.6.4+)<\/li>\n<\/ul>\n<h1 id=\"3\">About IR Track Sensor<\/h1>\n<p><img loading=\"lazy\" class=\"alignnone size-large aligncenter\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/lesson20\/modules-tracking.png\" width=\"1010\" height=\"315\" \/><\/p>\n<h1 id=\"5\">Connection<\/h1>\n<p>First, please plug OSOYOO Magic I\/O shield into UNO board:<\/p>\n<p><img loading=\"lazy\" class=\"size-full aligncenter\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/lesson4\/UNO%2BBase-min.png\" width=\"1000\" height=\"600\" \/><\/p>\n<p>Then connect the OSOYOO IR tracking sensor to the port D2 of the Magic I\/O shield with 3-pin PNP cables as below digram (please put the switch on tracking sensor to D):<\/p>\n<p><img loading=\"lazy\" class=\"aligncenter size-full\" src=\"http:\/\/osoyoo.com\/picture\/OSOYOO_PnP_Kit_for_Arduino\/Lesson23\/Track.jpg\" width=\"1000\" height=\"489\" \/><\/p>\n<h1 id=\"6\">Code Program<\/h1>\n<p>After above operations are completed, connect the Arduino board to your computer using the USB cable. The green power LED (labelled <strong>PWR<\/strong>) should go on.Open the Arduino IDE and choose corresponding board type and port type for you project. Then load up <a href=\"https:\/\/github.com\/osoyoo\/Osoyoo-development-kits\/blob\/master\/Osoyoo%20lessons%20for%20Arduino\/IR_Track_sensor.zip\" target=\"_blank\" rel=\"noopener noreferrer\">the following sketch<\/a> onto your Arduino.<\/p>\n<pre><span style=\"color: #00979c;\">const<\/span> <span style=\"color: #00979c;\">int<\/span> <span style=\"color: #000000;\">trackingPin<\/span> <span style=\"color: #434f54;\">=<\/span> <span style=\"color: #000000;\">2<\/span><span style=\"color: #000000;\">;<\/span> <span style=\"color: #434f54;\">\/\/the tracking module attach to pin 2<\/span>\r\n<span style=\"color: #00979c;\">const<\/span> <span style=\"color: #00979c;\">int<\/span> <span style=\"color: #000000;\">ledPin<\/span> <span style=\"color: #434f54;\">=<\/span> <span style=\"color: #000000;\">13<\/span><span style=\"color: #000000;\">;<\/span> <span style=\"color: #434f54;\">\/\/pin13 built-in led<\/span>\r\n<span style=\"color: #00979c;\">void<\/span> <span style=\"color: #5e6d03;\">setup<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">)<\/span>\r\n<span style=\"color: #000000;\">{<\/span>\r\n  <span style=\"color: #d35400;\">pinMode<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">trackingPin<\/span><span style=\"color: #434f54;\">,<\/span> <span style=\"color: #00979c;\">INPUT<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span> <span style=\"color: #434f54;\">\/\/ set trackingPin as INPUT<\/span>\r\n  <span style=\"color: #d35400;\">pinMode<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">ledPin<\/span><span style=\"color: #434f54;\">,<\/span> <span style=\"color: #00979c;\">OUTPUT<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span> <span style=\"color: #434f54;\">\/\/set ledPin as OUTPUT<\/span>\r\n<span style=\"color: #000000;\">}<\/span>\r\n<span style=\"color: #00979c;\">void<\/span> <span style=\"color: #5e6d03;\">loop<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">)<\/span>\r\n<span style=\"color: #000000;\">{<\/span>\r\n  <span style=\"color: #00979c;\">boolean<\/span> <span style=\"color: #000000;\">val<\/span> <span style=\"color: #434f54;\">=<\/span> <span style=\"color: #d35400;\">digitalRead<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">trackingPin<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span> <span style=\"color: #434f54;\">\/\/ read the value of tracking module<\/span>\r\n  <span style=\"color: #5e6d03;\">if<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">val<\/span> <span style=\"color: #434f54;\">==<\/span> <span style=\"color: #00979c;\">HIGH<\/span><span style=\"color: #000000;\">)<\/span> <span style=\"color: #434f54;\">\/\/if it is HiGH<\/span>\r\n  <span style=\"color: #000000;\">{<\/span> \r\n    <span style=\"color: #d35400;\">digitalWrite<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">ledPin<\/span><span style=\"color: #434f54;\">,<\/span> <span style=\"color: #00979c;\">LOW<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span> <span style=\"color: #434f54;\">\/\/turn off the led<\/span>\r\n  <span style=\"color: #000000;\">}<\/span>\r\n  <span style=\"color: #5e6d03;\">else<\/span>\r\n  <span style=\"color: #000000;\">{<\/span>\r\n    <span style=\"color: #d35400;\">digitalWrite<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">ledPin<\/span><span style=\"color: #434f54;\">,<\/span> <span style=\"color: #00979c;\">HIGH<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span> <span style=\"color: #434f54;\">\/\/turn on the led<\/span>\r\n  <span style=\"color: #000000;\">}<\/span>\r\n<span style=\"color: #000000;\">}<\/span>\r\n\r\n<\/pre>\n<h1 id=\"7\">Running Result<\/h1>\n<p>A few seconds after the upload finishes, set it down on a piece of paper with a dark line (at least \u00bd&#8221; wide). You may use a Sharpie Marker, electrical tape, or dark paint. When the module gets on a black line, it output high and the corresponding LED stays off; when it meets a white area, it outputs low and the LED lights up.<\/p>\n<p><strong><em>Note<\/em><\/strong><em>: <\/em><\/p>\n<p><em>1.The black line should be wider than the IR track<\/em><em> sensor<\/em><em>. <\/em><\/p>\n<p><em>2.The sensitivity of the infrared sensor is adjustable \u2013 you may adjust it by the potentiometer.<\/em><\/p>\n<p><center><iframe src=\"https:\/\/www.youtube.com\/embed\/S2Hz5OR83-Y\" width=\"1280\" height=\"720\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/center><a href=\"https:\/\/osoyoo.info\/index.php\/2020\/10\/12\/arduino-basic-lesson-21-mini-fan\/\"><img loading=\"lazy\" class=\"alignnone\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/Previous.png\" alt=\"\" width=\"206\" height=\"59\" \/><\/a>\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0<a href=\"https:\/\/osoyoo.info\/index.php\/2020\/10\/11\/arduino-basic-tutorial-directory\/\"><img loading=\"lazy\" class=\"alignnone\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/Tutorial.png\" alt=\"\" width=\"206\" height=\"59\" \/><\/a>\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0<a href=\"https:\/\/osoyoo.info\/index.php\/2020\/10\/12\/arduino-basic-lesson-23-ultrasonic-sensor\/\"><img loading=\"lazy\" class=\"alignright\" src=\"https:\/\/osoyoo.com\/picture\/Arduino_Graphical_Programming_Kit\/Next.png\" alt=\"\" width=\"206\" height=\"59\" \/><\/a><\/p>\n<div align=\"center\">\n<div align=\"center\"><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0 Buy from US Buy from UK Buy from DE Buy from IT Buy from FR Buy from ES Buy from JP Content Introduction Preparations Hardware Software About the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":165,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"_links":{"self":[{"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/posts\/123"}],"collection":[{"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/comments?post=123"}],"version-history":[{"count":3,"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/posts\/123\/revisions"}],"predecessor-version":[{"id":138,"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/posts\/123\/revisions\/138"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/media\/165"}],"wp:attachment":[{"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/media?parent=123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/categories?post=123"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/osoyoo.info\/index.php\/wp-json\/wp\/v2\/tags?post=123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}