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);
}
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().
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.
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
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--;
}