'Reading data from ADLX345 using ESP-IDF

I was trying to get data from ADXL345 using esp32 in ESP-idf. The data sheet link for the same can be found here.

The code I tried is

#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/i2c.h"
#include "sdkconfig.h"

//! I2C bus defines
#define I2C_SCL_IO 19
#define I2C_SDA_IO 18
#define I2C_FREQ_HZ 400000
#define I2C_MASTER_PORT_NUM 0
#define I2C_TX_BUF_DISABLE 0
#define I2C_RX_BUF_DISABLE 0
#define I2C_MASTER_TIMEOUT_MS 1000

//! I2C common protocol defines
#define WRITE_BIT I2C_MASTER_WRITE
#define READ_BIT I2C_MASTER_READ
#define ACK_CHECK_EN 0x1
#define ACK_CHECK_DIS 0x0
#define ACK_VAL 0x0
#define NACK_VAL 0x1

//! ADXL defines
#define ADXL_I2C_ADDR 0x53
#define POWER_CTRL 0x2D
#define BW_RATE 0x2C
#define DATA_FORMAT 0x31
#define DATA_START_ADDR 0x32

#define ADXL_2G 0x00
#define ADXL_4G 0x01
#define ADXL_8G 0x02
#define ADXL_16G 0x03

#define ADXL_3200 0x0F
#define ADXL_1600 0x0E
#define ADXL_800 0x0D
#define ADXL_400 0x0C
#define ADXL_200 0x0B
#define ADXL_100 0x0A // default

typedef struct ADXL_DATA
{
    int16_t AcX, AcY, AcZ;
} stADXL_DATA_t;

static esp_err_t i2c_master_init()
{
    uint8_t i2c_master_port = 0;
    i2c_config_t conf = {
        .mode = I2C_MODE_MASTER,
        .sda_io_num = I2C_SDA_IO,
        .scl_io_num = I2C_SCL_IO,
        .sda_pullup_en = GPIO_PULLUP_ENABLE,
        .scl_pullup_en = GPIO_PULLUP_ENABLE,
        .master.clk_speed = I2C_FREQ_HZ};
    i2c_param_config(i2c_master_port, &conf);
    return i2c_driver_install(
        i2c_master_port,
        conf.mode,
        I2C_RX_BUF_DISABLE,
        I2C_TX_BUF_DISABLE,
        0);
}

