Esmacat
Published © GPL3+

Raspberry Pi with Codesys PLC UI Controlling an Arduino

Use a Raspberry Pi with Codesys to control the LED on the EtherCAT Arduino Shield by Esmacat attached to an Arduino Uno.

BeginnerProtip1 hour8,525

Things used in this project

Hardware components

Raspberry Pi 4 Model B
Raspberry Pi 4 Model B
Raspberry Pi to be used as EtherCAT Master with free open-source EtherCAT master code provided by Esmacat
×1
Raspberry Pi Power Supply
×1
Esmacat Master S
Optional EtherCAT Master
×1
Esmacat Master C
Optional EtherCAT Master
×1
Arduino UNO
Arduino UNO
×1
EtherCAT Arduino Shield by Esmacat (EASE)
×1
Ethernet Cable, Cat6a
Ethernet Cable, Cat6a
×2
Power over Ethernet (POE) Injector
×1
DC Adapter
For powering the POE injector
×1

Software apps and online services

Arduino IDE
Arduino IDE
Codesys
Software to program the Master PLC.

Story

Read more

Schematics

Physical Hardware Connection schematic

hardware setup after final connections

Hardware Setup Schematic

Schematic of the connection used in the tutorial

Code

codesys_onboard_led

Arduino
Code to be uploaded to Arduino board with EASE
/*
  Codesys Onboard LED Control

  This sketch does the following:
  1) Controls the onboard led in Arduino using input from the EtherCAT master (Pi) using Codesys.
  2) Depending on the value updated in one of the EASE registers it does the following
      2.1) If the register value is 0 the led is turned off.
      2.2) If the register value is greater than 0, the led is turned on.      

  created 27 Jan 2020
  by Harmonic Binonics Inc. (https://www.harmonicbionics.com/).  
*/

#include <Esmacatshield.h>      //Include the Esmacat Arduino Library

# define ARDUINO_UNO_SLAVE_SELECT 10      // The chip selector pin for Arduino Uno is 10 

Esmacatshield ease_test(ARDUINO_UNO_SLAVE_SELECT);      // Create a slave object and specify the Chip Selector Pin

// Required variables Initialization
int led_state = 0,led_pin = 8,delay_time = 1000;
int ease_registers[8];   // EASE 8 registers

void setup() 
{
//  Serial.begin(9600);   // Begin Serial communication at the specified baud rate
  pinMode(led_pin,OUTPUT);
  ease_test.start_spi();   // Start SPI for EASE
}

void loop()
{
  ease_test.get_ecat_registers(ease_registers);   // Update EASE registers with new data
  led_state = ease_registers[0];   // Assign register 0 value to variable
  delay_time = ease_registers[1];  
//  Serial.print("LED State is ");   
//  Serial.print(led_state);   // Print variable to the Serial monitor (for debugging)
  if(led_state == 1)
  {
    digitalWrite(led_pin,HIGH);
    ease_test.write_reg_value(1,led_state,true);
    delay(delay_time);
    ease_test.write_reg_value(1,led_state,false);
  }
}

EtherCAT Master Code

C/C++
Code to be run on Raspberry Pi using codesys (.project file)
No preview (download only).

EtherCAT Arduino Shield by Esmacat (EASE) Arduino Library

Esmacat Arduino Library

Credits

Esmacat

Esmacat

11 projects • 15 followers
Esmacat is an easy yet powerful EtherCAT solution for robotics. Our EtherCAT tech allow users to run EtherCAT applications in minutes!

Comments