Martha MigliacioAlex Wong
Published © GPL3+

Using the Pmod ENC with Arduino Uno

Application notes for Pmod ENC and Arduino Uno. In this app the direction of rotation of the encoder axis is displayed in the serial monitor

BeginnerProtip1 hour1,781
Using the Pmod ENC with Arduino Uno

Things used in this project

Story

Read more

Schematics

Pmod ENC and Arduino Uno Fritzing file

Fritzing file displaying the connection of the Pmod ENC to the Arduino Uno.

Pmod ENC and Arduino Uno Fritzing Image

Fritzing image displaying the connection between the Pmod ENC and Arduino Uno.

Code

Pmod ENC and Arduino Uno Code

Arduino
Using this code will display the direction of rotation of the encoder axis as well as using the pushbutton to memorize the state of a counter in a variable.
/************************************************************************
*
* Test of the Pmod 
*
*************************************************************************
* Description: Pmod_ENC
* The direction of rotation of the encoder axis is displayed in the serial monitor.
* Pressing the push button memorizes the state of a counter in a variable
*
*
* Material
* 1. Arduino Uno
* 2. Pmod ENC 
*
************************************************************************/

// Affectation of pins
#define A 2 // output A of the encoder
#define B 3 // output B of the encoder
#define BTN 4 // button of the encoder

volatile boolean rotation;
volatile boolean sens;
int compteur = 0;
int validation = 0;

// Programme d'interruption
void Interruption ()
{
 if (digitalRead(A)==HIGH)
  {
  sens = digitalRead(B); // Clockwise
  }
 else
  {
  sens = !digitalRead(B); // anticlockwise
  }
 rotation = HIGH;
}
void setup()
{
 attachInterrupt (0,Interruption,FALLING); // interruption on falling edge of output A
 Serial.begin(9600); // initialization of the serial monitor
 pinMode(A,INPUT); // configuration of pin 2 in input
 pinMode(B,INPUT); // configuration of pin 3 in input
 pinMode(BTN,INPUT); // configuration of pin 4 in input
}

void loop()
{
 if (rotation==HIGH) // If detection of rotation
 {
  if (sens==HIGH) // if clockwise
   {
   compteur++; // increase counter 
   }
  else
   {
   compteur--; // decrease counter
   }
  rotation = LOW;
  Serial.print ("Compteur="); // write counter variable
  Serial.println (compteur);
 }
  if (digitalRead(BTN)==HIGH) // if the button have been activate
  {
  validation=compteur;
  delay(200); // debounce
  Serial.print ("Validation="); // write validation variable
  Serial.println (validation);
  }
}

Credits

Martha Migliacio

Martha Migliacio

5 projects • 18 followers
Alex Wong

Alex Wong

14 projects • 53 followers
I work in Digilent and like creating projects
Thanks to Lextronics.

Comments