Labs / Bipes + MicroPython / 10 Comprehensive Case

10.6 Somatosensory Control of Small Light Array

Author: Li Zisheng · Source: FreakStudio open documentation · License: CC BY 4.0

Contents

Young creators, do you remember that when playing motion-sensing games, you only need to shake the controller to control the character's movement?

Today, we will use the MPU6050 six-axis gyroscope to create a "somatosensory-controlled small LED light array" ✨! As long as you tilt the development board in different directions while holding it, the corresponding LEDs will light up automatically, which is super cool just like "directing" the lights to dance with your body!

1. Case Introduction

1.1 Modules Used

1.2 Creative Gameplay

We set a "motion-sensing rule" for the light array: when you tilt the handheld development board, the LEDs will move in sync accordingly.

1.3 Core Learning Points (Essential for Beginners to Master)

  1. Learn to read the attitude data of the 6-axis gyroscope (obtain the tilt angles of the X/Y axes);
  2. Understand the tilt judgment logic for absolute value comparison (to determine which direction has a more pronounced tilt);
  3. Master the assignment and usage of state variables (using the led variable to record the current tilt direction);
  4. Implement coordinated control of multiple LEDs (turn on/off different combinations of LEDs according to the posture);
  5. Develop a programming mindset for somatosensory interaction (translating "physical movements" into "code logic").

2. Wiring Instructions

10.6 DIO0 DIO1 AIN0 UART0

This case requires connecting 2 modules. Just plug them directly into the standard interfaces of the development board, and there is no need to check the pins one by one.

2.1 Wiring of MPU6050 6-axis Gyroscope

We connect the MPU6050 six-axis gyroscope module to the I2C0 interface of the development board.

2.2 Piranha LED Array Wiring

We connect the 4 Piranha LED light modules according to the following correspondence:

📌 Wiring Tips

  1. Basic Wiring Requirements:
    • The GND of all LED modules must be connected to the common ground of the development board; otherwise, the LEDs will either "flash briefly then turn off" or fail to light up entirely.
    • The MPU6050 needs to warm up for 1-2 minutes before use; otherwise, the initial attitude data will be unstable.
  2. Important Wiring Knowledge (Essential for Primary and Secondary School Students):
    • ✅ Why can an LED (digital output module) be connected to the AIN0/UART0 interface? The AIN0 and UART0 interfaces on the development board are essentially "multi-function general-purpose pins", just like the compartments of a schoolbag — even though they are marked for "notebooks" and "pens", you can totally tuck an eraser in there temporarily. An LED only needs a simple "high level on / low level off" signal, so it can work properly when connected to any of these interfaces.
    • ❌ Why can't the MPU6050 (I2C module) be connected to a regular DIO port? The MPU6050 needs to transmit "clock-synchronized posture data" via the I2C protocol, which is similar to transferring files with a dedicated data cable. Regular DIO ports do not have clock synchronization functionality, so connecting the MPU6050 to one is like connecting a printer with a regular USB cable — the core required feature is missing, making data transmission completely impossible.

3. Program Description

The core of this case is "read the posture → judge the direction → assign the state → control the LED". We will break it down according to the sequence of building block assembly, so even beginners can understand it step by step:

MPU6050 200
  1. Hardware Initialization Block (Pre-step, Critical!)
    • MPU6050 Initialization: Configure the I2C0 interface (SDA = Pin6, SCL = Pin7) to activate the attitude detection function of the six-axis sensor;
    • LED initialization: Set the pins corresponding to the 4 LEDs (DIO0/DIO1/AIN0/UART0) to "output mode" to prepare for controlling their on/off status.
  2. Global state variable
    • Add an led variable with an initial value of 0 (indicating the "no tilt" state);
    • Function: Record the current tilt direction with numbers 1-4 (1 = forward, 2 = backward, 3 = right, 4 = left) to make the code logic clearer.
  3. Infinite Loop Block (repeat while true)
    • Function: This enables the system to continuously read posture data and update the status of the LEDs, so that the LED array responds in real time whenever the handheld development board is tilted.
    • Location: All function blocks must be placed inside this loop.
  4. Attitude Data Reading Block
    • Read gyro data: Use the Gyro-X Axis and Gyro-Y Axis blocks to obtain the tilt angle values of the X/Y axes;
    • Store variables: Store the X-axis data into the x variable and the Y-axis data into the y variable to facilitate subsequent judgment.
  5. Tilt direction judgment block (nested if-else logic)
    • Step 1: Determine forward/backward or left/right tilt Use abs (y) > abs (x) to judge: if the absolute value of the Y-axis is larger → it is forward/backward tilt; if the absolute value of the X-axis is larger → it is left/right tilt.
    • Step 2: Determine the specific direction
      • When tilting forward or backward: y > 0 → tilt forward (led= 1);y < 0 → tilt backward (led= 2);
      • When tilting left or right: x > 0 → tilt to the right (led= 3);x < 0 → tilt to the left (led= 4).
  6. LED Control Block (Multi-Conditional Branching)

Based on the value of the led variable, control the on/off state of the corresponding LED:

  1. Debounce Delay Block
    • Add a delay of 200 milliseconds to prevent frequent LED switching caused by slight hand jitter, making the status of the LED array more stable.

Program running effect:

C2180.MP4 132.0 MB

I2C MPU6050
3 7 9 26 1 4 200

Directly importable building blocks:

Motion-controlled light array.xml 36 KB

A Beginner's Guide to Avoiding Common Pitfalls

  1. MPU6050 data jittering / inaccurate tilt detection?
    • Check whether the SDA/SCL pins of the I2C0 interface are securely connected, and whether the module has been preheated for 1-2 minutes;
    • You can appropriately increase the "difference threshold" of the judgment condition (for example, change absolute (y) > absolute (x) to absolute (y) > absolute (x) + 5) to reduce false positives caused by slight jitter.
  2. LED not lighting up / lighting up in the wrong position?
    • Check whether the LED pins correspond to the configuration in the program (e. g., whether the top-left LED is connected to DIO0);
    • Make sure that the GND pins of all LEDs are connected to the common ground of the development board; insufficient power supply will cause the LEDs to dim or fail to light up.
  3. LED not switching sensitively when tilted?
    • Set the delay time from 200ms to 100ms to improve the response speed;
    • When holding the development board, keep it as steady as possible and avoid tilting it in multiple directions at the same time (for example, tilting it forward and to the left simultaneously).

✨ Interactive Mini Quiz

  1. Think about it: if you change the "absolute value comparison" in the judgment condition to a direct comparison y > x, what problems will arise? (Answer: When both the X and Y axes are negative, the tilt direction will be misjudged. For example, when tilting backward + leftward, y > x holds true but the actual tilt direction is backward; absolute value comparison avoids the interference of positive and negative signs, making it possible to judge the tilt amplitude more accurately)
  2. Try it out: Add a logic of "all LEDs turn off when placed horizontally" to the example. Can you add one at the end of the if-else structure else branch to implement it?