Hi there, thanks for the responses.
Currently I have the Arduino Uno connected to the accelerometer using the 3v3. It is ground and the sda and scl pins are connected to a4 and a5 respectively.
I do not have a schematic of the system, is there a software I can use to quickly mock this up?
I have posted the code as suggested below. I am completely new to this so have been trying to debug issues with Chat GPT is this advisable?
Thanks again for the help ![]()
#include <__accel3_driver.h>
#include <Wire.h>
#define ACCEL3_I2C_ADDRESS 0x18 // I2C address
#define END_MODE_STOP 0 // Define END_MODE_STOP
#define END_MODE_RESTART 1 // Define END_MODE_RESTART
void hal_i2cMap()
{
Wire.begin(); // Initialize the I2C library
}
void hal_i2cRead(uint8_t slaveAddress, uint8_t *pBuf, uint16_t nBytes, uint8_t endMode)
{
Wire.requestFrom(slaveAddress, nBytes); // Request nBytes from the device
while (Wire.available()) {
*pBuf++ = Wire.read(); // Read data from the device
}
if (endMode == END_MODE_STOP) {
Wire.endTransmission(); // End transmission with stop condition
} else if (endMode == END_MODE_RESTART) {
Wire.endTransmission(false); // End transmission with restart condition
}
}
// Constants for acceleration calculation
const float gForcePerLSB = 0.244; // 100g full scale range, 16-bit data
// Function to read acceleration values
void readAcceleration(int16_t &x, int16_t &y, int16_t &z) {
uint8_t buffer[6];
hal_i2cRead(ACCEL3_I2C_ADDRESS, buffer, 6, END_MODE_STOP);
x = (int16_t)(buffer[1] << 8 | buffer[0]);
y = (int16_t)(buffer[3] << 8 | buffer[2]);
z = (int16_t)(buffer[5] << 8 | buffer[4]);
}
// Function to calculate linear acceleration
void readLinearAcceleration(float &linearAccX, float &linearAccY, float &linearAccZ) {
int16_t accelX, accelY, accelZ;
readAcceleration(accelX, accelY, accelZ); // Read raw acceleration data
// Convert raw accelerometer data to linear acceleration
linearAccX = (accelX * gForcePerLSB) - 0; // Subtract 0 g from X-axis (no gravity component along X-axis)
linearAccY = (accelY * gForcePerLSB) - 0; // Subtract 0 g from Y-axis (no gravity component along Y-axis)
linearAccZ = (accelZ * gForcePerLSB) - 1; // Subtract 1 g from Z-axis (gravity component along Z-axis)
// Print raw accelerometer readings and linear acceleration values for debugging
Serial.print("Raw Acceleration - X:");
Serial.print(accelX);
Serial.print(", Y:");
Serial.print(accelY);
Serial.print(", Z:");
Serial.print(accelZ);
Serial.print(" | Linear Acceleration - X:");
Serial.print(linearAccX);
Serial.print(", Y:");
Serial.print(linearAccY);
Serial.print(", Z:");
Serial.println(linearAccZ);
}
void setup() {
Serial.begin(38400); // Initialize serial communication for Bluetooth
hal_i2cMap(); // Initialize I2C
}
void loop() {
float linearAccX, linearAccY, linearAccZ;
readLinearAcceleration(linearAccX, linearAccY, linearAccZ); // Read linear acceleration data
// Sending linear acceleration data over Bluetooth
Serial.print("Linear Acceleration - X:");
Serial.print(linearAccX);
Serial.print(", Y:");
Serial.print(linearAccY);
Serial.print(", Z:");
Serial.println(linearAccZ);
delay(1000); // Delay for 1 second before sending the next data
}