# (502) New (better) Soil Moisture Sensors and RS485 Modbus on ESP32 Arduino

## Метаданные

- **Канал:** Andreas Spiess
- **YouTube:** https://www.youtube.com/watch?v=tDR09WTAcpA

## Содержание

### [0:00](https://www.youtube.com/watch?v=tDR09WTAcpA) Segment 1 (00:00 - 05:00)

soil moisture sensors are essential for successful working with plants in video number 207 I showed some of the most common sensors some of them were bad and some were okay in the meantime I discovered a new sensor type that promises accuracy and more data these sensors are also waterproof and can be buried so let's check them out we will also learn about the rs485 and modbus interface and how to connect such sensors to mqtt and home assistant grey YouTubers here is the guy with a Swiss accent bringing you a new episode with fresh ideas around sensors and microcontrollers remember if you subscribe you'll always sit in the first row if you search YouTube you will find many videos about the cheap PCB sensors but hardly any videos about this new kind of sensor this is why we will look at the different sensors you get and the parameters they measure look at the different transmission standards the sensors offer look at how the rs485 and the modbo standard work and how it can be read by an Arduino sketch that transmits it via mqtt we will also cover two different rs485 interface ports and show which one to choose look at how the same sensor could be built with ESP home do some testing with the sensors to get an idea of their quality on AliExpress you find several such sensors none of them has a proper brand on the sensor itself they seem to be the same and the vendors only use differently colored stickers they are also not well documented only one vendor Comin top provides data sheets and even test and setup programs for Windows we will later see other indications that they are all the same because each vendor sells many sensors we have to bring an order into the offerings you get sensors that deliver analog voltages a 4 to 20 milliamp Loop or an rs485 interface and you get simple sensors that only measure temperature and moisture up to quite elaborate ones that add conductivity pH and NPK is the abbreviation for nitrogen phosphorus and potassium the three main components of fertilizers you might ask why it is called NPK and not NPP the chemical symbol for potassium is K like NPK seems to be the de facto standard for all kinds of fertilizers current Loops are used for sensors if you want to bridge long distances why would you use current and not voltage long cables can cause a voltage drop along the wires which induces errors current on the other hand is always the same along the wires if we introduce a resistor close to the ADC we do not have any voltage loss cool idea but not for me because I do not want to use one of the not so good adcs of the esp32 high currents of 4 to 20 milliamp are used to create some immunity against external influences by the way whether you use an analog interface or rs485 depends on what your measuring instruments accepts the number of values your sensor offers and the number of sensors you want to connect in parallel the expected results should be the same you can connect up to 32 devices on one rs485 connection this is why such sensors have an address I only bought rs485 sensors this standard is also made for long wires in harsh environments it uses a differential signal that is not referenced to ground twisted pair cables are usually used to reduce the influence of external Fields common differential voltages are plus - 1. 5 to plus - 5 volt and the protocol is the same as for rs232 rs485 is often used with the modbus rtu standard data is held in registers sometimes called coils for discrete outputs these registers can be read and written by the master device the slave device is passive and waits till the master pulls it because of the only two wires it uses a half duplex communication our sensors start to work

### [5:00](https://www.youtube.com/watch?v=tDR09WTAcpA&t=300s) Segment 2 (05:00 - 10:00)

at 4. 5 volts so I use 5vt and we will get about plusus 2 volts on the data cables this is a problem if we want to use an esp32 with its 3. 3 volt pins how can we solve this issue the easiest way is to buy an rs485 to TTL converter module most of them use a Max 485 chip to do the interface because the sensors use half duplex the max 485 has to switch directions between transmission and reception a fact we must consider in the future and because the max 485 also runs at 5vt I use a level converter module to adapt the 5vt signals to the 3. 3 volt signals requested and provided by the esp32 unfortunately I only have rs485 to USB modules perfect to connect sensors to a PC or Raspberry Pi but less than ideal for the task at hand as a maker we always find a way these boards have jumpers to disconnect the signals between the ftdi chip and the max 485 like that it is easy to connect ground 5vt RX and TX directly to the max 485 unfortunately the problem was not entirely solved because the sensors did not work no signals were produced at the interface wires further investigations revealed that the ftdi chip produces a signal to switch the direction of the max 485 to transmit this signal has no chumper and therefore is lacking if we do not use USB fortunately this signal can be inserted by soldering a wire to pins two and three these two pins are anyway connected now we are ready to rumble I thought I used ESP home to connect the sensors to home assistant however the developers were brighter than me and choose a proper rs485 to TTL board for that purpose as the ftdi chip that board also creates a switching signal and their yel definition does not include the signal needed for my hacked board so if you want to use your sensors with ESB home you have to buy the proper adapter board you will later see that I was happy with my USB board because it solved another problem because of this situation I decided to use the Arduino IDE to connect the sensors and use mqtt including autodiscovery to integrate it into home assistant with mqtt you can also incorporate in other platforms frequent viewers know that I stopped to program myself so I asked CH GPT to write the code for my temperature and moisture sensor this time I went a step further I asked it to write a code for one of my sensors and included a simple link to the sensor data sheet this little Wonder saw that it has to use the mod poos Library adjusted the addresses the registers as well as the names and the multiplication factors I got proper results on the first run in incredible if you ask me adding the ESP home interface with autodiscovery created a few discussions but because of my video number 495 I knew that it is easiest done using the Arduino Jason Library this is what I told chat GPT the following sensor provides seven parameters this is why I gave chat GPT the task of extending the first sketch and writing a second one including all seven parameters of course I attached its data sheet and really I got a second sketch for the more complex sensor I now have two esps connected to two sensors because I was unsure if the same Source manufactured all the sensors I purchased the second one for temperature and moisture after connecting it I discovered that it uses exactly the same registers a strong indication that it is the same sensor just with a differently colored sticker but at least we now have three of the new sensors to compare I added this me Flora BL sensor to the comparison it uses the capacitive method after this discovery I ask jpt to add the purple sensor with address two like that I could attach both sensors in parallel to the interface after all my measurements were done I changed the address of the NPK sensor to three and ask J GPT again

