infwin
Published © CC BY-NC-SA

1-Wire Soil Moisture Sensor demo Arduino/ESP32/Raspberry PI

Read 1-Wire soil moisture sensor by ONLY ONE I/O , including soil moisture, temperature, and EC (Conductivity).

BeginnerShowcase (no instructions)1,976
1-Wire Soil Moisture Sensor demo Arduino/ESP32/Raspberry PI

Things used in this project

Hardware components

MT05S - Soil Moisture/Temperature/EC Sensor
×1
Arduino UNO R3
×1
ESP32S
Espressif ESP32S
×1
Raspberry Pi 3 Model B
Raspberry Pi 3 Model B
×1

Hand tools and fabrication machines

Arduino IDE
OneWire Library by Paul Stoffregen

Story

Read more

Schematics

Application Note for Wiring, Coding and demostration

Code

Arduino Demo

C/C++
This is demostration sourcecode for Arduino UNO R3, ESP32 DoIt DevKit V1, NodeMCU 32S, to read soil moisture, temperature, and EC (Conductivity) from MT05S sensor using SINGLE I/O.
#include <OneWire.h>

#define TEST_MT05S_ONEWIRE_CONVERT_T            1

// Define a 1-Wire signal pin (a 2.0K~5.1K pull-up resistor is necessary)
// Change according to your wiring and connection.
OneWire  ds(2);  

void setup(void) {
  Serial.begin(9600);
}

void loop(void) {
  Serial.println("//-------------------------");
  Serial.println("// Start Testing MT05S     ");
  Serial.println("//-------------------------");
  uint8_t i;

#ifdef TEST_MT05S_ONEWIRE_CONVERT_T
  //--------------------------------------------
  // Tested - Convert T and Read Scratchpad
  // Attention: 
  //--------------------------------------------
  Serial.print("Test Function Command- Convert T [0x44]  & Read Scratchpad [0xBE]: \n");
  byte scratchpad[9];

  Serial.print("Convert T [0x44]: ");
  ds.reset();//Send RESET
  ds.skip();//Send ROM Command-Skip ROM
  ds.write(0x44);//Send Function command- convert T

  //Wait conversion done (Whether to pull down the DQ line during operation can be configured in scratchpad CONFIG0-Bit3- SensorPowerMode)
  while(ds.read_bit() == 0)
  {
    delay(10);
    Serial.print(".");
  }
  Serial.print("Conversion Done\n");

  Serial.print("Read Scratchpad [0xBE]: ");
  ds.reset();//Send RESET
  ds.skip();//Send ROM Command-Skip ROM
  ds.write(0xBE);//Send Function command- Read Scratchpad
  for (int i = 0; i < 9; i++) {// we need 9 bytes
    scratchpad[i] = ds.read();
  }
  for (int i = 0; i < 9; i++) {// we need 9 bytes
    Serial.print(scratchpad[i],HEX);
    Serial.print(" ");
  }
  Serial.print(" CrcCalculated=");
  Serial.print(OneWire::crc8(scratchpad, 8),HEX);
  Serial.println();

  if(OneWire::crc8(scratchpad, 8) == scratchpad[8])
  {
    int16_t temperature = makeWord(scratchpad[0],scratchpad[1]);
    int16_t moisture = makeWord(scratchpad[2],scratchpad[3]);
    int16_t conductivity = makeWord(scratchpad[4],scratchpad[5]);
    Serial.print("TEMP(C) = "); Serial.print(temperature/100.00);
    Serial.print("  MOISTURE(%) = "); Serial.print(moisture/100.00);
    Serial.print("  CONDUCTIVITY(ms/cm) = "); Serial.print(conductivity/1000.00);
    Serial.print("  CONFIG0 = "); Serial.print(scratchpad[6],HEX);
    Serial.print("  CONFIG1 = "); Serial.print(scratchpad[7],HEX);
    Serial.print("  CRC8 = "); Serial.print(scratchpad[8],HEX);
  }
  else
  {
    Serial.print("CRC ERROR!");
  }
  Serial.print("\n\n");
  
  delay(1000);
#endif
}

Raspberry Pi Demo

Python
This is demostration sourcecode for Raspberry Pi 3B, to read soil moisture, temperature, and EC (Conductivity) from MT05S sensor using SINGLE I/O.
#!/usr/bin/python3
import os,time

ROMCODE='28-060504030201'
device_file ='/sys/bus/w1/devices/'+ROMCODE+'/w1_slave'
print("//----------------------------------------------------------------");
print("// Start Testing MT05S");
print("// Rom Code= " + ROMCODE);
print("// Change ROM CODE FOR YOUR SENSOR !!!!!!");
print("//----------------------------------------------------------------");

def read_raw():
    f = open(device_file,'r')
    lines = f.readlines()
    f.close()
    return lines

def read_mt05s():
    lines = read_raw()
    while lines[0].strip()[-3:] != 'YES':
        print('Incorrect Data')
        time.sleep(0.2)
        lines = read_raw()
    byteList = lines[1].rsplit((' '))
    if len(byteList) >9 :
        soilTempHi8=int(byteList[0], 16)
        soilTempLo8=int(byteList[1], 16)
        soilTemp = soilTempHi8*256+soilTempLo8

        soilMoistureHi8=int(byteList[2], 16)
        soilMoistureLo8=int(byteList[3], 16)
        soilMoisture = soilMoistureHi8*256+soilMoistureLo8

        soilECHi8=int(byteList[4], 16)
        soilECLo8=int(byteList[5], 16)
        soilEC = soilECHi8*256+soilECLo8
    return [ soilTemp, soilMoisture, soilEC  ]

while True:
    values = read_mt05s()
    print('Soil Temp(C)= %.2f  Moisture(%%)= %.2f  EC(ms/cm)= %.3f' %(values[0]/100.0,  values[1]/100.0,  values[2]/1000.0))
    time.sleep(1)

Credits

infwin

infwin

1 project • 2 followers

Comments