I2C with Adafruit soil sensor and ESP32

mahadh
Posts: 2
Joined: Fri Apr 24, 2020 7:23 am

I2C with Adafruit soil sensor and ESP32

Postby mahadh » 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?
  1. #include <stdio.h>
  2. #include "esp_log.h"
  3. #include "driver/i2c.h"
  4. #include "sdkconfig.h"
  5.  
  6. int app_main(){
  7.     int i2c_master_port = I2C_NUM_0;
  8.     i2c_config_t conf = {
  9.         .mode = I2C_MODE_MASTER,
  10.         .sda_io_num = 13,         // select GPIO specific to your project
  11.         .sda_pullup_en = GPIO_PULLUP_ENABLE,
  12.         .scl_io_num = 16,         // select GPIO specific to your project
  13.         .scl_pullup_en = GPIO_PULLUP_ENABLE,
  14.         .master.clk_speed = 100000  // select frequency specific to your project
  15.     };
  16.    
  17.     i2c_param_config(i2c_master_port, &conf);
  18.     i2c_driver_install(i2c_master_port, I2C_MODE_MASTER, 0, 0, 0);
  19.    
  20.     uint8_t buf[4];
  21.     while(1){
  22.         i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  23.         i2c_master_start(cmd);
  24.         i2c_master_write_byte(cmd, (0x36 << 1) | I2C_MASTER_READ, 1);
  25.         i2c_master_read(cmd, buf, 4, 0);
  26.         for(int i = 0; i < 4; i++){
  27.         printf("%d", buf[i]);
  28.         }
  29.         printf("\n");
  30.         i2c_master_stop(cmd);
  31.         vTaskDelay(200/ portTICK_PERIOD_MS);       
  32.     }
  33. }
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!

krzyshio
Posts: 1
Joined: Sun May 28, 2023 11:20 am

Re: I2C with Adafruit soil sensor and ESP32

Postby krzyshio » Sun May 28, 2023 11:28 am

Hello,

It seems like there's an issue with your current setup and the code might not be interacting properly with the Adafruit soil sensor. I would suggest checking out two particular GitHub repositories which provide example code and a library specifically designed for the Adafruit STEMA Soil Sensor and ESP32.

ESP32 Adafruit STEMA Soil Sensor Example: This repository provides an example on how to use the Adafruit STEMA Soil Sensor with an ESP32. Here's the link:
https://github.com/Krzyshio/ESP32_Adafr ... or_Example

ESP32 I2C Adafruit STEMA Soil Sensor: This library gives a simplified interface for using the soil sensor with an ESP32 over I2C. Here's the link:
[ESP32 I2C Adafruit STEMMA Soil Sensor Library for ESP-IDF
https://github.com/Krzyshio/ESP32_I2C_A ... oil_Sensor

Please try to incorporate the code from these repositories into your project. Don't forget to carefully follow any setup instructions provided in the README of these repositories. Let me know if you face any more difficulties. Good luck!

Who is online

Users browsing this forum: Baidu [Spider] and 86 guests