{"id":4151,"date":"2025-01-17T11:19:03","date_gmt":"2025-01-17T02:19:03","guid":{"rendered":"https:\/\/www.elehobby.fun\/?p=4151"},"modified":"2025-01-17T11:19:03","modified_gmt":"2025-01-17T02:19:03","slug":"raspberry-pi-pico-line-following-robot","status":"publish","type":"post","link":"https:\/\/www.elehobby.fun\/?p=4151","title":{"rendered":"Raspberry Pi Pico: Line Following Robot"},"content":{"rendered":"\n<h6 class=\"wp-block-heading\"><a href=\"https:\/\/www.tedizen.com\/category\/raspberry-pi\/\">Raspberry Pi<\/a>,&nbsp;<a href=\"https:\/\/www.tedizen.com\/category\/raspberry-pi\/raspberry-pi-pico\/\">Raspberry Pi Pico<\/a><\/h6>\n\n\n\n<h1 class=\"wp-block-heading\">Raspberry Pi Pico: Line Following Robot \u2013 MicroPython<\/h1>\n\n\n\n<p>Posted on&nbsp;<a href=\"https:\/\/www.tedizen.com\/raspberry-pi-pico-line-following-robot-micropython\/\"><time datetime=\"2022-10-10T23:07:57+05:30\">October 10, 2022<\/time><\/a>&nbsp;by&nbsp;<a href=\"https:\/\/www.tedizen.com\/author\/admin\/\">Sharath<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.tedizen.com\/raspberry-pi-pico-line-following-robot-micropython\/\"><img decoding=\"async\" src=\"https:\/\/www.tedizen.com\/wp-content\/uploads\/2022\/10\/pico-line-following-robot-circuit-code-1024x538.jpg.webp\" alt=\"\"\/><\/a><\/figure>\n\n\n\n<p>A Line Following robot is a popular beginner-friendly tutorial for all microcontrollers. It is a simple and fun way to learn to code. In this blog, we will see how to build a line follower robot using Raspberry Pi Pico Microcontroller and Python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Working Principle of IR Proximity Sensor:<\/h2>\n\n\n\n<p>Black is the color that absorbs light and all other colors reflect some light back based on their color (most reflecting color is pure white). Every color has a wavelength, our eyes can be visible only in a specific range of wavelengths only. One example of an invisible wavelength for our eyes is called Infrared Light. We can\u2019t see infrared light with our naked eyes (but can by our mobile phone with poor IR Filters).<\/p>\n\n\n\n<p>IR Proximity Sensor or Infrared Proximity Sensor is a kind of proximity sensor that has an IR Transmitter LED and an IR Receiver LED. The transmitter LED emits an Infrared Light and the receiver LED senses infrared light. So, there will be no reflection when there is no object (or) black object in front of an IR Proximity Sensor. However, if there is any other color, the infrared light gets reflected back by the surface to the IR Receiver LED.<\/p>\n\n\n\n<p>By the above Infrared Principle, we can make a robot that can sense the black line and keep it away from the black line on both sides, so that it will stay on the line.<\/p>\n\n\n\n<p><strong>Components Required:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/amzn.to\/3MiJMyN\" target=\"_blank\" rel=\"noreferrer noopener\">Raspberry Pi Pico<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/amzn.to\/3EsnpFe\" target=\"_blank\" rel=\"noreferrer noopener\">IR Proximity Sensor<\/a>\u00a0x 2<\/li>\n\n\n\n<li><a href=\"https:\/\/amzn.to\/3EvT60v\" target=\"_blank\" rel=\"noreferrer noopener\">Tiny Breadboard<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/amzn.to\/3SSVimQ\" target=\"_blank\" rel=\"noreferrer noopener\">Jumper Wires<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/amzn.to\/3Vei0Yh\" target=\"_blank\" rel=\"noreferrer noopener\">L298N Motor Driver<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/amzn.to\/3MzWOs1\" target=\"_blank\" rel=\"noreferrer noopener\">2WD Chassis Kit<\/a>\u00a0(Motor, Wheel, Chassis)<\/li>\n\n\n\n<li><a href=\"https:\/\/www.factoryforward.com\/product\/18650-rechargable-li-ion-battery-2200mah-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">18650 Battery<\/a>\u00a0x 3<\/li>\n\n\n\n<li><a href=\"https:\/\/amzn.to\/3CJxah2\" target=\"_blank\" rel=\"noreferrer noopener\">3 Cell 18650 Battery Holder<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.factoryforward.com\/product\/tomo-v6-2-intelligent-battery-charger-for-3-7v-and-1-2v-aa-aaa-18650-17650-16340-14500-10500-batteries\/\" target=\"_blank\" rel=\"noreferrer noopener\">Lithium Battery Charger<\/a>\u00a0(Optional)<\/li>\n<\/ul>\n\n\n\n<p><strong>Useful Resources:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.tedizen.com\/wp-content\/uploads\/2022\/11\/Pico-R3-A4-Pinout.pdf\" target=\"_blank\" rel=\"noreferrer noopener\">Raspberry Pi Pico Pinout<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.raspberrypi.com\/documentation\/microcontrollers\/raspberry-pi-pico.html\" target=\"_blank\" rel=\"noreferrer noopener\">Raspberry Pi Pico Documentation, Design Files, etc<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/projects.raspberrypi.org\/en\/projects\/getting-started-with-the-pico\" target=\"_blank\" rel=\"noreferrer noopener\">Getting Started with Raspberry Pi Pico<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Circuit Diagram for Line Following Robot:<\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.tedizen.com\/wp-content\/uploads\/2022\/10\/pico-linefollowing-robot-circuit_bb-tedizen-1024x880.png.webp\" alt=\"line following robot circuit\" class=\"wp-image-2536\"\/><\/figure>\n\n\n\n<p><strong>Note:&nbsp;<\/strong>If you are using 4 Wheel Drive, You can connect the motors in parallel on each side. As we are using an L298N driver (2Amps Max Current), it can handle 4 BO Motors (Yellow). When mounting on the chassis, the left two motors are from M1 and the Right two motors from M2. This way both wheels run in the same direction. You can check the video for a reference of rotation.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Line Follower Robot Code (Python \u2013 Raspberry Pi Pico):<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">from machine import Pin,PWM #importing required PIN and PWM from modules\nimport time #importing time module\nimport utime\n\n#Define and map Pico pins with L298N Motor Driver Pins as OUTPUTS\nIN1=Pin(9,Pin.OUT)\/\/IN1 Motor Driver\nIN2=Pin(8,Pin.OUT)\/\/IN2 Motor Driver\nIN3=Pin(7,Pin.OUT)\/\/IN3 Motor Driver\nIN4=Pin(6,Pin.OUT)\/\/IN4 Motor Driver\n\n# Defining enable pins and PWM object\nEN1=PWM(Pin(10))\/\/ Enable Pins used for PWM Speed Control\nEN2=PWM(Pin(5))\/\/Enable Pins used for PWM Speed Control\n\n#Set Pins for IR Sensors as INPUTS\nright_ir = Pin(3, Pin.IN)\/\/Right IR Sensor\nleft_ir = Pin(2, Pin.IN)\/\/Left IR Sensor\n\n#Set PWM Switching Speed in Hertz for EN Pins\nEN1.freq(1000)\nEN2.freq(1000)\n\n#PWM Duty Cycle\nEN1.duty_u16(65025)\/\/Maximum for Pico 65025\nEN2.duty_u16(65025)\/\/Maximum Pico 65025\n\n# Move Forward\ndef move_forward():\n    IN1.low()\n    IN2.high()\n    IN3.high()\n    IN4.low()\n    \n# Move Backward\ndef move_backward():\n    IN1.high()\n    IN2.low()\n    IN3.low()\n    IN4.high()\n    \n#Turn Right\ndef turn_right():\n    IN1.low()\n    IN2.high()\n    IN3.low()\n    IN4.high()\n    \n#Turn Left\ndef turn_left():\n    IN1.high()\n    IN2.low()\n    IN3.high()\n    IN4.low()\n   \n#Stop\ndef stop():\n    IN1.low()\n    IN2.low()\n    IN3.low()\n    IN4.low()\n    \nwhile True:\n    \n    right_val=right_ir.value() #Read Sensor 1 Value\n    left_val=left_ir.value() #Read Sensor 2 Value\n    \n    print(str(right_val)+\"-\"+str(left_val))\n    \n    #Set Conditions for Obstacle Avoidance\n    if right_val==0 and left_val==0:\n        move_forward()\n    elif right_val==1 and left_val==0:\n        turn_right()\n    elif right_val==0 and left_val==1:\n        turn_left()\n    else:\n        stop()<\/pre>\n\n\n\n<p>PythonCopy<\/p>\n\n\n\n<p><strong>Upload and AutoRun the Code on Pico:<\/strong><\/p>\n\n\n\n<p>You can save and run this file locally by any name. But if you want to auto-run your code on Raspberry Pi Pico, the name should be&nbsp;<strong>main.py<\/strong>&nbsp;only. You can save the file by clicking File&gt; Save As &gt; then select Raspberry Pi Pico on the popup (Pico should be connected). Then save the file as&nbsp;<strong>main.py<\/strong><\/p>\n\n\n\n<p>We have to draw a black line using black chart paper or black tape (avoid reflecting\/shiny tapes). We will be placing the sensors away from this line. One on the left and another on the right side of the line.<\/p>\n\n\n\n<p><strong>Note:<\/strong>&nbsp;Most digital sensors will be in HIGH State(1) at normal conditions and LOW State(0) when something is detected. This is because electrical noises in the surrounding causes false trigger. To avoid this, Pull-Up resistors are used in most of the sensors.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The function of the code and Robot:<\/h2>\n\n\n\n<p><strong>Forward Sensing:<\/strong>&nbsp;By default, the white surface on both sides, both&nbsp;<strong>sensors<\/strong>&nbsp;detected&nbsp;<strong>(LOW, LOW<\/strong>), and the motors run in the forward direction.<\/p>\n\n\n\n<p><strong>Right Sensing:<\/strong>&nbsp;When the&nbsp;<strong>Right Sensor<\/strong>&nbsp;is detected the black line (<strong>LOW<\/strong>,&nbsp;<strong>HIGH<\/strong>), the motor rotates in the Right direction.<\/p>\n\n\n\n<p><strong>Left Sensing:<\/strong>&nbsp;When&nbsp;<strong>Left Sensor<\/strong>&nbsp;is detected the black line (HIGH, LOW), the motor rotates in the Left direction.<\/p>\n\n\n\n<p><strong>Stop:&nbsp;<\/strong>When Both&nbsp;<strong>Sensors<\/strong>&nbsp;detected the black line&nbsp;<strong>(HIGH, HIGH)<\/strong>, the motors stopped.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Sensor Logic:<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Function<\/th><th>Left Sensor<\/th><th>Right Sensor<\/th><\/tr><\/thead><tbody><tr><td><strong>Forward<\/strong><\/td><td>LOW<\/td><td>LOW<\/td><\/tr><tr><td><strong>Right<\/strong><\/td><td>LOW<\/td><td>HIGH<\/td><\/tr><tr><td><strong>Left<\/strong><\/td><td>HIGH<\/td><td>LOW<\/td><\/tr><tr><td><strong>Stop<\/strong><\/td><td>HIGH<\/td><td>HIGH<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Motor Logic:<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Function<\/th><th>IN1<\/th><th>IN2<\/th><th>IN3<\/th><th>IN4<\/th><\/tr><\/thead><tbody><tr><td><strong>Forward<\/strong><\/td><td>LOW<\/td><td>HIGH<\/td><td>HIGH<\/td><td>LOW<\/td><\/tr><tr><td><strong>Right<\/strong><\/td><td>LOW<\/td><td>HIGH<\/td><td>LOW<\/td><td>HIGH<\/td><\/tr><tr><td><strong>Left<\/strong><\/td><td>HIGH<\/td><td>LOW<\/td><td>HIGH<\/td><td>LOW<\/td><\/tr><tr><td><strong>Stop<\/strong><\/td><td>LOW<\/td><td>LOW<\/td><td>LOW<\/td><td>LOW<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>By this logic, the robot follows the line until the line ends.<\/p>\n\n\n\n<p><strong>Tips:<\/strong><\/p>\n\n\n\n<p>If your motor runs in the opposite direction, there are multiple ways to correct it. You can change the pin numbers on the code, Interchange Terminals of the Motor. Below is how to change via interchanging terminals.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>You can interchange motor terminals on M1\u00a0<strong>(or)<\/strong>\u00a0M2 in the motor driver. Important!\u00a0<strong>You must interchange terminals on the same side only<\/strong>. Do not connect one terminal to M1 and another terminal to M2. Otherwise, it will create a short circuit and can damage the L298N Driver.<\/li>\n\n\n\n<li>If your sensor detects the opposite direction, the simplest way is to interchange the mounting of sensors without any changes in wiring or code.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-embed is-type-rich\"><div class=\"wp-block-embed__wrapper\">\nhttps:\/\/youtube.com\/watch?v=Nbk8ejD2y-s%3Ffeature%3Doembed\n<\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Recommended Batteries and Chargers for robots:<\/strong><\/h2>\n\n\n\n<p>Motors consume more power, so it is recommended to use Lithium Ion Batteries for the best performance, recharging, and long shelf-life. The BO Motors we use is rated at 3-12Volt DC, 40-180mA. For our robot, 7-12V is recommended for better performance. You can use a cheap 9v battery, but it will not last long and is not rechargeable. So go with rechargeable lithium-ion batteries.<\/p>\n\n\n\n<p>Lithium batteries connected in series add up the voltage, for example, 1 lithium cell\u2019s nominal voltage is 3.7 Volt, and for the 7.4 Volt requirement, you need a 2 Cell 18650 Holder. For our robot, this is enough. So&nbsp;<strong>you can use a 2Cell or 3 Cell Holder<\/strong>. Below are the recommended battery and its accessories,<\/p>\n<div class=\"veu_socialSet veu_socialSet-position-after veu_contentAddSection\"><script>window.twttr=(function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],t=window.twttr||{};if(d.getElementById(id))return t;js=d.createElement(s);js.id=id;js.src=\"https:\/\/platform.twitter.com\/widgets.js\";fjs.parentNode.insertBefore(js,fjs);t._e=[];t.ready=function(f){t._e.push(f);};return t;}(document,\"script\",\"twitter-wjs\"));<\/script><ul><li class=\"sb_facebook sb_icon\"><a class=\"sb_icon_inner\" href=\"\/\/www.facebook.com\/sharer.php?src=bm&u=https%3A%2F%2Fwww.elehobby.fun%2F%3Fp%3D4151&amp;t=%E5%AE%9F%E8%B7%B5%E3%81%A7%E3%81%8D%E3%81%9F%E9%9B%BB%E5%AD%90%E5%B7%A5%E4%BD%9C%E3%81%A8%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3%83%9F%E3%83%B3%E3%82%B0%E9%96%8B%E7%99%BA\" target=\"_blank\" onclick=\"window.open(this.href,'FBwindow','width=650,height=450,menubar=no,toolbar=no,scrollbars=yes');return false;\"><span class=\"vk_icon_w_r_sns_fb icon_sns\"><\/span><span class=\"sns_txt\">Facebook<\/span><span class=\"veu_count_sns_fb\"><\/span><\/a><\/li><li class=\"sb_twitter sb_icon\"><a class=\"sb_icon_inner\" href=\"\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fwww.elehobby.fun%2F%3Fp%3D4151&amp;text=%E5%AE%9F%E8%B7%B5%E3%81%A7%E3%81%8D%E3%81%9F%E9%9B%BB%E5%AD%90%E5%B7%A5%E4%BD%9C%E3%81%A8%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3%83%9F%E3%83%B3%E3%82%B0%E9%96%8B%E7%99%BA\" target=\"_blank\" ><span class=\"vk_icon_w_r_sns_twitter icon_sns\"><\/span><span class=\"sns_txt\">twitter<\/span><\/a><\/li><li class=\"sb_hatena sb_icon\"><a class=\"sb_icon_inner\" href=\"\/\/b.hatena.ne.jp\/add?mode=confirm&url=https%3A%2F%2Fwww.elehobby.fun%2F%3Fp%3D4151&amp;title=%E5%AE%9F%E8%B7%B5%E3%81%A7%E3%81%8D%E3%81%9F%E9%9B%BB%E5%AD%90%E5%B7%A5%E4%BD%9C%E3%81%A8%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3%83%9F%E3%83%B3%E3%82%B0%E9%96%8B%E7%99%BA\" target=\"_blank\"  onclick=\"window.open(this.href,'Hatenawindow','width=650,height=450,menubar=no,toolbar=no,scrollbars=yes');return false;\"><span class=\"vk_icon_w_r_sns_hatena icon_sns\"><\/span><span class=\"sns_txt\">Hatena<\/span><span class=\"veu_count_sns_hb\"><\/span><\/a><\/li><li class=\"sb_pocket sb_icon\"><a class=\"sb_icon_inner\"  href=\"\/\/getpocket.com\/edit?url=https%3A%2F%2Fwww.elehobby.fun%2F%3Fp%3D4151&title=%E5%AE%9F%E8%B7%B5%E3%81%A7%E3%81%8D%E3%81%9F%E9%9B%BB%E5%AD%90%E5%B7%A5%E4%BD%9C%E3%81%A8%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3%83%9F%E3%83%B3%E3%82%B0%E9%96%8B%E7%99%BA\" target=\"_blank\"  onclick=\"window.open(this.href,'Pokcetwindow','width=650,height=450,menubar=no,toolbar=no,scrollbars=yes');return false;\"><span class=\"vk_icon_w_r_sns_pocket icon_sns\"><\/span><span class=\"sns_txt\">Pocket<\/span><span class=\"veu_count_sns_pocket\"><\/span><\/a><\/li><li class=\"sb_copy sb_icon\"><button class=\"copy-button sb_icon_inner\"data-clipboard-text=\"\u5b9f\u8df5\u3067\u304d\u305f\u96fb\u5b50\u5de5\u4f5c\u3068\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u958b\u767a https:\/\/www.elehobby.fun\/?p=4151\"><span class=\"vk_icon_w_r_sns_copy icon_sns\"><i class=\"fas fa-copy\"><\/i><\/span><span class=\"sns_txt\">Copy<\/span><\/button><\/li><\/ul><\/div><!-- [ \/.socialSet ] -->","protected":false},"excerpt":{"rendered":"<p>Raspberry Pi,&nbsp;Raspberry Pi Pico Raspberry Pi Pico: Line Following Robot \u2013 MicroPython Posted on&nbsp;Octo [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"vkexunit_cta_each_option":"","footnotes":""},"categories":[16],"tags":[],"class_list":["post-4151","post","type-post","status-publish","format-standard","hentry","category-electronicswork"],"_links":{"self":[{"href":"https:\/\/www.elehobby.fun\/index.php?rest_route=\/wp\/v2\/posts\/4151","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.elehobby.fun\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.elehobby.fun\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.elehobby.fun\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.elehobby.fun\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4151"}],"version-history":[{"count":2,"href":"https:\/\/www.elehobby.fun\/index.php?rest_route=\/wp\/v2\/posts\/4151\/revisions"}],"predecessor-version":[{"id":4154,"href":"https:\/\/www.elehobby.fun\/index.php?rest_route=\/wp\/v2\/posts\/4151\/revisions\/4154"}],"wp:attachment":[{"href":"https:\/\/www.elehobby.fun\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.elehobby.fun\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4151"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.elehobby.fun\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}