Engine encoder to account for speed and RPM

Hello everyone, I am currently working on the project "Barrier". And for this I ordered 12V motors with encoder.
I connected the motors to the power supply, everything is working. But I uploaded the code to esp 32, and the encoders do not take into account the speed. Can you please help?
Here is the code and diagram:

#define ENCODER_A 32 // Pin for Encoder A
#define ENCODER_B 33 // Pin for Encoder B
volatile int encoder_value = 0; // Global variable for storing the encoder position
void encoder_isr() {
  // Reading the current state of encoder A and B
  int A = digitalRead(ENCODER_A);
  int B = digitalRead(ENCODER_B);
  // If the state of A changed, it means the encoder has been rotated
  if ((A == HIGH) != (B == LOW)) {
    encoder_value--;
  } else {
    encoder_value++;
  }
}
void setup() {
  Serial.begin(115200); // Initialize serial communication
  pinMode(ENCODER_A, INPUT_PULLUP);
  pinMode(ENCODER_B, INPUT_PULLUP);
  // Attaching the ISR to encoder A
  attachInterrupt(digitalPinToInterrupt(ENCODER_A), encoder_isr, CHANGE);
}
void loop() {
  Serial.println("Encoder value: " + String(encoder_value));
  delay(100);
}

The diagram shows the arduino uno. The only difference is not pin 2 and 3, but 32 and 33

I do not see any place in your code where you attempt to compute speed.
I do see that you do not protect "encoder_value" from being changed while you are printing it in loop().

1 Like

The end result is that I have to calculate the speed, but I started small.
That is, if I turn the shaft, nothing will change, that is, the result will be 0

Nice picture but same as is a big red flag. Please post an annotated schematic, your petty picture is useless to me. Also post a link giving technical information on the motor and other parts.

1 Like

I am unable to read you mind from this far away. What is your definition of speed?

1 Like

OK, the motor model is "maxon motor 344515"

How I connected:

I did not find basic and understandable technical information. The seller also does not inform

Connected in the same way and uploaded the same code.
Manually screwed the motor shaft, nothing changes. I tried the same arduino nano

I would just like to understand how it works.
First I want to set the speed and make a certain turn. And maybe I'll change the speed and turnover in the code.
Having understood how it works, after that I would like to move on to the main algorithm

As for the drivers, I chose:
DRV8833
image

The encoder works with this code, but it is reset to zero.

int E1 = 4; //MOTOR_1
int M1 = 5; //MOTOR_1

const byte encoder0pinA = 2; //A pin ->interrupt pin 0
const byte encoder0pinB = 3; //B pin ->digital pin 3
byte encoder0PinALast;
int duration;//the number of the pulses
boolean Direction; //the roration direction

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

  pinMode(M1, OUTPUT);
  digitalWrite(M1, LOW);
  analogWrite(E1, 100);

}
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--;
}

Since this function is NEVER called in your code, the compiler deletes it.

1 Like

How do I call this function correctly?
Should I Do so:

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

Well, that does call it. The delay(100) will not be of much help in determining the speed of something.

Thank you, I understood this code. Now I can start executing the algorithm.
I wanted to make sure I could change the rotation speed and shaft rotations in the code.

The speed couldn't. If I decrease the value by 100 instead of power, the shaft will not rotate.
After a turnover of more than 20,000, this is not possible. That is, in Serial, the value 20,000 is not shown.

Is there anything that can be done?

int E1 = 4; //MOTOR_1
int M1 = 5; //MOTOR_1
int S1 = 12; //PWM
const byte encoder0pinA = 2; //A pin ->interrupt pin 0
const byte encoder0pinB = 3; //B pin ->digital pin 3
byte encoder0PinALast;
int duration;//the number of the pulses
boolean Direction; //the roration direction

void setup() {
  Serial.begin(57600); //Initialize the serial port
  EncoderInit(); //Initialize the module
  pinMode(M1, OUTPUT);
  pinMode(E1, OUTPUT);
  pinMode(S1, OUTPUT);
  digitalWrite(M1, LOW);
  digitalWrite(E1, LOW);


}
void loop()
{
  Serial.println(duration);
  if (duration < 20000)
  {
   analogWrite(S1, 10);
    digitalWrite(M1,HIGH);
  }
  else {
    digitalWrite(M1,LOW);
  };
  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--;
}

Probably not, since you never change duration.

1 Like

How do I change this duration?
So that I can change the value more than 20,000

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.