Using Raspberry Pi 0 and the Bosch 220 Accelerometer by Ty Harness Mar. 2017
BMA220 Acceleration logger using the Raspberry Pi 0.

Use a simple c program to log data on the Raspberry Pi 0. I've used gnuplot to visualise the logged data.
To view the sensor's default register values and then choose capture interval and time via the terminal.

./BMA220Pi0Test

Use 2 arguments capture interval in uS and total capture time in S to start logging from the get-go.

./BMA220Pi0Test 500000 10


http://wiringpi.com/
https://thepihut.com/products/triple-axis-accelerometer-bma220-tiny
https://gcc.gnu.org
http://www.gnuplot.info/



#gnuplot whole data set
set key autotitle columnhead
set datafile separator ","
set xrange [0:10000000]
set yrange [-20.000:20.000]
set title "Acceleration whole data set"
set xlabel "Time[uS]"
set ylabel "Acceleration [m/s^2]"
plot 'test.dat' using 1:2 with lines ls 1 ti "X Data" , '' using 1:3 with lines ls 2 ti "Y Data" , '' using 1:4 with lines ls 3 ti "Z Data"

//Bosch BMA220 Triaxial Accelerometer and Raspberry Pi Acceleration logger
//Use with caution, no promise this code is fit for your purpose
//Ty Harness April 2017


//wiring up BMA220 to the Raspberry Pi I2C Pins
//Bosch BMA220 -- Raspberry Pi
//3.3V         -- 3.3V pin1
//GND          -- GND  pin 9
//SDA          -- SDA  pin 2 GPIO 8
//SCK          -- SCK  pin 5 GPIO 9
//INT          -- (Haven't wired it up as yet)


//to compile the code on the raspberry pi with GNU C compiler
//install GH's wiring pi:  http://wiringpi.com/
//gcc BMA220Pi0Test.c -o BMA220Pi0Test -l wiringPi 

//use the Geany IDE installed on Raspbian by Default set the compile
//and build options:
//gcc -Wall -c "%f" -l wiringPi
//gcc -Wall -o "%e" "%f" -l wiringPi


#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<time.h>
#include <sys/time.h>
#include<wiringPiI2C.h>


//function prototypes
double realdata (int data);
void   getDefaultData();
int    capture(int capS,unsigned int freq);

//main procedure//////////////////////////////////////////////////
int main(int argc, char **argv)
		
	{		
		
	int capSec = 0; //S
	int capInterval = 1000000;// uS 
	
	 if (argc == 1) {
	  //no arguments
	  getDefaultData();	 
	  
	  printf("Enter Capture Interval in uS ?: ");
      scanf("%d", &capInterval);
      
	  
	  printf("Enter Capture Time in S  : ");
      scanf("%d", &capSec);	
      
      
      capture(capSec,capInterval); 
             
      }
	  else {
		 printf("Bosch BMA220 Accelerometer + Raspberry Pi0W Test \n");
	     printf("%d arguments\n", argc);
	    
	    
	    int i = 0;
	    while (i < argc){		  
		  printf("%s \n", argv[i]);
		  i++;			  
		  }
		 if (i == 3){
			capInterval = atoi(argv[1]); 
			capSec      = atoi(argv[2]); 
			capture(capSec,capInterval); 
		 }else  capture(10,500000); 
		  
		 	  		 
	   	 	   
	  }

     
     
	return 0;
}//end of main procedure///////////////////////////////////



