Rotary encoder output to lcd 1602a

Hi, I am new to arduino and need some direction. I would like to output the position of my rotary encoder shaft to a lcd screen. The shaft position would start at 1 and terminate at 100, which would be 1 full revolution of the shaft. So if the shaft was turned 1/4 turn to the right the lcd readout would indicate 25.00. And a 1/2 turn would indicate 50.00 on the lcd readout. If the shaft was rotated to the left the lcd readout would indicate 75.00 or 50.00 if rotated 1/2 turn to left. I have Arduino 1.6.5 installed on my computer. I have a Mega 2560 board 1620a lcd screen Yumo-E6B2-CWZ3E Rotary encoder with a resolution 1024 P/R I also have a basic adruino kit which includes a bread board, wires, resisters etc. I have looked through tutorials and examples but havn't come across anything resembling this. Does anyone have any sample sketches and wiring diagrams that I could work with. Bill

Wow, 100 pulse/revolution rotary encoders are expensive! http://www.digikey.com/product-search/en?pv395=4&FV=fff4001e%2Cfff80033&k=rotary+encoder&mnonly=0&newproducts=0&ColumnSort=0&page=1&stock=1&quantity=0&ptm=0&fid=0&pageSize=25

I don't see a datasheet for Yumo-E6B2-CWZ3E. Counting encoder pulses and displaying them is pretty straightforward. See the code here for counting them, change as needed for your display. http://forum.arduino.cc/index.php?topic=318170.0

hi, Thanks for the reply. Really appreciate the help. This is the encoder i'm using.

https://www.sparkfun.com/products/11102

I followed your link and used this code. I was able to hook everything up and have it output to serial. How can I calibrate or modify this sketch so that one revolution of the shaft would display 100.00. if I turn CCW 1/4 turn it would display 75.00. There would be a dial on the encoder shaft that would be marked out evenly 1 - 100. I also want to use the high resolution of this encoder to indicate the dial position when it is between a number ie: 12.6572.

Once I can get it working and display it on the serial monitor I would like to have the encoder ouput to my lcd 1620a. I have no idea how to wire this to my adruino mega 2560 board, or how to include the lcd 1620a in the code. Bill

/* Rotary encoder with attachInterrupt Counts pulses from an incremental encoder and put the result in variable counter. Taking also into account the direction and counts down when the rotor rotates in the other direction. This code is used attachInterrupt 0 and 1 which are pins 2 and 3 moust Arduino. For more information about attachInterrupt see: http://arduino.cc/en/Reference/AttachInterrupt

created 2014 by Ben-Tommy Eriksen https://github.com/BenTommyE/BenRotaryEncoder

*/

// Encoder connect to digitalpin 2 and 3 on the Arduino.

volatile unsigned int counter = 0; //This variable will increase or decrease depending on the rotation of encoder

