I finally got my hands on a temperature / humidity sensor for the Gadgeteer board. This means I can continue the quest for building a smart thermostat.
In this blog part I’ll show you how to attach a sensor module and read the values from the sensor.
Attaching the sensor module
We start of from where we’ve left the previous blog (IoT - Gadgeteer - Getting started)
We open the toolbox and search for the ‘TempHumid SI70’.
Drag the object from the toolbox into the Program.Gadgeteer workplace.
Again we have several options to attach the sensor.
When we select the sensor object on the workplace the mainboard will highlight the possible sockets in green to which the sensor can be attached.
You can just drag a link from the sensor to a highlighted socket on the mainboard and attach the sensor. You can also open the context menu on the sensor object by right clicking it. Next you can select ‘Connect all modules’ and let the application decide for itself where it attaches the sensor.
After a connection is created please make sure you connect the sensor to the main board identically.
In the solution explorer expand the ‘Program.Gadgeteer’ icon and open the ‘Program.generated.cs’ class.
Here you can see the generated code which creates and initializes the temperature sensor. The parameter on initialization defines the port which the module is attached to. So in this case it says ‘5’:
this.tempHumidSI70 = new GTM.GHIElectronics.TempHumidSI70(5);
You can change the name of the sensor to your liking but to keep everything clear I’ll keep the naming as is.
Close the ‘Program.generated.cs’ class as we don’t need this anymore.
Reading the sensor values
Open the ‘Program.cs’ class.
Before we can use the sensor we need to add a reference to the GHIElectronics class.
Insert the following using at the top of the class:
using Modules = Gadgeteer.Modules.GHIElectronics;
Next all we have to do to read the sensor is calling the sensor’s ‘TakeMeasurement()’ function like so:
var measurement = tempHumidSI70.TakeMeasurement();
The TakeMeasurement function returns a ‘Measurement’ object which contains the following values:
- double Temperature, the measured temperature in degrees Celsius.
- double TemperatureFahrenheit, the measured temperature in degrees Fahrenheit.
- double RelativeHumidity, the relative humidity **.
In the ‘OnMainTimerTick’ function, which we used earlier in our previous blog. We add the following code to reader our sensor data and write it to the Output window.
Tip: You can use the keyboard combination ‘ALT’ + ‘248’ to insert a ‘Degrees’ symbol.
var measurement =tempHumidSI70.TakeMeasurement();
When we run this code our output windows will show the measurement values:
It’s nice to see the huge amount of decimals returned. This was we can measure the changes in temperature and humidity fairly precise later on.
I also noticed that the Systems DateTime property is returning incorrect values. We might want to fix that later on. For now it’s no problem but we might want to run into problems when we further enhance the application.
To conclude; adding a sensor module to the Gadgeteer board is fairly easy. You just search the module in the toolbox and drag it onto the workplace. Next you attach the module to an empty slot. When you then reference the correct namespace you can simply read the values of the sensor by calling it’s appropriate functions.
** : Relative humidity (abbreviated RH) is the ratio of the partial pressure of water vapor to the equilibrium vapor pressure of water at the same temperature. Relative humidity depends on temperature and the pressure of the system of interest.