Photocoupler Input
This documentation includes installation guidelines and sample code for your hardware.
PhotocouplerInput library is responsible for reading isolated inputs from the photocouplers.
To use the library, add the following using statement to the top of your C# code.
using TurtaIoTHAT;
Then, create an instance of the PhotocouplerInput class.
PhotocouplerInput photocouplerInput
Now you're ready to access the library by calling the photocouplerInput instance.
Initiates the LTV-827S photocouplers to detect isolated 5V input.
PhotocouplerInput(int debounceTimeout)
Parameters
Int debounceTimeout: Debounce timeout in milliseconds to eliminate the contact flickering. 0 to disable the filter.
Reads current input state.
bool ReadInputState(int ch)
Parameters
Int ch: Photocoupler input channel. 1, 2, 3 or 4. Returns
Bool: True if input is high. False if input is low.
Notifies on photocoupler input change.
PhotocouplerInputEventHandler PhotocouplerInputChanged
Event Args
Int Ch: Photocoupler input channel. 1, 2, 3 or 4. Bool State: Input state. True if input is high. False if input is low.
To clean up the resources used by the class, call the Dispose() method it when the application is closing.
void Dispose()
This method cleans the resources used by the class.
You can copy the example code from https://github.com/Turta-io/IoTHAT/tree/master/Samples/Windows10IoTCore address. There is one example of this library.
This application demonstrates reading isolated inputs from photocoupler input channels.
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.Diagnostics;
using TurtaIoTHAT;
namespace PhotocouplerSampleApp
{
public sealed partial class MainPage : Page
{
// Photocoupler Inputs
static PhotocouplerInput photocouplerInput;
public MainPage()
{
this.InitializeComponent();
// Initialize Photocoupler Inputs and set event handler
Initialize();
}
private void Initialize()
{
// Initialize and configure Photocoupler Input
photocouplerInput = new PhotocouplerInput(20);
// Subscribe to Photocoupler Input Changed event
photocouplerInput.PhotocouplerInputChanged += PhotocouplerInputChanged;
}
private void PhotocouplerInputChanged(object sender, PhotocouplerInputEventArgs e)
{
// Write photocoupler input state to output / immediate window
Debug.Write("Photocoupler Interrupt: ");
Debug.Write("Ch " + e.Ch.ToString() + " ");
Debug.WriteLine(e.State ? "Active" : "Passive");
}
}
}
Result
When you run the sample, it prints the photocoupler input state changes to the Visual Studio Output / Immediate Window.
Photocoupler Interrupt: Ch 1 Active
Photocoupler Interrupt: Ch 2 Passive
-----
To stop the application, use the "Stop" button on the Visual Studio.
Last modified 4yr ago