Optical encoder too output moter adafruit motor sheld help

Please can someone help me I'm still learning and don't fully understand coding yet I am able to read my optical encoder from a Epson printer and I want to use the optical readings to control a motor on my motor sheld basically motor would mimic encoder position . I can get either motor to work on test or encoder to work but for the life of me I can't get the motor to follow the encoder is there a basic library I can use to get me going

You need to post your code so we can see what you can see.

Sounds like you have most of the problem solved.

And please use the code button </>so your code looks like thisand is easy to copy to a text editor

…R

#include <Encoder.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>

Encoder myEnc(5, 6);
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_StepperMotor *myMotor1 = AFMS.getStepper(200, 2);
Adafruit_StepperMotor *myMotor2 = AFMS.getStepper(200, 1);



void setup() {
  Serial.begin(9600);
  Serial.println("MY bad coding:");
  AFMS.begin();  // create with the default frequency 1.6KHz
  myMotor1->setSpeed(100);
  myMotor2->setSpeed(100);
}

long oldPosition  = -999;

void loop() {

{
}
  long newPosition = myEnc.read();
  if (newPosition != oldPosition) {
    oldPosition = newPosition;
    Serial.println(newPosition);
    
  if(newPosition == oldPosition){
       myMotor1->setSpeed(0);
        oldPosition = 1;}


}

}

also another try lol

*/


#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include <Encoder.h>

Encoder myEnc(2, 3);
// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 
// Or, create it with a different I2C address (say for stacking)
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61); 

// Connect a stepper motor with 200 steps per revolution (1.8 degree)
// to motor port #2 (M3 and M4)
Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);

const int stepsPerRevolution = 200;  // change this to fit the number of steps per 

int stepCount = 0;  // number of steps the motor has taken
void setup() {
  Serial.begin(9600);
  Serial.println("Basic Encoder Test:");
  AFMS.begin();  // create with the default frequency 1.6KHz
  //AFMS.begin(1000);  // OR with a different frequency, say 1KHz
  
  myMotor->setSpeed(10);  // 10 rpm   
}
void loop() {
  // read the sensor value:
//  Encoder myEnc = read (2, 3);
//  int digitalRead (2, 3); 
  // map it to a range from 0 to 100:
//  int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
  // set the motor speed:
//  if (motorSpeed > 0) {
 //   myMotor->setSpeed(motorSpeed);
    // step 1/100 of a revolution:
    myMotor->step(100, FORWARD, SINGLE);
  }
[\code]

You need to explain what happens when you try the programs - and preferrably just present one program for advice. One of those programs has most of the stuff commented out.

You don't seem to have any debugging code (Serial.println(XXX)) so that you can see if the encoder is producing proper data.

I'm afraid my mind goes blank when I see Adafruit_MotorShield AFMS = Adafruit_MotorShield();. I have no idea how that library works and it seems to me it just makes simple stuff complicated.

What sort of stepper motor are you using? Post a link to its datasheet.

...R Stepper Motor Basics Simple Stepper Code

Hi, What is the motor you are trying to get to act like a servo and follow an encoder.

If its a little DC motor that is rated at 10,000rpm, and no gearbox, you will have to do some rethinking.

Tom... :)

Could you post a picture of your encoder and motor.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Hey guys thanks for taking the time I am a bit new to this and have a slight vision problem nothing like a challenge anyway Ive take some shots to show my poor color codding and wiring lol normal would be neater but for now it is what it is anyways. pins 5-6 red n black wire is the output from the optical encoder orange and purple wire is just 3vlts supplying power to the encoder. I get output signals from encoder in first code i provided but not in second i think it still works in second one but just will not show in the serial monitor application that Ive just discovered how cool and handy . I will be using a nema 17 stepper but i am waiting for this to arrive also using a multi-meter works fine for testing. also this old Epson stepper Ive got and runs on motor test fine . also Power jumper is in off position in photo and external PSU wasn't connected i normally connect a 12vlt 3amp PSU just slapped it together for some pics

|500x375 |500x375 |500x375

Hi,

How are you powering the stepper motor?

Tom... :)

