Example Code for MakeCode of Soil Moisture

Last revision 2026/01/27

The article explains how to use MakeCode and soil moisture sensors to monitor and maintain optimal plant hydration levels, emphasizing the importance of water in plant health, the role of forests, and photosynthesis in environmental sustainability.

Principle

1.Forest Knowledge
On earth, there are a lot of forests. Global forests are concentrated in South America, Russia, Central Africa and Southeast Asia. These four regions take up 60% of the world's forests, with Russia, Brazil, Indonesia and Congo’s accounting for 40% of the world's forests.

Total global forest cover is 32%, among them, North America takes 34%, South America and Europe are around 30%, Asia 15%, the Pacific 10% and Africa only 6%. The continent with the largest forests is Latin America, which accounts for 24% of the world's forest edified area and reaches 44% of forest cover. These forests play an important role in regulating the climate, preventing desert invasion and protect the natural ecology.

The United Nations Environment Office reports that global forests have been halved in history, mainly due to human activity. According to a report in 2001 by the United Nations Food and Agriculture Organization, global forests fell from 3.96 billion hectares in 1990 to 3.87 billion hectares in 2000. Nearly 10 million hectares of forest are lost each year worldwide.
The role of plants in protecting the living environment of human beings.

  • Beautifying the environment
    Many plants have high aesthetic value, especially garden plants. The graceful posture and rich colors of garden plants beautify the city's landscape and play a good role in beautifying the environment.
  • Protecting and improving the environment
    Plant leaves can be photosynthesis, absorbing carbon dioxide in the air, releasing oxygen, so that the air turns fresher. Tall plant canopy can effectively block dust in the air, hairy leaf surface can absorb dust and absorb harmful substances in the air, such as sulfur dioxide, hydrogen fluoride, chlorine gas and so on.
    Regulating and improving the environmental microclimate
    Tall plants have good shade effect, plant leaves can increase the humidity in the air, and reduce the air temperature by heat absorption, effective regulate and improve the environment microclimate. Some plants can also secrete a large amount of fungicides, effectively killing harmful bacteria in the air. Plants can also be effective in blocking noise. The root system of plants can effectively fix the soil and prevent soil erosion.
  • Photosynthesis of plants
    The process by which green plants use the sun's light energy to assimilate carbon dioxide (CO2) and water (H2O) to make organic matter and release oxygen, called photosynthesis. The organic matter produced by photosynthesis is mainly carbohydrates and releases energy.

    2.Effects of the environment on plants
    Water, sunlight, temperature, humidity, atmospheric pressure will have an impact on the shape and survival of plants, of which the water content has the greatest impact.
    Moisture is an important part of plants. The average plant contains 60% to 80% water, or even more than 90%. Without water, the plants cannot take actions like absorption, transportation, as well as photosynthesis and breathing.
    For example, the main characteristics of plants that can survive in desert environments are drought-resistant. The general desert plant leaves are relatively small in order to reduce the water loss caused by the role of steaming, mainly represented by the fairy ball, stick tree, aloe vera and so on.
    In the rainforest, due to the abundance of rain, abundant water, a large number of broad-leaved plants, such as banana, eucalyptus and so on.

Preparation

Plant moisture measurement
Through learning, we knew the importance of water to plants. Then, in the process of farming, we need to pay attention to the water situation of plants to prevent water shortage nor too much water which will cause plant roots to be flooded.
Let's use the soil moisture sensor in the kit to learn how to measure soil moisture. The soil moisture sensor is shown below:

Connecting the soil sensor:

Calibration


Download the program to micro: bit, according to the figure shown as below to obtain and record the reading in dry and in water. My data is 841 in dry, 543 in the water.

Then modify the program and put the two calibration values in the previous dry and water to the program. In the program, we map 543 to 10, 841 to 0. In the actual test, the humidity is 0 when reaches the lowest, and the highest humidity is 10.

Sample Code

Result

After downloading the program, insert the soil moisture sensor to the flower pot soil to be tested, it shows that the humidity value of the current soil is between 0 and 10.
According to the water requirements of different plants, we can make a preliminary judgment on the suitability of humidity.

Conclusion

Through the learning above, we have learned the following:

  • We learned about the forest cover in different regions and the importance of plants to the environment.
  • Learned about the photosynthesis of plants.
  • Understand the environmental factors required for plant growth.
  • Learned how to use soil moisture sensors to detect soil moisture.

Was this article helpful?

TOP