Tailwhip
Posts: 3
Joined: Fri May 17, 2019 5:36 am

SparkFun LIS3DH SPI communication using wiringPi

Fri May 17, 2019 5:53 am

Hi,

i'm trying to communicate via SPI with LIS3DH accelerometer using wiringPi to get X and Z axis acceleration and turn it into velocity. What i managed to write is:

Code: Select all

#include <wiringPiSPI.h>

#define CHANNEL_0 0
#define CHANNEL_1 1
#define MAX_VELOCITY 1

void velSetup() {
	if (wiringPiSPISetup(CHANNEL_1, 500000) < 0)
	{
		printf("Unable to open SPI device \n");
	}
}

float velGetVelocityX() {
	float result;
	unsigned char buffer[10];
	buffer[0] = 0x20;
	buffer[1] = 0x09;
	buffer[2] = 0x0A;
	buffer[3] = 0x0B;
	buffer[4] = 0x0C;
	buffer[5] = 0x0D;
	
	buffer[6] = 0x28;
	buffer[7] = 0x29;
	buffer[8] = 0x2A;
	buffer[9] = 0x2B;
	buffer[10] = 0x2C;
	
	wiringPiSPIDataRW(CHANNEL_1, buffer, 10);
	//printing all registers data just to preview if something is in there
	for (int i = 0; i < 10; i++)
		printf("Result: %d |", buffer[i]); 
	
	result = buffer[0];
	return (float)result / MAX_VELOCITY;
}

int main(){
    int i = 0;

    velSetup();
    
    while (i == 0){
        velGetVelocityX();
    }
    
    return 0;
}
What i get as a results are only 0 value. Can anyone please tell me what am I missing?
Thanks in advance!

User avatar
joan
Posts: 13917
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: SparkFun LIS3DH SPI communication using wiringPi

Fri May 17, 2019 10:41 am

Most likely is the wiring is wrong. If you want help you will have to post clear photos of the wiring. Clear enough to trace wires from pin to pin.

Tailwhip
Posts: 3
Joined: Fri May 17, 2019 5:36 am

Re: SparkFun LIS3DH SPI communication using wiringPi

Fri May 17, 2019 11:12 am

Yes i've messed up with wiring by connecting Vcc to the ground. After correcion as a results im getting 255 values instead of 0 values.

I did the SPI test https://importgeek.wordpress.com/2017/0 ... k-testing/ and it seems that connection is fine i just don't know how to read data properly.

I also must mention that the hex numbers in buffer of my code are registers addresses from datasheet: https://cdn.sparkfun.com/assets/learn_t ... 30rev1.pdf p. 26
Im not sure if it's what it should be because no one ever described what exactly should be in buffer.

ddahms
Posts: 66
Joined: Tue Mar 18, 2014 3:38 pm

Re: SparkFun LIS3DH SPI communication using wiringPi

Fri May 17, 2019 5:42 pm

One minor problem is that you declare buffer[10] but then put 11 things into it. However I think the problem is more basic than that. There is an app note on the Sparkfun site (here) that explains the registers and the initialization procedure.

Tailwhip
Posts: 3
Joined: Fri May 17, 2019 5:36 am

Re: SparkFun LIS3DH SPI communication using wiringPi

Mon May 20, 2019 5:42 pm

Thanks for reply!

I managed to gain an acceleration values but i switched to communication via I2C and ddhams post was verry usefull. Thanks again!

Return to “C/C++”