I have external PSU 12vlt 3amp that connects too the the motor shield but I mainly just use my multimeter and test coil output m1-m2-m3-m4 connectors for voltage changes . If testing without external psu I just connect jumper and use my multimeter that works fine on all the motor test library’s dc motor test also works as It should

If the Adafruit motor shield is just a h-bridge it probably consumes a lot of Arduino resources just to make the motor move. A specialized stepper driver (such as a Pololu DRV8825) takes a large computational load off the Arduino which only needs to produce step and direction signals - in your case just a step signal.

...R

Hi thanks as far as I know it has two H Bridge chips and some other processing chip on the motor shield it would be quite disappointing if this board can't do this basic task surly someone has done or it prior too me maybe if I some how could integrate the encoder library into the Adifruit library somehow it could work I really have know idea but I certainly don't like giving up I have a external motor driver I can use but I was hoping I could just use this motor shield as I have additional plans for this board

Post a link to the datasheet for the Adafruit board.

...R

https://cdn-shop.adafruit.com/datasheets/TB6612FNG_datasheet_en_20121101.pdf

That link is for the chip - not the Shield. However assuming that is the chip on the shield the first line says it all

Driver IC for Dual DC motor

It is not specialized for a stepper motor.

...R

It definitely can run two steppers or 4 DC Motors maybe this link will explain more

https://cdn-learn.adafruit.com/downloads/pdf/adafruit-motor-shield-v2-for-arduino.pdf

Thanks for your help much appreciated

Also i just seen in the link above this command sounds like it can follow another motor I'm wondering if I can map encoder to run a fake motor on Arduino and copy that action too motor shield maybe a long shot .

uint8_t onestep(uint8_t dir, uint8_t style); The oneStep() function is a low-level internal function called by step(). But it can be useful to call on its own to implement more advanced functions such as acceleration or coordinating simultaneous movement of multiple stepper motors. The direction and style parameters are the same as for step(), but onestep() steps exactly once.

Mydakota: It definitely can run two steppers or 4 DC Motors maybe this link will explain more

I know that. And I can just about carry a 25kg bag of coal - it's much the same problem.

Controlling steppers with a h-bridge is hard work for an Arduino because it has to calculate the timing and order of the pulses for each stepper coil.

A specialized stepper driver takes that load off the Arduino just like a wheelbarrow makes it easier to move coal.

Also, and importantly, reading an encoder puts a high computational burden on the Arduino so you need to relieve it of other avoidable jobs.

Unfortunately the marketing guys like to claim that their super-duper-shield can do everything.

...R

Omg such a waste of money and time yeah there marketing guys make it sound like it can do anything grrr tempted to ask for my money back lol but that would be even more time wasted I have order a cheep $40 driver from eBay . |500x500 Please tell me this will work lol Thanks for all your help

you could try finding a stepper motor driver used for 3d printer, Those are really cheap and could do the job well since you do use a small stepper motor. The stepper motor driver you link are used for the big guns ( Very big stepper motor ) unless you have plans to change the motor into somthing bigger in the future.

maybe a good google search term is " Stepper motor driver 3d printer"

@Mydakota, we have been concentrating on the Adafruit board and I don't think you have posted a link to the datasheet for your stepper motor. We need that to know if a motor driver is suitable.

And the picture of the driver in Reply #15 is not a substitute for a link to the driver datasheet which will have all the details.

...R

Ok I think we have worked out the the adafruit driver shield can't perform this task anyway the motor in photo is just some old stepper out of a old Epson r210 printer I am upgrading stepper soon too a nema17 maybe bigger in future . Anyway I mainly test the driver boards motor out puts with a volt meter the type u test voltage with . I do not find its really required to connect a motor . As it's very easy to tell if the code is working or not. I would be interested to see if this board can even run a DC motor in the way I want but if you know a way to make a DC motor follow the encoders out put using the motor shield m1-m4 ports I would love to see this work . And possibly see if that code can be modified for a stepper.

|500x287

This is what it's too control but I have multiple builds I'm playing with that I want to have motors follow the encoders. I was just hopping to use the adafruit motor shield after there sales video made it sound amazing lol but sounds like it's not possible or know one knows how