void getDefaultData(){

int fd, result;
int xdata,ydata,zdata;
double xreal, yreal, zreal;
	
    printf("Bosch BMA220 Accelerometer + Raspberry Pi0W Test \n");
	
	printf("BMA220 I2C Slave address\t 0x20\n");
	printf("Register\tDefault Data\n");
	
	fd = wiringPiI2CSetup(0xA); //0xA BMA220 Slave address
	
	result = wiringPiI2CReadReg8(fd, 0x0);  printf("0x00 = %x\n", result);
	result = wiringPiI2CReadReg8(fd, 0x02); printf("0x02 = %x\n", result);
	
	//Acceleration data///////////////////////////////////////////////////
	xdata  = wiringPiI2CReadReg8(fd, 0x04); 
	xreal = realdata(xdata);		
	printf("0x04 = %d  %f\n", xdata, xreal);
	
	ydata  = wiringPiI2CReadReg8(fd, 0x06); 
	yreal = realdata(ydata);
	printf("0x06 = %d  %f\n", ydata, yreal);
	
	zdata  = wiringPiI2CReadReg8(fd, 0x08); 
	zreal = realdata(zdata); 
	printf("0x08 = %d  %f\n", zdata, zreal);
	/////////////////////////////////////////////////////////////////////
	
	
	result = wiringPiI2CReadReg8(fd, 0x0A); printf("0x0A = %x\n", result);
	result = wiringPiI2CReadReg8(fd, 0x0C); printf("0x0C = %x\n", result);
	result = wiringPiI2CReadReg8(fd, 0x0E); printf("0x0E = %x\n", result);
	result = wiringPiI2CReadReg8(fd, 0x10); printf("0x10 = %x\n", result);
	result = wiringPiI2CReadReg8(fd, 0x12); printf("0x12 = %x\n", result);
	result = wiringPiI2CReadReg8(fd, 0x14); printf("0x14 = %x\n", result);
	result = wiringPiI2CReadReg8(fd, 0x16); printf("0x16 = %x\n", result);
	result = wiringPiI2CReadReg8(fd, 0x18); printf("0x18 = %x\n", result);
	result = wiringPiI2CReadReg8(fd, 0x1A); printf("0x1A = %x\n", result);
	result = wiringPiI2CReadReg8(fd, 0x1C); printf("0x1C = %x\n", result);
	result = wiringPiI2CReadReg8(fd, 0x1E); printf("0x1E = %x\n", result);
	result = wiringPiI2CReadReg8(fd, 0x20); printf("0x20 = %x\n", result);
	result = wiringPiI2CReadReg8(fd, 0x22); printf("0x22 = %x\n", result);
	//result = wiringPiI2CReadReg8(fd, 0x24); printf("0x24 = %x\n", result);
	//result = wiringPiI2CReadReg8(fd, 0x26); printf("0x26 = %x\n", result);
	//result = wiringPiI2CReadReg8(fd, 0x28); printf("0x28 = %x\n", result);
	//result = wiringPiI2CReadReg8(fd, 0x30); printf("0x30 = %x\n", result);
	//result = wiringPiI2CReadReg8(fd, 0x2A); printf("0x2A = %x\n", result);
	//result = wiringPiI2CReadReg8(fd, 0x2C); printf("0x2C = %x\n", result);
	//result = wiringPiI2CReadReg8(fd, 0x2E); printf("0x2E = %x\n", result);
	//result = wiringPiI2CReadReg8(fd, 0x30); printf("0x30 = %x\n", result);
	//result = wiringPiI2CReadReg8(fd, 0x32); printf("0x32 = %x\n", result);	
	
}

int capture(int capS,unsigned int freq){
	
	
    int fd;	
	int xdata,ydata,zdata;
	double xreal, yreal, zreal;
	struct timeval tvalA, tvalB;  	
	time_t t = time(NULL);
    struct tm tm = *localtime(&t);		
	FILE *fp;
	
      fd = wiringPiI2CSetup(0xA); //0xA BMA220 Slave address
	  
	  gettimeofday (&tvalB, NULL);  //must be intilised
	  gettimeofday (&tvalA, NULL); //must be intilised
	
	 //To do remove hard file path
	 	
	  fp = fopen ("/home/pi/myc/i2cwiringpitest/test.dat","wb");	
	   if (fp)
	   {	
	   fprintf(fp,"%d-%d-%d %d:%d:%d\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
	
	
	   while(  (tvalB.tv_sec - tvalA.tv_sec) < capS  ){			
	   usleep(freq);
	   		
	   //Acceleration data//////////////////////////////////////////////
	   xdata  = wiringPiI2CReadReg8(fd, 0x04); 
	   xreal = realdata(xdata);		
		
	   ydata  = wiringPiI2CReadReg8(fd, 0x06); 
	   yreal = realdata(ydata);
		
	   zdata  = wiringPiI2CReadReg8(fd, 0x08); 
	   zreal = realdata(zdata); 
	   printf("X,Y,Z [m/s^2] = %f, %f,  %f\n", xreal, yreal, zreal);
	   ////////////////////////////////////////////////////////////////
	
	   gettimeofday (&tvalB, NULL);
	
	   long int timdur;
	
	   timdur = ((tvalB.tv_sec - tvalA.tv_sec)*1000000L +tvalB.tv_usec) - tvalA.tv_usec ; 
			
	   fprintf(fp,"%ld,%f,%f,%f\n",timdur, xreal, yreal, zreal);
					
       } //end of while
	 	  	  
	 fclose(fp);
   }	
 return 1;	
}



double realdata (int data){
	
	double x,d;
	double nfactor = 1;	
	double slope = 0.0625;//  1.94/31;    sensitivity   +/- 2g range 
	int dat;
	
	data = data >> 2;
	
	
	
	if (data > 31){          //MSB represents a negative number    
       dat = ~data + 1;      //2s compliment make it negative
       dat = dat & 0b011111; //bit mask anded to make positive 
       nfactor = -1;
      }else
      {
      dat = data; 
      }

      d =  (double)dat;  
      x = d*slope*9.81*nfactor;            
      return x;  
		
	
}