Encoder Example With MKR Vidor 4000

Manage easily quadrature encoders and never lose an impulse with the MKR Vidor 4000.

Hardware Required

Circuit

The encoder is connected as follows:

  • ENC_A to A0

  • ENC_B to A1

The circuit for this tutorial.
The circuit for this tutorial.

Pin nameDescriptionPin number
AREF0
A0ENC0_A1
A1ENC0_B2
A2ENC1_A3
A3ENC1_B4
A4ENC2_A5
A5ENC2_B6
A6ENC3_A7
D0ENC3_B8
D1ENC4_A9
D2ENC4_B10
D3ENC5_A11
D4ENC5_B12
D5ENC6_A13
D6ENC6_B14
D7ENC7_A15
D8ENC7_B16
D9ENC8_A17
D10ENC8_B18
D11ENC9_A19
D12ENC9_B20
D13ENC10_A21
D14ENC10_B22

Code

Include the VidorEncoder library, which is part of VidorPeripherals.

#include "VidorPeripherals.h"
#include "VidorEncoder.h"

You have a number of functions available to manage the encoder:

  • VidorEncoder(int index)
    - refer to pinout tab for pinout (index) in this page

  • void write(int32_t p);
    - resets the count value to "p"

  • int32_t read();
    - returns the actual count

1/*
2
3 This sketch shows how to use the Encoder IP in MKRVidor 4000
4
5 Quadrature encoders are a very common way to detect position (and speed) of a moving part, like a DC motor.
6
7 Normally, in the microcontroller world, decoding a quadrature encoder has serious limitations since all edges must be counted, otherwise the final number will be wrong. This is usually accomplished using interrupts, but over a certain revolution speed the intinsic overhead of servicing an interrupt destroys the count reliability.
8
9 Using the FPGA to perform decoding allows:
10
11 - not to lose any edge until the revolution speed is less than some million RPM :)
12
13 - we can "read" the encoder at any time, because the FPGA is counting independently
14
15 Circuit:
16
17 connect ENC_A to A0 and ENC_B to A1
18
19*/
20
21#include "VidorGraphics.h"
22#include "VidorEncoder.h"
23
24// Initialize Encoder #0 (connected to A0 and A1)
25// Refer to the online documentation to find which pins correspond to a given index
26// This assignment may change between bitstreams
27
28VidorEncoder encoder(0);
29
30void setup() {
31
32 Serial.begin(9600);
33
34 // wait for the serial monitor to open,
35
36 // if you are powering the board from a USB charger remove the next line
37
38 while (!Serial);
39
40 // Let's start the FPGA
41
42 if (!FPGA.begin()) {
43
44 Serial.println("Initialization failed!");
45
46 while (1) {}
47
48 }
49}
50
51void loop() {
52
53 // Read the encoder
54
55 int value = encoder.read();
56
57 Serial.print("Encoder value: ");
58
59 Serial.println(value);
60
61#if 1
62
63 // Wait one second with interrupts disabled
64
65 noInterrupts();
66
67 // We can't call delay() since it uses interrupts, so use busy loops
68
69 for (int i = 0; i < F_CPU / 10; i++) {
70
71 asm ("nop");
72
73 }
74
75 interrupts();
76
77#else
78
79 delay(200);
80#endif
81
82 if (value >= 200 || value <= -200) {
83
84 // Reset the count
85
86 encoder.write(0);
87
88 }
89}

Last revision 2018/07/22 by SM

Contribute to Arduino

Join the community and suggest improvements to this article via GitHub. Make sure to read out contribution policy before making your pull request.

Missing something?

Check out our store and get what you need to follow this tutorial.

Suggest Changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. You can read more on how to contribute in the contribution policy.