Example Code for MakeCode of Water Temperature

The article provides example code for measuring and controlling water temperature using MakeCode, explains the principle of water temperature sensors, and includes sample code for converting temperature from Celsius to Fahrenheit, offering practical insights for projects involving temperature measurement.

Principle

What occasions do we need to measure and control water temperature?
In our daily life, we can come into contact with water at many different temperatures.
For example, tea, coffee, bath, swimming and so on, involving equipment including heaters, water dispensers, water heaters, temperature controllers and so on. So how do we detect and control the water temperature? First of all, let's get to know the water temperature sensor.

Preparation

Get to know the water temperature sensor
The water temperature sensor consists of a sealed metal housing and an internal temperature sensor, as shown below: when we put the water temperature sensor in the water, the temperature of the water is transmitted to the internal temperature sensor through the metal thermal conductor, which causes the sensor value to change. That’s how the water temperature sensor works.

For more information, refer to the Getting Started guide.

Sample Code 1

Like the projects above, we can also write a program in a very simple way to measure the temperature of water:

Sample Code 2

In the first chapter, we measured the temperature at a Celsius, and there is a "Fahrenheit" that is also a widely used unit of temperature measurement. Let's take a look and make the corresponding conversion.
First, look at the definition of two units of temperature measurement.

Celsius: the temperature of the ice water mixture is set to 0 degrees C, the temperature of boiling water is set to 100 degrees C, and the two are divided into 100 equal parts, each of which is a unit of Celsius, called 1 degree Celsius.

Fahrenheit: at standard atmospheric pressure, the melting point of ice is 32 degrees F, the boiling point of water is 212 degrees F, the middle is 180 equal points, each equal unit is 1-degree Fahrenheit.

Formula to convert °C to °F:
Fahrenheit = Celsius x 1.8 + 32°C

Use the conversion formula to convert the measured temperature value to Fahrenheit:

Result 2

In this program, we use mathematical operators to convert the value of Celsius to a value of Fahrenheit, and we use two new program modules: truncation and combining strings.

Truncation: truncate the fraction already obtained values, only leaving integers.

Combining strings: combining two strings to show on one line.

In the end, the display shown the result as below:

Conclusion

Through the learning above, we have learned the following:

  • Water heaters, smart water cups, water dispensers and other occasions need to measure water temperature and control water temperature.
  • Knew about the water temperature sensor and the principle.
  • The water temperature was measured by graphical programming.
  • Learned the conversion of degrees Celsius Fahrenheit.

Was this article helpful?

TOP