Non-QEC Example – Digital Input/Output

Non-QEC Example – Digital Input/Output

[QEC Tutorial]

For the full tutorial, see Example: Digital Input/Output on non-QEC with EtherCAT Library.

Digital Input/Output

Let the external LEDs be blinked by Digital Output, and the status of the LEDs is received by Digital Input and printed out through Serial Port.

ハードウェア

  • QEC-M-01
  • QEC-R11D0F: EtherCAT 16 Channel Digital Output Slave Module
  • Others: Switch

Circuit

Please refer to QEC Example – Digital Input/Output’s circuit part for circuit construction.

Code

Please refer to QEC Example – Digital Input/Output’s code part first.

We will set Digital Output as the first slave (slave0) and Digital Input as the second slave (slave1) through the EthercatDevice_Generic object.

EthercatMaster master;
EthercatDevice_Generic slave0;
EthercatDevice_Generic slave1;

In the main loop, we’ll first set pin0 in output to High and read pin8 in input, with a delay of 4000 ms, then print in Serial Port; after that, we set pin0 in output to Low and read pin8 in input, with a delay of 1000 ms, then print in Serial Port. The code is as follows.

slave0.pdoWrite8(0, 1);
delay(10);
Serial.println(slave1.pdoRead8(1), HEX);
delay(4000);
slave0.pdoWrite8(0, 0);
delay(10);
Serial.println(slave1.pdoRead8(1), HEX);
delay(1000);

Among them, between pdoWrite8 and pdoRead8, we need to add a delay(10) to wait for the Input PDO to be updated.

Example

#include "Ethercat.h"

EthercatMaster master;
EthercatDevice_Generic slave0;
EthercatDevice_Generic slave1;

void setup(void) {
    Serial.begin(115200);
    /* EtherCAT */
    Serial.print("Master begin:"); Serial.println(master.begin());
    Serial.print("Slave0 attach:"); Serial.println(slave0.attach(0, master));
    Serial.print("Slave1 attach:"); Serial.println(slave1.attach(1, master));
    Serial.print("Master start:"); Serial.println(master.start());
}
void loop() {
    slave0.pdoWrite8(0, 1);
    delay(10);
    Serial.println(slave1.pdoRead8(1), HEX);
    delay(4000);
    slave0.pdoWrite8(0, 0);
    delay(10);
    Serial.println(slave1.pdoRead8(1), HEX);
    delay(1000);
}

Learn More

You can find more information about the basic QEC applications in the EtherCAT application.

You can also explore Language or Libraries for a more detailed collection of 86Duino IDE programming.

コメントする

上部へスクロール