Library 35- LIS302DL or LIS3DSH accelerometer

Maybe you’ve noticed one chip between four leds on STM32F4-Discovery board. This chip is accelerometer. On market, there are 2 versions of STM32F4-Discovery board. First release had LIS302DL (old board) and new release (current) has LIS3DSH device. LIS3DSH has 5 selectables full scales (2/4/6/8/16G), old LIS302DL has only 2 (2.3/9.2G).

I made a simple library which supports both devices. Library automatically recognize connected device, if there is any. You just have to check which is on board, because different devices have different possible settings. Both devices uses the same pinout on board. They use SPI for communication with STM32F4.

I have a new version of STM32F4-Discovery board, so LIS3DSH is in use. In that case i had to find someone who will test this for me. I found one, he is also very good with STM32F4 devices. His website is here.

Library

Features

  • Supports LIS302DL accelerometer
    • old STM32F4-Discovery boards
  • Supports LIS3DSH accelerometer
    • new STM32F4-Discovery boards
  • Selectable full scales
  • Supports sensor detection

Dependencies

  • CMSIS
    • STM32F4xx
    • STM32F4xx RCC
    • STM32F4xx GPIO
    • STM32F4xx SPI
  • TM
    • TM SPI
    • defines.h
LIS302DL/LIS3DSH STM32F4-Discovery Description
MOSI PA7 Master out, Slave in for SPI1
MISO PA6 Master in, Slave out for SPI1
SCK PA5 Serial clock for SPI1
CS PE3 Chip select

If you are working with custom application, and you want to use custom pinout, open defines.h file and edit settings:

Remember: This library also works if you are working with custom application!

Functions and enumerations

New LIS3DSH is a little more powerful, has more selectable full scales and also high pass output filter. For that purpose you have more possible settings on initialization. Possible configuration with functions is described below:

Example

Below is simple example. You have to use STM32F4-Discovery board for this. It works like this:

  • If LIS302DL device is detected, then RED and GREEN leds will be turned on for 2 seconds
  • If LIS3DSH device is detected, then BLUE and ORANGE leds will be turned on for 2 seconds
  • If all 4 leds are on, there was an error, because device is not recognized
  • After 2 seconds leds will be off.
  • If you move your board, leds will be on according to direction you move your board.

This example first checks for connected device. After you know which device is connected, you don’t need to always check that. You can simple just initialize device that you know is connected on board.

Project is available on my Github account, download library below.

Icon
TM STM32F4 LIS302DL/LIS3DSH Library

tilz0R

Owner of this site. Also electronic enthusiasts, web developer, 3D printer fan, handball player and more. Big fan of STM32F4 devices. In anticipation of the new Discovery board for STM32F7 lines.

You may also like...

Read before commenting!

Before you make a new comment, make sure you agree with things listed below:

  • - Read post to make sure if it is already posted what you are asking for,
  • - Make sure you have the latest version of libraries used in your project,
  • - Make a clean and grammatically correct written message,
  • - Report as many details as possible, including what have you done so far,
  • - Do NOT post any code here. Use Pastebin,
  • - Do NOT post any error codes here. Use Pastebin,
  • - Specify STM32Fxxx family and used Discovery/EVAL/Nucleo or custom made board,
  • - Make sure your clock is set correct for PLL,
  • - If you are using my HAL drivers, please check this post how to start.
