esp32 and JGY-371 12V DC motor with encoder

Thread Starter

Mike Weide

Joined Dec 18, 2018
4
I am having a problem with the coding for the esp32 and DC motors with encoders. Most of the examples on the internet use a pid system that includes analogWrite (). However, esp32 doesn't support analogWrite() functions. Does anyone have experience with the esp32 and motor encoders? By the way I am connecting the motor using an L293D if that makes any difference.
I have the above mentioned motor that I would like to run in a clockwise and counterclockwise direction as well as have it go a certain amount then return the same amount. This is not for a forward, backward type of project like a robot or making two different motors correct to each other to drive straight. Mine is to raise and lower an object. I have it working with a simple JX180-370 motor but when it moves the amount designated by seconds in the code it is not consistent and that is why I am switching to one with an encoder.
I have tried the many encoder examples for general boards...compiling error. Then I tried the esp32 encoder library example...compiling error. The last error has to do with encoder.attachFullQuad not defined. This is just the example without any modifications so there must be something wrong with the library.
Anyways, any help or websites you can direct me to would be appreciated. I am kind of new at coding so I need an example then I can change to fit my needs.
 

Thread Starter

Mike Weide

Joined Dec 18, 2018
4
I actually have a limit switch that I send the object down to. Then when I move the object say delay(5000) when it returns the same delay time it doesn't return to the same point. The differences keep growing as it moves multiple times. That is why I want the limit switch set the counter at zero then I can move the same each time that it runs.
 

MaxHeadRoom

Joined Jul 18, 2013
28,704
Maybe a simple motor shaft slot opto sensor or a prox SW etc, depending on how precise a move you need, just wonder if you need PID?
Max.
 

Thread Starter

Mike Weide

Joined Dec 18, 2018
4
This first motor is the JGY-371. I had first made up the prototype with another motor, the jsx180, and this one is the same size so it fits into the project well. it has an encoder built into the design so I would like to make it happen with this one. It doesn't need to be that precise but it needs to loop for 5-10 times without huge difference in movement
JGY-371.jpg .jsx180.jpg
 

Thread Starter

Mike Weide

Joined Dec 18, 2018
4
Code:
This is for the ble functionality
void home() {
  pinMode (home_switch, INPUT_PULLUP);

  while (digitalRead(home_switch)) {  // Do this until the switch is activated
    mot_down();
   }
   while (!digitalRead(home_switch)) {
    mot_up();
   }
}

void mot_up() {
   digitalWrite(MOT_1F, 1); // turn on
  
}

void mot_down() {
   digitalWrite(MOT_1R, 1); // turn on
    
}

void mot_stop() {
   digitalWrite(MOT_1F, 0); // turn off
   digitalWrite(MOT_1R, 0); // turn off
}

void mot_multi() {
          home();
          steps = 0;
          int n;
          for (n = 0; n < 2; n++) {     // Loop to do "something" n times
            digitalWrite(MOT_1R, 1);    // turn on
            delay (8000);
            digitalWrite(MOT_1R, 0);    // turn off
            delay (1000);
            digitalWrite(MOT_1F, 1);    // turn on
            delay (8000);
            digitalWrite(MOT_1F, 0);    // turn off
            delay (1000);
            }
[code]
 
Top