Comment on page
Windows 10 IoT Core
To get maximum performance from your hardware, please make sure you'll meet the following requirements.
We recommend using the latest version of Windows 10 IoT Core. Download the latest version using Windows 10 IoT Core Dashboard.
We recommend keeping Microsoft Visual Studio up to date.
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.
using TurtaRelayHAT;
Then, create an instance of the RelayController class.
relayController = new RelayController();
Now you're ready to access the library by calling the relayController instance.
The library automatically initializes the required components when its instance is being created, so there is no need to do a manual initialization.
Turns the desired relay on or off.
void SetRelay(int ch, bool state)
Parameters
Int ch: Relay channel. 1 to 4. Bool state: Relay state. True or False. Returns
None
Reads the desired relay state.
bool ReadRelayState(int ch)
Parameters
Int ch: Relay channel. 1 to 4. Returns
Bool: Relay state. True or False.
To clean up the resources used by the class, call the Dispose() method it when the application is closing.
void Dispose()
This method turns all the relays off and then releases the used resources.
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.
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
using Windows.UI.Xaml.Controls;
using System.Threading;
using TurtaRelayHAT;
namespace RelayControllerSampleApp
{
public sealed partial class MainPage : Page
{
// Relay Controller
static RelayController relayController;
// Relay Timer
Timer relayTimer;
// Counter
static int counter = 0;
public MainPage()
{
this.InitializeComponent();
// Initialize controller and timer
Initialize();
}
private void Initialize()
{
// Initialize Relay Controller
relayController = new RelayController();
// Configure timer to 2000ms delayed start and 1000ms interval
relayTimer = new Timer(new TimerCallback(RelayTimerTick), null, 2000, 1000);
}
private static void RelayTimerTick(object state)
{
switch (counter)
{
case 0:
relayController.SetRelay(1, true); // Turn relay 1 on
break;
case 1:
relayController.SetRelay(2, true); // Turn relay 2 on
break;
case 2:
relayController.SetRelay(3, true); // Turn relay 3 on
break;
case 3:
relayController.SetRelay(4, true); // Turn relay 4 on
break;
case 4:
relayController.SetRelay(1, false); // Turn relay 1 off
break;
case 5:
relayController.SetRelay(2, false); // Turn relay 2 off
break;
case 6:
relayController.SetRelay(3, false); // Turn relay 3 off
break;
case 7:
relayController.SetRelay(4, false); // Turn relay 4 off
break;
default:
break;
}
// Increase the counter
counter++;
// Reset the counter if overflowed
if (counter > 7) counter = 0;
}
}
}
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 4yr ago