I2C communication

avi007
Posts: 11
Joined: Fri May 26, 2017 12:58 am

I2C communication

Postby avi007 » Fri May 26, 2017 1:06 am

Hi,

I am trying to use ESP32-IDF library in C++. I am trying to communication with LSM9DS1 sensor via i2c, but so far not able to to read even WHO_AM_I register.

I have checked the sensor my Arduino board and it works fine and communicated flawlessly.

Based on my little understanding I am using following code to communicate.

Initialization code ...

Code: Select all

         i2c_config_t conf;
	conf.mode = I2C_MODE_MASTER;
	conf.sda_io_num = GPIO_NUM_21;
	conf.scl_io_num = GPIO_NUM_22;
	conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
	conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
	conf.master.clk_speed = 100000;
	i2c_param_config(I2C_NUM_0,&conf);
	i2c_driver_install(I2C_NUM_0, conf.mode, 0,0,0);
	uint8_t sensor_data_h, sensor_data_l;
	read_mag_channel(&sensor_data_h, &sensor_data_l);
Here is how I am reading the WHO_AM_I register

Code: Select all

i2c_cmd_handle_t cmd = i2c_cmd_link_create();
	i2c_master_start(cmd);
	i2c_master_write_byte(cmd,(LSM9DS1_M << 1)| WRITE_BIT, ACK_CHECK_EN );
	i2c_master_write_byte(cmd, WHO_AM_I_M, ACK_CHECK_EN);
	i2c_master_stop(cmd);
	int ret = i2c_master_cmd_begin(I2C_NUM_0, cmd, 1000/portTICK_RATE_MS);
	printf(" error value %d\n", ret);
	i2c_cmd_link_delete(cmd);

	vTaskDelay(30/portTICK_RATE_MS);

	cmd = i2c_cmd_link_create();
	i2c_master_start(cmd);
	i2c_master_write_byte(cmd, (LSM9DS1_M << 1) | READ_BIT, ACK_CHECK_EN);
	i2c_master_read_byte(cmd, data_h, ACK_VAL);
	//i2c_master_read_byte(cmd, data_l, NACK_VAL);
	i2c_master_stop(cmd);
	ret = i2c_master_cmd_begin(I2C_NUM_0, cmd, 1000 / portTICK_RATE_MS);
	printf(" error value %d\n", ret);
	i2c_cmd_link_delete(cmd);
Please let me know if you know what I am doing wrong. I also read through the datasheet, this should be the sequence.

Thanks,
avi

onehorse
Posts: 70
Joined: Mon Feb 15, 2016 1:35 am

Re: I2C communication

Postby onehorse » Fri May 26, 2017 4:03 am

Here's a bunch of sketches for the Arduino IDE (C++) that use I2C to read sensors. Not the LSM9DS1 but this should be trivial since all of these sketches are trivially adapted from sketches written for the Teensy or STM32L4 Arduino cores, and the LSM9DS1 works for these.

https://github.com/kriswiner/ESP32

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: I2C communication

Postby kolban » Fri May 26, 2017 4:25 am

Have you attached a logic analyzer to the I2C bus (CLK and SDA pins?). I have always found examining the protocol on the bus to resolve most of the issues I have had.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

avi007
Posts: 11
Joined: Fri May 26, 2017 12:58 am

Re: I2C communication

Postby avi007 » Fri May 26, 2017 12:46 pm

Thank you for your response onehorse and Kolban.

Onehorse,

I am trying not to use Arduino library for my project as I am not sure what works and what does not work. I don't know where I would get stuck using Arduino library for esp32. I also looked into Arduino Wire.h to figure out where I could be wrong but no luck. I have been struggling with this problem for about a week now.

Kolban,
Since you did not see any obvious mistake in my code, that tells me that I need to start looking into the logic analyzer.
but it's so perplexing I have tried through various example but still can not figure it out.

is there a way to use part of Arduino core in eclipse esp-idf c++ development? May be I can use Wire.h some way, since I am able to successfully test my sensor using Arduino Core in Arduino IDE.

Btw I am using your book to learn about esp32. Thank you so much for the book.

kind regards to both of you ...

avi007
Posts: 11
Joined: Fri May 26, 2017 12:58 am

Re: I2C communication

Postby avi007 » Sat May 27, 2017 12:39 am

Thank you for your replies.

I am working using esp-idf framework using eclipse in C++. So I would prefer in C++. I was able to hook up esp32 with the sensor and successfully run it via Areduino IDE and Arduino-esp32 core.

I will look into the logic analyzer, if you look through the code I am assuming you dont see any obvious mistakes ?

Is there a way to mix arduino core into C++ and eclipse ? Then I can use Wire.h to communicate and keep using the ESP-IDF c++ functionality.

Thank you,
Avi

Who is online

Users browsing this forum: No registered users and 135 guests