.. note:: Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts. **Why Join?** - **Expert Support**: Solve post-sale issues and technical challenges with help from our community and team. - **Learn & Share**: Exchange tips and tutorials to enhance your skills. - **Exclusive Previews**: Get early access to new product announcements and sneak peeks. - **Special Discounts**: Enjoy exclusive discounts on our newest products. - **Festive Promotions and Giveaways**: Take part in giveaways and holiday promotions. 👉 Ready to explore and create with us? Click [|link_sf_facebook|] and join today! .. _2.2.5_py: 2.2.5 IR Obstacle Avoidance Sensor =================================== Introduction ----------------- In this project, we will learn IR obstacle avoidance module, which is a sensor module that can be used to detect obstacles at short distances, with small interference, easy to assemble, easy to use, etc. It can be widely used in robot obstacle avoidance, obstacle avoidance trolley, assembly line counting, etc. .. image:: ../img/2.2.5IR_Obstacle.png :width: 300 :align: center Required Components ------------------------------ In this project, we need the following components. .. image:: ../img/2.2.5component.png :width: 700 :align: center It's definitely convenient to buy a whole kit, here's the link: .. list-table:: :widths: 20 20 20 :header-rows: 1 * - Name - ITEMS IN THIS KIT - LINK * - Raphael Kit - 337 - |link_Raphael_kit| You can also buy them separately from the links below. .. list-table:: :widths: 30 20 :header-rows: 1 * - COMPONENT INTRODUCTION - PURCHASE LINK * - :ref:`cpn_gpio_board` - |link_gpio_board_buy| * - :ref:`cpn_breadboard` - |link_breadboard_buy| * - :ref:`cpn_wires` - |link_wires_buy| * - :ref:`cpn_avoid_module` - |link_obstacle_avoidance_buy| Schematic Diagram ----------------------- .. image:: ../img/IR_schematic.png :width: 500 :align: center Experimental Procedures ------------------------- **Step 1:** Build the circuit .. image:: ../img/2.2.5fritzing.png :width: 700 :align: center **Step 2:** Change directory. .. raw:: html .. code-block:: cd ~/raphael-kit/python **Step 3:** Run. .. raw:: html .. code-block:: sudo python3 2.2.5_IrObstacle.py After the code runs, when you put your hand in front of the module's probe, the output indicator on the module lights up and the "Detected Barrier!" will be repeatedly printed on the screen until the your hand is removed. **Code** .. note:: You can **Modify/Reset/Copy/Run/Stop** the code below. But before that, you need to go to source code path like ``raphael-kit/python``. After modifying the code, you can run it directly to see the effect. .. raw:: html .. code-block:: python #!/usr/bin/env python3 import RPi.GPIO as GPIO import time ObstaclePin = 17 def setup(): GPIO.setmode(GPIO.BCM) # Numbers GPIOs by physical location GPIO.setup(ObstaclePin, GPIO.IN, pull_up_down=GPIO.PUD_UP) def loop(): while True: if (0 == GPIO.input(ObstaclePin)): print ("Detected Barrier!") time.sleep(1) def destroy(): GPIO.cleanup() # Release resource if __name__ == '__main__': # Program start from here setup() try: loop() except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed. destroy() **Code Explanation** .. code-block:: python def setup(): GPIO.setmode(GPIO.BCM) # Numbers GPIOs by physical location GPIO.setup(ObstaclePin, GPIO.IN, pull_up_down=GPIO.PUD_UP) Set the GPIO mode to BCM Numbering. Set ``ObstaclePin`` to input mode and initial it to High level (3.3v). .. code-block:: python def loop(): while True: if (0 == GPIO.input(ObstaclePin)): print ("Detected Barrier!") When ``ObstaclePin`` is low level, print "Detected Barrier!". It means that an obstacle is detected. Phenomenon Picture ----------------------- .. image:: ../img/2.2.5IR.JPG :width: 500 :align: center