ESP32 encoder quadrature/rotary encoder

Bazuka1988
Posts: 1
Joined: Mon Dec 14, 2020 10:22 pm

ESP32 encoder quadrature/rotary encoder

Postby Bazuka1988 » Mon Dec 14, 2020 10:28 pm

Hello, I have a problem with the implementation of the code that would support esp32 quadrature encoders. Anyone know how to do it on interrupts? I only need to read impulses. I have 1920 per revolution on these engines. The arduino waved very nicely. Anyone have any idea ?? There are some libraries on google, but I don't understand how to implement it.

It's arduino code, it worked great. Taken from the website https://wiki.dfrobot.com/Micro_DC_Motor ... U__FIT0450
As in the link, I have the same motors and encoders.

[Codebox]
//The sample code for driving one way motor encoder
const byte encoder0pinA = 2;//A pin -> the interrupt pin 0
const byte encoder0pinB = 3;//B pin -> the digital pin 3
byte encoder0PinALast;
int duration;//the number of the pulses
boolean Direction;//the rotation direction


void setup()
{
Serial.begin(57600);//Initialize the serial port
EncoderInit();//Initialize the module
}

void loop()
{
Serial.print("Pulse:");
Serial.println(duration);
duration = 0;
delay(100);
}

void EncoderInit()
{
Direction = true;//default -> Forward
pinMode(encoder0pinB,INPUT);
attachInterrupt(0, wheelSpeed, CHANGE);
}

void wheelSpeed()
{
int Lstate = digitalRead(encoder0pinA);
if((encoder0PinALast == LOW) && Lstate==HIGH)
{
int val = digitalRead(encoder0pinB);
if(val == LOW && Direction)
{
Direction = false; //Reverse
}
else if(val == HIGH && !Direction)
{
Direction = true; //Forward
}
}
encoder0PinALast = Lstate;

if(!Direction) duration++;
else duration--;
}
[/Codebox]

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 70 guests