Encoder Reading Using LabVIEW and Arduino
Encoder Reading Using LabVIEW and Arduino
Encoder Reading Using LabVIEW and Arduino
Faculty of Engineering
Credit Hours Engineering Program
MCT 432 Hybrid Control
LAB 1
Reading Quadrature Encoder
Position
2. HARDWARE REQUIREMENTS 1
3. SOFTWARE REQUIREMENTS 2
4. PROCEDURE 2
5. RESULTS 8
6. FURTHER EXERCISE 8
7. REFERENCES 9
LIST OF FIGURES
FIG. 1 ARDUINO UNO R3 BOARD ........................................................................................................... 1
FIG. 2 SIX POLES, TWO CHANNELS MAGNETIC ENCODER ................................................................................ 1
FIG. 3 LIQUID CRYSTAL DISPLAY .............................................................................................................. 2
FIG. 4 ARDUINO USB CABLE .................................................................................................................. 2
FIG. 5 ENCODER LIBRARY BY PAUL STOFFREGAN ......................................................................................... 2
FIG. 6 PROTEUS MODEL......................................................................................................................... 5
FIG. 7 ARDUINO CODE AND PROTEUS ....................................................................................................... 5
FIG. 8 SIMULATION RESULT .................................................................................................................... 6
FIG. 9 COM PORT CONNECTIONS ............................................................................................................ 7
FIG. 10 LABVIEW BLOCK DIAGRAM......................................................................................................... 7
FIG. 11 LABVIEW - ARDUINO CO-SIMULATION FOR ENCODER READING.......................................................... 8
FIG. 12 GITHUB COURSE REPOSITORY ...................................................................................................... 9
MCT 432 Hybrid Control Lab 1: Reading Quadrature Encoder Position
Prepare the necessary hardware for reading motor position (angle) from mounted
magnetic encoder.
Understand the Arduino code for successful reading of encoder pulses and show
reading on LCD.
Transfer Arduino reading to LabVIEW.
Prevent recurring transfer and LCD update when no change in reading.
2. Hardware requirements
Arduino Uno R3 (or any board with at least two pins supporting external interrupt).
Quadrature magnetic encoder. Experiment was conducted using Sha Yang Ye geared
motor with onboard encoder available in Mechatronics lab.
P a g e 1 | 12
MCT 432 Hybrid Control Lab 1: Reading Quadrature Encoder Position
3. Software requirements
Arduino IDE for programming Arduino.
NI LabVIEW 2015 or later.
NI VISA 16 or later to allow LabVIEW to connect to available serial ports.
Proteus ISIS 7.5 or later for offline simulation.
FabulaTech Virtual serial port kit or any alternative to allow serial communication
between LabVIEW and Arduino.
4. Procedure
4.1. Display encoder position on an LCD
The code will utilize encoder library by Paul Stoffregan which can be downloaded from
library manager
P a g e 2 | 12
MCT 432 Hybrid Control Lab 1: Reading Quadrature Encoder Position
After installing the library, create a new sketch and copy the below code, then save it with
any name appropriate.
#include <Encoder.h>
#include <LiquidCrystal.h>
void setup()
{
Serial.begin(9600);
lcd.begin(16, 2);
lcd.setCursor(0,0);
lcd.print("Encoder Count:");
}
void loop()
{
int newPosition = myEnc.read();
if (newPosition>231)
myEnc.write(0);
if (newPosition<-231)
myEnc.write(0);
if (newPosition != oldPosition) {
oldPosition = newPosition;
sprintf(buffer,"%04d", newPosition);
Serial.println(buffer);
lcd.setCursor(0,1);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print(buffer);
delay(50);
}
}
Snippet for reading motor angle from magnetic encoder and display it on LCD
You are supposed to be acquainted with Arduino commands. However, few highlights to
comprehend the code flow would be useful.
P a g e 3 | 12
MCT 432 Hybrid Control Lab 1: Reading Quadrature Encoder Position
Define the encoder pins (channel A and Channel B). It is recommended that both pins
are interrupt pins for better performance of the library [1]
const int encaPin=2;
const int encbPin=3;
Read the encoder pulses.
int newPosition = myEnc.read();
The used encoder has 6 poles and mounted on 19:1 gearbox. So encoder resolution is
calculated as:
ResolutionPPR = (No. of poles)x(No. of channels)x(Grear Ratio) Eq. 1
= (6)(2)(19.25) = 231
Where PPR indicates Pulse Per Revolution.
P a g e 4 | 12
MCT 432 Hybrid Control Lab 1: Reading Quadrature Encoder Position
Before you start the simulation, make sure your code is compiled into hex file. Double click
on Arduino chip in simulation and point to the generated hex file. It is recommended that you
place the hex file alongside your Proteus
model.
P a g e 5 | 12
MCT 432 Hybrid Control Lab 1: Reading Quadrature Encoder Position
4.3. Exercise
Modify the Arduino code to display the actual position in radian and degree
considering that a 1 = 231 .
4.4. Communication with labVIEW
The second part of lab activities is to communicate LabVIEW with Proteus for reading
encoder position and plot it on a chart.
4.4.1. Arduino Code
4.4.2. LabVIEW VI
As mentioned in earlier, your PC must have NI VISA [2] installed in addition to
LabVIEW to allow LabVIEW to detect available serial port. Since there is no
connected physical Arduino so far, we will also use virtual com port emulator
which virtually connect two com ports together. One will be used on Arduino
(inside Proteus) and the other with LabVIEW.
P a g e 6 | 12
MCT 432 Hybrid Control Lab 1: Reading Quadrature Encoder Position
VISA resource name : to enumerate available com ports. You either select the virtual
com port or in reality the Arduino com port.
Configure serial port : after selecting the com port, we may edit the baudrate and
reading timeout
P a g e 7 | 12
MCT 432 Hybrid Control Lab 1: Reading Quadrature Encoder Position
VISA read : Reading six characters from Arduino (you can change that in Arduino code
buffer variable to suit your needs).
String to Number conversion : Since Arduino passes data as encoded string, this block
converts the transmitted characters into single decimal value.
VISA close : This is to close connection and unlock com port so other applications can
use it if needed.
5. Results
Start Proteus simulation and then LabVIEW.
6. Further exercise
Modify the code to calculate velocity in PPR.
7. File repository
You can get all files of lab 1 from GitHub repository of the course [3].
P a g e 8 | 12
MCT 432 Hybrid Control Lab 1: Reading Quadrature Encoder Position
8. References
[1] Encoder Library for Arduino. Available:
https://www.pjrc.com/teensy/td_libs_Encoder.html
[2] N. Instruments. NI VISA. Available: http://www.ni.com/download/ni-visa-
15.0.1/5693/en/
[3] F. Tolbah and W. El-Badry. MCT 432 Hybrid Control Repository. Available:
https://github.com/wbadry/MCT432-Hybrid-Control
P a g e 9 | 12