.. 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! .. _py_flowing_light: 6.2 Flowing Light ================================= Have you ever wanted to add some fun and interactive element to your living space? This project involves creating a running light using WS2812 LED strip and a obstacle avoidance module. The running light changes direction when an obstacle is detected, making it an exciting addition to your home or office decor. **Required Components** In this project, we need the following components. 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 * - ESP32 Starter Kit - 320+ - |link_esp32_starter_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_esp32_wroom_32e` - |link_esp32_wroom_32e_buy| * - :ref:`cpn_esp32_camera_extension` - |link_esp32_extension_board| * - :ref:`cpn_wires` - |link_wires_buy| * - :ref:`cpn_avoid` - |link_avoid_buy| * - :ref:`cpn_ws2812` - |link_ws2812_buy| **Schematic Diagram** .. image:: ../../img/circuit/circuit_6.2_flowing_led.png :align: center The WS2812 LED strip is composed of a series of individual LEDs that can be programmed to display different colors and patterns. In this project, the strip is set up to display a running light that moves in a particular direction and changes direction when an obstacle is detected by the obstacle avoidance module. **Wiring** .. image:: ../../img/wiring/6.2_flowing_light_bb.png :width: 800 **Code** .. note:: * Open the ``6.2_flowing_led.py`` file located in the ``esp32-starter-kit-main\micropython\codes`` path, or copy and paste the code into Thonny. Then, click "Run Current Script" or press F5 to execute it. * Make sure to select the "MicroPython (ESP32).COMxx" interpreter in the bottom right corner. .. code-block:: python from machine import Pin import neopixel import time import random # Set the number of pixels for the running light num_pixels = 8 # Set the data pin for the RGB LED strip data_pin = Pin(14, Pin.OUT) # Initialize the RGB LED strip object pixels = neopixel.NeoPixel(data_pin, num_pixels) # Initialize the avoid sensor avoid = Pin(25, Pin.IN) # Initialize the direction variable direction_forward = True # Initialize the reverse direction flag reverse_direction = False # Continuously loop the running light while True: # Read the input from the infrared sensor avoid_value = avoid.value() # Generate a random color for the current pixel color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) # If no obstacle is detected if avoid_value: for i in range(num_pixels): # Turn on the current pixel with the random color pixels[i] = color # Update the RGB LED strip display pixels.write() # Turn off the current pixel pixels[i] = (0, 0, 0) time.sleep_ms(100) # If detects an obstacle, change the direction of the LED strip else: for i in range(num_pixels-1, -1, -1): pixels[i] = color pixels.write() pixels[i] = (0, 0, 0) time.sleep_ms(100) LEDs on the RGB Strip light up one by one when the script runs. As soon as an object is placed in front of the obstacle avoidance module, the LEDs on the RGB Strip light up one by one in the opposite direction.