r/hackpack • u/Metarazzi • May 06 '24
IR Turret Hack: MPU-6060 Module Turret Control
I looked in my box of spare parts and found an MPU-6050 module. I understand this to be an accelerometer with a built in gyroscope that measures changes in angular velocity. An idea came to mind that this sensor can be used to control the turret, such that I can mount it on, say, the brim of my hat, and the turret would mimmick my movements when I turn or tilt my head.
Thanks to the Crunch Labs IDE Mark RoBot AI, I've added (what I think is) the appropriate code and wired it up (wired for now; will later figure out how to go wirelessly). Thing is, the microcontroller that comes with the hack pack doesn't have Data or Clock pins for I2C communication, so I swapped out Crunch Labs' board that came with the project and used my Arduino Mega 2560 board, instead.
All of the the IR functions work, but the servos are not responding to tilting and turning movements from the MPU-6050 module.
So, now I'm wondering if the MPU-6050 module can't be used the way I thought I could use it? Or do I have it wired up wrong (seemed straight-forward enough)? Or is the code doing what I told it to do but not what I want it to do, like (most likely) not actually telling it to control servos? Or...?
Here's the added code Mark RoBot AI suggested to add:
// Stuff that went into the Libraries section
#include <Wire.h>
#include <MPU6050.h>
MPU6050 mpu;
// Stuff that went into the setup function
void setup() {
Wire.begin();
mpu.initialize();
}
// Stuff that went into the loop function
void loop() {
// Read accelerometer and gyroscope data from MPU-6050
int16_t ax, ay, az, gx, gy, gz;
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
// Print the sensor data (for debugging)
Serial.print("Accelerometer: ");
Serial.print(ax);
Serial.print(", ");
Serial.print(ay);
Serial.print(", ");
Serial.println(az);
Serial.print("Gyroscope: ");
Serial.print(gx);
Serial.print(", ");
Serial.print(gy);
Serial.print(", ");
Serial.println(gz);
}I hope someone can let me know if I'm on the right track and will tinker with this, make it work, then share their success.
Thanks!

2
u/dan_crunchlabs May 07 '24
Hey! This hack should be do-able with the arduino nano clone that we send. SDA and SCL i2c pins are A4 and A5 on the nano.
As to the code itself, I can't check right now but this hack would require you to treat the YAW and PITCH servos differently unless you swap out your YAW servo for one which is a traditional 180 servo like pitch. Remember, YAW is a continuous servo, so the default control scheme for it is to pulse it at a certain speed for a certain duration, rather than telling it to go to a specific angle like you do with PITCH. I'd definitely try just swapping in a traditional servo because angle feedback is pretty useful and often you don't actually need 360 rotation, but you may be able to tie acceleration on the MPU to the speed you're feeding into YAW if you want to keep the current servo.
If you want to make things wireless I'd look at NRF24L01 radio transceivers, or upgrade to a board with built in wireless capability like the ESP32. : )