Devices & Components
Arduino Uno Rev3
MT05S - Soil Moisture/Temperature/EC Sensor
ESP32S
Raspberry Pi 3 Model B
Hardware & Tools
OneWire Library by Paul Stoffregen
Software & Tools
Arduino IDE
Project description
Code
Arduino Demo
c_cpp
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.
1#include <OneWire.h> 2 3#define TEST_MT05S_ONEWIRE_CONVERT_T 1 4 5// Define a 1-Wire signal pin (a 2.0K~5.1K pull-up resistor is necessary) 6// Change according to your wiring and connection. 7OneWire ds(2); 8 9void setup(void) { 10 Serial.begin(9600); 11} 12 13void loop(void) { 14 Serial.println("//-------------------------"); 15 Serial.println("// Start Testing MT05S "); 16 Serial.println("//-------------------------"); 17 uint8_t i; 18 19#ifdef TEST_MT05S_ONEWIRE_CONVERT_T 20 //-------------------------------------------- 21 // Tested - Convert T and Read Scratchpad 22 // Attention: 23 //-------------------------------------------- 24 Serial.print("Test Function Command- Convert T [0x44] & Read Scratchpad [0xBE]: \ 25"); 26 byte scratchpad[9]; 27 28 Serial.print("Convert T [0x44]: "); 29 ds.reset();//Send RESET 30 ds.skip();//Send ROM Command-Skip ROM 31 ds.write(0x44);//Send Function command- convert T 32 33 //Wait conversion done (Whether to pull down the DQ line during operation can be configured in scratchpad CONFIG0-Bit3- SensorPowerMode) 34 while(ds.read_bit() == 0) 35 { 36 delay(10); 37 Serial.print("."); 38 } 39 Serial.print("Conversion Done\ 40"); 41 42 Serial.print("Read Scratchpad [0xBE]: "); 43 ds.reset();//Send RESET 44 ds.skip();//Send ROM Command-Skip ROM 45 ds.write(0xBE);//Send Function command- Read Scratchpad 46 for (int i = 0; i < 9; i++) {// we need 9 bytes 47 scratchpad[i] = ds.read(); 48 } 49 for (int i = 0; i < 9; i++) {// we need 9 bytes 50 Serial.print(scratchpad[i],HEX); 51 Serial.print(" "); 52 } 53 Serial.print(" CrcCalculated="); 54 Serial.print(OneWire::crc8(scratchpad, 8),HEX); 55 Serial.println(); 56 57 if(OneWire::crc8(scratchpad, 8) == scratchpad[8]) 58 { 59 int16_t temperature = makeWord(scratchpad[0],scratchpad[1]); 60 int16_t moisture = makeWord(scratchpad[2],scratchpad[3]); 61 int16_t conductivity = makeWord(scratchpad[4],scratchpad[5]); 62 Serial.print("TEMP(C) = "); Serial.print(temperature/100.00); 63 Serial.print(" MOISTURE(%) = "); Serial.print(moisture/100.00); 64 Serial.print(" CONDUCTIVITY(ms/cm) = "); Serial.print(conductivity/1000.00); 65 Serial.print(" CONFIG0 = "); Serial.print(scratchpad[6],HEX); 66 Serial.print(" CONFIG1 = "); Serial.print(scratchpad[7],HEX); 67 Serial.print(" CRC8 = "); Serial.print(scratchpad[8],HEX); 68 } 69 else 70 { 71 Serial.print("CRC ERROR!"); 72 } 73 Serial.print("\ 74\ 75"); 76 77 delay(1000); 78#endif 79}
Arduino Demo
c_cpp
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.
1#include <OneWire.h> 2 3#define TEST_MT05S_ONEWIRE_CONVERT_T 1 4 5// Define a 1-Wire signal pin (a 2.0K~5.1K pull-up resistor is necessary) 6// Change according to your wiring and connection. 7OneWire ds(2); 8 9void setup(void) { 10 Serial.begin(9600); 11} 12 13void loop(void) { 14 Serial.println("//-------------------------"); 15 Serial.println("// Start Testing MT05S "); 16 Serial.println("//-------------------------"); 17 uint8_t i; 18 19#ifdef TEST_MT05S_ONEWIRE_CONVERT_T 20 //-------------------------------------------- 21 // Tested - Convert T and Read Scratchpad 22 // Attention: 23 //-------------------------------------------- 24 Serial.print("Test Function Command- Convert T [0x44] & Read Scratchpad [0xBE]: \ 25"); 26 byte scratchpad[9]; 27 28 Serial.print("Convert T [0x44]: "); 29 ds.reset();//Send RESET 30 ds.skip();//Send ROM Command-Skip ROM 31 ds.write(0x44);//Send Function command- convert T 32 33 //Wait conversion done (Whether to pull down the DQ line during operation can be configured in scratchpad CONFIG0-Bit3- SensorPowerMode) 34 while(ds.read_bit() == 0) 35 { 36 delay(10); 37 Serial.print("."); 38 } 39 Serial.print("Conversion Done\ 40"); 41 42 Serial.print("Read Scratchpad [0xBE]: "); 43 ds.reset();//Send RESET 44 ds.skip();//Send ROM Command-Skip ROM 45 ds.write(0xBE);//Send Function command- Read Scratchpad 46 for (int i = 0; i < 9; i++) {// we need 9 bytes 47 scratchpad[i] = ds.read(); 48 } 49 for (int i = 0; i < 9; i++) {// we need 9 bytes 50 Serial.print(scratchpad[i],HEX); 51 Serial.print(" "); 52 } 53 Serial.print(" CrcCalculated="); 54 Serial.print(OneWire::crc8(scratchpad, 8),HEX); 55 Serial.println(); 56 57 if(OneWire::crc8(scratchpad, 8) == scratchpad[8]) 58 { 59 int16_t temperature = makeWord(scratchpad[0],scratchpad[1]); 60 int16_t moisture = makeWord(scratchpad[2],scratchpad[3]); 61 int16_t conductivity = makeWord(scratchpad[4],scratchpad[5]); 62 Serial.print("TEMP(C) = "); Serial.print(temperature/100.00); 63 Serial.print(" MOISTURE(%) = "); Serial.print(moisture/100.00); 64 Serial.print(" CONDUCTIVITY(ms/cm) = "); Serial.print(conductivity/1000.00); 65 Serial.print(" CONFIG0 = "); Serial.print(scratchpad[6],HEX); 66 Serial.print(" CONFIG1 = "); Serial.print(scratchpad[7],HEX); 67 Serial.print(" CRC8 = "); Serial.print(scratchpad[8],HEX); 68 } 69 else 70 { 71 Serial.print("CRC ERROR!"); 72 } 73 Serial.print("\ 74\ 75"); 76 77 delay(1000); 78#endif 79}
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.
1#!/usr/bin/python3 2import os,time 3 4ROMCODE='28-060504030201' 5device_file ='/sys/bus/w1/devices/'+ROMCODE+'/w1_slave' 6print("//----------------------------------------------------------------"); 7print("// Start Testing MT05S"); 8print("// Rom Code= " + ROMCODE); 9print("// Change ROM CODE FOR YOUR SENSOR !!!!!!"); 10print("//----------------------------------------------------------------"); 11 12def read_raw(): 13 f = open(device_file,'r') 14 lines = f.readlines() 15 f.close() 16 return lines 17 18def read_mt05s(): 19 lines = read_raw() 20 while lines[0].strip()[-3:] != 'YES': 21 print('Incorrect Data') 22 time.sleep(0.2) 23 lines = read_raw() 24 byteList = lines[1].rsplit((' ')) 25 if len(byteList) >9 : 26 soilTempHi8=int(byteList[0], 16) 27 soilTempLo8=int(byteList[1], 16) 28 soilTemp = soilTempHi8*256+soilTempLo8 29 30 soilMoistureHi8=int(byteList[2], 16) 31 soilMoistureLo8=int(byteList[3], 16) 32 soilMoisture = soilMoistureHi8*256+soilMoistureLo8 33 34 soilECHi8=int(byteList[4], 16) 35 soilECLo8=int(byteList[5], 16) 36 soilEC = soilECHi8*256+soilECLo8 37 return [ soilTemp, soilMoisture, soilEC ] 38 39while True: 40 values = read_mt05s() 41 print('Soil Temp(C)= %.2f Moisture(%%)= %.2f EC(ms/cm)= %.3f' %(values[0]/100.0, values[1]/100.0, values[2]/1000.0)) 42 time.sleep(1)
Downloadable files
Application Note for Wiring, Coding and demostration
https://github.com/INFWIN/mt05s-demo
Application Note for Wiring, Coding and demostration
https://github.com/INFWIN/mt05s-demo
Comments
Only logged in users can leave comments