{"id":2138,"date":"2016-01-08T15:37:42","date_gmt":"2016-01-08T14:37:42","guid":{"rendered":"https:\/\/playembedded.org\/?p=2138"},"modified":"2018-12-28T12:02:48","modified_gmt":"2018-12-28T11:02:48","slug":"detecting-obstacle-with-ir-sensor-and-arduino","status":"publish","type":"post","link":"https:\/\/playembedded.org\/blog\/detecting-obstacle-with-ir-sensor-and-arduino\/","title":{"rendered":"Detecting obstacle with IR Sensor and Arduino"},"content":{"rendered":"<head><style>\r\n.step-further-content {\r\n    width:100%; \r\n    padding:10px 20px; \r\n    margin:10px 0; \r\n    color:#1c7f8c; \r\n    background-color:#cef0f4; \r\n    border: 1px solid #38c5d8; \r\n    border-radius:4px;\r\n}\r\n  \r\n.step-further-content a {\r\n    font-weight:bold; \r\n    color: #a83800;\r\n}\r\n  \r\n.step-further-content b {\r\n    font-weight:bold; \r\n} \r\n<\/style>\r\n<\/head><div class=\"step-further-content\">Are you interested to <b>become an expert of embedded systems<\/b>? You definitely <b>should know<\/b> how to deal with <b>ARM Cortex<\/b> architecture and <b>RTOSes<\/b>. We have <b>free courses<\/b> to learn how to deal with STM32 and ChibiOS which would guide you <b>step by step<\/b>. <a href=\"https:\/\/playembedded.org\/how-to-start\/\" title=\"How to start with STM32 and ChibiOS\">Ready to do a step further?<\/a><\/div>\n<h3 id=\"1_Introduction\" class=\"level_1\">Introduction<\/h3>\n<p>An object can be detected with an infrared system consisting of an infrared transmitter and a receiver. More in detail an <b>IR transmitter<\/b>, also known as IR LED, sends an infrared signal with a certain frequency compatible with an <b>IR receiver<\/b> which has the task to detect it. There are different kind of IR sensors for different type of application. IR technology is used, for example, in proximity sensors to detect a near object, in contrast sensors to find a path or in counting sensors to count objects.<!--more--><\/p>\n<h3 id=\"2_Principle_of_operation\" class=\"level_1\">Principle of operation<\/h3>\n<figure id=\"attachment_2556\" aria-describedby=\"caption-attachment-2556\" style=\"width: 160px\" class=\"wp-caption alignright\"><a href=\"https:\/\/playembedded.org\/wp-content\/uploads\/2016\/04\/art_019_obstacle.png\" rel=\"attachment wp-att-2556\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-2556\" src=\"https:\/\/playembedded.org\/wp-content\/uploads\/2016\/04\/art_019_obstacle-300x178.png\" alt=\"IR sensor principle of operation\" width=\"160\" height=\"95\" srcset=\"https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/art_019_obstacle-300x178.png 300w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/art_019_obstacle-150x89.png 150w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/art_019_obstacle-1024x607.png 1024w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/art_019_obstacle-1200x711.png 1200w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/art_019_obstacle-24x14.png 24w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/art_019_obstacle-36x21.png 36w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/art_019_obstacle-48x28.png 48w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/art_019_obstacle.png 1350w\" sizes=\"auto, (max-width: 160px) 100vw, 160px\"><\/a><figcaption id=\"caption-attachment-2556\" class=\"wp-caption-text\">IR sensor principle of operation with\/without object.<\/figcaption><\/figure>\n<p>The IR transmitter sends an infrared signal that, in case of a reflecting surface (e.g. white color), bounces off in some directions including that of the IR receiver that captures the signal detecting the object.<\/p>\n<p>When the surface is absorbent (e.g. black color) the IR signal isn&rsquo;t reflected and the object cannot be detected by the sensor. This result would occur even if the object is absent.<\/p>\n<h4 id=\"3_IR_transmitter_and_IR_receiver\" class=\"level_2\">IR transmitter and IR receiver<\/h4>\n<p>The IR transmitter is a particular LED that emits radiation in the frequency range of infrared, invisible to the naked eye. An infrared LED just works as a simple LED with a voltage of 3V DC and a current consumption of about 20mA. The IR receiver, such as a photodiode or a phototransistor, is capable of detect infrared radiation emitted from the IR transmitter. Aesthetically it is similar to a LED but the external capsule can be wrapped by a dark color film.<\/p>\n<h3 id=\"4_IR_Sensor_FC_51\" class=\"level_1\">IR Sensor FC-51<\/h3>\n<figure id=\"attachment_2555\" aria-describedby=\"caption-attachment-2555\" style=\"width: 204px\" class=\"wp-caption alignleft\"><a href=\"https:\/\/playembedded.org\/wp-content\/uploads\/2016\/04\/art_019_IR_sensor.png\" rel=\"attachment wp-att-2555\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-2555\" src=\"https:\/\/playembedded.org\/wp-content\/uploads\/2016\/04\/art_019_IR_sensor-300x125.png\" alt=\"FC-51 Pinmap\" width=\"204\" height=\"85\" srcset=\"https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/art_019_IR_sensor-300x125.png 300w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/art_019_IR_sensor-150x63.png 150w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/art_019_IR_sensor-24x10.png 24w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/art_019_IR_sensor-36x15.png 36w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/art_019_IR_sensor-48x20.png 48w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/art_019_IR_sensor.png 648w\" sizes=\"auto, (max-width: 204px) 100vw, 204px\"><\/a><figcaption id=\"caption-attachment-2555\" class=\"wp-caption-text\">Pin map of the FC-51 sensor.<\/figcaption><\/figure>\n<p>The sensor used in our demo is model <b>FC-51<\/b>. It is a cheap sensor easily available on the internet for less than 2$ but unfortunately we didn&rsquo;t find the datasheet. In any case we will explain the operation of the related electronic circuit and subsequently implement some demo to test its functioning.<\/p>\n<p>&nbsp;<\/p>\n<h4 id=\"5_Pinout_and_schematic\" class=\"level_2\">Pinout and schematic<\/h4>\n<figure id=\"attachment_2600\" aria-describedby=\"caption-attachment-2600\" style=\"width: 300px\" class=\"wp-caption alignright\"><a href=\"https:\/\/playembedded.org\/wp-content\/uploads\/2016\/04\/art_019_circuit.jpg\" rel=\"attachment wp-att-2600\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-2600\" src=\"https:\/\/playembedded.org\/wp-content\/uploads\/2016\/04\/art_019_circuit-300x224.jpg\" alt=\"FC-51 schematic\" width=\"300\" height=\"224\" srcset=\"https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/art_019_circuit-300x224.jpg 300w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/art_019_circuit-150x112.jpg 150w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/art_019_circuit-24x18.jpg 24w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/art_019_circuit-36x27.jpg 36w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/art_019_circuit-48x36.jpg 48w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/art_019_circuit.jpg 750w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\"><\/a><figcaption id=\"caption-attachment-2600\" class=\"wp-caption-text\">This is the schematic of the IR sensor FC-51.<\/figcaption><\/figure>\n<p>The package has three connection pins:<\/p>\n<ol>\n<li><b>Vcc<\/b> to the power supply 3-5V DC;<\/li>\n<li><b>Gnd<\/b> to the ground reference;<\/li>\n<li><b>Out<\/b> for the digital output signal of the sensor.<\/li>\n<\/ol>\n<p>This sensor detects objects at a distance in range between 2~30cm. With the potentiometer you can calibrate the sensitivity according to the application and environmental conditions (e.g. brightness).&nbsp;The IC <b>LM393<\/b> is an open-collector voltage comparator which provides an output if there is a pull-up R between the output of the IC (DO) and the power supply Vcc (R=10K&Omega;). The output DO is:<\/p>\n<ul>\n<li>high if the object is not detected;<\/li>\n<li>low if the object is detected.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h3 id=\"6_Proposed_Demos_explained\" class=\"level_1\">Proposed Demos explained<\/h3>\n<h4 id=\"7_Test_IR_sensor_FC_51_with_serial_terminal_Demo_01_\" class=\"level_2\">Test IR sensor FC-51 with serial terminal (Demo 01)<\/h4>\n<figure id=\"attachment_2737\" aria-describedby=\"caption-attachment-2737\" style=\"width: 188px\" class=\"wp-caption alignright\"><a href=\"https:\/\/playembedded.org\/wp-content\/uploads\/2016\/04\/art_019_demo01.jpg\" rel=\"attachment wp-att-2737\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-2737\" src=\"https:\/\/playembedded.org\/wp-content\/uploads\/2016\/04\/art_019_demo01-288x300.jpg\" alt=\"Demo 1 schematic\" width=\"188\" height=\"196\" srcset=\"https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/art_019_demo01-288x300.jpg 288w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/art_019_demo01-144x150.jpg 144w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/art_019_demo01-300x313.jpg 300w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/art_019_demo01-32x32.jpg 32w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/art_019_demo01-24x24.jpg 24w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/art_019_demo01-46x48.jpg 46w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/art_019_demo01.jpg 768w\" sizes=\"auto, (max-width: 188px) 100vw, 188px\"><\/a><figcaption id=\"caption-attachment-2737\" class=\"wp-caption-text\">Demo 1 schematic.<\/figcaption><\/figure>\n<p>In the first demo, through the connection between the Arduino serial port and the PC, we will read about the detection of the object.<\/p>\n<p>Lets take a look to steps required by this demo:<\/p>\n<ol>\n<li>We connect the OUT pin of the sensor to digital pin 2 of Arduino called <b>IR<\/b>.<\/li>\n<li>The <b>setup()<\/b> function is performed only once before the main loop. We insert here the initialization code which enables serial port Arduino and sets the digital pin 2 as input.<\/li>\n<li><b>loop()<\/b> is the main function and is cyclically repeated until you turn off the Arduino board. We convert in C language the operation of the electronic circuit analyzed before. We save in the variable <b>detection<\/b> the value taken from the pin IR with the specific function <b>digitalRead<\/b>, if the value is low there is an object otherwise there isn&rsquo;t.<\/li>\n<\/ol>\n<p>Lets take a look to our code:<\/p>\n<pre>#define IR 2   \r\nint detection = HIGH;    \/\/ no obstacle\r\nvoid setup() {\r\n  Serial.begin(9600);   \r\n  pinMode(IR, INPUT); \r\n}\r\nvoid loop() {  \r\n  detection = digitalRead(IR);\r\n  if(detection == LOW){\r\n    Serial.print(\"There is an obstacle!\\n\");\r\n  }\r\n  else{ \r\n    Serial.print(\"No obstacle!\\n\");\r\n  }\r\n  delay(500);    \/\/ in ms\r\n}<\/pre>\n<h4 id=\"8_IR_sensor_FC_51_and_LED_Demo_02_\" class=\"level_2\">IR sensor FC-51 and LED (Demo 02)<\/h4>\n<figure id=\"attachment_3582\" aria-describedby=\"caption-attachment-3582\" style=\"width: 191px\" class=\"wp-caption alignright\"><a href=\"https:\/\/playembedded.org\/wp-content\/uploads\/2016\/05\/Demo-2-schematic.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-3582\" src=\"https:\/\/playembedded.org\/wp-content\/uploads\/2016\/05\/Demo-2-schematic-252x300.jpg\" alt=\"Demo 2 schematic\" width=\"191\" height=\"228\" srcset=\"https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/05\/Demo-2-schematic-252x300.jpg 252w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/05\/Demo-2-schematic-126x150.jpg 126w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/05\/Demo-2-schematic-861x1024.jpg 861w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/05\/Demo-2-schematic-300x357.jpg 300w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/05\/Demo-2-schematic-20x24.jpg 20w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/05\/Demo-2-schematic-30x36.jpg 30w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/05\/Demo-2-schematic-40x48.jpg 40w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/05\/Demo-2-schematic.jpg 1032w\" sizes=\"auto, (max-width: 191px) 100vw, 191px\"><\/a><figcaption id=\"caption-attachment-3582\" class=\"wp-caption-text\">Demo 2 schematic<\/figcaption><\/figure>\n<p>In this demo we associate an input to each operation state of IR sensor. The required components are:<\/p>\n<ul>\n<li><b>IR sensor FC-51<\/b>;<\/li>\n<li><b>3 x Green LEDs<\/b>;<\/li>\n<li><b>3 x R=150&Omega;<\/b>;<\/li>\n<li><b>3 x White LEDs<\/b>;<\/li>\n<li><b>3 x R=100&Omega;<\/b>;<\/li>\n<li><b>3 x Red LEDs<\/b>;<\/li>\n<li><b>3 x R=160&Omega;<\/b>.<\/li>\n<\/ul>\n<p>Remember that the I\/O pins can absorb\/disburse up to 40mA max, total maximum 200mA (see <b>ATmega328P<\/b> datasheet at page 313).<\/p>\n<p>Lets take a look to steps required by this demo:<\/p>\n<ol>\n<li>We connect the OUT pin of the sensor to digital pin 2 of the Arduino. We define the digital pins of LEDs as an array of pins, from 3 to 11 called <b>LedPIN<\/b>.<\/li>\n<li>The <b>setup()<\/b> function is executed only once before the main loop. In addition to the initialization already seen, we call the 9 LED as output using a for loop.<\/li>\n<li>The <b>loop()<\/b> function is the main function and is cyclically repeated until you turn off the Arduino board. We save in the variable <b>detection<\/b> the value taken from pin <b>IR<\/b> with the specific function <b>digitalRead()<\/b>. This value can be low, if there is an object, or high if there is no object. We do this loop every millisecond.<\/li>\n<\/ol>\n<p>Lets take a look to our code:<\/p>\n<pre>#define IR 2    \/\/ digital pin input for ir sensor\r\nint detection = HIGH;    \/\/ no obstacle\r\nint i = 0;\r\n\/\/ array digital pin for: green led(3,4,5) - white led (6,7,8)- red led (9,10,11)\r\nint LedPIN[] = {3, 4, 5, 6, 7, 8, 9, 10, 11};   \r\n \r\nvoid setup() {\r\n  pinMode(IR, INPUT);\r\n  for(i = 0; i &lt; 9; i++){\r\n    pinMode(LedPIN[i], OUTPUT);   \r\n  } \r\n}\r\nvoid loop() {\r\n  detection = digitalRead(IR);\r\n  if(detection == LOW){ \r\n    BlinkLED();\r\n  }          \r\n  else{                 \r\n    LedOFF();\r\n  }      \r\n  delay(1);\r\n}<\/pre>\n<h4 id=\"9_Download_for_Arduino_IDE\" class=\"level_2\">Download for Arduino IDE<\/h4>\n<p>These demos have been tested under Arduino IDE 1.6.7:<\/p>\n<p><a href=\"https:\/\/playembedded.org\/wp-content\/uploads\/2016\/04\/DEMO01_IRsensor.rar\" rel=\"\">DEMO01_IRsensor<\/a> <a href=\"https:\/\/playembedded.org\/wp-content\/uploads\/2016\/04\/DEMO02_IRsensorLED.rar\" rel=\"\">DEMO02_IRsensorLED<\/a><\/p>\n\n","protected":false},"excerpt":{"rendered":"<p>Introduction An object can be detected with an infrared system consisting of an infrared transmitter and a receiver. More in detail an IR transmitter, also known as IR LED, sends an infrared signal with a certain frequency compatible with an IR receiver which has the task to detect it. There are different kind of IR [&hellip;]<\/p>\n","protected":false},"author":8,"featured_media":2391,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1018],"tags":[],"coauthors":[252],"class_list":["post-2138","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-arduino"],"views":94939,"jetpack_featured_media_url":"https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/Detecting-obstacle-with-IR-Sensor-and-Arduino.jpg","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/playembedded.org\/blog\/wp-json\/wp\/v2\/posts\/2138","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/playembedded.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/playembedded.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/playembedded.org\/blog\/wp-json\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"https:\/\/playembedded.org\/blog\/wp-json\/wp\/v2\/comments?post=2138"}],"version-history":[{"count":0,"href":"https:\/\/playembedded.org\/blog\/wp-json\/wp\/v2\/posts\/2138\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/playembedded.org\/blog\/wp-json\/wp\/v2\/media\/2391"}],"wp:attachment":[{"href":"https:\/\/playembedded.org\/blog\/wp-json\/wp\/v2\/media?parent=2138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/playembedded.org\/blog\/wp-json\/wp\/v2\/categories?post=2138"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/playembedded.org\/blog\/wp-json\/wp\/v2\/tags?post=2138"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/playembedded.org\/blog\/wp-json\/wp\/v2\/coauthors?post=2138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}