void setup() { Serial.begin (9600); //Setting up interrupt //A rising pulse from encodenren activated ai0(). AttachInterrupt 0 is DigitalPin nr 2 on moust Arduino. attachInterrupt(0, ai0, RISING);

//B rising pulse from encodenren activated ai1(). AttachInterrupt 1 is DigitalPin nr 3 on moust Arduino. attachInterrupt(1, ai1, RISING); }

void loop() { // Send the value of counter Serial.println (counter); }

void ai0() { // ai0 is activated if DigitalPin nr 2 is going from LOW to HIGH // Check pin 3 to determine the direction if(digitalRead(3)==LOW) { counter++; }else{ counter--; } }

void ai1() { // ai0 is activated if DigitalPin nr 3 is going from LOW to HIGH // Check with pin 2 to determine the direction if(digitalRead(2)==LOW) { counter--; }else{ counter++; } }

Try:

Serial.println(counter * 100.0 / 1024);

Thanks :) I tried it and it moved the decimal place to 0.00. Full rotation goes to 6400.00

Can't be that difficult but beyond me at the moment.

If a full rotation goes to 6400.00, divide it by another 64.

1024/64 = 16, so 100/16= 6.25

Serial.println(counter * 6.25); // default two decimal places

or

Serial.println(counter * 6.25, 3); // three decimal places

Leo..

Wow, Sparkfun is so expensive!

http://www.ebay.com/itm/360P-R-Incremental-Rotary-Encoder-AB-phase-encoder-6mm-Shaft-W-coupling-New-/141686141372?hash=item20fd26f1bc

MarkT: Wow, Sparkfun is so expensive!

http://www.ebay.com/itm/360P-R-Incremental-Rotary-Encoder-AB-phase-encoder-6mm-Shaft-W-coupling-New-/141686141372?hash=item20fd26f1bc

Just wanna throw out that this one is the closest one to what OP bought. OP's has almost three times the p/r of what you posted.

Wawa: If a full rotation goes to 6400.00, divide it by another 64.

1024/64 = 16, so 100/16= 6.25

Serial.println(counter * 6.25); // default two decimal places

or

Serial.println(counter * 6.25, 3); // three decimal places

Leo..

hi Leo, Thanks for the input. I tried your suggestion but no luck. I was also mistaken about the total value after 1 revolution. it showed 65535 in the serial monitor. I also noticed the display in the serial monitor was jumpy . I guess this is because of the high resolution of the encoder 1024/pr. I wanted a high res encoder to track the movement of the shaft to something like 4 decimal places. so if the shaft stopped at 10 it would show 10.0000. The idea of this project is to be able to accurately measure the distance between the start and stop position of the encoder shaft. For now I would just take the start value displayed and subtract it from the stop value. In the future I would like to be able to display the results in a graph. Bill

Hi,
The encoder you have is a chinese knock off of an Omron encoder.

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

Can you please post a copy of your sketch, using code tags?
They are made with the </> icon in the reply Menu.
See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

Tom… :slight_smile:

e6b2.PDF (125 KB)

Hi, That ebay one is a bit old too. One of the notes in the spec says.

Notice:AB 2phase output must not be directly connected with VCC, otherwise, will burn the output triode, because different batches, and may not have the terminal

So if you use it you will have to wait for it to stop humming and warm up. Also some batches have no terminals.

Tom.... :)

I think if you want exactly 100 steps/rev, you should use an encoder that can produce 100 when divided by an integer. e.g 1000 or 2000 steps, not 1024 steps.

Are you expecting a higher resolution than the encoder resolution? Leo..

Hi, If you have 1024 steps per revolution and you want 100 steps with 1 step precision, it is not going to happen. 1024/100 = 10.24 steps per needed step. You cannot get 0.24 of a step input.

The OEM, Omron and the chinese mob, quote a 100 and a 2000 ppr model. The 2000 model will give you 2000/100=20 step per needed step. 1/20= 0.05 resolution.

What is your application?

Tom.... :)

Thanks for replying, With this project I want to use the rotary encoder to indicate the position of a dial. In this case a dial for a safe. Safe dials are numbered 0-100. By attaching the encoder to the dial i would be able to relay the exact position of the dial to an lcd readout. So if I set the safe dial at 0, turn on the adruino board and rotate the dial to the right and stop in the middle of 10-11 the encoder would relay the position and print to screen something like 10.56. If I rotate the dial to the left past zero and stop on 75, the encoder would read this and print to screen 75. I know I have a high resolution encoder but was hoping I could still make it work... Maybe by using the map() function with something like this Serial.print(map(counter, 0, max_encoder_count, 0, 99))

My hardware setup is very basic at this point. the encoder is wired to my mega 2650 board this way Encoder Wire Black Out A to Digital Pin 2 Mega Board Encoder Wire White Out B to Digital Pin 3 Mega Board Encoder Wire Brown 5V to 5V Mega Board Encoder Wire Blue OV(Common) to GND Mega Board I have the Board Plugged into my laptop using the USB serial cable and view the results using the serial monitor in the adruino ide. I am using the following code example

/* Rotary encoder with attachInterrupt
Counts pulses from an incremental encoder and put the result in variable counter. 
Taking also into account the direction and counts down when the rotor rotates in 
the other direction.
This code is used attachInterrupt 0 and 1 which are pins 2 and 3 moust Arduino.
For more information about attachInterrupt see:
http://arduino.cc/en/Reference/AttachInterrupt

created 2014
by Ben-Tommy Eriksen
https://github.com/BenTommyE/BenRotaryEncoder

*/

// Encoder connect to digitalpin 2 and 3 on the Arduino.

volatile unsigned int counter = 0;  //This variable will increase or decrease depending on the rotation of encoder

void setup() {
  Serial.begin (9600);
  //Setting up interrupt
  //A rising pulse from encodenren activated ai0(). AttachInterrupt 0 is DigitalPin nr 2 on moust Arduino.
  attachInterrupt(0, ai0, RISING);
  
  //B rising pulse from encodenren activated ai1(). AttachInterrupt 1 is DigitalPin nr 3 on moust Arduino.
  attachInterrupt(1, ai1, RISING);
}

void loop() {
  // Send the value of counter
   // Serial.println (counter);
   Serial.print(map(counter, 0, max_encoder_count, 0, 99)).
}

void ai0() {
  // ai0 is activated if DigitalPin nr 2 is going from LOW to HIGH
  // Check pin 3 to determine the direction
  if(digitalRead(3)==LOW) {
    counter++;
  }else{
    counter--;
  }
}

void ai1() {
  // ai0 is activated if DigitalPin nr 3 is going from LOW to HIGH
  // Check with pin 2 to determine the direction
  if(digitalRead(2)==LOW) {
    counter--;
  }else{
    counter++;
  }
}

This code does not compile due to the "max_encoder_count" was not declared error. Any help in pointing me in the right direction would be greatly appreciated Bill

adruino.jpg|288x149

williamjcoates: hi, Thanks for the reply. Really appreciate the help. This is the encoder i'm using.

https://www.sparkfun.com/products/11102

I followed your link and used this code.

The code you quoted is not reliable as it samples only rising edges.

You have to sample rising and falling edges to get reliable quadrature decoding.

Hi MarkT, Thanks For pointing that out. Do you have a link to some sample code that I could work with. Being very new to arduino I am not sure what would be better code. For now I just want to have the encoder output from 0 - 100 for one rotation to the serial monitor. But because the encoder is 1024 P/R there is no way to code the output to read 0-100? If not that would it be possible to measure in degrees of rotation? Bill

Did you look at the playground?

http://playground.arduino.cc/Main/RotaryEncoders#Example3

Not too much experience with encoders, but isn't OP's one outputting 3-bit Grey code.

I think 360 steps from a 1024 encoder would be as impossible as 100 steps. Leo..

Thanks MarkT, I will have a look. I have been reading threads and going back over some as the coding becomes clearer. I did do this

void loop() {
  // Send the value of counter
   Serial.println (counter / 20.24);
   
}

And it goes up to 100.00 on a CW rotation but when i go CCW it starts at 3230.00. weird. The thing to remember is that I am using the rotary encoder as a means to measure the distance between two points on the dial. I then would take this value and put it on a graph. I am starting to get somewhere with this. Bill

Hi Do you have to have exactly 100 steps per turn, do you have the encoder knob marked off in 1/100 divisions.

If not. In other words a blank knob, you can have 1024/100= 10.24pulses per 1/100 of turn. Who's to know that its not exact. I gather you have the position readout on the display.

If you turn the knob 1 turn, how are you to know that its only 99 steps, a little turn more and it 100. the knob position has nothing to be referenced against. You only want the numbers to be displayed. That is if you do not mark/calibrate the knob.

Tom... :)