Site Tools


Sidebar

Content

pub:rotary_encoder

Rotary encoder

what's Rotary encoder

Rotary incremental encoder outputs pulse when rotating and locates it by counting equipment, when the encoder stop or the power is off, it remembers the location by the inside memory of counting equipment. In this way, when the power is off, the encode can’t move in any way, when the power is connected, in the process of outputting, there can’t be any interruption which leads to losing pulse, otherwise, the memorial null point of counting equipment will change and the amount of changes can’t be measured, we can only know it after mistake happened.
Adeept Rotary encoder:


A Rotate clockwise
B Rotate counterclockwise
S Encoder pressed
+ 3.3V
- GND



Microbit

1 * microbit
1 * microbit expansion board
1 * USB cable
1 * Rotary encoder
- Several Jumper wires

connection diagram:


Code:
MU python:

#-----------------------------------------------------------
# File name   : Rotary_encoder.py
# Description : The rotary encoder controls the red dot moving
#               on the matrix lamp.
# Author      : jason
# E-mail      : jason@adeept.com
# Website     : www.adeept.com
# Date        : 2019/01/16
#-----------------------------------------------------------
from microbit import *
 
 
#LED display
A = Image("00900:00000:00000:00000:00000")
B = Image("00090:00000:00000:00000:00000")
C = Image("00009:00000:00000:00000:00000")
D = Image("00000:00009:00000:00000:00000")
E = Image("00000:00000:00009:00000:00000")
F = Image("00000:00000:00000:00009:00000")
G = Image("00000:00000:00000:00000:00009")
H = Image("00000:00000:00000:00000:00090")
I = Image("00000:00000:00000:00000:00900")
J = Image("00000:00000:00000:00000:09000")
K = Image("00000:00000:00000:00000:90000")
L = Image("00000:00000:00000:90000:00000")
M = Image("00000:00000:90000:00000:00000")
N = Image("00000:90000:00000:00000:00000")
O = Image("90000:00000:00000:00000:00000")
P = Image("09000:00000:00000:00000:00000")
 
A1 = 0
A2 = 0
val1 = 0
val2 = 0
count = 7   
 
while True:   
    A1 = pin19.read_digital()
    if A1 != val1:
        A2 = pin20.read_digital()
        if A2 != val1:   
            count = count + 1   
        else:
            count = count - 1
    if count < 0:
       count = 0
    if count >16:
       count = 16    
 
    if count == 0: 
        display.show(A)
    if count == 1:
        display.show(B)
    if count == 2:
        display.show(C) 
    if count == 3:
        display.show(D)
    if count == 4:
        display.show(E)
    if count == 5:
        display.show(F)
    if count == 6:
        display.show(G)
    if count == 7:
        display.show(H)
    if count == 8:
        display.show(I)
    if count == 9:
        display.show(J)
    if count == 10:
        display.show(K)
    if count == 11:
        display.show(L)
    if count == 12:
        display.show(M)
    if count == 13: 
        display.show(N)
    if count == 14:
        display.show(O)
    if count == 15:
        display.show(P)  
    if count == 16:
        display.show(A)
 
    val1 = A1
    val2 = A2

Click “flash” and download the code onto the micro:bit.
Now you can see that when we rotate the rotary encoder clockwise and counterclockwise, we can see the corresponding content display on the matrix LED.
Effect Picture:



Arduino

Components
- 1 * Adeept Arduino UNO R3 Board
- 1 * Rotary Encoder Module
- 1 * USB Cable
- 1 * 3-Pin Wires

Step 1: Build the circuit


Adeept UNO R3 Board Rotary Encoder Module
D4 S
D3 B
D2 A
5V +
GND -

Step 2:Code

/***********************************************************
File name: _16_RotaryEncoderModule.ino
Description: The information of rotary encoder module has been
             detected by UNO R3,and displayed in the serial monitor 
             When the rotary encoder turns clockwise, the angular 
             displacement is increased;when it turns counterclockwise,
             it’s decreased.If you press the switch on the rotary 
             encoder, related readings will return to zero
Website: www.adeept.com
E-mail: support@adeept.com
Author: Tom
Date: 2019/01/16 
***********************************************************/
const int APin= 2; //Set the digital 2 to A pin
const int BPin= 3; //Set the digital 3 to B pin
const int SPin= 4 ;//Set the digital 4 to S pin
 
int encoderVal = 0;
 
void setup()
{
  pinMode(APin, INPUT);//initialize the A pin as input
  pinMode(BPin, INPUT);//initialize the B pin as input
  pinMode(SPin, INPUT);//initialize the S pin as input
  Serial.begin(9600); //opens serial port, sets data rate to 9600 bps
}
 
void loop()
{
  int change = getRotaryEncoder();
  encoderVal = encoderVal - change;
  if(digitalRead(SPin) == LOW)
  {
    encoderVal = 0;
  }
  Serial.println(encoderVal);
}
 
int getRotaryEncoder(void)
{
  static int oldA = HIGH; //set the oldA as HIGH
  static int oldB = HIGH; //set the oldB as HIGH
  int result = 0;
  int newA = digitalRead(APin); //read the value of APin to newA
  int newB = digitalRead(BPin); //read the value of BPin to newB
  if (newA != oldA || newB != oldB)//if the value of APin or the BPin has changed
  {  
    if (oldA == HIGH && newA == LOW)// something has changed
    {
      result = (oldB * 2 - 1);
    }
  }
  oldA = newA;
  oldB = newB;
  return result;
}

