Arduino in combination with rotary switch - encoder MOD-16

The example demonstrates the basic operation of an encoder module with an Arduino microcontroller.

The following elements are used in the example:

Connection

The module is powered directly from Arduino, so we don't need an external power source. Pins are connected according to the table below:

Pin of the module Pin Arduino
+ V 5 V
GND GND
S1A 5 V
S1B 10
S2 7
S3 6

Additionally, the board contains LEDs connected by 220 Ω resistors between pin 11 and 12, and ground. The whole circuit is visible in the diagram below.

The diagram of module and Arduino Uno connection.

Operation .

The S1A and S1B outputs are connectors of a button located in the knob. After pressing it they are shorted. The outputs S2 and S3, on the other hand, detect the direction of movement of the knob, right and left respectively. When motion is detected at the output of a given pin, a high state appears.

The following program code is used in the example:

int led1 = 11; //diode connected to pin 11
int led2 = 12; //diode connected to pin 12
int brightness = 0; // brightness of led1
int fadeAmount = 5; // change of brightness when turning the dial for led1

void setup(){
  pinMode(6,INPUT); // setting pins 6, 7 and 10 as inputs
  pinMode(7,INPUT);
  pinMode(10,INPUT);
  pinMode(led1, OUTPUT); //pins with diodes as outputs
  pinMode(led1, OUTPUT);
  
}

void loop(){
 
  analogWrite(led1, brightness); //p Assign brightness value to led1
  
  if (brightness = 255)
    brightness = 255 ;
  if(digitalRead(6) == 1) // if a clockwise or counterclockwise rotation is detected, then led1 will increase or decrease its brightness
    {brightness = brightness + fadeAmount;}
  if(digitalRead(7) == 1)
    {brightness = brightness - fadeAmount;}
  
  if(digitalRead(10) == 1 && digitalRead(led2, HIGH))   // if the button is pressed in the dial, led2 will change its state (if it is lit, it will turn off, if it is off - it will turn on)
    {digitalWrite(led2,LOW)}
  if(digitalRead(10) == 1 && digitalRead(led2,LOW))
    {digitalWrite(led2,HIGH)}
}

Pressing the button on the dial changes the status of the yellow diode (it will alternately light up and go out). By turning the knob you can change the brightness of the red diode (brighten it and turn off).

Botland.store - shop for makers!