How to read an encoder with Arduino based PLC’s

How to read an encoder with Arduino based PLC’s

In this post it is showed how to count pulses of an encoder with one of Arduino based PLC’s. 

The requirements for this example are:

  • Encoder
  • One of our PLC’s
  • 2 resistors of 4k7

In this example we have used an E6B2-C OMRON encoder and an M-Duino 21 I/O’s

How to connect the devices?

The power supply of the E6B2-C OMRON encoder is between 5V to 24V, we used the 7V from the M-Duino to power supply the encoder. The two resistors are connected, as a Pull-up configuration, between the two phases to the 7V of the M-Duino. And the two phases are connected to I0.4 and I0.3. And all the GND are connected to the M-Duino GND.

Once all is connected we can proceed to programming the M-Duino. Remember that the encoder convert revolutions to pulses. 

How to read these pulses? How to know the rotate direction?

Every change on the previous value of one phase to actual value of this phase will indicate us that the encoder is rotating. In this example we will compare this two values to know that the encoder is rotating. But to know rotate direction we have to locate the rising edge of the phase A. If there is a rising edge and the value of the phase B is low we know that the rotate direction is forward (CW) ant if the value of phase B is high we know that the rotate direction is going backward (CCW). Next it is showed a signal to get a better understanding of that.

Once we have are available to count the pulses we need to covert the pulses to an angle to know which position is the encoder to the initial position. The E6B2-C OMRON encoder that is used on this example has a resolution of 500 pulses for revolution. So, it is used the function map() of Arduino to convert the pulses to angle, you have the explanation of this function here

In this example we display the position of the encoder on serial monitor with an angle with respect to the initial position. To display the angle you have to introduce a “p” of print to the serial monitor.

IMPORTANT: remember, the function Serial.print() takes a considerable time to execute, you can lose some pulses while you are printing the values (Aliasing)!

Next it is showed how to convert this to code on Arduino IDE:

const int phaseB = I0_4;
const int phaseA = I0_3;
int valuePhaseA = 0;
int valuePhaseB = 0;
int prevValuePhaseA = 0;
int prevValuePhaseB = 0;
long pulses = 0; 
float Angle = 0;

void setup() {
  Serial.begin(9600);
  pinMode(phaseA, INPUT); // Configure the I.0 as a INPUTS
  pinMode(phaseB, INPUT);
}

void loop() {
  valuePhaseA = digitalRead(phaseA); // Reading values from the different phases of the encoder
  valuePhaseB = digitalRead(phaseB);
  
  if (prevValuePhaseA != valuePhaseA) {
    if (valuePhaseA == HIGH && prevValuePhaseA == LOW) { // Confirm if there is a rising edge on phase A
      if (valuePhaseB == LOW) { //Confirm if we are rotating on CW direction (forward)
        pulses++;
      }
      else { //Confirm if we are turning on CCW direction.
        pulses--;
      }
    }
  }
  
  prevValuePhaseA = valuePhaseA;
  prevValuePhaseB = valuePhaseB;

  if (Serial.available() && Serial.read() == 'p') {
    Angle = map(pulses, 0, 500, 0, 360);// Convert pulses to an angle
    Serial.print("Angle position:");
    Serial.println(Angle);
  }
}

 

See also:

HOW TO READ ENCODER SPEED THROUGH AN INTERRUPT WITH AN ARDUINO BASED PLC’SHOW TO TOUCH UP DATA FROM THE EEPROMHOW TO USE STEPPER MOTOR WITH ARDBOX ANALOGHOW TO CONNECT AND CONFIGURE A LOAD CELL USING HX711 TRANSMITTER WITH OUR EQUIPMENT’SHOW TO GENERATE A DIFFERENT PWM’S SIGNALS WITH OURS EQUIPMENT’SHOW TO USE THE WATCHDOGRS-485 COMMUNICATION EXAMPLE USING OUR LIBRARIESHOW TO CREATE A WEB SERVER TO READ A SD CARD WITH OUR PLC’SHOW TO CONNECT A SD CARD TO A PLCHOW TO UPLOAD A BOOTLOADER ON A ARDBOX/M-DUINO PLC

Comments are closed.