# How to properly initialize adxl345 on I2C stm32?

**URL:** <https://community.sparkfun.com/t/how-to-properly-initialize-adxl345-on-i2c-stm32/22644>\
**Category:** MicroMod\
**Tags:** arm-lpc\
**Created:** [November 29, 2011, 3:18pm UTC](https://community.sparkfun.com/t/how-to-properly-initialize-adxl345-on-i2c-stm32/22644 "2011-11-29T15:18:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![SevenDays](https://avatars.discourse-cdn.com/v4/letter/s/f17d59/32.png) [@SevenDays](https://community.sparkfun.com/u/SevenDays)\
**Post date:** [November 29, 2011, 3:18pm UTC](https://community.sparkfun.com/t/how-to-properly-initialize-adxl345-on-i2c-stm32/22644/1 "2011-11-29T15:18:32Z")

</div>

I want to get data from ADXL345 accelerometer,but seems that I incorrectly connect it.

SCL- PC6(with 10k resistor)

SDA- PC7(with 10k resistor)

SDO- GND

CS - VCC

GND - GND

3.3v - VCC

Here is my code to initalize:

```auto
#define I2CG I2C1
void I2CG_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    I2C_InitTypeDef I2C_InitStructure;

    RCC_AHBPeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
    RCC_AHBPeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
        // I2CG clock enable
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2CG, ENABLE);
    RCC_AHBPeriphClockCmd(RCC_APB1Periph_I2CG, ENABLE);
    // GPIOB clock enable
    // I2CG SCL and SDA configuration
    GPIO_InitStructure.GPIO_Pin = SCL|SDA;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
    GPIO_Init(GPIOC, &GPIO_InitStructure);
    // Enable I2CG reset state
    RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2CG, ENABLE);
       // Release I2CG from reset state
       RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2CG, DISABLE);
    I2C_DeInit(I2C1);
    I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
    I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_16_9;
    I2C_InitStructure.I2C_OwnAddress1 = 1;
    I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
    I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
    I2C_InitStructure.I2C_ClockSpeed = ClockSpeed;

    I2C_Init(I2CG, &I2C_InitStructure);
    I2C_Cmd(I2CG, ENABLE);
    I2C_AcknowledgeConfig(I2CG, ENABLE);
}

```

In one example I saw

```auto
GPIO_PinAFConfig(GPIOC,SCLSource,GPIO_AF_I2CG);
GPIO_PinAFConfig(GPIOC,SDASource,GPIO_AF_I2CG);

```

But I don’t have this API available.

Please help me. I tried many solutions and also tried to connect through SPI, but no success ☹ Please help with I2C.

---

<div class="post-metadata">

**Author:** ![rmteo1](https://avatars.discourse-cdn.com/v4/letter/r/f17d59/32.png) [@rmteo1](https://community.sparkfun.com/u/rmteo1)\
**Post date:** [November 29, 2011, 4:16pm UTC](https://community.sparkfun.com/t/how-to-properly-initialize-adxl345-on-i2c-stm32/22644/2 "2011-11-29T16:16:34Z")

</div>

Try [[here.](https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/AllItems.aspx)]([https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex\_mx\_stm32/AllItems.aspx](https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/AllItems.aspx))
