Photocoupler Sample

This application demonstrates the isolated input reading function of the module. It uses Arduino IDE's Serial Monitor and IoT Node's onboard LED to display input states.

To run the example:

  1. Open the IoT_Node_Photocoupler sketch from the examples menu.

  2. Select Turta IoT Node from the Tools > Board menu.

  3. Select your device's COM port from Tools > Port menu.

  4. Open Serial Monitor from Tools > Serial Monitor.

  5. Select 115.200 baud from the Serial Monitor's status bar.

  6. Upload the code to your device.

Sample Code

#include <Turta_Photocoupler_Module.h>
#define LED 13

// Global variables.
bool input1State = false;
bool input2State = false;
bool oldInput1State = false;
bool oldInput2State = false;

// Create Photocoupler Module instance.
Turta_Photocoupler_Module pc;

void setup() {
  // Initialize onboard LED.
  pinMode(LED, OUTPUT);

  // Initialize Photocoupler Module.
  pc.begin();

  // Configure serial port.
  Serial.begin(115200);
  delay(200);
}

void loop() {
  // Read input states.
  input1State = pc.readInput(1);
  input2State = pc.readInput(2);

  if (input1State || input2State) {
    digitalWrite(LED, HIGH);
  }
  else {
    digitalWrite(LED, LOW);
  }

  // On input 1 change.
  if (oldInput1State != input1State) {
    Serial.print("Input 1 State: ");
    Serial.println(input1State ? "High." : "Low.");
  }

  // On input 2 change.
  if (oldInput2State != input2State) {
    Serial.print("Input 2 State: ");
    Serial.println(input2State ? "High." : "Low.");
  }

  // Store last input states.
  oldInput1State = input1State;
  oldInput2State = input2State;

  // Delay 10ms.
  delay(10);
}

Result

After the application is uploaded to the device, it writes input state to the Serial Monitor if the state is changed. The onboard LED is lit if any of the input channels are high. The example output should be like this:

Input 1 State: High.
Input 1 State: Low.
Input 2 State: High.
Input 2 State: Low.

The application runs forever until you clear it from memory.

Last updated