Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Temperature Sensing and Control using Raspberry Pi

Guest blog by  Charig Yang. I am a second-year engineering student at the University of Oxford and Microsoft Student Partner.



I am interested in electronic and information engineering, particularly in telecommunications and digital control systems. I also have enthusiasm in education and science communication. I teach part-time as a tutor during term time and organise educational camps during my holidays. Outside academics, I am into dogs and board games.
https://www.linkedin.com/in/charig-yang-509945128

Introduction

In this project, I built a model for a temperature regulation system that can be used for small-scale temperature control. Admittedly the motor output can be too weak to have a high influence in non-trivial scale systems, but the underlying principles are still the same. A simplified flowchart of how things work looks like this:

Simplified block diagram of the process

I intend to make this blog beginner-friendly, starting with what to do after getting the Pi, setting up, and the problems that might be encountered.

Part 1: Setting Up the Raspberry Pi

In this part I will cover how to setup the Raspberry Pi and start using it. To do this, you need a Raspberry Pi, an SD card (which should come with your Pi) with an adapter, a power supply connected through the micro-USB port (bottom left), an Ethernet cable

Do note that if you are using wifi for internet connection, you will need to check whether your version of Raspberry Pi has pre-installed wifi capability (Raspberry Pi 3), if not you will need a wifi dongle. Be careful here as not all wifi dongles are supported in the Iot Core. Check [https://docs.microsoft.com/en-us/windows/iot-core/learn-about-hardware/hardwarecompatlist] before you buy one!

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

Instructions

- Download Windows 10 IoT Core Dashboard here: https://developer.microsoft.com/en-us/windows/iot/Downloads.htm

- You will see this page, click on Set up a new device

- Fill in your device name and password, make sure you get the Device type right, then click on Download and Install

- The installation should take a while, but will be ready in a bit. Once it is ready, plug in your SD card into the slot at the left side, from below the board.

- Optionally, you may want to connect your Pi in a similar manner to a computer. (Without this you can still control your Pi through your PC, which will be illustrated in a while)

The extra connection to the right are the mouse and keyboard. The small thing lying on top is the Wifi dongle (but I am using Ethernet here, so that is not needed). Below is a connection to a monitor screen via a HDMI(digital, left)-to-VGA(analog, right) converter. The Pi only has a digital output (left), so you will need a converter if your screen is analog (right). I got mine from [https://thepihut.com/products/raspberry-pi-hdmi-to-vga-convertor]

- Here’s a fully set-up Raspberry Pi using IoT Core - it functions just like a computer! The picture shows the Pi playing a Youtube video.

- You can also access the Pi from your computer by using the Windows Device Portal. Once connected, re-open the IoT Core Dashboard (the one that is used for IoT Core installation). Under My Devices tab (which was empty), you can now open the Windows Device Portal

With the Device Portal, you can do several stuff from your PC such as Capture Screenshot (under Device Settings)

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

Or even run an app! This is done under the Apps Manager section


Part 2: FEZ Hat and Hardware Setup

This time we will be using a Fez Hat (stands for Fast and Easy) to do the project. It is pretty convenient as you can just put the Hat on top of the Pi. The setup looks like this:

Raspberry Pi Set-up

Note the breadboard behind is unrelated, but it shows how a temperature sensor, button, and LED separately, but they are all built-in within the FEZ Hat, so we will stick to that.

The connections to the Pi might look a lot, but they are things that are very close to us! Clockwise from the top are

- a Wifi Dongle (Raspberry Pi 2 does not come with wifi connection, so this allows the Pi to connect to the internet

- 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 screen via a HDMI(digital)-to-VGA(analog) converter. Raspberry Pi only has a digital output, but my screen is analog, so that is needed

- a power supply

- a servo motor (which, in practice, can be replaced by a fan, heater, or something of similar nature)

Things to note:

The FEZ Hat has to be plugged into the Pi, simply putting it on top of the Pi, while it actually looks secure, does not connect them. Also make sure you plug into the right socket, it is quite easy to miss it by one pin, and doing so will short the Pi.

Also, the wires of the servo are connected this way, with positive at red, negative at black, and signal at orange/yellow. Some other servos may use other colours for signal but positive and negative should always be red and black.

Part 3: Using FEZ Hat’s Temperature Sensor

Before we begin, it is necessary to have Microsoft Visual Studio downloaded, it can be downloaded via this link [https://www.visualstudio.com/downloads]

To create a new project, follow the instructions in the photo below, note that it would be convenient to set the name of the project as FEZHATDemo so that it matches the code, but the code can be edited if the name is different, not a big issue here.

In Microsoft Visual Studio, there is a Package by the FEZ Hat provider called GHIElectronics.UWP.Shields.FEZHAT which commands the locations within the Hat (otherwise we won’t know where the temperature sensor is!).

This can be installed via Nuget, shown below

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

MainPage.xaml.cs (C#)

using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using GIS = GHIElectronics.UWP.Shields;
 
namespace FEZHATDemo {
    public sealed partial class MainPage : Page {
        private GIS.FEZHAT hat;
        private DispatcherTimer timer;
        private bool next;
        private int i;
 
        public MainPage() {
            this.InitializeComponent();
            this.Setup();
        }
 
        private async void Setup() {
            this.hat = await GIS.FEZHAT.CreateAsync();
            this.hat.S1.SetLimits(500, 2400, 0, 180);
            this.hat.S2.SetLimits(500, 2400, 0, 180);
            this.timer = new DispatcherTimer();
            this.timer.Interval = TimeSpan.FromMilliseconds(100);
            this.timer.Tick += this.OnTick;
            this.timer.Start();
        }
 
        private void OnTick(object sender, object e) {
            double x, y, z;
            this.hat.GetAcceleration(out x, out y, out z);
 
            this.LightTextBox.Text = this.hat.GetLightLevel().ToString("P2");
            this.TempTextBox.Text = this.hat.GetTemperature().ToString("N2");
            this.AccelTextBox.Text = $"({x:N2}, {y:N2}, {z:N2})";
            this.Button18TextBox.Text = this.hat.IsDIO18Pressed().ToString();
            this.Button22TextBox.Text = this.hat.IsDIO22Pressed().ToString();
            this.AnalogTextBox.Text = this.hat.ReadAnalog(GIS.FEZHAT.AnalogPin.Ain1).ToString("N2");
 
            if ((this.i++ % 5) == 0) {
                this.LedsTextBox.Text = this.next.ToString();
                this.hat.DIO24On = this.next;
                this.hat.D2.Color = this.next ? GIS.FEZHAT.Color.White : GIS.FEZHAT.Color.Black;
                this.hat.D3.Color = this.next ? GIS.FEZHAT.Color.White : GIS.FEZHAT.Color.Black;
 
                this.hat.WriteDigital(GIS.FEZHAT.DigitalPin.DIO16, this.next);
                this.hat.WriteDigital(GIS.FEZHAT.DigitalPin.DIO26, this.next);
 
                this.hat.SetPwmDutyCycle(GIS.FEZHAT.PwmPin.Pwm5, this.next ? 1.0 : 0.0);
                this.hat.SetPwmDutyCycle(GIS.FEZHAT.PwmPin.Pwm6, this.next ? 1.0 : 0.0);
                this.hat.SetPwmDutyCycle(GIS.FEZHAT.PwmPin.Pwm7, this.next ? 1.0 : 0.0);
                this.hat.SetPwmDutyCycle(GIS.FEZHAT.PwmPin.Pwm11, this.next ? 1.0 : 0.0);
                this.hat.SetPwmDutyCycle(GIS.FEZHAT.PwmPin.Pwm12, this.next ? 1.0 : 0.0);
                this.next = !this.next;
            }
 
            if (this.hat.IsDIO18Pressed()) {
                this.hat.S1.Position += 5.0;
                this.hat.S2.Position += 5.0;
 
                if (this.hat.S1.Position >;= 180.0) {
                    this.hat.S1.Position = 0.0;
                    this.hat.S2.Position = 0.0;
                }
            }
 
            if (this.hat.IsDIO22Pressed()) {
                if (this.hat.MotorA.Speed == 0.0) {
                    this.hat.MotorA.Speed = 0.5;
                    this.hat.MotorB.Speed = -0.7;
                }
            }
            else {
                if (this.hat.MotorA.Speed != 0.0) {
                    this.hat.MotorA.Speed = 0.0;
                    this.hat.MotorB.Speed = 0.0;
                }
            }
        }
    }
}

MainPage.xaml (Markup)

;Page x:Class="FEZHATDemo.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:App1" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d">
    ;StackPanel Orientation="Horizontal" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        ;StackPanel Width="75">
            ;TextBlock Text="Light: " />
            ;TextBlock Text="Temp: " />
            ;TextBlock Text="Accel: " />
            ;TextBlock Text="Button 18: " />
            ;TextBlock Text="Button 22: " />

Share the post

Temperature Sensing and Control using Raspberry Pi

×

Subscribe to Msdn Blogs | Get The Latest Information, Insights, Announcements, And News From Microsoft Experts And Developers In The Msdn Blogs.

Get updates delivered right to your inbox!

Thank you for your subscription

×