Look for in our Blog. You will find multiple applications, solutions, code examples. Navigate using the tag cloud or search using specific criteria

Servo Motor with Arduino based PLC

Introduction

In this post we will control the speed of a servo motor 57HSE2N-D25. For this we will also need a driver, HSS57 Hybrid Servo Driver, for the servo motor that will be connected to our Arduino based PLC for the automation of the speed control. For the software, a pulses.h library is implemented  ( software without using pulses.h library is also attached).

Requirements

Connections


Servo Motor - Driver connections:

Red     -->       A+
Green      -->       A-
Yellow   -->       B+
Blue  -->       B-

Ethernet PLC - Driver connections: 

PIN 3   -->  PUL - (pulses)      
PIN2    -->  DIR- (rotational direction)
PIN 50 (MISO)  -->  ENA- (enable)                     
GND    --> PUL+, DIR +, ENA +

* Signals = 5Vdc

Odoo CMS - a big picture


*Remember to power supply the PLC between 12 and 24V.

*In this case we are using 5V signal, so, not resistors needed between PUL+,ENA+, DIR+  and PIN2, PIN3, PIN50.

*Controller is M-Duino 21+

*Encoder in not used in this post


Software

Using this sketch you are able to control the speed of the stepper, direction and number of revolutions.

Change PIN2 to control the direction, change NUM_REVOLUTIONS to decide how many revolutions do you require and change FREQ to change the frequency (rotation speed).

Below is the code:

/*
   Copyright (c) 2018 Boot&Work Corp., S.L. All rights reserved
   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Lesser General Public License for more details.
   You should have received a copy of the GNU Lesser General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include <Pulses.h>

#define PIN_STEPPER_PUL 3
#define PIN_STEPPER_DIR 2
#define PIN_STEPPER_ENA 50
#define STEPPER_PULSES_PER_REV 1600UL
#define FREQ (2000UL) // Hz
#define NUM_REVOLUTIONS 10
#define STEPPER_STEPS ((NUM_REVOLUTIONS) * (STEPPER_PULSES_PER_REV))
 
volatile uint32_t stepsCnt = STEPPER_STEPS;
ISR(TIMER3_COMPA_vect) {
  if (stepsCnt) {
    --stepsCnt;
    if (stepsCnt == 0UL) {
      stopPulses(PIN_STEPPER_PUL);
    }
  }
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
  pinMode(PIN_STEPPER_DIR, OUTPUT);
  pinMode(PIN_STEPPER_PUL, OUTPUT);
  pinMode(PIN_STEPPER_ENA, OUTPUT);
  digitalWrite(PIN_STEPPER_DIR, HIGH);
  digitalWrite(PIN_STEPPER_ENA, HIGH);
  // It depends on the PIN_STEPPER_PUL
  TIMSK3 |= 1 << OCIE3A;
  startPulses(PIN_STEPPER_PUL, FREQ, 3);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void loop() {
}