QEC Example – Digital Input

QEC Example – Digital Input

[QEC Tutorial]

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

Digital Input

This chapter will receive external digital signals through the EtherCAT digital input module, QEC-R11DF0, and print them out through Serial Port.

ハードウェア

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

Circuit

Same as Digital Output, please connect the EtherCAT Out network from QEC-M to the EtherCAT In network of QEC-R11DF0 first. This example will connect an external switch, using Pin8 of the QEC-R11DF0 as the receive signal pin:

  1. Vp power connection external switch
  2. Connect the external switch to Pin8+ of QEC-R11DF0
  3. Pin8- of QEC-R11DF0 is connected back to the GND of the Vp power supply

As shown in the figure.

rn_image_picker_lib_temp_228850c5-c615-466d-a50b-ec309e6c6616

Code

After constructing the circuit, you need to set the EtherCAT master and slave object names.

EthercatMaster master;
EthercatDevice_QECR11DF0H slave; // Depend on your QEC device name

Next, the EtherCAT master and slave are initialized and the slave is mounted to EtherCAT.

master.begin();
slave.attach(0, master);
master.start();

The digital read signal is printed out through the Serial Port, and the serial baud rate is set to 115200.

Serial.begin(115200);

In the main loop, you need to read out the Pin8 signal from the QEC-R11DF0 using the following command.

slave.digitalRead(8);

In this example, in order to keep the signal from Digital Input from being continuously output in Serial Port, you can create two variables as the basis for reading the signal with changes and then print out the signal through Serial Port in the main program after determining whether the received signal is different twice by if-else, as follows.

bool din=0, dinOld=0;
/* ------------- ellipsis --------------- */
din = slave.digitalRead(8);
if (din != dinOld) {
    Serial.println(din);
    dinOld = din;
}

Example

#include "Ethercat.h"

EthercatMaster master;
EthercatDevice_QECR11DF0H slave; // Depend on your QEC device name
bool din=0,dinOld=0;

void setup() {
    Serial.begin(115200);
    while (!Serial);
    
    master.begin();
    slave.attach(0, master);
    master.start();
}

void loop() {
    din = slave.digitalRead(8);
    if (din != dinOld) {
        Serial.println(din);
        dinOld = din;
    }
}

Learn More

You can find more information about the basic QEC applications in the EtherCAT application.
You can also explore コード構文 または コードライブラリ for a more detailed collection of 86Duino IDE programming.

コメントする

上部へスクロール