Hello all!
I'm fairly new to electronics. I salvaged a motor that has a yc-52010 encoder attached. My goal is to run the motor, and use the encoder to calculate its speed/position.
I got this motor driver and it runs the motor no problem. But I'm struggling to figure out how to use the encoder. I'm using an Arduino uno r3.
The encoder looks like this:
So I've obviously figured out that I put the output from the motor driver into the Motor+ and Motor-. This works fine and the motor moves forwards and backwards how I expect.
I connected Arduino's 5v into Hall Sensor Vcc, and connected Hall Sensor GND to ground. The part I haven't figured out how to properly read the output from Hall Sensor A and B. I've tried a bunch of different stuff, to no avail.
Attempt #1:
My first, nieve, attempt was to simply connect them to Analog In pins, do an analogRead and see if I could make sense of the data I got back.
I did get data back, but it didn't seem to line up with what I wanted. Sensor A would always send 0 when the motor was idle, and 1 or 2 when I was driving it. But even when I manually prevented the motor from moving it was still sending 1 while the motor was trying to move. Sensor B would send numbers between 48 and 60.
At least, since I am getting some data from the encoder it leads to to believe that it's not broken. But I suspect I am reading it wrong here. So I started doing research into motor encoders.
Attempt #2:
I found this article which has a library for using motor encoders. So I download that, set it up as their examples show, but all I get back are zeros from the read() function even when the motor is turning. I see this library uses interrupts, so I start reading about those to help narrow down the issue.
Attempt #3:
I found this article which has a very short code sample for reading data from an encoder using interrupts. I'm trying to get this working, just to experiment with interrupts as I've never used them before. But I never see my interrupt functions get called.
I made sure to use digital pins 2 and 3. I passed these to digitalPinToInterrupt() as per the documentation, which returns 0 and 1, and also made sure to make my variables volatile as per the documentation. I tried both RISING, FALING, and CHANGE, but I never see callbacks no matter which I use.
My Question:
Are interrupts the right way to go about reading from this encoder? If so, why don't I see my callbacks getting called? If not, what should I be using?
Below is my code for attempt #3, where I don't see any interrupts being called (_signalA and _signalB are always 0).
Thanks for reading!
const int Button1Pin = 6; //left one, controls direction
const int Button2Pin = 7; //right one, turns on motor
const int Pot1Pin = 0;
const int MotorPWM = 10;
const int MotorIn1 = 11;
const int MotorIn2 = 12;
const int MotorStby = 8;
const int MotorSensorAPin = 2;
const int MotorSensorBPin = 3;
#include "SparkFun_TB6612.h"
Motor motor1 = Motor(MotorIn1, MotorIn2, MotorPWM, 1, MotorStby);
volatile int _signalA = 0; void signalA() { _signalA++; }
volatile int _signalB = 0; void signalB() { _signalB++; }
void setup()
{
Serial.begin(9600);
Serial.println("setup()");
pinMode(Button1Pin, INPUT);
pinMode(Button2Pin, INPUT);
pinMode(MotorSensorAPin, INPUT);
attachInterrupt(digitalPinToInterrupt(MotorSensorAPin), signalA, FALLING);
pinMode(MotorSensorBPin, INPUT);
attachInterrupt(digitalPinToInterrupt(MotorSensorBPin), signalB, FALLING);
}
bool _backwards = false;
int _backwardsLastChanged = 0;
void loop()
{
int button1State = digitalRead(Button1Pin);
int button2State = digitalRead(Button2Pin);
float pot1State = analogRead(Pot1Pin) / 1020.0;
int now = millis();
if (button1State)
{
if (now - _backwardsLastChanged > 100)
_backwards = !_backwards;
_backwardsLastChanged = now;
}
int motorSpeed = 255 * pot1State;
if (_backwards)
motorSpeed = -motorSpeed;
Serial.print("button1=");
Serial.print(button1State);
Serial.print(" button2=");
Serial.print(button2State);
Serial.print(" pot1=");
Serial.print(pot1State);
Serial.print(" motorSpeed=");
Serial.print(motorSpeed);
Serial.print(" _signalA=");
Serial.print(_signalA);
Serial.print(" _signalB=");
Serial.print(_signalB);
Serial.println("");
if (button2State == HIGH)
motor1.drive(motorSpeed);
else
motor1.standby();
delay(1);
}