Author Topic: Trying to read from a KY-040 rotary encoder  (Read 7109 times)

Shimakaze

  • Newbie
  • *
  • Posts: 4
Trying to read from a KY-040 rotary encoder
« on: September 26, 2015, 09:10:11 am »
I have a rotary encoder KY-040 that I connected to the Digispark like this:



CLK go to p0 and and DT go to p2, SW is disconnected for now.

I did everything like the instructions here: http://henrysbench.capnfatz.com/henrys-bench/keyes-ky-040-arduino-rotary-encoder-user-manual/

I only changed the pin numbers in the code:

Code: [Select]

 int pinA = 0;  // Connected to CLK on KY-040
 int pinB = 2;  // Connected to DT on KY-040
 int encoderPosCount = 0;
 int pinALast; 
 int aVal;
 boolean bCW;

 void setup() {
   pinMode (pinA,INPUT);
   pinMode (pinB,INPUT);
   /* Read Pin A
   Whatever state it's in will reflect the last position   
   */
   pinALast = digitalRead(pinA);   
   Serial.begin (9600);
 }

 void loop() {
   aVal = digitalRead(pinA);
   if (aVal != pinALast){ // Means the knob is rotating
     // if the knob is rotating, we need to determine direction
     // We do that by reading pin B.
     if (digitalRead(pinB) != aVal) {  // Means pin A Changed first - We're Rotating Clockwise
       encoderPosCount ++;
       bCW = true;
     } else {// Otherwise B changed first and we're moving CCW
       bCW = false;
       encoderPosCount--;
     }
     Serial.println("Rotated: ");
     if (bCW){
       Serial.println ("clockwise");
     }else{
       Serial.println("counterclockwise");
     }
     Serial.print("Encoder Position: ");
     Serial.println(encoderPosCount);
     
   }
   pinALast = aVal;
 }

Now I don't see anything in the serial monitor in the arduino program, does anyone have any idea what's wrong?
« Last Edit: September 27, 2015, 03:00:28 pm by Shimakaze »

exeng

  • Sr. Member
  • ****
  • Posts: 454
Re: Trying to read from a rotary encoder
« Reply #1 on: September 26, 2015, 01:27:28 pm »
Wonder if you need to use DigiUSB to achieve any serial output. I haven't used serial com with Digispark. See the Wiki on Digispark Basics. From Wiki... "The Digispark does not have a hardware serial port nor a hardware serial to USB converter. An example library (DigiUSB) is provided, as well as some example code and a serial monitor like program, but communication with the computer will not always be plug and play, especially when other libraries are involved."

Shimakaze

  • Newbie
  • *
  • Posts: 4
Re: Trying to read from a rotary encoder
« Reply #2 on: September 26, 2015, 03:29:22 pm »
So to get any indication I'll have to print text to my cursor on windows for  example?

Shimakaze

  • Newbie
  • *
  • Posts: 4
Re: Trying to read from a rotary encoder
« Reply #3 on: September 27, 2015, 10:24:49 am »
So I changed the
Code: [Select]
Serial.println (... with any of the following:

Code: [Select]
DigiKeyboard.write("1");
DigiKeyboard.println("1");
DigiKeyboard.sendKeyStroke(KEY_V);

All of them didn't do anything when rotating the knob...

Any ideas?
Maybe the 10K resistors are too much?

Of course I included <DigiKeyboard.h>
« Last Edit: September 27, 2015, 10:32:31 am by Shimakaze »

exeng

  • Sr. Member
  • ****
  • Posts: 454
Re: Trying to read from a KY-040 rotary encoder
« Reply #4 on: September 27, 2015, 04:41:56 pm »
I was thinking that you would need to use DigiUSB not DigiKeyboard to send to the serial monitor. I've not done this and have only used DigiKeyboard to send keystrokes to a app running on the PC.

If I were debugging this I would first write a simple sketch using DigiUSB to see if I can write text to the serial monitor, say a delayed loop writing "Hello World".

For your encoder, I would get out my volt meter and make should the encoder outputing what you are expecting to see.

Basically,
1. Verify that you can write to the serial monitor with a simple sketch using DigiUSB.
2. Verify that the encoder (without Digispark) is doing what you expect.

exeng

  • Sr. Member
  • ****
  • Posts: 454
Re: Trying to read from a KY-040 rotary encoder
« Reply #5 on: September 27, 2015, 05:19:41 pm »
OK this DigiUSB thing looks to be a little more involved than I first thought. Again, I have not used Digisparks to output to a serial monitor. I've only used them for specific tasks as standalone or slave mircos to control things. Not well versed in how to produce serial output to a monitor.

Perhaps there is someone here better versed in how to produce serial output using DigiUSB.


exeng

  • Sr. Member
  • ****
  • Posts: 454
Re: Trying to read from a KY-040 rotary encoder
« Reply #6 on: September 27, 2015, 05:27:16 pm »
I guess you could use DigiKeyboard and dump to Notepad. I would still verify the encoder behavior.