Windows 10 IoT Core

Prerequisites

To get maximum performance from your hardware, please make sure you'll meet the following requirements.

Operating System

We recommend using the latest version of Windows 10 IoT Core. Download the latest version using Windows 10 IoT Core Dashboard.

Development Environment

We recommend keeping Microsoft Visual Studio up to date.

Windows 10 IoT Core Library

You can download the library from NuGet. To do that, go to Visual Studio Solution Explorer, and then right-click to the References. Select Manage NuGet packages. Type "Turta.RelayHAT.RelayDriver" and then install the latest stable version.
You can download the library source code from https://github.com/Turta-io/RelayHAT/tree/master/DriverSource/Windows10IoTCore address.
To use the library, add the following using statement to the top of your C# code.
1
using TurtaRelayHAT;
Copied!
Then, create an instance of the RelayController class.
1
relayController = new RelayController();
Copied!
Now you're ready to access the library by calling the relayController instance.

Initialization

The library automatically initializes the required components when its instance is being created, so there is no need to do a manual initialization.

Basic Members

Set Relay

Turns the desired relay on or off.
1
void SetRelay(int ch, bool state)
Copied!
    Parameters
      Int ch: Relay channel. 1 to 4.
      Bool state: Relay state. True or False.
    Returns
      None

Read Relay State

Reads the desired relay state.
1
bool ReadRelayState(int ch)
Copied!
    Parameters
      Int ch: Relay channel. 1 to 4.
    Returns
      Bool: Relay state. True or False.

Disposal

To clean up the resources used by the class, call the Dispose() method it when the application is closing.
1
void Dispose()
Copied!
This method turns all the relays off and then releases the used resources.

Samples

You can copy the example code from https://github.com/Turta-io/RelayHAT/tree/master/Samples/Windows10IoTCore address. There is one example of this sensor.

Relay Controller Sample App

This application demonstrates relay functions. It turns relays on and off in a loop.
To run the example:
    1.
    Open the sample project you downloaded.
    2.
    Make sure "ARM" is selected from the solution platforms selection.
    3.
    Deploy the sample to the Raspberry Pi using the "F5" key.
Sample Code
MainPage.xaml.cs
1
using Windows.UI.Xaml.Controls;
2
using System.Threading;
3
using TurtaRelayHAT;
4
5
namespace RelayControllerSampleApp
6
{
7
public sealed partial class MainPage : Page
8
{
9
// Relay Controller
10
static RelayController relayController;
11
12
// Relay Timer
13
Timer relayTimer;
14
15
// Counter
16
static int counter = 0;
17
18
public MainPage()
19
{
20
this.InitializeComponent();
21
22
// Initialize controller and timer
23
Initialize();
24
}
25
26
private void Initialize()
27
{
28
// Initialize Relay Controller
29
relayController = new RelayController();
30
31
// Configure timer to 2000ms delayed start and 1000ms interval
32
relayTimer = new Timer(new TimerCallback(RelayTimerTick), null, 2000, 1000);
33
}
34
35
private static void RelayTimerTick(object state)
36
{
37
switch (counter)
38
{
39
case 0:
40
relayController.SetRelay(1, true); // Turn relay 1 on
41
break;
42
43
case 1:
44
relayController.SetRelay(2, true); // Turn relay 2 on
45
break;
46
47
case 2:
48
relayController.SetRelay(3, true); // Turn relay 3 on
49
break;
50
51
case 3:
52
relayController.SetRelay(4, true); // Turn relay 4 on
53
break;
54
55
case 4:
56
relayController.SetRelay(1, false); // Turn relay 1 off
57
break;
58
59
case 5:
60
relayController.SetRelay(2, false); // Turn relay 2 off
61
break;
62
63
case 6:
64
relayController.SetRelay(3, false); // Turn relay 3 off
65
break;
66
67
case 7:
68
relayController.SetRelay(4, false); // Turn relay 4 off
69
break;
70
71
default:
72
break;
73
}
74
75
// Increase the counter
76
counter++;
77
78
// Reset the counter if overflowed
79
if (counter > 7) counter = 0;
80
}
81
}
82
}
Copied!
Result
When you run the sample, the relays turn on one after another and then turn off in the same order.
To stop the application, use the "Stop" button on the Visual Studio.
Last modified 2yr ago