Step 3: Compile and download the sketch to the UNO R3 board.



Open the Serial Monitor in Arduino IDE. Turn or press down the knob of the Rotary Encoder, the value on the window will increase, decrease, or be cleared.



Raspberry pi

Components
- 1 * Raspberry Pi
- 1 * GPIO Extension Board
- 1 * 40-Pin GPIO Cable
- 1 * Breadboard
- 1 * Rotary Encoder Module
- 1 * 5-Pin Wires

Step 1: Build the circuit


Code:
C code

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <wiringPi.h>
 
#define  SWPin     0
#define  RoAPin    1
#define  RoBPin    2
 
static volatile int globalCounter = 0 ;
 
unsigned char flag;
unsigned char Last_RoB_Status;
unsigned char Current_RoB_Status;
 
void btnISR(void)
{
	globalCounter = 0;
}
 
void rotaryDeal(void)
{
	Last_RoB_Status = digitalRead(RoBPin);
 
	while(!digitalRead(RoAPin)){
		Current_RoB_Status = digitalRead(RoBPin);
		flag = 1;
	}
 
	if(flag == 1){
		flag = 0;
		if((Last_RoB_Status == 0)&&(Current_RoB_Status == 1)){
			globalCounter ++;	
		}
		if((Last_RoB_Status == 1)&&(Current_RoB_Status == 0)){
			globalCounter --;
		}
	}
}
 
int main(void)
{
	if(wiringPiSetup() < 0){
		fprintf(stderr, "Unable to setup wiringPi:%s\n",strerror(errno));
		return 1;
	}
 
	pinMode(SWPin,  INPUT);
	pinMode(RoAPin, INPUT);
	pinMode(RoBPin, INPUT);
 
	pullUpDnControl(SWPin, PUD_UP);
 
    if(wiringPiISR(SWPin, INT_EDGE_FALLING, &btnISR) < 0){
		fprintf(stderr, "Unable to init ISR\n",strerror(errno));	
		return 1;
	}
 
	while(1){
		rotaryDeal();
		printf("%d\n", globalCounter);
		//printf("%d\n",globalCounter);
	}
 
	return 0;
}

Python code

#!/usr/bin/env python
import RPi.GPIO as GPIO
import time
 
RoAPin = 11    # pin11
RoBPin = 12    # pin12
BtnPin = 13    # Button Pin
 
globalCounter = 0
 
flag = 0
Last_RoB_Status = 0
Current_RoB_Status = 0
 
def setup():
	GPIO.setmode(GPIO.BOARD)       # Numbers GPIOs by physical location
	GPIO.setup(RoAPin, GPIO.IN)    # input mode
	GPIO.setup(RoBPin, GPIO.IN)
	GPIO.setup(BtnPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
	GPIO.add_event_detect(BtnPin, GPIO.FALLING, callback=btnISR)
 
def rotaryDeal():
	global flag
	global Last_RoB_Status
	global Current_RoB_Status
	global globalCounter
	Last_RoB_Status = GPIO.input(RoBPin)
	while(not GPIO.input(RoAPin)):
		Current_RoB_Status = GPIO.input(RoBPin)
		flag = 1
	if flag == 1:
		flag = 0
		if (Last_RoB_Status == 0) and (Current_RoB_Status == 1):
			globalCounter = globalCounter - 1
		if (Last_RoB_Status == 1) and (Current_RoB_Status == 0):
			globalCounter = globalCounter + 1
 
def btnISR(channel):
	global globalCounter
	globalCounter = 0
 
def loop():
	global globalCounter
	tmp = 0	# Rotary Temperary
 
	while True:
		rotaryDeal()
		if tmp != globalCounter:
			print 'globalCounter = %d' % globalCounter
			tmp = globalCounter
 
def destroy():
	GPIO.cleanup()             # Release resource
 
if __name__ == '__main__':     # Program start from here
	setup()
	try:
		loop()
	except KeyboardInterrupt:  # When 'Ctrl+C' is pressed, the child program destroy() will be  executed.
		destroy()
For C language users:


Step 2: Edit and save the code with vim or nano.

(code path: /home/Adeept_Sensor_Kit_for_RPi_C_Code/14_RotaryEncoder/rotaryEncoder.c)

Step 3: Compile

$ sudo gcc rotaryEncoder.c -o rotaryEncoder -lwiringPi

Step 4: Run

$ sudo ./rotaryEncoder
For Python users:


Step 2: Edit and save the code with vim or nano.

(code path: /home/Adeept_Sensor_Kit_for_RPi_Python_Code/14_rotaryEncoder.py)

Step 3: Run

$ sudo ./rotaryEncoder.py

Now rotate the shaft of the rotary encoder, and the value printed on the screen will change. Rotate the rotary encoder clockwise, the value will increase; Rotate it counterclockwise, the value will decrease; Press the rotary encoder, the value will be reset to 0.


Link for code download:http://www.adeept.com/learn/ Download the kit information as needed.
Video link:http://www.adeept.com/video/

pub/rotary_encoder.txt · Last modified: 2019/01/17 13:51 by Adeept