Simple sketch for BMA180

Hi everyone, so I just have a school project where I need to measure the acceleration of a wooden "building" that I made, I'll do it with the BMA180 accelerometer, but considering that I'm completely new to arduino, I really need help !
I just need a simple sketch that would give me the values of acceleration, and how to wire it. Thanks in advance!

Welcome to the forum

It took me about 10 seconds to find this
https://www.electrodragon.com/bma180-three-axis-accelerometer-demo-test-with-arduino/

Hi @UKHeliBob,

I guess chaima_1 will not get happy with the sketch you linked ... The person who uploaded it to the electrodragon page

  • is using the old Wire.send() and Wire.receive() functions (rather than Wire.write() and Wire.read())
  • did not #include <Wire.h> and
  • created some nice "code" by "C++ to HTML" like this

data += Wire.receive() &lt ; &lt ; 8;

which can be understood as

data += Wire.receive() << 8;

and (also nice :wink: ):

Wire.send(data &amp ; 0x0F); // low pass filter to 10 Hz

which I understand as

Wire.write(data & 0x0F);

This would be the corrected sketch (at least it compiles); would you mind to look over it?

#include <Wire.h>

void setup()
{
  Serial.begin(115200);
  Wire.begin();

  Serial.println("Demo started, initializing sensors");

  AccelerometerInit();
  // GyroInit();

  Serial.println("Sensors have been initialized");
}

void AccelerometerInit()
{
  Wire.beginTransmission(0x40); // address of the accelerometer
  // reset the accelerometer
  Wire.write(0x10);
  Wire.write(0xB6);
  Wire.endTransmission();
  delay(10);

  Wire.beginTransmission(0x40); // address of the accelerometer
  // low pass filter, range settings
  Wire.write(0x0D);
  Wire.write(0x10);
  Wire.endTransmission();

  Wire.beginTransmission(0x40); // address of the accelerometer
  Wire.write(0x20); // read from here
  Wire.endTransmission();
  Wire.requestFrom(0x40, 1);
  byte data = Wire.read();
  Wire.beginTransmission(0x40); // address of the accelerometer
  Wire.write(0x20);
  Wire.write(data & 0x0F); // low pass filter to 10 Hz
  Wire.endTransmission();

  Wire.beginTransmission(0x40); // address of the accelerometer
  Wire.write(0x35); // read from here
  Wire.endTransmission();
  Wire.requestFrom(0x40, 1);
  data = Wire.read();
  Wire.beginTransmission(0x40); // address of the accelerometer
  Wire.write(0x35);
  Wire.write((data & 0xF1) | 0x04); // range +/- 2g
  Wire.endTransmission();
}

void AccelerometerRead()
{
  Wire.beginTransmission(0x40); // address of the accelerometer
  Wire.write(0x02); // set read pointer to data
  Wire.endTransmission();
  Wire.requestFrom(0x40, 6);

  // read in the 3 axis data, each one is 16 bits
  // print the data to terminal
  Serial.print("Accelerometer: X = ");
  short data = Wire.read();
  data += Wire.read() << 8;
  Serial.print(data);
  Serial.print(" , Y = ");
  data = Wire.read();
  data += Wire.read() << 8;
  Serial.print(data);
  Serial.print(" , Z = ");
  data = Wire.read();
  data += Wire.read() << 8;
  Serial.print(data);
  Serial.println();
}

/*void GyroInit()
  {
  Wire.beginTransmission(0x69); // address of the gyro
  // reset the gyro
  Wire.write(0x3E);
  Wire.write(0x80);
  Wire.endTransmission();

  Wire.beginTransmission(0x69); // address of the gyro
  // low pass filter 10 Hz
  Wire.write(0x16);
  Wire.write(0x1D);
  Wire.endTransmission();

  Wire.beginTransmission(0x69); // address of the gyro
  // use internal oscillator
  Wire.write(0x3E);
  Wire.write(0x01);
  Wire.endTransmission();
  }

  void GyroRead()
  {
  Wire.beginTransmission(0x69); // address of the gyro
  Wire.write(0x1D); // set read pointer
  Wire.endTransmission();

  Wire.requestFrom(0x69, 6);

  // read in 3 axis of data, 16 bits each, print to terminal
  short data = Wire.read() << 8;
  data += Wire.read();
  Serial.print("Gyro: X = ");
  Serial.print(data);
  Serial.print(" , Y = ");
  data = Wire.read() << 8;
  data += Wire.read();
  Serial.print(data);
  Serial.print(" , Z = ");
  data = Wire.read() << 8;
  data += Wire.read();
  Serial.print(data);
  Serial.println();
  } */

void loop()
{
  AccelerometerRead();
  // GyroRead();

  delay(500); // slow down output
}

He/she can be as unhappy as they like. My main point was that I found information on the subject in no time at all.

No offence, your approach is really appreciated! The skills in using Google are as (or sometimes maybe even more) helpful than programming skills ... At least Google skills help in much more situations :wink:

P.S.: Just looked up the page a second time and went all the way down the comments just to find somebody had made the same changes already in Oct 2012 (only missed to include Wire.h) ... Ok, re-invention of the wheel appears every day ...

It's déjà vu all over again

Hi
I searched too and found this, but I had some doubts, so I thought I'd better ask some well experienced people before

It would have been a good idea to mention that in your original post

Thank you so much!

Does it work? That's the question ...

P.S.: And - as @UKHeliBob mentioned - giving some more information about what you tried, what did not work etc. will do several things:

  • It shows that you have already made your own efforts to find a solution
  • It may save other people's time to google themselves
  • It allows to focus on "the problem" rather than finding out what the problem is

Most of the tasks here can/could be solved by

  • searching for the same or similar issue on the net
  • gaining some basic knowledge in C++, microcontrollers and electronics

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