HOW TO READ ENCODER SPEED THROUGH AN INTERRUPT WITH AN ARDUINO BASED PLC?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 (phase B) and I0.5 (phase A). 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?

On this example we use an external interrupt to know when the encoder is rotating. The phase A is connected to the interrupt 0 (I0.5). The interrupt will be configured to active when it appears a rising edge on I0.5. Every rising edge on the phase A will indicate us that the encoder is rotating (one pulse). But to know rotate direction we have to read if value of the phase B is low or high. If the rotate direction is forward (CW; phase B = LOW) and if the rotate direction is going backward (CCW; phase B = HIGH). Next it is showed a signal to get a better understanding of that.

Counting pulses through an external interrupt is the best option, because you get sure that you do not lose any pulse while the microcontroller is executing other code.

With every interrupt we add pulses (pulses++) if the encoder is rotating forward and we subtract pulses if the encoder is rotating backward. Once we have the pulses counting on the right way we just need to know the time to be able to convert these pulses to a speed value. To know the time while we are?getting ?pulses we will use two variables, one for initial time (time0) and other to final time (timeF). Using the function micros() (also you can use millis() function) we will set the two values to implement our equation.

The?E6B2-C OMRON encoder that is used on this example has a resolution of 500 pulses for revolution. So, our equation will be:

Speed = (pulses/500) ? / ? ( (timeF-timeO)/60000000 ) ? rev/min

In this example we display the speed of the encoder on serial monitor. To display it you have to introduce a ?p? of print through?the serial monitor.

IMPORTANT: remember, the interrupt has to be as fast as possible, if not you can get some Aliasing!

To add more information of the functions that are used on this code take a look on the Arduino Reference:?millis(),?micros(),?attachInterrupt(),?detachInterrupt()

 

Introducing Adafruit Feather

Wouldn’t it be great if, instead of collecting shields, HATs, PMODs, Clicks, Booster Packs, Props… you could have a cross-compatible platform? Something that doesn’t force you into one chipset? and allows you to mix-and-match the microcontroller, wireless protocol, and functional extensions?

Yeah, me too! That’s why I created Feather. Feather is a flexible and powerful family of microcontroller main-boards (Feathers) and daughter-boards (Wings) with a wide range of capabilities.

Feathers!

Since we aren’t tied to one chip manufacturer, we feature the best-of-the-market chipsets, including:

  • Atmel ATmega32u4 – 8 bit AVR
  • Atmel ATSAMD21 – 32 bit ARM Cortex M0+
  • Broadcom/Cypress WICED – STM32 with WiFi
  • Espressif ESP8266 and ESP32 – Tensilica with WiFi/BT
  • Freescale MK20 – ARM Cortex M4, as the Teensy 3.2 Feather Adapter
  • Nordic nRF51 and nRF52 – ARM Cortex & Bluetooth LE
  • Packet radio modules featuring SemTech SX1231
  • LoRa radio modules featuring SemTech SX127x

and many more to come!

Wings!

Once you’ve picked your main dish (the Feather mainboard) you can pick and choose from a wide wide variety of Wings. Wings are like little “shields” or “HATs” for Feathers. You can (in theory) add as many Wings as you like to a single Feather main-board, as long as you don’t run out of power, space or have pin collisions.

Best of all, we’ve tested all of our Wings against all of our Feathers. So, other than a few exceptions (mostly the GPS Featherwing), you can re-use, re-cycle, and upgrade your Feathers and Wings between projects and as need arises.

For example, start out with a Feather 32u4, then decide to upgrade to the Feather M0 for more Flash – the pin out is the same! Or, make an IoT project with the ESP8266 and then super-power it with an ESP32 just by swapping out the main Feather. Maybe build a project with a Feather M0 Bluefruit, then realize you need more range so you switch to a Feather M0 LoRa.

The Feathers and Wings all have example code written in Arduino C/C++ so as your projects adapt, all your wings and code will come along with you.