This circuit is designed to interface an Arduino Nano with an HC-05 Bluetooth module and an MPU-6050 accelerometer. The system is powered by a single 18650 Li-Ion battery, and a rocker switch is used to control the power supply to the circuit. The Arduino Nano reads the accelerometer data from the MPU-6050 and sends commands via the HC-05 Bluetooth module based on the accelerometer's readings. This setup could be used for wireless motion sensing or control applications.
#include <SoftwareSerial.h>
SoftwareSerial BT_Serial(2, 3); // RX, TX
#include <Wire.h> // I2C communication library
const int MPU = 0x68; // I2C address of the MPU6050 accelerometer
int16_t AcX, AcY, AcZ;
int flag = 0;
void setup() {
Serial.begin(9600); // start serial communication at 9600bps
BT_Serial.begin(9600);
// Initialize interface to the MPU6050
Wire.begin();
Wire.beginTransmission(MPU);
Wire.write(0x6B);
Wire.write(0);
Wire.endTransmission(true);
delay(500);
// Read initial accelerometer values to set the flag
Read_accelerometer();
UpdateFlag();
}
void loop() {
Read_accelerometer(); // Read MPU6050 accelerometer
UpdateFlag(); // Update the flag based on accelerometer readings
// Send commands based on accelerometer readings and flag status
if (AcX < 60 && flag == 0) {
flag = 1;
BT_Serial.write('f');
}
if (AcX > 130 && flag == 0) {
flag = 1;
BT_Serial.write('b');
}
if (AcY < 60 && flag == 0) {
flag = 1;
BT_Serial.write('l');
}
if (AcY > 130 && flag == 0) {
flag = 1;
BT_Serial.write('r');
}
if (flag == 1 && IsNeutralPosition()) {
flag = 0;
BT_Serial.write('s');
}
delay(100);
}
void Read_accelerometer() {
// Read the accelerometer data
Wire.beginTransmission(MPU);
Wire.write(0x3B); // Start with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(MPU, 6, true); // Read 6 registers total, each axis value is stored in 2 registers
AcX = Wire.read() << 8 | Wire.read(); // X-axis value
AcY = Wire.read() << 8 | Wire.read(); // Y-axis value
AcZ = Wire.read() << 8 | Wire.read(); // Z-axis value
// Map the values to a range of 0 to 180 for easier interpretation
AcX = map(AcX, -17000, 17000, 0, 180);
AcY = map(AcY, -17000, 17000, 0, 180);
AcZ = map(AcZ, -17000, 17000, 0, 180);
// Debugging output
Serial.print(AcX);
Serial.print("\t");
Serial.print(AcY);
Serial.print("\t");
Serial.println(AcZ);
}
void UpdateFlag() {
// Update the flag based on the current accelerometer readings
if (IsNeutralPosition()) {
flag = 0;
} else {
flag = 1;
}
}
bool IsNeutralPosition() {
// Check if the accelerometer is in a neutral position
return (AcX > 70 && AcX < 120) && (AcY > 70 && AcY < 120);
}
This code is designed to run on the Arduino Nano. It initializes the serial communication for debugging and the Bluetooth communication with the HC-05 module. The MPU-6050 accelerometer's data is read and processed to send commands via Bluetooth based on the orientation of the accelerometer. The Read_accelerometer
function reads the accelerometer data, and the UpdateFlag
function updates a flag based on the orientation to prevent repeated commands when the device is stationary. The IsNeutralPosition
function checks if the device is in a neutral position, which is used to reset the flag.