hello guys,im trying to make a wind direction determine device on a weather station im on,im using a rotary encoder for this,from a car radio,when im analyzing the rotary encoder it seems it is losing steps,for example i mark a start location on the encoder,then i make a full turn and i get on my monitor 48"pulses",btw i added on the code a reset for the counter,when im turning faster sometimes i get 30-34 pulses on a full rotation,what is going on? is the encoder? or the code?
#define outputA 3 #define outputB 4
int counter = 0;
int aState;
int aLastState;
void setup() {
pinMode (outputA,INPUT);
pinMode (outputB,INPUT);
Serial.begin (9600);
// Reads the initial state of the outputA
aLastState = digitalRead(outputA);
}
void loop() {
aState = digitalRead(outputA); // Reads the "current" state of the outputA
// If the previous and the current state of the outputA are different, that means a Pulse has occured
if (aState != aLastState)
{
// If the outputB state is different to the outputA state, that means the encoder is rotating clockwise
Am I correct in assuming the encode sends a pulse for each Position it moves? If so, how Long does it stay low after the pulse? You are constantly checking for a high on the Pins and comparing it to the previous reading. What it you tested, got a high and while the rest of your code was running, the Pin went low and then high again? On the second test, you would see that it is the same state as last time (high) and incorrectly assume that it has not moved. That would cause you to lose pulses.
I don't know how Commercial weather sensors work, but I think it would be better to use a potentiometer attached to an analog port. The resistance varies depending on the Rotation Position and all you have to do is read the resistance (Volts).
JaBa:
Am I correct in assuming the encode sends a pulse for each Position it moves? If so, how Long does it stay low after the pulse? You are constantly checking for a high on the Pins and comparing it to the previous reading. What it you tested, got a high and while the rest of your code was running, the Pin went low and then high again? On the second test, you would see that it is the same state as last time (high) and incorrectly assume that it has not moved. That would cause you to lose pulses.
if you have some white and some black, you can use an IR reflector to know if it is seeing black or white.
paint half your shaft white, the other half black and you can know which side you are on.
make a disk and punch holes. 360 holes ? then use an optical-interuper to see through the holes. this would allow you to see one hole for each degree of rotation.
add one more hole somewhere else and that becomes 'home' so, if you think you are at 355 when you hit that home hole, you just re-set to 0/360
as for post #5, if you have two IR sensors, offset by 1/2 hole size. one would be turing off as the hole passed, the other would be turning on as it pased.
this gives you more resolution as with only one sensor, you only know you are either on a hole or on a solid.
as for your picture, it has no value. what is the spec ? how many pulses per revolution ? does it have a home output ?
Your actual device is not what you pictured, this picture is something off a website.
take some time with google and encoder and read some of the different types.
I agree with JaBa in that you may be missing some counts with your polling method. Here is your code adopted to use an interrupt on pin 3. The interrupts require certain adjustments in making variable volatile and making sure that readings can not be changing. See Nick Gammon's tutorial Gammon Forum : Electronics : Microprocessors : Interrupts
#define outputA 3
#define outputB 4
volatile int counter = 0;
int copy_counter;
int last_copy_counter;
volatile boolean aState;
volatile boolean bState;
void setup() {
pinMode (outputA, INPUT);
pinMode (outputB, INPUT);
Serial.begin (115200);
Serial.println("Starting Encoder Counts");
attachInterrupt(digitalPinToInterrupt(3), readEncoder, CHANGE);
}
void loop() {
noInterrupts();
copy_counter = counter;
if (counter >= 47 || counter <= -47)
{
counter = 0;
}
interrupts();
if (copy_counter != last_copy_counter)
{
Serial.print("Position: ");
Serial.println(copy_counter);
}
last_copy_counter = copy_counter;
}
void readEncoder()
{
aState = digitalRead(outputA);
bState = digitalRead(outputB);
// If the outputB state is different to the outputA state, that means the encoder is rotating clockwise
if (bState != aState)
{
counter ++;
}
else
{
counter --;
}
}
kaloudis94:
Ive just tried your code,and now im getting something like that,same readings over and over and after so many rotation maybe they change
Ok,i've looked up to your code and i changed my pins to 2,3 as i saw on an article those two are the interupt pins,well now im getting readings (i changed the code inpute pins) but,i still miss some readings but its way better than i was,is there any way its the encoder problem?
The interrupt based code in my first reply should not be missing counts. How have you determined it is missing counts? What speed is it turning when it is missing counts?
Can you link the data sheet for your encoder? Does it have detents? The code you started with and I converted to interrupts only measures half of the available quadrature states. Are the "missing counts" when you stop turning at a position which is not one of the increment positions.
The low pass filter in your debounce circuit my be slowing your signal rise and fall times, but I'm not very knowledgeable about hardware. You could try to lower the value of the 10K pull up resistors (4.7K,2K?) and try lower the 10K R on the output side. Perhaps just leave the capacitor across the encoder switch.
Thank you all for your responses!!!!!!thanks for the hardware Opinions,well now
cattledog:
The interrupt based code in my first reply should not be missing counts. How have you determined it is missing counts? What speed is it turning when it is missing counts?
Can you link the data sheet for your encoder? Does it have detents? The code you started with and I converted to interrupts only measures half of the available quadrature states. Are the "missing counts" when you stop turning at a position which is not one of the increment positions.
Well I cant tell you the datasheet of the product,Its just a Rotary ancoder from a a car audio player,just 3 pins and 2 extra for button press but im not using that option in my circuit.So now,a full rotation of my Rotary encoder is about 48 pulses as i saw from my serial port monitor,with my code when i was just rotating the Rotary encoder one full rotation the monitoring count 48 pulses,but when i was just jugling it faster full rotation monitor counted about "38" or "30",with your code its obviously better but after many rotation i still lose the positioning,what i mean is again when im doing for example 4 full rotations with my mark i dont get the 0 positioning,im getting like 10 or 20 steps under that. again thank you for your response!
but after many rotation i still lose the positioning,what i mean is again when im doing for example 4 full rotations with my mark i dont get the 0 positioning,im getting like 10 or 20 steps under that
Can you please provide the serial output for this?
Have you actually constructed the wind vane itself yet? I would be interested to see the design. I am currently using the inexpensive plastic vanes sold on eBay as spares for certain weather stations. But I find they blow about too easily and I can not get a reliable reading even when I average over hundreds of readings.
Here is my 0 mark position i should be after 5-6 full rounds
You need to be precise here. Which is it 5 or 6. Where there any reversals or was it all unidirectional?
In the future, please just cut and paste from the serial monitor and post within a code block instead of attaching an image.
Why do you believe that you have 48 (0-47) positions on the encoder?
If there were 52 positions, then 5 x 52 = 260. Are there any detents to help you judge? Is the expected difference from the unidirectional 48 turn calculation consistent and linear?
the design is very simple i can attach pics...Well i know how to construct a very accurate wind vayne,but it costs time and money,so i got that idea and i thought i could get it to work
here is a gallery link cause i didnt manage to link the images PIcs
You need to be precise here. Which is it 5 or 6. Where there any reversals or was it all unidirectional?
Why do you believe that you have 48 (0-47) positions on the encoder?
i have marked a 0 position on a paper,then im doing a full rotation,then im getting a reading of 48 "pulses" thats how i know i got 48 positions on my encoder,and it doesnt matter what number of rotation i get false readings,it could be on the first if you turn it fast,the point is that is happening,there is not a standard spot it happens