Comment will be deleted on breaking these rules without notification!
  • h.mkh

    hi

    i start using stm32f407 discovery board recently,i tried to use this code but i have keil 4.7 so i couldn’t open the project,i tried to open main.c and add any thing this program needs but unfortunately errors don’t finish,the last errors are

    Build target ‘Target 1’

    compiling system_stm32f4xx.c…

    linking…

    .accel.axf: Error: L6218E: Undefined symbol RCC_HSEConfig (referred from tm_stm32f4_delay.o).

    .accel.axf: Error: L6218E: Undefined symbol RCC_WaitForHSEStartUp (referred from tm_stm32f4_delay.o).

    .accel.axf: Error: L6218E: Undefined symbol SystemCoreClock (referred from tm_stm32f4_delay.o).

    .accel.axf: Error: L6218E: Undefined symbol TM_DISCO_LedInit (referred from main.o).

    “.accel.axf” – 4 Errors, 0 Warning(s).

    Target not created

    can u help me please?

    thanks

    • Hi.

      You need my DISCO library for leds on discovery board and stm32f4xx CMSIS files.
      Install newest Keil, it has CMSIS files for STM32F4 inside.

      • chaitanya

        Hi ,iam using LIS3DSH in my project,suddenly in all 3 axis 2G value,if i select 4 G im getting 4 G value in o/p registers,can you help me

  • aydin

    Hello
    I want to do this example but l have problems.I attached image about my problems.Can you help me please?
    image link : http://postimg.org/image/m1b70b7q1/

    • Download my disco library.

      • aydin

        is your library’s name tm_stm32f4_disco.h?

        • Yes. And don’t forget .c file.
          Open .h file and look how to set it up for your board.

          • aydin

            I added tm_stm32f4_disco.h and tm_stm32f4_disco.c but the same erors. can you look image? image link: http://s29.postimg.org/iz4k03fcn/Ads_z.jpg
            Can you chech my library documents?What is the worng?

          • again!

            Open tm_stm32f4_disco.h file and look for what you have to set in defines.h file to get working.

          • aydin

            hello again.

            I didn’t create .hex file.Write in console:

            compiling main.c…

            tm_stm32f4_disco.h(178): error: #35: #error directive:
            “tm_stm32f4_disco.h: Please select your board”

            #error
            “tm_stm32f4_disco.h: Please select your board”

            main.c: 0 warnings, 1 error

            compiling tm_stm32f4_disco.c…

            tm_stm32f4_disco.h(178): error: #35: #error directive:
            “tm_stm32f4_disco.h: Please select your board”

            #error
            “tm_stm32f4_disco.h: Please select your board”

            tm_stm32f4_disco.c: 0 warnings, 1 error

            compiling tm_stm32f4_spi.c…

            tm_stm32f4_spi.c(242): error: #20: identifier “GPIO_AF_SPI4” is
            undefined

            GPIO_PinAFConfig(GPIOE, GPIO_PinSource2, GPIO_AF_SPI4);

            tm_stm32f4_spi.c(252): error: #20: identifier “GPIO_AF_SPI4” is
            undefined

            GPIO_PinAFConfig(GPIOE, GPIO_PinSource12, GPIO_AF_SPI4);

            tm_stm32f4_spi.c(257): error: #20: identifier
            “RCC_APB2Periph_SPI4” is undefined

            RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI4, ENABLE);

            tm_stm32f4_spi.c(300): error: #20: identifier “GPIO_AF_SPI5” is
            undefined

            GPIO_PinAFConfig(GPIOF,
            GPIO_PinSource7, GPIO_AF_SPI5);

            tm_stm32f4_spi.c(314): error: #20: identifier “GPIO_AF_SPI5” is
            undefined

            GPIO_PinAFConfig(GPIOF, GPIO_PinSource11, GPIO_AF_SPI5);

            tm_stm32f4_spi.c(319): error: #20: identifier “RCC_APB2Periph_SPI5”
            is undefined

            RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI5, ENABLE);

            tm_stm32f4_spi.c(361): error: #20: identifier “GPIO_AF_SPI6” is
            undefined

            GPIO_PinAFConfig(GPIOG, GPIO_PinSource13, GPIO_AF_SPI6);

            tm_stm32f4_spi.c(366): error: #20: identifier
            “RCC_APB2Periph_SPI6” is undefined

            RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI6, ENABLE);

            tm_stm32f4_spi.c: 0 warnings, 8 errors

            “.ivme.axf” – 10 Error(s), 0 Warning(s).

  • Jack

    Hi
    when i did this project , there were lots of erors.if you prepare this project from Keil,Will you attach project files?i need this project,canyou help about it?

    • Download Repo from Github and open from there.

      • Jack

        i found it but i have this eror

        tm_stm32f4_disco.h(189): error: #35: #error directive: “tm_stm32f4_disco.h: Please select your board. Open tm_stm32f4_disco.h and follow instructions!!”

        #error “tm_stm32f4_disco.h: Please select your board. Open tm_stm32f4_disco.h and follow instructions!!”

        main.c: 0 warnings, 1 error

        compiling stm32f4xx_it.c…

        compiling system_stm32f4xx.c…

        system_stm32f4xx.c(496): warning: #550-D: variable “pllsource” was set but never used

        uint32_t tmp = 0, pllvco = 0, pllp = 2, pllsource = 0, pllm = 2;

        system_stm32f4xx.c(496): warning: #550-D: variable “pllm” was set but never used

        uint32_t tmp = 0, pllvco = 0, pllp = 2, pllsource = 0, pllm = 2;

        system_stm32f4xx.c: 2 warnings, 0 errors

        compiling tm_stm32f4_lis302dl_lis3dsh.c…

        compiling tm_stm32f4_disco.c…

        tm_stm32f4_disco.h(189): error: #35: #error directive: “tm_stm32f4_disco.h: Please select your board. Open tm_stm32f4_disco.h and follow instructions!!”

        #error “tm_stm32f4_disco.h: Please select your board. Open tm_stm32f4_disco.h and follow instructions!!”

        tm_stm32f4_disco.c: 0 warnings, 1 error

        compiling stm32f4xx_rcc.c…

        compiling stm32f4xx_gpio.c…

        stm32f4xx_gpio.c(178): error: #20: identifier “GPIOJ” is undefined

        else if (GPIOx == GPIOJ)

        stm32f4xx_gpio.c(180): error: #20: identifier “RCC_AHB1Periph_GPIOJ” is undefined

        RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOJ, ENABLE);

        stm32f4xx_gpio.c(185): error: #20: identifier “GPIOK” is undefined

        if (GPIOx == GPIOK)

        stm32f4xx_gpio.c(187): error: #20: identifier “RCC_AHB1Periph_GPIOK” is undefined

        RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOK, ENABLE);

        • Did yyou follow instructions? Im sure you didnt open my keil project directly.

          • Jack

            hi again,
            I use stm32f407vg.
            I looked tm_stm32f4_disco.h and write that
            STM32F4 Discovery: (STM32F407VG)

            * Open project options and add “STM32F407VG” define (without quotes)

            * – Leds:

            * – LED_GREEN on PD12

            * – LED_ORANGE on PD13

            * – LED_RED on PD14

            * – LED_BLUE on PD15

            * – Button: (HIGH when pressed)

            * – Blue button on PA0

            I don’t understand how l can define it.Help me please.

          • Disco library is meant for discovery/nucleo boards where leds are exactly defined where they are.
            If you are using one of these board, then you can also open defines.h file and add folliwing line:
            #define TM_DISCO_STM32F4_DISCOVERY

          • Jack

            thank you very much it is working now 🙂

          • vanco

            dude drop the exclamation marks

          • Explanation please.

  • Ricardo Teixeira

    Hi, when I run my program, All the leds light up. But I already add define for my board in compiler’s global settings… What is the problem?
    Please Help me

  • shaun

    Really like your library, it was very easy to work with. Here’s a link to the project that I did with it:

    http://www.element14.com/community/community/designcenter/single-board-computers/blog/2015/06/21/sending-a-signal-over-wifi-based-on-an-accelerometer

  • Pulkit Prajapati

    Thanx
    Working nice
    Red,Green LED ON for two seconds
    But after that When I move my board no changes in axis.data array`s value.
    How to check that all values?

    • USART, debug..plenty options to see values.

      • Pulkit Prajapati

        I have checked using debug.
        But the array value of x , y , z is not changed while I moved it.

  • Axarsu

    Hi
    I want to read out specific number of samples from accelerometer and send them using usart. I’m doing this in systick_handler using a while(count < 100) but it just shows 4 or 5 samples and when i delete while it shows all samples.
    I debugged program and i saw in lis302dl_Read function, count is changed and therefor it jumps out the loop. and i'm using ST libraries.
    What should i do to fix it? Can you help me please??

    • Hi,

      sorry, but you are not clear here with your question. Please describe this problem again.

      • Axarsu

        ok, sorry.
        This is my code to read 100 samples of lis302dl but it’s output is just 4 samples (which i see with usart on computer).
        but when i remove while loop (second pic) it works fine and shows large number of samples which is updating.

        • You are always just increasing pointer. Where do you set it to initial value?

          • Axarsu

            I initialize it as a global variable in the main.c

          • You used malloc for allocation? Where do you reset pointer? Your pointer will go to infinity and memory will overflow.

            Use pastebin and show entire code or send to email to me.

          • Axarsu

            I have sent an email to tilen@majerle.eu.
            Thanks

          • Ok, I’ve looked at your main and found:
            1. You initialized ptr variable to start of RAM directly. Linker may put there somewhere another variable and now you are overwriting its content all the time you write there. You may overwrite “count” and “counter” variables also! Use malloc to allocate memory ONLY for pointer or create buffer array to working with it!
            2. You have while (count = 100) {
            ptr = 0x20000001;
            //You don’t change count value, while loop never ends!
            }

          • Axarsu

            yes, it was the problem. thank you very much.
            it was overwriting it by another value. Very funny mistakes 🙂

  • Ilya Mordasov

    What is the average value are you getting? I have about -120 for x axe, 200 for y and around 1000 for z while my stm is on my desk. I looked at some charts on google and there are all values in g points. How can I convert those values into g value, I mean acceleration gravity

    • Response value / Max response possible * scale in G

      • Ilya Mordasov

        Thank you!

        • Jay!

          If you want to get the length of the total acceleration vector, it`d be easier to use L=sqrt(X^2+Y^2+Z^2)

          • Ilya Mordasov

            Thank you Jay! I found it already, but now I have a new issue. stm32 doesn’t want to calculate sqrt. I included , use this formula but it shows me 0.0, also I tried srqtf… the same result. Do you know how to use sqrt?

          • Jay!

            Are you sure that X, Y, Z != 0? 🙂

            I`ve tried to use it already… works well 😉
            sqrt(Axes_Data.X*Axes_Data.X + Axes_Data.Y*Axes_Data.Y + Axes_Data.Z*Axes_Data.Z)

            Use step-by-step debugging and the “watch” window (Keil) to check the values of the variables.

          • Ilya Mordasov

            Yes, there is enough that one of values was not equal 0. I set breakpoint and can see, that x, y, z have positive and negative values. But sqrt is equal 0. Maybe are there some additional settings in my project? I use eclipse instead of keil

          • Jay!

            Try using pow(squares, 0.5) 😉

            Did you find the max response for any g-scale?

          • Ilya Mordasov

            Well, pow function is good for this one.

            What did you mean to find max response for g-scale?

            First step, I made 100 samples to calibrate each axes using max value for each one.

            And in while loop I use the next formulas

            float G = 9.80665;
            float SCALE_2G = 0.001;

            while(1)
            {
            int16_t X = fabs(fabs(Axes_Data.X)-fabs(calibX));
            int16_t Y = fabs(fabs(Axes_Data.Y)-fabs(calibY));
            int16_t Z = fabs(fabs(Axes_Data.Z)-fabs(calibZ));

            float x = (float)X*G*SCALE_2G;
            float y = (float)Y*G*SCALE_2G;
            float z = (float)Z*G*SCALE_2G;

            step = getSteps(X, Y, Z); //this func works with raw data

            float f = x*x + y*y + z*z;
            //float g = sqrtf(f);
            float g = pow(f, 0.5);

            // should I make a delay here and what’s the value?
            }

            The main purpose to count steps…

            I checked the raw data on my iPhone and made a decision that when I make a step I can see the high amplitude of g scale

          • Jay!

            // should I make a delay here and what’s the value?

            I make a delay. This accelerometer couldnt response faster than 1600 Hz, (you can configure this) thats why, I ask axes using systick handler.

            You can calculate acceleration in G without max response. Sensivity for 16G mode is 0.00073G per digit. So G-value = Response*Sensivity.

            But in this mode I get 24G maximum. I dont know why.
            For any other mode(8g , 2g…) it works correctly.

          • Ilya Mordasov

            How did you calculate sensitivity or did you find it in datasheet?

            I found the mistake with sqrt, the problem was with the value’s type. I fixed it with (float) before data value. Something like that
            float x = (float)Axes_Data.X;
            Now sort works perfect.

            I captured signal from g-sensor and write simple app to visualize it. Working with pedometer I found peaks when I make a step on my image. Using exponential moving average I smoothed a little bit my signal and trying to use FFT to smooth it more.

          • Ilya Mordasov

            Hey Jay!, could you help me, how to find max response for g-scale?

  • Jay!

    Hello
    When I`m asking LIS3DSH every msec, I get blocks with 8-9 similar values for every axis.

    What could be a cause? Low speed?

  • JC

    Just wondering if anyone has some basic code for programming this board as a pedometer?

  • Hi Tilen,
    Thanks for the code.
    I have a doubt about the function TM_SPI_ReadMulti().

    Here, before receiving data , we are adding a dummy data(0x00) to SPIx->DR.

    /* Fill output buffer with data */

    SPIx->DR = dummy;

    Why do we do this, I tried commenting this code, and system stopped working for me.
    Can you please let me know the importance of loading DR with 0x00 while receiving data.

    Thanks

    • Why would you doubt? Does my code work? It does. This is how spi works in general. If you want to read something, you must clock out pulses. To do this, you must send something. And what you send is not important.

      Next time, please learn how spi works then make doubts in code which works OK.

      • Your code works perfect.
        I am trying to learn SPI using your code. The doubt is not about your code, the doubt is on SPI.

        Thanks for the clarification.

  • Ludoo14

    HEy i search the Pcb off LIS3DSH ???

    • Yes, I totally agree with you. What are you talking about actually?

      • Ludoo14

        I am trying to make a PCB a scheme and I use the sensor LIS3DSH I find no library for this sensor is you got a circuit idea of this sensor,
        it’s really urgent !

  • Nicolás Kischinovsky

    Hi! I want to use the SPI2, I pasted in defines.h file this lines:

    /* Put your global defines for all libraries here used in your project */

    #define LIS302DL_LIS3DSH_SPI SPI2
    #define LIS302DL_LIS3DSH_SPI_PINSPACK TM_SPI_PinsPack_1

    /* Select custom CS pin for accelerometer for your application */
    #define LIS302DL_LIS3DSH_CS_RCC RCC_AHB1Periph_GPIOE
    #define LIS302DL_LIS3DSH_CS_PORT GPIOE
    #define LIS302DL_LIS3DSH_CS_PIN GPIO_Pin_3

    When I flash, the four leds turn on. I cant find the mistake, if you know something tell me that please. Thanks!

    • Nicolás Kischinovsky

      The value of the variable “id” after the function
      TM_LIS302DL_LIS3DSH_INT_ReadSPI(&id, LIS302DL_LIS3DSH_REG_WHO_I_AM, 1);

      is 0 and I don’t understand why