Rotary encoder with DC motor help

hello I'm working on a project where I'm using a rotary encoder with DC motor to control the speed regulation of a fan. So far I have two LED pins (one in CW and other in CCW) to indicate when the motor is turned on and off. What I want to try to do is when I turn the encoder nozzle CW I want the fan to activate but have the CW LED pin turn on once it reaches a certain level (say at 20). I also want to reverse it back CCW to where it reaches 0 the CCW LED will turn on and motor will stop. What could I do in this case to do that?

[code]
 // Rotary Encoder Inputs
 #define inputCLK 4
 #define inputDT 5
 
 // LED Outputs
 #define ledCW 8
 #define ledCCW 9

int motor = 12; 
 
 int counter = 0; 
 int currentStateCLK;
 int previousStateCLK; 

 //direction of encoder
 String encdir ="";
 
 void setup() { 
   
   // Set encoder pins as inputs  
   pinMode (inputCLK,INPUT);
   pinMode (inputDT,INPUT);
   
   // Set LED pins as outputs
   pinMode (ledCW,OUTPUT);
   pinMode (ledCCW,OUTPUT);
   
   // Setup Serial Monitor
   Serial.begin (9600);
   
   // Read the initial state of inputCLK
   // Assign to previousStateCLK variable
   previousStateCLK = digitalRead(inputCLK);
 
 } 
 
 void loop() { 
  
  // Read the current state of inputCLK
   currentStateCLK = digitalRead(inputCLK);
    
   // If the previous and the current state of the inputCLK are different then a pulse has occured
   if (currentStateCLK != previousStateCLK){ 
       
     // If the inputDT state is different than the inputCLK state then 
     // the encoder is rotating counterclockwise
     if (digitalRead(inputDT) != currentStateCLK) { 

       counter --;
       encdir ="CCW";
       digitalWrite(ledCW, LOW);
       digitalWrite(ledCCW, HIGH);

       analogWrite(motor, 0); 
       
     } else {
       // Encoder is rotating clockwise
       counter ++;
       encdir ="CW";
       digitalWrite(ledCW, HIGH);
       digitalWrite(ledCCW, LOW);

       analogWrite(motor, 255); 
       
     }
     Serial.print("Direction: ");
     Serial.print(encdir);
     Serial.print(" -- Value: ");
     Serial.println(counter);
   } 
   // Update previousStateCLK with the current state
   previousStateCLK = currentStateCLK; 
 }
[/code]

Can you be a little more precise regarding the setup, is the encoder connected to the motor or are you using the encoder to drive the motor directon manually?
You speak about CCW and CW, does the fan turn into 2 directions?
One remark though, the pulses out of an enocder (can be 1024 pulses per revolution or more), come quite fast, so maybe you need an ISR to manage properly.

the encoder is connected to the two LED pins and motor is being controlled with the use of a transistor and diode. As for the fan, it only has to move in one direction when turning the dial CW. I just want to know how could Modify the program to where the CW led turns on when it hits a certain number and make the motor stop when I turn the dial back CCW to 0 for example. Sorry if im making it a bit confusing, I'm new to working with motors and encoders.

So first describe what your current code does. This way we can eliminate a lot of questions and fill in information you have not told us about.

what the code does as of now is when I turn the dial CW the CW led pin activates along with the motor, when I turn dial CCW the CCW led pin activates and motor turns off

So make the line that turns off the motor say

I think you can work out how to use this to only turn that motor on when a certain count is exceeded.

1 Like

thanks, I'll see what I can do

It could be that the problem you have is linked to your type of encoder:

  • Encoder with pulse + direction signals: you only have to look at the directon bit and count the clock pulses.
  • Encoder with A not(A), B, not(B) : both generate pulses and depending on the phase you need to count forward or backwards?
    Have you checked the type of encoder using the datasheet?
    I think it could be of the first type, and then adapt your code:
    DT = on => count pulses
    DT = off => count pulses down
    DT from On to off or otherwise = reset counter of pulses.

I have never seen one of these in a passive encoder. Normally you make a direction and clock circuit out of discrete components to add onto a normal quadrature signal one. These only have two signals A & B not the four you were suggesting.

Encoder with A not(A), B, not(B) : Suggests to me four signals.

There are two types of these when you add detents (clicks) , ones that do a full cycle of states between for one click and those that do only two steps between clicks. The former are by far the most common.

@Grumpy_Mike,
I work in industrial automation, steel plant, harsh environment with variable frequency drives and the works. Our biggest motors are > 1.3MW @ 690V on VFD.

There most of the encoders have A, Not(A), B, Not(B) and North Not(North) as output, sometimes on 5V but also on 24V. The wires used to transmit the pulse signals to the substation are twisted to prevent problems with EMC and even then sometimes we have troubles. The transfer using A and not(A) over a twisted pair you know: a differential amplifier a the end can remove the EMC if present.

From very old Automaton systems we have all kinds of encoders. I think in some cases the electronics circuit you talk about is part of the encoder itself.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.