Things used in this project

Schematics

Pmod ENC and Arduino Uno Fritzing file
Fritzing file displaying the connection of the Pmod ENC to the Arduino Uno.
pmodenc-arduinouno_F4JCwbsFyC.fzz
Pmod ENC and Arduino Uno Fritzing Image
Fritzing image displaying the connection between the Pmod ENC and Arduino Uno.
Pmodenc arduinouno bb sk00fs681e

Code

Pmod ENC and Arduino Uno CodeArduino
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

01aa44817a11ceb1a3050ebac4b68bbc
Martha Migliacio
1 project • 0 followers
Contact
Thanks to Lextronics.

Replications

Did you replicate this project? Share it!

I made one

Love this project? Think it could be improved? Tell us what you think!

Give feedback

Comments

Add projectSign up / Login