### [10:00](https://www.youtube.com/watch?v=tDR09WTAcpA&t=600s) Segment 3 (10:00 - 15:00)

to integrate this different sensor into one sketch you can find the resulting sketch on my gtop by the way I also tried deep seek with similar results this incredible speed up of programming created a lot of Joy on my side I could concentrate on describing the problem not programming the nitty-gritty by the way I changed the sensors address using the PC s software provided by one of the suppliers and here I was happy I had an rs485 to USB converter I will include all manuals and software on my GitHub that you do not need to search for them unfortunately I found no information about whether the new sensors use time domain reflectometry because of their low price I assume they are capacitive sensors maybe you know more let's start the test I purchased standard Swiss soil with some data marked on the package so we have an indication if our instruments measure correctly of course this is a natural product and I assume there are variations in these values this soil should have a pH of 7. 4 which is a bit above neutral in the same table we find the NPK values from before Unfortunately they are in milligram per l our sensors should measure milligram per kilogram dry soil is relatively light so its weight varies with the water content but it will always be below 1 kg per liter this is the conversion formula because our material is lighter than water the measured values should be smaller than those written on the package for comparison I purchased this package for plants that love acid soil this is why it has a pH of 5. 8 so our sensor should show a clear difference between the two soils otherwise it would be useless to start the tests I dried some of the soil in our oven in the kitchen for a few hours of course when my wife was out this pot now contains extremely dry soil you see our setup neatly displays all values in home assistant cool here are the results for moisture the two blue and showed 4 and 4. 8% however the fluctuation of the values was around 1% the miora was also quite close the purple sensor showed 0% no pH or NPK values were measured so let's check the Other Extreme a glass of water fortunately we can submerge all new sensors because they are waterproof after a while all sensors except the me FL AA showed the expected 100% next I weighted a pot of dry soil it is 1. 2 kilog to reach 25% humidity I mix 3 dcil of water with the soil as before I press the soil by hand to compact it this mix is already quite wet not being a gardener I assume plants would quickly grow in such soil now NPK as well as conductivity start to show values because I'm no chemist I have to ask chat GPT it shows these relations so my soil should have 85 mgram of phosphorus and 705 mgram of potassium per liter nitrogen is contained in its Elemental form as said before my soil is lighter than water so nitrogen should show around 100 mgram phosphorus about 70 and potassium about 600 mgram the measured values are disappointingly low it seems the manufacturer knows about this and therefore prints a warning that the NPK values are just indicative however we have already learned that these values change with moist levels if we go on with moisture we see that the two blue sensors are right at the point the purple one is a bit High and the mlora is a bit low the mlora also shows a fertility value of 294 I assume this contains something like the NPK value to check if the sensors are influenced by temperature I used original soil which was stored outside its temperature is 6° the moist values seem okay and the three sensors show similar values however the NPK values are not measured at all and the pH is too low this

### [15:00](https://www.youtube.com/watch?v=tDR09WTAcpA&t=900s) Segment 4 (15:00 - 18:00)

fact does not change with temperature increase it seems that below around 25% moisture the NPK sensors do not work the myora shows a too low moisture and the fertility value also dropped significantly looking at the temperature we see that the mlora measures room temperature not soil temperature next I add water to increase moisture to 50% the measured moisture value are okay for the three new sensors the Mi Flora is too low I have to admit that it is not easy to get a completely even mix of the soil the values are pretty stable over time but depend on where you place your sensors the soil I use is a mix of different materials and includes some small stones the NPK values are now higher so we can confirm that they change with a moisture level let's prove that fact by pouring even more water into the soil now the soil is completely soaked the two simple sensors show nearly 100% moisture the NPK sensor shows 94 and the Mi Flora 90% for the first time we see a value for nitrogen and the NPK values again increased a lot now it's clear that the moisture level has a massive influence on these values the pH is still too low and probably has to be adjusted Ed in software as said before I have bought a second type of soil that should have a much lower pH let's check it out with a moist level of around 60% the pH is definitely lower than before and the NPK values are higher than with the other soil so the pH measurement is at least usable what did we learn rs485 sensors can be connected to our typical development ports using a converter with a Max 485 chip because it is 5vt we have to use a level converter for 3. 3 volt devices these sensors use the modbus rtu protocol which allows the connection of up to 32 devices in parallel rs485 cables can Bridge more than 1 kilometer because such cables could be expensive we used Wi-Fi for the connection with ch gpt's help the catches were quickly written it even included autodiscovery for home assistant these new sensors measure moisture and salt temperature pretty accurately the ph and the mpk values are more indications they vary a lot with moisture level and are not measured below 25% moisture however the pH for the acid soil was lower than the one for alkaline soil the values take a few minutes to stabilize after that they are pretty stable over time the Mi Flora sensor followed the measurements of the other sensors however its moist levels were too low still it is a good choice for our pot plants because it delivers a good signal for watering and also the fertility value can indicate that you need to add fertilizer the price of around $20 for the simple and $40 for the NPK sensors is a reasonable price for the accuracy and because they are waterproof and have stainless steel probes they can be buried into the ground to measure where it matters most the rods are fragile so pay attention with stones that was all for today as always you find all the relevant links in the description if you found this video useful or interesting please support the channel thanks for watching and see you in the next video bye

---
*Источник: https://ekstraktznaniy.ru/video/39248*