I2C with Adafruit soil sensor and ESP32
Posted: Wed Mar 24, 2021 11:05 am
Hello!
I want to get readings of a soil sensor on my ESP32 by using I2C but so far the code that I've written doesn't seem to connect with the sensor. I've tried the following test code. I'm wondering if the code looks correct? Does anyone else have any experience with adafruit sensors with I2C with an ESP32?
I'm pretty sure that the sensor works because I tried it with an arduino and it worked there and the connection seems fair aswell.
I'm using an ESP32PoE from Olimex with the following schematic https://www.olimex.com/Products/IoT/ESP ... O-GPIO.png
For sensor background: https://learn.adafruit.com/adafruit-ste ... duino-test
Thanks!
I want to get readings of a soil sensor on my ESP32 by using I2C but so far the code that I've written doesn't seem to connect with the sensor. I've tried the following test code. I'm wondering if the code looks correct? Does anyone else have any experience with adafruit sensors with I2C with an ESP32?
- #include <stdio.h>
- #include "esp_log.h"
- #include "driver/i2c.h"
- #include "sdkconfig.h"
- int app_main(){
- int i2c_master_port = I2C_NUM_0;
- i2c_config_t conf = {
- .mode = I2C_MODE_MASTER,
- .sda_io_num = 13, // select GPIO specific to your project
- .sda_pullup_en = GPIO_PULLUP_ENABLE,
- .scl_io_num = 16, // select GPIO specific to your project
- .scl_pullup_en = GPIO_PULLUP_ENABLE,
- .master.clk_speed = 100000 // select frequency specific to your project
- };
- i2c_param_config(i2c_master_port, &conf);
- i2c_driver_install(i2c_master_port, I2C_MODE_MASTER, 0, 0, 0);
- uint8_t buf[4];
- while(1){
- i2c_cmd_handle_t cmd = i2c_cmd_link_create();
- i2c_master_start(cmd);
- i2c_master_write_byte(cmd, (0x36 << 1) | I2C_MASTER_READ, 1);
- i2c_master_read(cmd, buf, 4, 0);
- for(int i = 0; i < 4; i++){
- printf("%d", buf[i]);
- }
- printf("\n");
- i2c_master_stop(cmd);
- vTaskDelay(200/ portTICK_PERIOD_MS);
- }
- }
I'm using an ESP32PoE from Olimex with the following schematic https://www.olimex.com/Products/IoT/ESP ... O-GPIO.png
For sensor background: https://learn.adafruit.com/adafruit-ste ... duino-test
Thanks!