I2C transmission of sensor data

what seems to be the problem here?? i can't get the exact value of the sensor

#include <Wire.h>

int lightVal, moistureVal_0, moistureVal_1, moistureVal_2, humidityVal;

void setup() {
  Wire.begin(2);
  Serial.begin(115200);

  Wire.onReceive(receiveEvent);

}

void receiveEvent(int numBytes) {
  if(Wire.available() >= 4) {
    lightVal = Wire.read();
    moistureVal_0 = Wire.read();
    moistureVal_1 = Wire.read();
    moistureVal_2 = Wire.read();
    humidityVal = Wire.read();
    
  	Serial.print("Brightness: ");
    Serial.println(lightVal);
    Serial.print("Soil moisture0:");
  	Serial.print(moistureVal_0);
  	Serial.print(" | Soil moisture1:");
  	Serial.print(moistureVal_1);
  	Serial.print(" | Soil moisture2:");
  	Serial.println(moistureVal_2);
    Serial.print("Humidity: ");
    Serial.println(humidityVal);
  }
}  

void loop() {
  // put your main code here, to run repeatedly:

}

Have you read the topic as recommended when subscribing to the forum?

If you read it then maybe tell us which sensor you are using and its datasheet, maybe we can help you.

ahh... I'm sorry for not providing the full info.

The sensors I use are dht11 for humidity and for those soil moisture sensor are capacitive soil moisture sensor v1.2. As for the microcontroller the one who receive is esp32 wroom and the one that sends data is arduino.

"I2C transmission of sensor data"

DHT11 not is a i2c device.

" The DHT-11 sensor uses a one-wire protocol to communicate the humidity and temperature values. The sensor act as a slave to a host controller. The digital communication between the DHT11 and the host controller (like Arduino) can be breakdown into four steps: ......."

Ref: Arduino compatible coding 15: Reading sensor data from DHT-11 without using a library.

oh, if I use I2C compatible sensor will it fix the issue?

Do you have a DHT11 I2C model?

What is the kind?

1. Connect onnly DHT11 with UNO. Acquire the data and show on Serial Monitor.

2. Connect 5V UNO and 3.3V ESP32 over I2C Bus using Level Shifter.

3. Create sketch for Master-UNO to send DHT11 data to Slave-ESP32 using I2C Commands at 1-sec interval.

4. Create sketch for Slave-ESP32 to collect data from I2C Bus and show on Serial Monitor of ESP32.

5. Upload the sketches into UNO and ESP32 and chcek the quality of received data.

6. Connect Moisture Sensor with UNO; acquire data and send them along with DHT11 data to ESP32.

Don't hesitate to ask if you need more information, help, code, schematic, etc.

nahh, I mean, I swap it to any sensor that is compatible for I2c like this one.

So, even though I'm not sure if the other man mean it when he said that the DHT11 sensor is not I2C compatible. That in order for me to send the data to another micro controller, I must have a certain kind of I2C sensor.

will the lever shifter have an impact to data transmission?
also from what you said. Do I need to separate the sensor arduino to another arduino to act as master?

Tell me the type number or picture of your Soil Moisture Sensor. I will show you the schematic (connection diagram among DHT11, SM Sensor, UNO, Level Shifter, and ESP32) based on your descrption of post #3.

I use this one.
image

will you show me the schematic diagram of what you say. That will save me a lot of money if ever. If you also have program recommendation it will be great. Thank you so much.

1. Build the setup as per Fig-1. You must use Level shifter; otherwise, one or both MCU will be damamged due to mismatch of logic levels (ESP32 is a 3.3v device and UNO is a 5V device).


Figure-1:

2. Upload the following sketch into Master-UNO to collect data from Moisture Sensor, show it on Serial Monitor and send to ESP32. (Sketch is taken from net; it is compiled and NOT tested.)

#include<Wire.h>
#define slaveAddress 0x23

const int AirValue = 620;   //you need to replace this value with Value_1
const int WaterValue = 310;  //you need to replace this value with Value_2
int soilMoistureValue = 0;
int soilmoisturepercent = 0;

void setup()
{
  Serial.begin(9600); // open serial port, set the baud rate to 9600 bps
  Wire.begin();
}

void loop()
{
  soilMoistureValue = analogRead(A0);  //put Sensor insert into soil
  Serial.println(soilMoistureValue);
  soilmoisturepercent = map(soilMoistureValue, AirValue, WaterValue, 0, 100);
  if (soilmoisturepercent >= 100)
  {
    Serial.println("100 %");
  }
  else if (soilmoisturepercent <= 0)
  {
    Serial.println("0 %");
  }
  else if (soilmoisturepercent > 0 && soilmoisturepercent < 100)
  {
    Serial.print("Soil Moidture: ");
    Serial.print(soilmoisturepercent);
    Serial.println(" %");
  }

  Wire.beginTransmission(slaveAddress);
  Wire.write(highByte(soilmoisturepercent));
  Wire.write(lowByte(soilmoisturepercent));
  byte busStatus = Wire.endTransmission();
  if(busStatus != 0)
  {
    Serial.println("I2C Bus error!");
    while(true);   //wait for ever
  }
  delay(1000);
}

3. Upload the following sketch in ESP32 to acquire "SoilMoisturePercent" quantity from I2C bus and show on the Serial Monitor. (Compiled and NOT tested.)

#include<Wire.h>
#define slaveAddress 0x23
byte yH, yL;
int soilmoisturepercent;
volatile bool flag = false;

void setup() 
{
  Serial.begin(9600);
  Wire.begin(slaveAddress);
  Wire.onReceive(receiveEvent);
}

void loop() 
{
   if(flag == true)
   {
    soilmoisturepercent = yH << 8 | yL;
    Serial.print("Soil Moisture: ");
    Serial.print(soilmoisturepercent);
    Serial.println(" %");
    flag = false;  
   }
}

void receiveEvent(int howMany)
{
  yH = Wire.read();
  yL = Wire.read();
  flag = true;
}

4. Check that the Serial Monitor of both Arduino shows reasonable values.
5. Test the soil moisture value by dipping the soil moisture sensor probe in water or in soil or any liquid.

6. Use the sketches of Step-2, 3 as building blocks to develop sketch ofr the programming of DHT11.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.