esp_err_t writeADXL(uint8_t reg_addr, uint8_t data)
{
    uint8_t write_buff[2] = {reg_addr, data};

    return i2c_master_write_to_device(I2C_MASTER_PORT_NUM,
                                      ADXL_I2C_ADDR,
                                      write_buff,
                                      sizeof(write_buff),
                                      I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
}

esp_err_t readADXL(uint8_t reg_addr, uint8_t *data, size_t len)
{
    return i2c_master_write_read_device(
        I2C_MASTER_PORT_NUM,
        ADXL_I2C_ADDR,
        &reg_addr,
        1,
        data,
        len,
        I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
}

static void initADXL(uint8_t range, uint8_t rate)
{
    //? Set range
    uint8_t temp_value;
    ESP_ERROR_CHECK(readADXL(DATA_FORMAT, &temp_value, 1));
    temp_value &= ~0x0F;
    temp_value |= range;
    temp_value |= 0x08; // full resolution
    ESP_ERROR_CHECK(writeADXL(DATA_FORMAT, temp_value));
    //? Activate measure
    ESP_ERROR_CHECK(writeADXL(POWER_CTRL, 0x8));
    //? Output data rate
    ESP_ERROR_CHECK(writeADXL(BW_RATE, rate));
}

void printReg(uint8_t addr)
{
    uint8_t temp_value = 0;
    ESP_ERROR_CHECK(readADXL(addr, &temp_value, 1));
    printf("%X\t %X this is data\n", temp_value, addr);
    vTaskDelay(3000 / portTICK_PERIOD_MS);
}

void xReadADXL(void *params)
{
    int i = 0;
    while (true)
    {
        uint8_t data[6];

        ESP_ERROR_CHECK(readADXL(DATA_START_ADDR, data, 6));

        stADXL_DATA_t adxlData;
        adxlData.AcX = (data[0] | data[1] << 8) / 256;
        adxlData.AcY = (data[2] | data[3] << 8) / 256;
        adxlData.AcZ = (data[4] | data[5] << 8) / 256;

        printf("%d\t\t%d\t%d\t%d\n", ++i, adxlData.AcX, adxlData.AcY, adxlData.AcZ);
        vTaskDelay(20 / portTICK_PERIOD_MS);
    }
}

void app_main()
{
    ESP_ERROR_CHECK(i2c_master_init());

    initADXL(ADXL_2G, ADXL_400);

    printf("Hello sky==> Specs2\n");
    printReg(DATA_FORMAT);
    printReg(POWER_CTRL);
    printReg(BW_RATE);

    vTaskDelay(3000 / portTICK_PERIOD_MS);

    xTaskCreate(xReadADXL, "adxl_read", 2048 * 2, NULL, 1, NULL);
}

and the result I am getting is

Executing action: flash
Running ninja in directory /home/lawliet_/espProjects/20AccEsP/Furnace/build
Executing "ninja flash"...
[1/10] Performing build step for 'bootloader'
[1/1] cd /home/lawliet_/espProjects/20AccEsP/Furnace/build/bootloader/esp-idf/esptool_py && /home/lawliet_/.espressif/python_env/idf4.4_py3.6_env/bin/python /home/lawliet_/esp/esp-idf/components/partition_table/check_sizes.py --offset 0x8000 bootloader 0x1000 /home/lawliet_/espProjects/20AccEsP/Furnace/build/bootloader/bootloader.bin
Bootloader binary size 0x62c0 bytes. 0xd40 bytes (12%) free.
[6/8] Generating binary image from built executable
esptool.py v3.2-dev
Merged 2 ELF sections
Generated /home/lawliet_/espProjects/20AccEsP/Furnace/build/Furnace.bin
[7/8] cd /home/lawliet_/espProjects/20AccEsP/Fu.../espProjects/20AccEsP/Furnace/build/Furnace.bin
Furnace.bin binary size 0x2f810 bytes. Smallest app partition is 0x100000 bytes. 0xd07f0 bytes (81%) free.
[7/8] cd /home/lawliet_/esp/esp-idf/components/...idf/components/esptool_py/run_serial_tool.cmake
esptool.py esp32 -p /dev/ttyUSB0 -b 460800 --before=default_reset --after=hard_reset write_flash --flash_mode dio --flash_freq 40m --flash_size 2MB 0x1000 bootloader/bootloader.bin 0x10000 Furnace.bin 0x8000 partition_table/partition-table.bin
esptool.py v3.2-dev
Serial port /dev/ttyUSB0
Connecting.....
Chip is ESP32-D0WDQ6-V3 (revision 3)
Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None
Crystal is 40MHz
MAC: 30:c6:f7:27:03:58
Uploading stub...
Running stub...
Stub running...
Changing baud rate to 460800
Changed.
Configuring flash size...
Flash will be erased from 0x00001000 to 0x00007fff...
Flash will be erased from 0x00010000 to 0x0003ffff...
Flash will be erased from 0x00008000 to 0x00008fff...
Compressed 25280 bytes to 15790...
Writing at 0x00001000... (100 %)
Wrote 25280 bytes (15790 compressed) at 0x00001000 in 0.9 seconds (effective 236.9 kbit/s)...
Hash of data verified.
Compressed 194576 bytes to 101956...
Writing at 0x00010000... (14 %)
Writing at 0x0001c559... (28 %)
Writing at 0x00021c42... (42 %)
Writing at 0x000275ef... (57 %)
Writing at 0x000301ba... (71 %)
Writing at 0x0003856c... (85 %)
Writing at 0x0003e1f2... (100 %)
Wrote 194576 bytes (101956 compressed) at 0x00010000 in 2.4 seconds (effective 646.9 kbit/s)...
Hash of data verified.
Compressed 3072 bytes to 103...
Writing at 0x00008000... (100 %)
Wrote 3072 bytes (103 compressed) at 0x00008000 in 0.1 seconds (effective 345.1 kbit/s)...
Hash of data verified.

Leaving...
Hard resetting via RTS pin...
Executing action: monitor
Running idf_monitor in directory /home/lawliet_/espProjects/20AccEsP/Furnace
Executing "/home/lawliet_/.espressif/python_env/idf4.4_py3.6_env/bin/python /home/lawliet_/esp/esp-idf/tools/idf_monitor.py -p /dev/ttyUSB0 -b 115200 --toolchain-prefix xtensa-esp32-elf- --target esp32 --revision 0 /home/lawliet_/espProjects/20AccEsP/Furnace/build/Furnace.elf -m '/home/lawliet_/.espressif/python_env/idf4.4_py3.6_env/bin/python' '/home/lawliet_/esp/esp-idf/tools/idf.py' '-p' '/dev/ttyUSB0'"...
--- idf_monitor on /dev/ttyUSB0 115200 ---
--- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
[0;32mI�ets Jul 29 2019 12:21:46

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0030,len:6612
load:0x40078000,len:14780
load:0x40080400,len:3792
0x40080400: _init at ??:?

entry 0x40080694
I (27) boot: ESP-IDF v4.4-dirty 2nd stage bootloader
I (27) boot: compile time 18:08:26
I (27) boot: chip revision: 3
I (30) boot_comm: chip revision: 3, min. bootloader chip revision: 0
I (38) boot.esp32: SPI Speed      : 40MHz
I (42) boot.esp32: SPI Mode       : DIO
I (47) boot.esp32: SPI Flash Size : 2MB
I (51) boot: Enabling RNG early entropy source...
I (57) boot: Partition Table:
I (60) boot: ## Label            Usage          Type ST Offset   Length
I (68) boot:  0 nvs              WiFi data        01 02 00009000 00006000
I (75) boot:  1 phy_init         RF data          01 01 0000f000 00001000
I (82) boot:  2 factory          factory app      00 00 00010000 00100000
I (90) boot: End of partition table
I (94) boot_comm: chip revision: 3, min. application chip revision: 0
I (101) esp_image: segment 0: paddr=00010020 vaddr=3f400020 size=094ech ( 38124) map
I (124) esp_image: segment 1: paddr=00019514 vaddr=3ffb0000 size=023a0h (  9120) load
I (127) esp_image: segment 2: paddr=0001b8bc vaddr=40080000 size=0475ch ( 18268) load
I (137) esp_image: segment 3: paddr=00020020 vaddr=400d0020 size=1762ch ( 95788) map
I (173) esp_image: segment 4: paddr=00037654 vaddr=4008475c size=08174h ( 33140) load
I (186) esp_image: segment 5: paddr=0003f7d0 vaddr=50000000 size=00010h (    16) load
I (193) boot: Loaded app from partition at offset 0x10000
I (193) boot: Disabling RNG early entropy source...
I (207) cpu_start: Pro cpu up.
I (207) cpu_start: Starting app cpu, entry point is 0x400810ec
0x400810ec: call_start_cpu1 at /home/lawliet_/esp/esp-idf/components/esp_system/port/cpu_start.c:156

I (0) cpu_start: App cpu up.
I (221) cpu_start: Pro cpu start user code
I (221) cpu_start: cpu freq: 160000000
I (221) cpu_start: Application information:
I (225) cpu_start: Project name:     Furnace
I (230) cpu_start: App version:      1
I (235) cpu_start: Compile time:     May  9 2022 18:08:10
I (241) cpu_start: ELF file SHA256:  8b8d76201ec12334...
I (247) cpu_start: ESP-IDF:          v4.4-dirty
I (252) heap_init: Initializing. RAM available for dynamic allocation:
I (259) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (265) heap_init: At 3FFB2C98 len 0002D368 (180 KiB): DRAM
I (272) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (278) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (284) heap_init: At 4008C8D0 len 00013730 (77 KiB): IRAM
I (292) spi_flash: detected chip: generic
I (295) spi_flash: flash io: dio
W (299) spi_flash: Detected size(4096k) larger than the size in the binary image header(2048k). Using the size in the binary image header.
I (313) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
Hello sky==> Specs2
8        31 this is data
8        2D this is data
C        2C this is data
1               0       255     1
2               0       255     1
3               0       255     1
4               0       255     1
5               0       255     1
6               0       255     1
7               0       255     1
8               0       255     1
9               0       255     1
10              0       255     1
11              0       255     1
12              0       255     1
13              0       255     1
14              0       255     1
15              0       255     1
16              0       255     1
17              0       255     1
18              0       255     1
19              0       255     1
20              0       255     1
21              0       255     1
22              0       255     1
23              0       255     1
24              0       255     1
25              0       255     1
26              0       255     1
27              0       255     1
28              0       255     1
29              0       255     1
30              0       255     1
31              0       255     1
32              0       255     1
33              0       255     1
34              0       255     1
35              0       255     1
36              0       255     1
37              0       255     1
38              0       255     1
39              0       255     1
40              0       255     1
41              0       255     1
42              0       255     1
43              0       255     1
44              0       255     1
45              0       255     1
46              0       255     1
47              0       255     1
48              0       255     1
49              0       255     1
50              0       255     1
51              0       255     1
52              0       255     1
53              0       255     1
54              0       255     1
55              0       255     1
56              0       255     1
57              0       255     1
58              0       255     1
59              0       255     1
60              0       255     1
61              0       255     1
62              0       255     1
63              0       255     1
64              0       255     1
65              0       255     1
66              0       255     1
67              0       255     1
68              0       255     1
69              0       255     1
70              0       255     1
71              0       255     1
72              0       255     1
73              0       255     1
74              0       255     1
75              0       255     1
76              0       255     1
77              0       255     1
78              0       255     1
79              0       255     1
80              0       255     1
81              0       255     1
82              0       255     1
83              0       255     1
84              0       255     1
85              0       255     1
86              0       255     1
87              0       255     1
88              0       255     1
89              0       255     1
90              0       255     1
91              0       255     1
92              0       255     1
93              0       255     1
94              0       255     1
95              0       255     1
96              0       255     1
97              0       255     1
98              0       255     1
99              0       255     1
100             0       255     1
101             0       255     1
102             0       255     1
103             0       255     1
104             0       255     1
105             0       255     1
106             0       255     1
107             0       255     1
108             0       255     1
109             0       255     1
110             0       255     1
111             0       255     1
112             0       255     1
113             0       255     1
114             0       255     1
115             0       255     1
116             0       255     1
117             0       255     1
118             0       255     1
119             0       255     1
120             0       255     1
121             0       255     1
122             0       255     1
123             0       255     1
124             0       255     1
125             0       255     1
126             0       255     1
127             0       255     1
128             0       255     1
129             0       255     1
130             0       255     1
131             0       255     1

Sensor is kept on a table top with z axis pointing up.

Whats he mistake I am making here.? It is expected to get {0,0,1} if i am missing something correct me.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source