3 Replies Latest reply on Aug 25, 2015 12:38 PM by Ron Beck

    intel galileo gen 2 working with a rotary encoder

    annu_shah

      hi i am using this encoder to work with the Galileo board.

      "ALPS STEC12E07 Encoder"

      To make it work i downloaded the encoder library from the following link :-Encoder Library, for Measuring Quadarature Encoded Position or Rotation Signals

      when i run the code i get lots of errors with the library.

      Do u have a library or a basic code to run encoder on Galileo.


      50.png



        • 1. Re: intel galileo gen 2 working with a rotary encoder
          Intel_Alvarado

          Hi,

           

          There are libraries that might not work on the Galileo without making some modifications first. The reason is that this library was made for Arduino boards and was written for the Arduino MCU architecture. The Galileo has a different MCU architecture than the Arduino, that’s why you are getting some errors.

          In order to make this library compatible you’ll have to write a new library for this sensor.

           

          Sergio

          • 2. Re: intel galileo gen 2 working with a rotary encoder
            annu_shah

            i am using the following code and the encoder works fine on pin no 8 and 9 but if i change the pin numbers the values are not as accurate.

             

            int val;

            int encoder0PinA = 6;

            int encoder0PinB = 7;

            int encoder0Pos = 0;

            int encoder0PinALast = LOW;

            int n = LOW;

             

             

            void setup() {

               pinMode (encoder0PinA,INPUT);

               pinMode (encoder0PinB,INPUT);

               Serial.begin (9600);

            }

             

             

            void loop() {

               n = digitalRead(encoder0PinA);

               if ((encoder0PinALast == LOW) && (n == HIGH)) {

                 if (digitalRead(encoder0PinB) == LOW) {

                   encoder0Pos--;

                 } else {

                   encoder0Pos++;

                 }

                 Serial.println (encoder0Pos);

                 //Serial.print ("/");

               }

               encoder0PinALast = n;

            }

            • 3. Re: intel galileo gen 2 working with a rotary encoder
              Ron Beck

              Reserve an interrupt pin for your encoder and the code gets dead simple.  Whenever the A input rises, the interrupt routine either increments or decrements the EncPos counter, depending on the state of the other quadrature input.  The "volatile" qualifier keeps references to EncPos atomic so that the value doesn't change because an interrupt occurred while you were reading it. 

               

              Check your hardware documentation.  Int1 isn't always on D3.

               

              Feel free to swap the += and the -= if you want the count to increment when the encoder rotates the other direction. 

               

              volatile long EncPos = 0L;

              int EncB = 4;

              int EncA = 3;  // D3 is Int1 on the Uno, Mega

               

              void EncoderInterrupt() {

                if (digitalRead(EncB))

                  EncPos += 1;

                else

                  EncPos -= 1;

              }

               

              void setupEncoder() {

                pinMode(EncA, INPUT);

                pinMode(EncB, INPUT);

                attachInterrupt(1, EncoderInterrupt, RISING);  // attach Int1

              }