Temperature Sensing and Management utilizing Raspberry Pi – Microsoft College Connection

Visitor weblog by  Charig Yang. I’m a second-year engineering scholar on the College of Oxford and Microsoft Pupil Associate.


I’m involved in digital and knowledge engineering, significantly in telecommunications and digital management techniques. I even have enthusiasm in training and science communication. I educate part-time as a tutor throughout time period time and organise instructional camps throughout my holidays. Outdoors teachers, I’m into canine and board video games.


On this venture, I constructed a mannequin for a temperature regulation system that can be utilized for small-scale temperature management. Admittedly the motor output might be too weak to have a excessive affect in non-trivial scale techniques, however the underlying rules are nonetheless the identical. A simplified flowchart of how issues work seems to be like this:


Simplified block diagram of the method

I intend to make this weblog beginner-friendly, beginning with what to do after getting the Pi, establishing, and the issues that could be encountered.

Half 1: Setting Up the Raspberry Pi

On this half I’ll cowl tips on how to setup the Raspberry Pi and begin utilizing it. To do that, you want a Raspberry Pi, an SD card (which ought to come together with your Pi) with an adapter, an influence provide linked via the micro-USB port (backside left), an Ethernet cable


Do word that if you’re utilizing wifi for web connection, you will want to test whether or not your model of Raspberry Pi has pre-installed wifi functionality (Raspberry Pi three), if not you will want a wifi dongle. Watch out right here as not all wifi dongles are supported within the IoT Core. Test [https://docs.microsoft.com/en-us/home windows/iot-core/learn-about-hardware/hardwarecompatlist] before you purchase one!

I bought mine from [https://thepihut.com/merchandise/official-raspberry-pi-wifi-adapter].


– Obtain Home windows 10 IoT Core Dashboard right here: https://developer.microsoft.com/en-us/home windows/iot/Downloads.htm

– You will note this web page, click on on Arrange a brand new gadget

– Fill in your gadget title and password, ensure you get the Gadget sort proper, then click on on Obtain and Set up


– The set up ought to take some time, however can be prepared in a bit. As soon as it’s prepared, plug in your SD card into the slot on the left facet, from beneath the board.

– Optionally, chances are you’ll wish to join your Pi in the same method to a pc. (With out this you may nonetheless management your Pi via your PC, which can be illustrated shortly)


The additional connection to the appropriate are the mouse and keyboard. The small factor mendacity on high is the Wifi dongle (however I’m utilizing Ethernet right here, so that isn’t wanted). Beneath is a connection to a monitor display screen through a HDMI(digital, left)-to-VGA(analog, proper) converter. The Pi solely has a digital output (left), so you will want a converter in case your display screen is analog (proper). I bought mine from [https://thepihut.com/merchandise/raspberry-pi-hdmi-to-vga-convertor]


– Right here’s a totally set-up Raspberry Pi utilizing IoT Core – it capabilities similar to a pc! The image reveals the Pi enjoying a Youtube video.


– You may as well entry the Pi out of your pc through the use of the Home windows Gadget Portal. As soon as linked, re-open the IoT Core Dashboard (the one that’s used for IoT Core set up). Below My Gadgets tab (which was empty), now you can open the Home windows Gadget Portal


With the Gadget Portal, you are able to do a number of stuff out of your PC resembling Seize Screenshot (beneath Gadget Settings)


Run Command beneath the Processes tab (see the command checklist and what they do at [https://docs.microsoft.com/en-us/home windows/iot-core/manage-your-device/commandlineutils])


And even run an app! That is accomplished beneath the Apps Supervisor part


Half 2: FEZ Hat and Setup

This time we can be utilizing a FEZ Hat (stands for Quick and Straightforward) to do the venture. It’s fairly handy as you may simply put the Hat on high of the Pi. The setup seems to be like this:


Raspberry Pi Set-up

Be aware the breadboard behind is unrelated, but it surely reveals how a temperature sensor, button, and LED individually, however they’re all built-in inside the FEZ Hat, so we’ll persist with that.

The connections to the Pi would possibly look quite a bit, however they’re issues which are very near us! Clockwise from the highest are

– a Wifi Dongle (Raspberry Pi 2 doesn’t include wifi connection, so this permits the Pi to connect with the web

– an extender that connects to a mouse and a keyboard

– an Ethernet cable that connects the Pi to the PC

– a connection to a monitor display screen through a HDMI(digital)-to-VGA(analog) converter. Raspberry Pi solely has a digital output, however my display screen is analog, so that’s wanted

– an influence provide

– a servo motor (which, in follow, might be changed by a fan, heater, or one thing of comparable nature)

Issues to notice:


The FEZ Hat needs to be plugged into the Pi, merely placing it on high of the Pi, whereas it really seems to be safe, doesn’t join them. Additionally ensure you plug into the appropriate socket, it’s fairly simple to overlook it by one pin, and doing so will brief the Pi.


Additionally, the wires of the servo are linked this fashion, with constructive at crimson, detrimental at black, and sign at orange/yellow. Another servos could use different colors for sign however constructive and detrimental ought to at all times be crimson and black.

Half three: Utilizing FEZ Hat’s Temperature Sensor

Earlier than we start, it’s essential to have Microsoft Visible Studio downloaded, it may be downloaded through this hyperlink [https://www.visualstudio.com/downloads]

To create a brand new venture, comply with the directions within the picture beneath, word that it might be handy to set the title of the venture as FEZHATDemo in order that it matches the code, however the code might be edited if the title is totally different, not an enormous concern right here.


In Microsoft Visible Studio, there’s a Package deal by the FEZ Hat supplier known as GHIElectronics.UWP.Shields.FEZHAT which instructions the areas inside the Hat (in any other case we gained’t know the place the temperature sensor is!).

This may be put in through Nuget, proven beneath


The code that reads the standing of the FEZ Hat is pasted right here, it may be downloaded from http://outdated.ghielectronics.com/docs/329/fez-hat-developers-guide

MainPage.xaml.cs (C#)

utilizing System;

utilizing Home windows.UI.Xaml;

utilizing Home windows.UI.Xaml.Controls;

utilizing GIS = GHIElectronics.UWP.Shields;


namespace FEZHATDemo {

    public sealed partial class MainPage : Web page {

        personal GIS.FEZHAT hat;

        personal DispatcherTimer timer;

        personal bool subsequent;

        personal int i;


        public MainPage() 


        personal async void Setup() 


        personal void OnTick(object sender, object e) 




MainPage.xaml (Markup)

<;Web page x:Class="FEZHATDemo.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:native="utilizing:App1" xmlns:d="http://schemas.microsoft.com/expression/mix/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d">

    <;StackPanel Orientation="Horizontal" Background="">

        <;StackPanel Width="75">

            <;TextBlock Textual content="Gentle: " />

            <;TextBlock Textual content="Temp: " />

            <;TextBlock Textual content="Accel: " />

            <;TextBlock Textual content="Button 18: " />

            <;TextBlock Textual content="Button 22: " />

            <;TextBlock Textual content="Leds: " />

            <;TextBlock Textual content="Analog: " />



            <;TextBlock Identify="LightTextBox" />

            <;TextBlock Identify="TempTextBox" />

            <;TextBlock Identify="AccelTextBox" />

            <;TextBlock Identify="Button18TextBox" />

            <;TextBlock Identify="Button22TextBox" />

            <;TextBlock Identify="LedsTextBox" />

            <;TextBlock Identify="AnalogTextBox" />



<;/Web page>


Double click on the names in daring in your file and paste the entire code into that. Edit all entries after the phrase namespace to ensure they match

Depart the remainder of the code as it’s. We additionally need to configure the distant debugging within the Raspberry Pi with a purpose to ship the code to the Pi and run it there, directions beneath


We’re lastly able to go!


For those who join your Pi to a display screen it is best to see this system working on the display screen after clicking the Distant Machine button. In any other case it is best to be capable to see it working within the Home windows Gadget Portal, beneath the Apps Supervisor tab


It could be helpful to realize some perception to what the code is doing, contemplate this:

this.timer = new DispatcherTimer();

this.timer.Interval = TimeSpan.FromMilliseconds(100);

this.timer.Tick += this.OnTick;



This implies the timer ticks each 100 milliseconds (Zero.1 second)


personal void OnTick(object sender, object e)


    this.LightTextBox.Textual content = this.hat.GetLightLevel().ToString("P2", CultureInfo.InvariantCulture);

    this.TempTextBox.Textual content = this.hat.GetTemperature().ToString("N2", CultureInfo.InvariantCulture);

    this.Button18TextBox.Textual content = this.hat.IsDIO18Pressed().ToString();

    this.Button22TextBox.Textual content = this.hat.IsDIO22Pressed().ToString();

This reads the worth and reveals it on each tick

Be aware that the code just isn’t steady! It’s extracted for user-friendliness

Potential Errors and Fixes:

Be sure you

– Alter the time [https://github.com/amykatenicho/IoTWorkshop beneath Setting Date_Time on IoTCore.docx]

– Label all of the indexes appropriately, particularly the phrases after namespace in every code

– Be sure you linked the Pi to the PC (test via IoT Core Dashboard)

– Be sure you plugged within the Pi all the best way in

Apart: Temperature Sensors and Sign Conditioning

This isn’t associated to the principle venture, however just a few perception to how a temperature sensor works. You would possibly know thermistor is a resistor that modifications its resistance when it’s heated. The query stays: how then does it turns into a numerical studying of temperature? That is the way it begins, the circuit measures Vin and Vout and therefore can discover the worth of R and X


If we all know R, Vout, Vin, we will discover X. And if we evaluate it to reference worth of X (e.g. X can be this at this temperature) then we all know what temperature that is. This sounds simple in idea, however not mathematically. It’s doable to construct circuits that can be utilized configured to be sure that the temperature deviation is (largely) proportional to the change within the output voltage, and therefore Vout can be utilized straight.

After we all know the temperature we should ship the (analog) sign to wherever we would like. To do that, we have to amplify first (as a result of Vout is often in micro- to millivolts, if we simply wish to measure the extent there isn’t a want for an enormous voltage anyway) Nevertheless do word that the output voltage can’t be too small as there could be random noises, and we’ve got to be sure that the noise doesn’t alter the studying. Then we go it via a converter to make it a digital sign (zeros and ones). You possibly can learn on bridge circuits, instrumentation amplifiers and analog-to-digital converters if you’re on this.

Half four: The “I” in IoT – Connecting Information to Azure

This half issues sending the info acquired from the sensors to Azure IoT Hub. The directions are absolutely specified by this hyperlink [https://blogs.msdn.microsoft.com/uk_faculty_connection/2016/01/27/windows-10-iot-core-beginners-walkthrough-using-raspberry-pi-2-and-the-fez-hat/] beneath the part Ship telemetry information to the Azure IoT Hub. What we’ll focus on this part, nevertheless, is tips on how to switch information from the IoT Hub right into a desk format after we’ve got linked the info.

First, we have to create a storage account. Do keep in mind to make use of the present useful resource group that’s the similar one used for IoT Core


Then we create Stream Analytics, this may be seen as a bridge linking the info between the IoT Hub and Storage Account



We choose the enter because the IoT Hub (named Raspberrypi right here) and the output as Desk. Every of them needs to be configured individually.

The diagram reveals that we’re choosing information from [Raspberrypi] into [Table]

Do word that Stream Analytics is rather more succesful than this – it may well take a number of inputs (say in an engine: stress, temperature, velocity, and many others) and current them in a single desk. Not solely this protects time, but it surely additionally makes positive that the info factors are, to a big extent, synchronised.

For example, let’s say if we wish to management the temperature of a pc. We are able to activate the fan as soon as the temperature is excessive sufficient, however that’s just about all we will do. If we’ve got sufficient information, nevertheless, we could be a lot extra predictive than that. We’ll know in common how lengthy is required earlier than the pc will get to a sure temperature, and shield it defensively relatively than ready for it to exceed. In a shorter time period, we will additionally achieve perception to the speed of improve of the temperature in the mean time and guess when it’s going to attain essential degree.

As a facet word, we might have used the Occasion Hub as a substitute of IoT Hub up thus far, because the Occasion hub solely permits Azure to obtain information from the gadget, however not the opposite manner spherical. Something after this requires IoT Hub to ship the info to the gadget.

Up thus far, we must always have a desk of the info recordings, and for the following part we’ll concentrate on tips on how to use them.

Apart: The Servo Motor

Though realizing tips on how to management the servo is beneficial, in precise temperature sensing techniques we’ll as a substitute connect with one thing like a fan or a heater, so going into element concerning the servo won’t be very helpful.


The servo motor right here is designed to resemble a fan or a heater in precise follow, however I changed it with the motor for comfort. Be aware that in precise truth this motor could be a foul alternative, as it may well solely flip 180 levels (half a spherical) and maximally at 100 rotations per minute (which may hardly cool one thing down. That being mentioned, the rules are comparable. After we all know what to will we then ship the sign again to the FEZ Hat (utilizing the identical manner, see above).

The pseudocode is straightforward (keep in mind the motor doesn’t spin all the best way, however half a spherical)

Flip left all the best way;

Flip proper all the best way;



Do word that the precise precept behind controlling a servo motor is kind of fascinating, it may be learn right here: https://makezine.com/initiatives/control-a-servo-motor-without-programming/, however since it is extremely probably to get replaced by one thing else in follow it’s not of curiosity.

Half 5: The Controller

What’s of our curiosity, nevertheless, is when do we’ve got to change on the management mechanism. This part will discover strategies of doing that. Be aware that for this we’ll concentrate on #Zero and #1 (as a result of there isn’t quite a bit to do with the servo) however all the things else is feasible to implement.

#Zero Guide

Let’s assume the scenario to be a heater that switches the room off after it’s getting too scorching

Learn temperature sensor

If it's >;25 levels,

Swap LED on

That is extra of a handbook system the place it simply tells us that the temperature is excessive, after which does nothing to cease it. Not a foul alternative to make use of in sensors the place there’s human supervision, resembling fireplace alarms, however might have been extra computerized. See the following part on Implementation of the Controller for a way to do that.

#1 Computerized, One-way

Learn temperature sensor

If it's >;25.5 levels, and it was on

Swap off

Else, if it's <;24.5 levels, and it was off

Swap on

Else, do nothing

This seems like an excellent begin! Be aware that we averted using 25 levels precisely as we didn’t need the the heater to fickle on and off repeatedly. This works in easy techniques, however in precise engineering we’d want greater than that.

Different methods of controlling

#2 Two-way

Let’s assume now that we wish to maintain the temperature of a field that accommodates residing cells fixed at 5 levels and we’ve got each a heater and a cooler

Learn temperature sensor

If it's >;5.1 levels, 

Heater off cooler on

Else, if it's <;four.9 levels,

Heater on cooler off

Else, do nothing

This sounds okay, however what this lacks is the magnitude of how a lot we modify issues.

#three Proportional

Learn temperature sensor, let it's x

If x>;5, apply cooler degree proportional to (x-5)

If x<;5, apply heater degree proportional to (5-x)

This sounds fairly good, in a way that if immediately the cell is moved to a 20 levels room, it will get extra cooling than whether it is only a slight disturbance. The fixed of proportionality might be discovered experimentally by making an attempt what is nice. (Though in some delicate techniques doing random trials will not be a alternative, in that case correct calculation can be required.

Nevertheless, all three earlier circumstances has an unaddressed drawback, we change off the heating system at a sure temperature, however the warmth already launched will nonetheless make an impact in the direction of the temperature drop – so in a way we’re inefficient! (An analogy to this may be the automotive doesn’t cease after we launch the pedal, however goes a good distance). A much bigger drawback with that is that it’ll by no means attain equilibrium! As a result of if we warmth as much as the temperature we would like we then have some warmth left that can warmth it additional, similar for cooling.

#four Predictive

Let’s say we’re doing a cooling system

Learn temperature sensor, let it's x

Have a look at the temperature graph, draw a greatest match line strive to extrapolate the place it's going to attain 5 levels

Regularly scale back the warmth in order that it reaches 5 levels and cease there

*word extra correct manner of doing that is known as integral management

That is the primary use of our desk we tabulated! We couldn’t have accomplished this if we solely have the current values, however not any of the previous. The maths right here appears somewhat extra difficult.

A drawback of that is that it’ll clearly be slower to succeed in the specified temperature than the traditional case. This may very well be made higher through the use of one thing known as a differential controller.

Implementation of the Controller

The directions on tips on how to management the LED and the servo is at [https://blogs.msdn.microsoft.com/uk_faculty_connection/2016/01/27/windows-10-iot-core-beginners-walkthrough-using-raspberry-pi-2-and-the-fez-hat] beneath Ship instructions to your gadget. The recordsdata related to that weblog might be downloaded from [https://github.com/amykatenicho/IoTWorkshop]

Apart: Discrete and Steady Management

The schematic steps of what we’re doing right here seems to be like this

Learn temperature worth (analog) each T milliseconds

Convert it to digital, ship to pc

Let the pc determine what to do digitally

Convert it to analog, ship to gadget


Be aware that that is known as the discrete manner of doing it, as a result of the info we’ve got and the motion we execute occur each T milliseconds. Whereas that is near actual time, we’ve got another which is a steady management system, which works like this

Temperature worth has an analog output Vout

Move via a circuit that has inputs of Vout and the reference voltage (what we would like it to be), and an output of the voltage wanted to make the change

Ship that output to the gadget

Doing it in analog makes it somewhat tougher to regulate issues, as a result of we’ve got to make use of circuits as a substitute of coding, however permits us to essentially do it in actual time.

The circuit is known as the Proportional-Differential-Integral (PID) Controller and could be very extensively used within the manufacturing trade to maintain the extent of warmth, acidity, humidity and extra. It principally calculates the Error (present worth minus desired worth) and tries to mitigate this error via three other ways – which the mixed impact can mitigate the error rapidly and reliably.

Apart: Connecting a Motor

After a lot concept, we will really join the motor to the circuit! Join the bottom to the detrimental terminal of the battery, and PWM12 to the motor.


Abstract and Synthesis

I hope to indicate that the precept behind every step is definitely fairly easy, earlier than they arrive right into a program. To summarise, this system does

Fetch temperature information;

Ship it to the cloud;

Calculate what to do subsequent; (or maybe let the cloud do it!)

Ship the directions again; (if any)



Taking a look at it extra typically, we will resolve two issues with such a system, first is disturbance rejection, the place we wish to maintain a worth fixed and mitigate any modifications which are occurring. Second is much less simple, however we will do some monitoring with it, which signifies that we’ve got a graph of how briskly a temperature ought to change, and we would like the temperature to rise in keeping with that graph, not too quick or too gradual.

A doable extension to this venture is to think about the truth that we’ve got logged all the info and we will analyse that into some helpful relationship about temperature, for instance what’s the relationship between growing the velocity of the fan to the speed of change of temperature afterwards, or extra merely how lengthy does it take for temperature to rise to a sure degree, and whether or not or not that’s constant over an extended time frame.

We are able to additionally analyse the effectiveness of various fan speeds utilizing the info we’ve got. By evaluating the temperature profile, we will estimate the effectiveness of utilizing several types of controllers.

References and Helpful Hyperlinks

Transmitting Information from FEZ Hat to Azure: https://www.hackster.io/stepanb/fez-hat-windows-10-iot-core-and-azure-iot-hub-1172b5

FEZ Hat Schematic Diagram


Servo Motors


Information Switch from Pi to Azure


Needed recordsdata


Supply hyperlink

Add a Comment

Your email address will not be published. Required fields are marked *