Micro:bit Picosat Series - Sensor Module #1 - Humidity Sensor

Introduction

The humidity sensor is an important sensor part of the weather monitoring assemble. It works by variation of resistance in the circuit as the moisture in the air content varies. As the water content increases, the resitance begins to decrease. As the water content decreases, the resistance increases.

The humidity sensor is linked the temperature as well. As the temperature increases when given a specific relative humidity, the resistance will also decrease.

Method

  1. Attach the Micro:bit and the sensor board to the Carrier board as shown.

  1. Using the USB cable, connect the Micro:bit to your computer.

  2. Startup Google Chrome and go to the website https://makecode.microbit.org/

  3. Start a New Project with the New Project button.

  4. Select Javascript in top center menu.

  5. Delete all the javascript code visible on the screen.

  6. Copy and paste the following javascript into the text area.

    let H = 0

    let rho = 0

    let T = input.temperature()

    let logH = 0

    let logR = 0

    let R = 0

    let R1 = 150000

    let coeffC = 0

    let coeffB = 0

    let coeffA = 0

    basic.forever(function () {

    basic.pause(500); //Limit the sensor reads

    T = input.temperature() - 6 //capture on board temperature and reduce by a value

    coeffA = -0.0000 * T - 0.0052 //coefficient A for equation

    coeffB = -0.0013 * T - 0.0209 //coefficient B for equation

    coeffC = 0.0061 * T + 5.0600 // cofficient C for equation

    rho = pins.analogReadPin(AnalogPin.P1) / 1023 //find the sensor voltage proportional to input voltage

    R = R1 * rho / (1 - rho) // calculate the resistance of the humidity sensor

    logR = Math.log(R); // convert the resitance to a log value to work with the generalise log equation

    logH = coeffA * logR ** 2 + coeffB * logR + coeffC //equation to determine humidity in log space

    H = Math.round(10 * Math.exp(logH)) / 10 //convert the log space value of humidity back to Humidity as percentage.

    serial.writeValue("Temperature Onboard", T) // write the value of temperature from onboard temperature sensor

    serial.writeValue("Relative Humidity (%)", H) //write the value of humidity.

    })

  7. Changethe file name from “Untitled” to “HumiditySensing”.

  8. Press the download button to automatically transfer the file to the connected Micro:bit.

  9. When the download is complete, the button for “Show Console Device” may appear. If so, click on it to display the relative humidity on screen.

  10. To see the effects of humidity rapidly change, try grab a cup of warm water or hot water. Place the sensor across the top as the steam rises from the cup. You should see the humidity levels rise over time.

Display of humidity graph increasing when placed over a cup of hot water. The steam from the water rises to contact the humidity sensor.

Display of humidity graph increasing when placed over a cup of hot water. The steam from the water rises to contact the humidity sensor.

Explanation

What does code do?

The code has to perform several calculations in order to return a human understandable value.

Firstly, we have to determine an equation from source data, that has been measured and test by the manufacturer of the humidity sensor. This can be found in the following datasheet. http://www.farnell.com/datasheets/2865737.pdf

Table of measured resistances vs the relative humidity.

Table of measured resistances vs the relative humidity.

In Excel, its possible to plot these datapoints into a graph and on the graph, generate a curve that lines up with the points. This is called curve fitting. The curve that is produced can be approximated by an equation. The less linear or “straight line” the curve appeasrs the be, the more complex the equation becomes. In our case, our curve follows an exponential decay.

When we plot each range of temperatures with their given humidity rating, we see eleven curves. This would be very annoying to have eleven sets of equations to write into the Micro:bit. So, using some maths, it’s possible to generate a combine equation that takes in the ambient temperature and the return value from the resistor, and create a unified equation that can provide the humidity across almost any temperature.

Graphing all the data points in log space.

Graphing all the data points in log space.

Patrick Wang