We no longer sell products directly from the GHI Electronics website, click here for more details. Please find a Distributor to complete your online order. If you have any questions regarding this notice please contact us at support@ghielectronics.com.

FEZ HAT Developers' Guide

Last modified November 10, 2015

The FEZ HAT is a Raspberry Pi 2 Model B compatible hat with many onboard peripherals.

To get started, make sure you are running Visual Studio 2015 on Windows 10. Create a new Universal application and make sure to add the Windows IoT Extension SDK. Then, add our FEZ HAT NuGet driver. The below example briefly shows the functionality on the board.

When using the onboard motors and servos, care needs to be taken with the PWM frequency. Motors typically require a frequency in excess of 10KHz while servos typically require 50Hz. The PWM driver we use has a frequency range up to 1.5KHz and requires that every PWM channel have the same frequency. As a result, some motor performance may be subpar given the low maximum frequency or may fail to function at all when using servos (use of servos automatically sets the frequency to 50Hz). Your results may vary, but adding a 470uF capacitor in parallel with the motor may help improve performance.

Make sure that you connect the motor power and ground terminals with an external power supply if you plan to use motors or servos as they draw their power from there. Also, the exposed 5V pad is an output only. Do not try to power the Raspberry Pi from there.

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;
				}
			}
		}
	}
}
<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: " />
            <TextBlock Text="Leds: " />
            <TextBlock Text="Analog: " />
        </StackPanel>
        <StackPanel>
            <TextBlock Name="LightTextBox" />
            <TextBlock Name="TempTextBox" />
            <TextBlock Name="AccelTextBox" />
            <TextBlock Name="Button18TextBox" />
            <TextBlock Name="Button22TextBox" />
            <TextBlock Name="LedsTextBox" />
            <TextBlock Name="AnalogTextBox" />
        </StackPanel>
    </StackPanel>
</Page>
Leave feedback about this document.
Let us know if the information presented here was accurate, helpful and if you have any suggestions.
Leave feedback about this document.
Let us know if the information presented here was accurate, helpful and if you have any suggestions.

* Indicates required fields.
This form is only for feedback not support.
Review our how to find information guide on locating helpful resources.