Using Accel3 from Mikroe

I am unable to get the mikroe accel3 working with my arduino Uno. It is not providing accurate data, it just records 0 for all 3 axis, x y and z. Help would be greatly appreciated

have a look at how-to-get-the-best-out-of-this-forum
e.g.

  1. have you read the Accel 3 Click documentation? the working voltage is 3.3V - connecting it directly to a UNO which uses 5volt logic could damage the device
  2. upload the code using code tags </>
  3. upload a schematic showing the wiring and power supplies

Welcome to the forum.

Please read the how to get the best from the forum post.

Use the IDE auroformat tool to format your code for easier reading.

Properly post the code in a code block as suggested by the how to get the best from the forum post.

As requested, post a detailed schematic or wiring diagram.

Photots showing the actual wiring can also be of value

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 :slight_smile:

#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
}

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