Reading a Rotary Encoder Using an Arduino (using Interrupt) - Part 2/2

preview_player
Показать описание
In this tutorial I will be showing you how to read an encoder using your Arduino board using Interrupts.

I have used 4X encoding principle to increase the resolution four times.

The link to the first part of this tutorial:

The link to the Arduino official page to read more about Interrupts:
Рекомендации по теме
Комментарии
Автор

I've edited the code in the video enabling it to output the wheel speed when the encoder is connected to a rotating wheel. Please let me know if you have any other problems regarding the tutorial.

//Encoder pins
const int encoderPinA = 2;
const int encoderPinB = 3;

//The number of pulses produced by the encoder within a revolution.
const int PPR = 12;
//The value is '1' if the encoder is not attached to any motor.
const int gearRatio = 64;
//When using 2X encoding the value is '2'. In this code 4X encoding is used.
const int decodeNumber = 4;
//record the cuurent number of pulses received
volatile long int currentPosition = 0;

unsigned int timer_previous = 0;
double wheel_turns = 0.0;
double current_wheel_distance = 0.0;
double previous_wheel_distance = 0.0;
double time_taken = 0;
double wheel_speed_mpms = 0.0;
double wheel_speed_mpm = 0.0;
double pi = 3.14;
double r = 0.04775; // Radius of the wheel in meters

void setup() {
pinMode (encoderPinA, INPUT_PULLUP);
pinMode (encoderPinB, INPUT_PULLUP);
attachInterrupt (digitalPinToInterrupt (encoderPinA), doEncoderA, CHANGE);
attachInterrupt (digitalPinToInterrupt (encoderPinB), doEncoderB, CHANGE);
Serial.begin (9600);
}
void loop() {
current_wheel_distance = (currentPosition * 2 * pi * r) / (PPR * gearRatio * decodeNumber) ; // Gives the distance which the wheel traveled. 'r' is the radius of the wheel in meters.
time_taken = micros() - timer_previous; // Time taken since the previous iteration of the loop.
wheel_speed_mpms = / time_taken; // This gives the speed in meters per microsecond.
wheel_speed_mpm = wheel_speed_mpms * * 60; // This gives the speed in meters per minute.

Serial.print(current_wheel_distance, 4);
Serial.print(" ");
Serial.print(wheel_speed_mpm, 4);
Serial.println();

timer_previous = micros();
previous_wheel_distance = current_wheel_distance;
}

void doEncoderA()
{
if (digitalRead(encoderPinA) != digitalRead(encoderPinB))
{
currentPosition++;
}
else
{
currentPosition--;
}
}
void doEncoderB()
{
if (digitalRead(encoderPinA) == digitalRead(encoderPinB))
{
currentPosition++;
}
else
{
currentPosition--;
}
}

letsdomechatronics
Автор

hey brw, can you help me my code didn’t work interrupt with encoder

HassanAzmeel
Автор

Can you make some code for Module Direct Transfer Film (DTF) to Emson L1800?

Revansid
Автор

Hey thanks a lot for this! It worked perfectly! :)

uvinidesilva
Автор

How to I get 1 rotaion from the ppr value?

asifmahmud
Автор

Dear sir i want to make a robot moves in a pattern with arduino and encoder to measure the distance how can i do the programming?

yudishmoonean
Автор

good work. how do i translate this to measure wheel rotation or speed

oluwayinkaesan
Автор

Hey, great vid man. How should I get pulse per rotation and the decodeNumber?

ducla
visit shbcf.ru