MPU6050 gyro sensor and WS2812B Led Strip Turn Indicator

Hi Sir,since few days I am trying to code about turn indicator use mpu6050 gyro and ws2812b led strip.

After upload this code,can't working.also can't understand the problem.
Please sir,help me.

Diagram is here.

#include <Wire.h>

#include <Adafruit_MPU6050.h>

#include <Adafruit_NeoPixel.h>

#define PIN1 6 
#define PIN2 7 
#define NUMPIXELS 8

Adafruit_MPU6050 mpu;


Adafruit_NeoPixel strip1 = Adafruit_NeoPixel(NUMPIXELS, PIN1, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel strip2 = Adafruit_NeoPixel(NUMPIXELS, PIN2, NEO_GRB + NEO_KHZ800);

void setup() {
				
  Serial.begin(115200);
  strip1.begin();
  strip1.show(); 
  
  strip2.begin();
  strip2.show(); 

  // Initialize MPU6050
  if (!mpu.begin()) {
    Serial.println("Failed to find MPU6050 chip");
    while (1) {
      delay(10);
    }
 }
  mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
  mpu.setGyroRange(MPU6050_RANGE_500_DEG);
  mpu.setFilterBandwidth(MPU6050_BAND_5_HZ);

  
			}
	
void loop() {
  sensors_event_t ax, gx, temp;
  mpu.getEvent(&ax, &gx, &temp);
				

				
  // Clear all LEDs on both strips
  strip1.clear();
  strip2.clear();

  if (ax.acceleration.x < -20) {
    // Tilted left
    for (int i = 0; i < 4; i++) {
      strip1.setPixelColor(i, strip1.Color(255, 0, 0)); 
												
      //strip2.setPixelColor(i, strip2.Color(255, 0, 0)); 
    }
  } else if (ax.acceleration.x > 120) {
    // Tilted right
    for (int i = 4; i < 4; i++) {
     // strip1.setPixelColor(i, strip1.Color(0, 0, 255)); 
      strip2.setPixelColor(i, strip2.Color(0, 0, 255)); 
    }
  }

  strip1.show(); 
  strip2.show(); 
  delay(100);
}

I moved your topic to an appropriate forum category @nisithmanna.

In the future, when creating a topic please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

Tell us what you expected to happen and what actually happens.

You might need this...

#include <Adafruit_Sensor.h>

... to use...

sensors_event_t 

Sir,when gyro sensor tilted right side > right led blink and tilted left side < left led blink.

I want this effect with this code.
Please help me.

I see the previous post beat me to it, use the library examples to start.

I thought I did... maybe not.

I made a typo. Retracting this...

Also, the "event" line throws an error... comment it out.

  // mpu.getEvent(&ax, &gx, temp);

Do you want acceleration or rotation? (this value seems more like a degree rotation than a "g" acceleration)

  if (ax.acceleration.x < -20) {

To measure tilt, the accelerometer provides the appropriate data.

The MPU6050 reports -32767 .. 32767, which integer must be used along with the accelerometer scale factor to get meaningful numbers in, say meters / seconds^2.

So yeah, those if statement numbers don't look sensible.

a7

Hii sir, I want to acceleration, only move y,pitch angel.

Run the example in Post #5 to see the values your MPU6050 reports. Post those values here.

[edit] Here is a basic MPU6050 sketch that you can use to produce data...

#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>

Adafruit_MPU6050 mpu;

void setup(void) {
  Serial.begin(115200);
  mpu.begin();
  mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
  mpu.setGyroRange(MPU6050_RANGE_500_DEG);
  mpu.setFilterBandwidth(MPU6050_BAND_5_HZ);
}

void loop() {
  sensors_event_t a, g, temp;
  mpu.getEvent(&a, &g, &temp);
  Serial.print("Acceleration X: ");
  Serial.print(a.acceleration.x);
  Serial.print(", Y: ");
  Serial.print(a.acceleration.y);
  Serial.print(", Z: ");
  Serial.print(a.acceleration.z);
  Serial.println(" m/s^2");
  delay(250);
}

Your original sketch... with comments and corrections...

#include <Wire.h> // NOT NECESSARY 
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h> // MISSING
#include <Adafruit_NeoPixel.h>

#define PIN1 6
#define PIN2 7
#define NUMPIXELS 8

Adafruit_MPU6050 mpu;
Adafruit_NeoPixel strip1 = Adafruit_NeoPixel(NUMPIXELS, PIN1, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel strip2 = Adafruit_NeoPixel(NUMPIXELS, PIN2, NEO_GRB + NEO_KHZ800);

void setup() {
  Serial.begin(115200);
  strip1.begin();
  strip1.show();
  strip2.begin();
  strip2.show();
  // Initialize MPU6050
  if (!mpu.begin()) {
    Serial.println("Failed to find MPU6050 chip");
    while (1) {
      delay(10);
    }
  }
  mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
  mpu.setGyroRange(MPU6050_RANGE_500_DEG);
  mpu.setFilterBandwidth(MPU6050_BAND_5_HZ);
}

void loop() {
  sensors_event_t ax, gx, temp;
  mpu.getEvent(&ax, &gx, &temp);
  // Clear all LEDs on both strips
  // strip1.clear(); // REMOVE THIS... IT CLEARS THE WS2812
  // strip2.clear(); // REMOVE THIS... IT CLEARS THE WS2812
  // if (ax.acceleration.x < -20) { // ACCELERATION WILL NEVER BE -20G
  if (ax.acceleration.x < -1) { // USE -1G IN WOKWI SIM
    // Tilted left
    for (int i = 0; i < 4; i++) { // COUNT 0 TO 3 WS2812... WHY? YOU HAVE 8 WS2812 ON EACH PIN
      strip1.setPixelColor(i, strip1.Color(255, 0, 0));
      //strip2.setPixelColor(i, strip2.Color(255, 0, 0)); // REMOVE THIS LINE
      strip1.show(); // YOU NEED TO SEND THE BUFFER TO THE WS2812
    } // END OF "FOR"
  } else { // IF ACCELERATION IS NOT < -1...
    strip1.clear(); // CLEAR THE WS2812
    strip1.show(); // SHOW THE BUFFER
  }
  // } else if (ax.acceleration.x > 120) { // ACCELERATION WILL NEVER BE 120G
  // } else if (ax.acceleration.x > 1) { // CAN NOT USE "ELSE" FOR < -1 BECAUSE "0" EXISTS

  if (ax.acceleration.x > 1) { // USE +1G IN WOKWI SIM
    // Tilted right
    // for (int i = 4; i < 4; i++) { // COUNTING FROM 4 TO 4 WILL NEVER COUNT
    for (int i = 0; i < 4; i++) { // COUNT 0 TO 3 WS2812... WHY? YOU HAVE 8 WS2812 ON EACH PIN
      // strip1.setPixelColor(i, strip1.Color(0, 0, 255)); // REMOVE THIS LINE
      strip2.setPixelColor(i, strip2.Color(0, 0, 255)); // BLUE?
    }
    strip2.show(); // SHOW WS2812 BLUE
  } else {
    strip2.clear(); // IF ACCELERATION < 1, CLEAR WS2812 
    strip2.show(); // SHOW CLEAR BUFFER
  }
  delay(100); // SLOW THE READINGS
}
diagram.json for the wokwi nerds
{
  "version": 1,
  "author": "foreignpigdog x",
  "editor": "wokwi",
  "parts": [
    { "type": "wokwi-arduino-nano", "id": "nano", "top": -4.8, "left": -0.5, "attrs": {} },
    { "type": "wokwi-mpu6050", "id": "imu1", "top": 90.22, "left": 40.72, "attrs": {} },
    {
      "type": "wokwi-neopixel-matrix",
      "id": "ring1",
      "top": 58.9,
      "left": 113.88,
      "rotate": 90,
      "attrs": { "pixleate": "1", "rows": "1", "cols": "8" }
    },
    {
      "type": "wokwi-neopixel-matrix",
      "id": "ring2",
      "top": 64.2,
      "left": -112.18,
      "rotate": 270,
      "attrs": { "pixleate": "1", "rows": "1", "cols": "8" }
    }
  ],
  "connections": [
    [ "imu1:SDA", "nano:A4", "green", [ "v0" ] ],
    [ "imu1:SCL", "nano:A5", "green", [ "v0" ] ],
    [ "imu1:GND", "nano:GND.1", "black", [ "v-28.8", "h38.48" ] ],
    [ "imu1:VCC", "nano:5V", "red", [ "v-19.2", "h9.68" ] ],
    [ "nano:5V", "ring1:VCC", "red", [ "v0" ] ],
    [ "nano:5V", "ring2:VCC", "red", [ "v0" ] ],
    [ "nano:GND.1", "ring1:GND", "black", [ "v0" ] ],
    [ "nano:GND.1", "ring2:GND", "black", [ "v0" ] ],
    [ "nano:7", "ring2:DIN", "green", [ "v0" ] ],
    [ "nano:6", "ring1:DIN", "green", [ "v0" ] ]
  ],
  "dependencies": {}
}

THX @xfpd for the simulation.

The constants in these lines are in error

  if (ax.acceleration.x < -1) { // USE -1G

// and

  if (ax.acceleration.x > 1) { // USE +1G

In the simulation, the accelerometer values can be adjusted from -2g to 2g, but the device itself reports in units of m/s^2. Just print ax.acceleration.x.

So those need to be -9.8 and 9.8.

a7

Yes, +/-9.8 (m/s²) in the Serial Monitor from ax.acceleration.x. I failed to note the +/-1 in the comment is from the SIM MPU slider. (comment corrected... I will also return the Serial.print() of the ax.acceleration.x to show the MPU library +/-9.8 output).

[edit] Reporting...
I find ax.acceleration.z output to yield the "9.8m/s²" feedback... but the lights can be triggered on either "+/-1" from ".x" or "+/- 9.8" from ".z"...

Like this... (using "z" to trigger the lights)....

#include <Wire.h> // NOT NECESSARY 
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h> // MISSING
#include <Adafruit_NeoPixel.h>

#define PIN1 6
#define PIN2 7
#define NUMPIXELS 8

Adafruit_MPU6050 mpu;
Adafruit_NeoPixel strip1 = Adafruit_NeoPixel(NUMPIXELS, PIN1, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel strip2 = Adafruit_NeoPixel(NUMPIXELS, PIN2, NEO_GRB + NEO_KHZ800);

void setup() {
  Serial.begin(115200);
  strip1.begin();
  strip1.show();
  strip2.begin();
  strip2.show();

  // Initialize MPU6050
  if (!mpu.begin()) {
    Serial.println("Failed to find MPU6050 chip");
    while (1) {
      delay(10);
    }
  }

  mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
  mpu.setGyroRange(MPU6050_RANGE_500_DEG);
  mpu.setFilterBandwidth(MPU6050_BAND_5_HZ);
}

void loop() {
  sensors_event_t ax, gx, temp;
  mpu.getEvent(&ax, &gx, &temp);

  // Clear all LEDs on both strips
  // strip1.clear(); // REMOVE THIS... IT CLEARS THE WS2812
  // strip2.clear(); // REMOVE THIS... IT CLEARS THE WS2812

  // if (ax.acceleration.x < -20) { // ACCELERATION WILL NEVER BE -20G
  if (ax.acceleration.z < -9.8) { // USE -9.8G in Z in WOKWI SIM
  // if (ax.acceleration.x < -1) { // USE -1G IN WOKWI SIM

    // Tilted left
    for (int i = 0; i < 4; i++) { // COUNT 0 TO 3 WS2812... WHY? YOU HAVE 8 WS2812 ON EACH PIN
      strip1.setPixelColor(i, strip1.Color(255, 0, 0));
      //strip2.setPixelColor(i, strip2.Color(255, 0, 0)); // REMOVE THIS LINE
      strip1.show(); // YOU NEED TO SEND THE BUFFER TO THE WS2812
    } // END OF "FOR"
  } else { // IF ACCELERATION IS NOT < -1...
    strip1.clear(); // CLEAR THE WS2812
    strip1.show(); // SHOW THE BUFFER
  }
  // } else if (ax.acceleration.x > 120) { // ACCELERATION WILL NEVER BE 120G
  // } else if (ax.acceleration.x > 1) { // CAN NOT USE "ELSE" FOR < -1 BECAUSE "0" EXISTS

  if (ax.acceleration.z > 9.8) { // USE +9.8G in Z for WOKWI SIM
  // if (ax.acceleration.x > 1) { // USE +1G IN WOKWI SIM

    // Tilted right
    // for (int i = 4; i < 4; i++) { // COUNTING FROM 4 TO 4 WILL NEVER COUNT
    for (int i = 0; i < 4; i++) { // COUNT 0 TO 3 WS2812... WHY? YOU HAVE 8 WS2812 ON EACH PIN
      // strip1.setPixelColor(i, strip1.Color(0, 0, 255)); // REMOVE THIS LINE
      strip2.setPixelColor(i, strip2.Color(0, 0, 255)); // BLUE?
    }

    strip2.show(); // SHOW WS2812 BLUE
  } else {
    strip2.clear(); // IF ACCELERATION < 1, CLEAR WS2812
    strip2.show(); // SHOW CLEAR BUFFER
  }

  Serial.print("z:");
  Serial.print(ax.acceleration.z);
  Serial.print(" x:");
  Serial.println(ax.acceleration.x);

  delay(100); // SLOW THE READINGS
}

Thanks all of you,sir. For give your valuable time and experience on this project.

Hi. @nisithmanna

Is this to make an automatic turn indicator on a vehicle?

If so, then turn indicators should be operated BEFORE the vehicle turns.
To give other motorists an indication of your INTENT to turn.

Tom.... :smiley: :+1: :coffee: :australia:

In addition to the coding issues being discussed, there may be some wiring flaw. Your photo resolution is far too poor to study that.

Hi, @nisithmanna

Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

Thanks.. Tom.... :smiley: :+1: :coffee: :australia:

You can put any number in there, the axes are symmetrical, x, y or z can be used for convenience.

In the resting state, z is typically pointing up and will read 1 g (sensor axis value 9.8), tilting that axis would show a maximum, which would be harder to use than an axis which will go positive and negative about a resting zero. But the mechanical arrangement may make one axis better than another.

The number you use just depends on how far you want to have to tilt the x axis before triggering the response.

a7

My grammar is lacking... srroy... I intended to separate "triggered on z" from "triggered on x"

are being triggered

I see no use of any other than the x-axis component.

Uusing an axis that is pointing up normally won't work, it would peak at 1 g (9.8) and read less than that when tilted any direction off perpendicular.

a7