Measure and display speed and direction of DC motor

I have built this circuit. I have a small DC motor and encoder disk is attached to it. I need a program so the RPM and Direction of rotation will display on 16x2 LCD display. The circuit diagram and original pictures are here.
Please Can anyone write a program for me.
I will be very thankful to you.





I need a program for Arduino Uno to measure the speed of motor in RPM and direction of rotation using this optical encoder.

Yes, but why do you not want to write the program yourself? The strength of Arduino is that you can learn about circuits and programming.

If you do not write the program, I guess you can score only 40 of the 80 marks available. That will not be enough for a pass.

Good work on drawing a schematic of your circuit. However, I would recommend that you connect the encoder to pins 2, 3. These 2 pins alone on Uno have a special function that will be important.

I don't have any knowledge about programing and also the time is running out so I don't have time to learn how to program. So can you or anyone write a program for me.
Will be appreciated :+1:.
I have used chatgpt but there are errors in the code and I don't know how to modify the program because as I have said I don't know programming.
So please write a program for me😘

Were you born with the ability to draw schematics and build circuits? No, I suspect you have learned that. If you can learn that, you can learn to program.

1 Like

Is this a class assignment?
How long have you got to complete it?
How long ago was this set?

We do not do this, especially for class assignments. It is called cheating.

If you want to cheat this way you can move the post to the section that offers to write code for you in exchange for payment. Say how much you are willing to pay to cheat.

1 Like

No it's is not related to school or college project. I am making this at home. I have this project last year but due to programing issues I cannot completed it. But now I want that to complete. Please write a program so that I can learn from the program.
This is not a cheating. It's my personal project.

So why the time constraint?

Because I am working on another project also and want to complete this before.


// Define the pins for the encoder
#define encoderPinA 10  // Pin for encoder signal A
#define encoderPinB 11  // Pin for encoder signal B
#define PPR 100        // Pulses per revolution

// Define the pins for the LCD
#define rs 13
#define en 12  // Rw is connected to GND, so 'en' is set manually
#define d4 6
#define d5 7
#define d6 8
#define d7 9

volatile long pulseCount = 0;
volatile bool direction = true;  // true = forward, false = backward
unsigned long lastTime;
float rpm = 0;

// Initialize the LCD object
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup() {
  pinMode(encoderPinA, INPUT_PULLUP);
  pinMode(encoderPinB, INPUT_PULLUP);
  
  // Attach interrupt to encoderPinA
  attachInterrupt(digitalPinToInterrupt(encoderPinA), pulseCounter, RISING);

  lcd.begin(16, 2);
  lcd.setCursor(0, 0);
  lcd.print("RPM: ");
  
  // Initialize timing for RPM calculation
  lastTime = millis();
}

void loop() {
  unsigned long currentTime = millis();
  if (currentTime - lastTime >= 1000) {
    // Calculate RPM every second
    rpm = (pulseCount / (float)PPR) * 60.0;  // RPM calculation
    pulseCount = 0;  // Reset pulse count
    
    // Display RPM on the LCD
    lcd.setCursor(5, 0);
    lcd.print(rpm);
    lcd.print("   ");  // Clear extra characters

    // Check motor direction
    lcd.setCursor(0, 1);
    if (direction) {
      lcd.print("Dir: Forward ");
    } else {
      lcd.print("Dir: Reverse ");
    }
    
    // Reset time
    lastTime = currentTime;
  }
}

// Interrupt service routine (ISR) for pulse counting
void pulseCounter() {
  // Count the pulses
  pulseCount++;
  
  // Determine direction based on the state of encoderPinB
  direction = digitalRead(encoderPinB);
}

I have write this code but there is an error and I am unable to resolve this after 3 attempts.

What did you try in each of these 3 attempts?

The error is strange. Normally the IDE creates forward declarations for functions so that it is not necessary to declare a function before it is used. I know there are limitations to what types of functions the IDE can do this for (variadic functions, for example) but I don't see anything like that here.

Try moving the definition of pulseCounter() to before setup().

WOW :exploding_head:, Thanks :+1:, the issue is solved.

The big problem in the code you posted in post #9 is that it does not include the LCD library once all the LCD commands are commented out the to code as written compiles.

The other big problem is this line:-

When you use a variable this size you need to disable the interrupts read the variable and re-enable the interrupts again. This is so you don't get an interrupt altering the value of pulseCount when you are part way through reading it. This is required for all volatile variables greater than one byte.

This will probably lead to missed interrupts causing a shortfall in the readings you get.

You have not understood in this thread and the other that badgering and demanding code is not working? Realistic Animatronic Robot Head That Can Talk

You should really learn to code. It will help you.

I will

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