In this tutorial, we will interface a Rotary Encoder with Surilli GSM. By rotating the knob of the rotary encoder, both the movement as well as the position can be monitored.
What Is a Rotary Encoder?
A rotary encoder is an electronic component capable of monitoring movement and position when rotating. Rotary encoders utilize optical sensors that can generate pulses when the rotary encoder rotates. Application of the rotary encoder usually as a mechanical or robotic motion monitor can also be used for menu selection on the display. Rotary encoders have two outputs so that it can distinguish between negative (CW) and positive (CCW) rotation and also has a single button.
Pinout of Rotary Encoder:
Pulse Flow of Rotary Encoder:
The pulse flow generated by the following rotary encoder is like the picture below:
ConnectionsBetween Rotary Encoder and Surilli GSM:
GND (ROTARY ENCODER) ---> GND (SURILLI GSM)
+ (ROTARY ENCODER) ---> USB (SURILLI GSM)
SW (ROTARY ENCODER) ---> PIN 5 (SURILLI GSM)
DT (ROTARY ENCODER) ---> PIN 6 (SURILLI GSM)
CLK (ROTARY ENCODER) ---> PIN 9 (SURILLI GSM)
STEP 1: Set Up Arduino IDE for SurilliMake sure you have selected the right port, board and processor for the Surilli as shown in the picture below and it is programmable (compile and upload “Blink” from File>Examples>Digital>Blink onto your Surilli to check if everything is working fine).
STEP 2: The CircuitryThe circuitry is very simple. It's mostly the programming. Follow the figure below to set up your hardware.
Now you have completed setting up your hardware and Arduino IDE. Copy and paste the Arduino sketch given below into your Arduino IDE and hit upload.After it is uploaded, the circuit will start working and the results can be viewed onto the Serial Monitor.
Arduino Code:
# define encoder0PinA 9
# define encoder0PinB 6
# define encoder0Btn 5
int encoder0Pos = 0;
void setup() {
Serial.begin(9600);
pinMode(encoder0PinA, INPUT_PULLUP);
pinMode(encoder0PinB, INPUT_PULLUP);
pinMode(encoder0Btn, INPUT_PULLUP);
attachInterrupt(0, doEncoder, CHANGE);
}
int valRotary,lastValRotary;
void loop() {
int btn = digitalRead(encoder0Btn);
Serial.print(btn);
Serial.print(" ");
Serial.print(valRotary);
if(valRotary>lastValRotary)
{
Serial.print(" CW");
}
if(valRotary) {
Serial.print(" CCW");
}
lastValRotary = valRotary;
Serial.println(" ");
delay(250);
}
void doEncoder()
{
if (digitalRead(encoder0PinA) == digitalRead(encoder0PinB))
{
encoder0Pos++;
}
else
{
encoder0Pos--;
}
valRotary = encoder0Pos/2.5;
}
Play with the program to see how it reacts to different values and logic. This will develop your understanding about ultrasonic sensors so you can use them in your practical application.
If you make something fun and interesting, do share it with our community.
That’s all for now. If you have any queries, visit surilli.io or contact our support. Stay connected with the Surilli family for more amazing stuff. :-)
Comments