Arduino with rotary encoder and 7 segment display

his topic shows how to connect Arduino UNO with rotary encoder and 7-segment display. Here the rotary encoder is an input device and the 7-segment display is an output device. The 7-segment display prints the values of the rotary encoder (positive or negative) on 4 digits where the first digit (most left) is used for the minus sign ( -). To see how to connect Arduino with 7-segment display visit the following post: Interfacing Arduino with 7-segment display | 4-Digit counter example To see how the rotary encoder works and how to interface it with Arduino in order to control a DC motor speed, take a look at the project below: DC Motor control with rotary encoder and Arduino Parts Required:
  • Arduino UNO board   —> ATmega328P datasheet
  • 4-Digit common anode 7-segment display
  • 4 x PNP transistor (2SA10152S90152N3906 …)
  • Rotary encoder
  • 7 x 100 ohm resistor
  • 4 x 4.7k ohm resistor
  • Breadboard
  • Jumper wires
Arduino UNO with rotary encoder and 7 segment display Arduino with rotary encoder and 7-segment display circuit: The image below shows example circuit schematic diagram. Arduino with rotary encoder and 7 segment display The rotary encoder board has 5 pins: GND, + , SW, DT (pin B or data pin) and CLK (pin A or clock pin) where: GND is connected to Arduino GND pin + is connected to Arduino 5V pin SW is push button pin, not used in this example DT is connected to Arduino analog pin 5 (A5) CLK is connected to Arduino analog pin 4 (A4) The 4 transistors are of the same type (PNP).
Arduino with rotary encoder and 7 segment display code: The Arduino code below doesn’t use any library for the 7-segment display. The rotary encoder pin A (CLK) and pin B (DT) are connected to Arduino UNO pins A4 and A5 respectively. Both pins can be used to interrupt the Arduino microcontroller (ATmega328P) whenever there is a change in the state of at least one pin. The following lines are used to enable interrupt-on-change for pins A4 (PCINT12) and A5 (PCINT13):     Since the 4 digits are multiplexed we need to refresh the display very quickly (display one digit at a time, others are off). For that I used Timer1 module interrupt with the following configuration:    
With the above configuration Timer1 module overflows every 4096 microseconds (4096 = 65536/16). Note that Timer1 module is 16-bit timer, prescaler = 1 (TCCR1B = 1) and Arduino UNO clock = 16MHz. Full Arduino code:     The following small video shows a test circuit of this project:

Discover more from Simple Circuit