1

I found a code from Astronomica Messier that allows to activate a servo from a Gyroscope MPU 6050 change in direction. I followed the wiring and uploading correctly. The thing is that the MPU is initializing but the connection is failing. Could you help me please to figure out whats happening here? I'm using a GY-86, an Arduino Nano and a Micro Servo 9g. Here's the code. Thank you.

#include "Wire.h"
#include "I2Cdev.h" 
#include "MPU6050.h" 
#include "Servo.h" 


MPU6050 mpu; 
int16_t ax, ay, az; 
int16_t gx, gy, gz; 
Servo servo1; 

int val1;  
int prevVal1; 

void setup() 
{ 
Wire.begin(); 
Serial.begin(38400); 
Serial.println("Initialize MPU"); 
mpu.initialize(); 
Serial.println(mpu.testConnection() ? "Connected" : 
"Connection failed"); 
servo1.attach(9); 
} 

void loop() 
{ 
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); 
val1 = map(ax, -17000, 17000, 120, 10); 
if (val1 != prevVal1) 
{ 
servo1.write(val1); 
prevVal1 = val1; 
} 
delay(50); 
}
2
  • 2
    Have you tried using an i2c scanner to see if the gyro is detected on the bus?
    – Sim Son
    Aug 14, 2019 at 22:20
  • 1
    Missing pullups are a common problem when struggling with i2c, but they might be included on the sensor module. Giving a link to the module you ctually use will help to odd this out.
    – Sim Son
    Aug 14, 2019 at 22:27

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.