Sample Code for MakeCode - Temperature and Humidity

Last revision 2026/01/23

This article provides an in-depth guide on using the micro:bit environment expansion board to explore natural science through sensors measuring environmental data such as temperature, humidity, and more. It emphasizes the importance of environmental protection and offers programming examples for measuring and understanding these data points.

Principle

The Comfortable Degree Evaluation of Environment: Temperature and Humidity

1.How Temp & Humidity affect the environmental comfort degree?
Among many meteorological elements, temperature and humidity are the biggest impacts on human health and comfort.

Generally, the human body feel comfortable when the temperature between 18-28 degrees and the humidity between 50%-60%. The ultra-low humidity will lead to excessive drying, one is easy to produce static electricity and increase the density of dust easily, so 50%-60% humidity is appropriate.

So, what affects the temperature and humidity of the environment? How can we measure temperature and humidity?

  • Plants: Plants can evaporate water to improve the ambient air humidity, and absorb the sun's light energy to reduce the ambient temperature, so in the summer heat, dense forest temperature is relatively lower.

  • Environment: Melting glaciers, air pollution all greatly affect and the environment temperature and humidity.

2.How to measure Temp & Humidity?
It’s too hard to control temp and humidity in the atmosphere, but it will not be hard to control the indoor temp and humidity with equipment such as air conditioners, humidifiers, electric fans and so on. Now, let’s get to know the units and measurements of temperature and humidity.

Unit and Measurement

  • Units: Currently the most common temperature units are: Celsius (C) and Fahrenheit (F), and humidity units are percentages (% RH)
  • Measurement: The most common device for measuring temperature is a thermometer.
    In the age of the Internet of Things, we also use a device called a "temperature sensor" and a "humidity sensor" to measure temperature and humidity, and can display the measured data on a screen or to a computer for analysis. Smaller and easier to use.

Preparation


The sensor in the center of the cloud and sun can measure the temperture and humidity.

For more information, refer to the Getting Started guide.

Sample Code 1

According to the image below to program the code.

Result 1

The first line of the OLED display shows the temperature is 27.28°C and the second line shows the humidity is 58.42%RH.

Hereafter, let’s check which hardware is designed to monitor temperature and humidity. In the top of the board, a tiny component in the center of the cloud and sun is the one that measures temperature, humidity and atmospheric pressure (what is the atmospheric pressure? We will introduce it in the next part)

Try holding our fingers down to this sensor, as shown in the figure:

We will find that two data changes, especially the temperature, the change is very obvious, the temperature is constantly rising, because our body temperature is higher than the ambient temperature, by holding down the sensor, the body temperature is transmitted to the sensor.

Sample Code 2

Temperature Alarm: we'll do some functional extensions to the program, as shown in the figure. The buzzer starts to alert us when the temperature exceeds 30 degrees.

Result 2

The buzzer starts to alert us when the temperature exceeds 30 degrees.
Through the learning above, we have learned the following:
Learned the effects of temperature and humidity on human comfort.
Mastered the current temperature and humidity measurement methods.
We measured the temperature and humidity around us through programming, and actually experienced the sensor's measurement.

Was this article helpful?

TOP