MPU 6050 to PWM

Hey Everyone!

I am completely new to this so please go easy on me!

I am trying to run a simple program where I can convert the Y readings on my MPU 6050 into analogue so I can adjust the brightness of the LED's depending on the Angle of the Y axis.
I seem to be getting somewhere with it but the program seems to stop and not loop like it should.

This is the code I have so far

#include <MPU6050_tockn.h>
#include <Wire.h>
//Four LED's to show MPU6050 movement
// updated so all pins are on PWM pins
int LED1 = 3;
int LED2 = 5;
int LED3 = 6;
int LED4 = 9;
int led_value;  // - Y AXIS
int led_value2; // + Y AXIS
int outputY;
int outputY2;

MPU6050 mpu6050(Wire);

void setup() {
 pinMode( LED1, OUTPUT);//select pins as output
 pinMode( LED2, OUTPUT);//select pins as output
 pinMode( LED3, OUTPUT);//select pins as output
 pinMode( LED4, OUTPUT);//select pins as output




  Serial.begin(9600);
  Wire.begin();
  mpu6050.begin();
  mpu6050.calcGyroOffsets(true);
  //All LED's blink shows start getting data from MPU6050
  digitalWrite(LED1, HIGH);
  digitalWrite(LED3, HIGH);

  digitalWrite(LED2, HIGH);
  digitalWrite(LED4, HIGH);

  delay(1000);
}

void loop() {
  mpu6050.update();

  Serial.print("\tangleY : ");
  Serial.print(mpu6050.getAngleY());

  outputY =   (mpu6050.getAngleY());
 led_value = map (outputY,-1,-90, 0, 255);
     outputY2 =   (mpu6050.getAngleY());
 led_value2 = map (outputY2,0, 90, 0, 255);
 
 
  if ( ((mpu6050.getAngleY())  >=-1))
  {

 analogWrite(LED1, led_value);
 analogWrite (LED3, led_value);
 analogWrite (LED2, 0);
 analogWrite (LED4, 0);

  }
  else   ( ((mpu6050.getAngleY())  <=0));

{
  
 analogWrite (LED2, led_value2);
 analogWrite (LED4, led_value2);
 analogWrite (LED1, 0);
 analogWrite (LED3, 0);

return 0;
  
  }

}

As I have mentioned before, I am new to this so apologies in advance for the shoddy layout etc. I'm sure there will be a simple fix for this but I can't seem to find it after hours of trying to figure it out!

Please post a link to the MPU6050 library you are using.

Please also describe in detail what happens. We don't know what the phrase "the program seems to stop and not loop" means.

If you leave out the code associated with PWM for the moment, do the angles that are printed out make sense, when you orient the sensor in different ways?

Hey!

Thanks for getting back to me so quickly!

The link to the library I am using is here
[[GitHub - tockn/MPU6050_tockn: Arduino library for easy communication with MPU6050](https://)](GitHub - tockn/MPU6050_tockn: Arduino library for easy communication with MPU6050)

It seems to upload perfectly with no errors. But after say one or two movements LED's 2 & 4 appear to get stuck on lit, so I get the impression that the program seems to get stuck there instead of going back to the start of the loop function.
Like I mentioned, I'm band new to all of this so forgive me if my explanation seems a little vague. Let me know if you need any more information.

Thanks again :slight_smile:

Also, yes the readouts seem to look perfectly normal and legible to me on the printout.

This

Should be

This works perfectly! Thank you for all of your help. However, if the else if provides a reading, the arduino just seems to stop as in freeze. Any ideas on this?

You already collected an angle in the if clause. Are you allowed to get another angle, even if one is not available? Check the library docs.

Best to get the angle, store it in a variable, then use it in the if statements.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.