analog Encoder in Matlab and arduino

Hi
I have an encoder which it's channels are analog require, I mean these channels must be connected to the analog pins of the arduino board so, for reading this encoder I have a problem in Matlab, I know that there is a library for encoders in the Matlab and it should be written as:
a=arduino('com7','uno','Libraries','RotaryEncoder');
encoder=rotaryEncoder(a,'D2','D3',12)
but, as I said my encoder channels must be connected to the analog pins and I write this code:
a=arduino('com7','uno','Libraries','RotaryEncoder');
encoder=rotaryEncoder(a,'A0','A1',12)
and it could not be accepted and it shows an error that these pins are not correct for rotary encoder library
my question is how I could read my analog encoder pulses?

 Accepted Answer

Just go ahead and connect the encoder outs to digital pins that you have access to. Then use rotaryEncoder function on those digital pins while spinning the encoder by hand and use readCount to see if it shows results appropriately.
This should work just fine.

3 Comments

tahnk you so much actually, I tested the rotaryEncoder function several days ago but it didn't work because the damage encoder was tested but, why in the Arduino program I must definite the encoder channels with analog pins but, in the Matlab I definite them with digital pins and it works in both of the softwares ?
Here is a link to the encoder library that you are using in your Arduino sketch - https://www.pjrc.com/teensy/td_libs_Encoder.html
This plus the sensor link you provided shows that you are indeed using a quadrature encoder.
The Notes section on the link shows that 'A0' and 'A1' can be used as digital pins. This might be the reason why everything worked seemlessly on the Arduino sketch or maybe the encoder library uses pinMode function if it sees an analog pin and makes it a digital pin.
HTH,
Madhu
ok thanks a lot

Sign in to comment.

More Answers (1)

The rotaryEncoder function is only going to work with quadrature encoders as noted in the documentation.
It seems to me like that analog encoders are usually potentiometers that have some sort of linear relationship between output voltage and degrees moved. You willl need to read the tech specification sheet or do your own testing to understand what the analog output voltages means with regards to the degree of the shaft. You can use the readVoltage function in MATLAB to bring in the voltages and establish the relationship.
Best bet for more help from community users - share existing arduino code to bring in data from this encoder or the spec sheet of the encoder itself.

9 Comments

ok thank you for your answer
I write this code:
for i=1:1000
pos = readVoltage(a,'A0');
fprintf('Current knob position: %d\n',pos);
pause(1);
end
and it shows just 0 and 5 !
Can you give more information on -
1) Is the encoder connected to a motor? If so, how fast is the motor spinning? If not, how are you controlling how fast the encoder is spinning.
2) What encoder are you using?
actually for testing I just turn the magnet handly and the encoder is incremental magnetic
When I google incremental magnetic encoders I get information about rotary encoders that are digital. Are you sure that yours is analog?
yes ofcourse I test this encoder with Arduino program and when I connect the encoder pins to the analog pins of the Arduino board the code read the pulses correctly
Please share the Arduino code you used to test it and possibly the wiring diagrams for the connections you made. Then it would be easier to tell if there are MATLAB equivalents or not.
ok, there is the code:
#include <Encoder.h>
Encoder myEncoder(A0,A1);
double Position;
int t;
void setup() {
Serial.begin(250000);
Serial.println("DC POSITION.");
}
long oldPosition = -999;
void loop() {
long newPosition = myEncoder.read();
if (newPosition != oldPosition) {
oldPosition = newPosition ;
}
Serial.print("t");
Serial.println(t);
Serial.print("Position = ");
Serial.println(newPosition);
analogWrite(5,60);
digitalWrite(8,LOW);
t=millis();
if (t<2000){
digitalWrite(7,HIGH);
}
else if(t>=2000 && t<4000){ digitalWrite(7,LOW);
digitalWrite(8,HIGH);
}
else{ digitalWrite(8,LOW);
}
}
and there is the diagram:
02.png
Could you also post a link to your encoder itself or provide its name? Because it seems more and more like this is a quadrature encoder, in which case you can use the rotaryEncoder function. The reason I ask for the link to encoder is if I can confirm that it is a quadrature encoder or an encoder that can be connected to digital pins then I can recommend exact steps.
Madhu
I have the data sheet of the encoder, I attached it

Sign in to comment.

Categories

Find more on MATLAB Support Package for Arduino Hardware in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!