Reading accelerometer in Arduino

26 views (last 30 days)
Good morning. I have to read the x,y,z values of a triple axis accelerometer (BMA220) connected to an Arduino Mega in MATLAB or Simulink. How can I do it?

Accepted Answer

Soham Chakraborty
Soham Chakraborty on 3 Jan 2020
Edited: Soham Chakraborty on 3 Jan 2020
Hi,
BMA220 has I2C connectivity interface and can be connectd to other I2C devices using the I2C bus. Each bus has an I2C Master connected to two bidirectional lines, serial data line (SDA) and serial clock (SCL). Each I2C device on an I2C bus has an unique address.
Connect BMA220 to I2C Master (Arduino or RaspberryPi), following the datasheet, example- Vcc -> 3.3V, Gnd -> Gnd, SDA -> Arduino A4 pin, SCL -> Arduino A5 pin.
You would have to connect the I2C Master to a device running MATLAB/Simulink.
Using Arduino (MATLAB R2014b and later version):
2. Use the ARDUINO command, you can load only the required library as follows:
>>a = arduino('com22', 'uno', 'Libraries', 'I2C');
% where com22 is the port on PC where arduino is identified. This can be viewed from the device manager
3. Once you have created the arduino object, use the 'scanI2CBus' command to scan the bus for I2C sensors.
4. Once you get the address of the sensors attached to the bus, create the I2C object restrictive to that address as follows:
>>BMA220 = i2cdev(a, '0x48');
% where 0x48 is where the sensor BMA220 identifies itself. This address can also be obtained from the datasheet of the sensor.
5. Use the 'readRegister' and 'writeRegister' commands to communicate with the device.
Using RaspberryPi (MATLAB 2014a and later version)
2. Create the RaspberryPi object using the command 'raspi' as follows:
>>mypi=raspi
3. Scan the i2C bus to look for the I2C sensors that are connected using the 'scanI2CBus' command as follows
>>scanI2CBus(mypi,'i2cbus')
4. Create the I2C object to use the sensor at a particular address.
>>b=i2cdev(mypi,'i2cbus','slave address');
% this slave address can be obtained by the datasheet of the sensor.
5. Use the readRegister or writeRegister commands to perform the operations on the sensor.
  3 Comments
Soham Chakraborty
Soham Chakraborty on 6 Jan 2020
Hi,
If you check the datasheet for BMA220, page9 lists out the memory map for data registers. Depending on the protocol (I2C / SPI), you should use respective Hex codes.
Data registers corresponding to accleration across:
uint8_t XAXIS = 0x04; % x-axis acceleration value register
uint8_t YAXIS = 0x06; % y-axis acceleration value register
uint8_t ZAXIS = 0x08; % z-axis acceleration value register
Read from register:
xacc = readRegister(BMA220,XAXIS,'int8');
yacc = readRegister(BMA220,YAXIS,'int8');
zacc = readRegister(BMA220,ZAXIS,'int8');
Then convert from 2's complement:
x_data=bitsra((int8_t)xacc,2);
y_data=bitsra((int8_t)yacc,2);
z_data=bitsra((int8_t)zacc,2);
Hope this helps.
Nimesha Witharana
Nimesha Witharana on 11 Aug 2020
Hi,
I have an Arduino mega 2560 board and an acceleration sensor, ADXL345 sensor. I have downloaded support package for Arduino. However matlab does not recognise 'scanI2CBus()'. Do I need to download anything else?
Thank you

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB Support Package for Arduino Hardware in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!