How to integrate this adjustable timer functionality using an Arduino for DC Motor Control

Hello everyone,

I'm currently working on a project to control the speed (in RPM) of a DC motor using an Arduino, and I'd like to implement an adjustable timer to vary the motor's running time based on user input. I am aiming to create a system where the user can set a desired operation time (start/stop and forward/reverse) through a user interface, and the Arduino will control the motor accordingly.

I would appreciate any guidance or resources on how to integrate this adjustable timer functionality using an Arduino. If anyone has experience with similar projects or could point me towards useful libraries or examples, that would be incredibly helpful. Btw, I'm using proteus for the schematic and simulation.

Thank you so much for your time and help!

See BlinkWithoutDelay IDE example.

have you decided how the user will enter the information? (the user interface)
do you have a driver for your motor?

For the use interface I might think potentiometer or other specific components, but any suggestions will do.

yes, I do have driver and currently using l298n motor driver

it's usually good to split a project in manageable "chunks"

Put the potentiometer away and play with your motor and motor driver.
write a code that makes the motor spin clockwise then counter clock wise at a given hardcoded speed.


Next, put the motor aside for a moment and write a small code reading a potentiometer and reporting changes (ie don't print if the value stayed the same) in the Serial monitor

that will be the building block for your user interface

You'll notice the data reporting will likely go from 0 to 1023


next is to decide how this number maps to movement and speed

  • may be 0 to 499 means rotating counter clock wise (CCW) 500 to 523 means stop and 524 to 1023 means rotating clock wise (CW) for example
    ➜ modify the code to report in a sentence "The motor is running CW at value xxx" for example if the potentiometer was set above 524 or "The motor is running CCW at value xxx" if the potentiometer was set below 499.

next is to calculate a speed: for example for the speed a pot value of 0 would be full speed CCW 499 slowest speed CCW and 524 lowest speed CW and 1023 full speed CW

➜ now modify the code so that "xxx" is an actual calculated number that makes sense for your l298n motor driver


Once you have both codes ready, you can merge them to get your desired functionality

Hello again guyss, I am reaching out to seek your expertise and guidance regarding an issue I am facing with DC motor control using Arduino.

Currently, I have a system in place where the DC motor operates in a fixed time cycle, specifically 9 seconds in the forward direction before switching to reverse. However, I am looking to integrate adjustable time functionality using a potentiometer for user input.

My goal is to allow users to adjust the operating time of the motor in both forward and reverse directions based on their requirements. I believe that incorporating a potentiometer into the circuitry would enable users to set their desired time intervals.

Could you please provide guidance or code snippets on how I can achieve this integration? Any insights or resources you can share would be greatly appreciated as I am eager to implement this feature into my project.

Thank you very much for your time and assistance.


here's the code:

#include <LiquidCrystal.h>
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

#define potentiometer  A0  //10k Variable Resistor
#define buttonForward A1 // Forward Button
#define buttonStop A2 // Stop Button
#define buttonReverse A3 // Reverse Button

#define greenLEDPin A4 // Green LED pin
#define redLEDPin A5 // Red LED pin
#define blueLEDPin 0 // Blue LED pin

#define M1_Ena 11 // Enable1 L298 for PWM
#define M1_in1 10 // In1  L298 for Clockwise
#define M1_in2 9  // In2  L298 for Anticlockwise

int motorSpeed = 0; // Variable to store motor speed
const int motorSpeedPin = 4; // PWM pin for motor speed control (L293D Enable)

int read_ADC =0;
int duty_cycle;
int duty_cycle_lcd;
int set = 0;		// Variable to store motor direction (0: Stop, 1: Forward, 2: Reverse)	

unsigned long startTime = 0; // Variable to store the start time
unsigned long elapsedTime = 0; // Variable to store the elapsed time
unsigned long reverseStartTime = 0; // Variable to store the start time for reverse direction
unsigned long directionChangeTime = 0; // Variable to store the time when direction is changed

// Timer variables
unsigned long previousMillis[5] = {0, 0, 0, 0, 0}; // Array for different timers

// Timer constants
const long debounceTime = 10;         // Debounce time for buttons (ms)
const long startDelay = 10000;         // Delay before starting motor (ms)
const long runTime = 10000;            // Time for motor to run (ms)
const long inactivityTimeout = 10000; // Time for inactivity before stopping (ms)
const long reverseInterval = 10000;      // Time interval for automatic direction reversal (ms)

void setup(){
   Serial.begin(9600);// initialize serial communication at 9600 bits per second:

   pinMode(potentiometer, INPUT);
   pinMode(buttonForward, INPUT_PULLUP);
   pinMode(buttonStop, INPUT_PULLUP);
   pinMode(buttonReverse, INPUT_PULLUP);
   
   // Motor control pins as outputs
   pinMode(M1_Ena, OUTPUT);
   pinMode(M1_in1, OUTPUT);
   pinMode(M1_in2, OUTPUT);

   lcd.begin(20,4);  
   lcd.print("Motor Speed:"); // Display text on LCD
   pinMode(motorSpeedPin, OUTPUT); // Set PWM pin as output
}

void loop(){ 
   // Read potentiometer value and map it to motor speed (0-255)
   read_ADC = analogRead(potentiometer);
   duty_cycle = map(read_ADC, 0, 1023, 0, 255);  
   duty_cycle_lcd = map(read_ADC, 0, 1023, 0, 100); 
   motorSpeed = map(read_ADC, 0, 1023, 0, 255); 

   // Debounce button presses (using timer)
   if (millis() - previousMillis[0] >= debounceTime) {
      previousMillis[0] = millis();
      int startButtonState = digitalRead(buttonForward);
      int stopButtonState = digitalRead(buttonStop);
      int reverseButtonState = digitalRead(buttonReverse);

      if (startButtonState == LOW) {
	 set = 1;
	 previousMillis[1] = millis(); // Reset start delay timer
	 directionChangeTime = millis(); // Reset direction change timer
	 startTime = millis(); // Reset the start time when motor stops
    } else if (stopButtonState == LOW) {
	 set = 0;
	 startTime = millis(); // Reset the start time when motor stops
    }else if (reverseButtonState == LOW) {
	 set = 2;
	 directionChangeTime = millis(); // Reset direction change timer
	 startTime = millis(); // Reset the start time when motor stops
    }
  }

   // Control motor speed using PWM
   analogWrite(M1_Ena, duty_cycle);
   analogWrite(motorSpeedPin, motorSpeed); // Control motor speed using PWM
   
   // Update LCD with duty cycle
   lcd.setCursor(0,0);
   lcd.print("Duty Cycle: ");
   lcd.print(duty_cycle_lcd); 
   lcd.print("%  ");
   
   lcd.setCursor(0,1);
   lcd.print("Direction:");
   if(set==0){ 
      lcd.print("Stop      ");
      analogWrite(M1_Ena, 0);
      digitalWrite(M1_in1, LOW);  
      digitalWrite(M1_in2, LOW);
      digitalWrite(greenLEDPin, LOW); // Turn off green LED
      digitalWrite(redLEDPin, HIGH); // Turn on red LED
      digitalWrite(blueLEDPin, LOW); // Turn off blue LED

 } else if(set==1){ 
      lcd.print("Forward   ");
      analogWrite(M1_Ena, duty_cycle);  // Set motor speed using PWM
      digitalWrite(M1_in1, HIGH);  
      digitalWrite(M1_in2, LOW);
      digitalWrite(greenLEDPin, HIGH); // Turn on green LED
      digitalWrite(redLEDPin, LOW); // Turn off red LED
      digitalWrite(blueLEDPin, LOW); // Turn off blue LED
   
} else if(set==2){ 
      lcd.print("Reverse  ");
      analogWrite(M1_Ena, duty_cycle);  // Set motor speed using PWM
      digitalWrite(M1_in1, LOW);  
      digitalWrite(M1_in2, HIGH);
      digitalWrite(greenLEDPin, LOW); // Turn off green LED
      digitalWrite(redLEDPin, LOW); // Turn off red LED
      digitalWrite(blueLEDPin, HIGH); // Turn on blue LED
   }
   
  // Timer for automatic direction reversal
   if (set != 0 && millis() - directionChangeTime >= reverseInterval) {
      if (set == 1) {
         set = 2; // Change direction to reverse after the interval
	 startTime = millis(); // Reset the start time when motor stops
      } else if (set == 2) {
         set = 1; // Change direction back to forward after reverse interval
	 startTime = millis(); // Reset the start time when motor stops
      }
      directionChangeTime = millis(); // Reset the timer
   }
   
    elapsedTime = millis() - startTime; // Calculate the elapsed time
    lcd.setCursor(0, 2);
    lcd.print("Time: ");
    lcd.print(elapsedTime / 1000); // Display elapsed time in seconds
    lcd.print("s");
    lcd.setCursor(0, 3); // Set cursor position on LCD
    lcd.print("Speed:");
    lcd.print(motorSpeed); // Display motor speed on LCD
    delay(50);
}

What is the name of the variable you want to change with the potentiometer setting?

the duration or time interval for which the motor operates in a specific direction (forward and reverse)

is now controlled by a variable. Can you identify the variable in your sketch that does this?

Over what range of duration do you wish to be able to adjust it?

These questions we ask are rhetorical in nature.

a7

please don't create multiple topics on the same subject

You already have these 2 that seems to be the same project...

and for the latter post which is very much the same question you've failed to answer my recommendation

First learn how to "keep On/Off" the onboard LED (L) of UNO Board for an adjustable amount of time using an external potentionmeter and then incorporate the philosophy in your Motor Project:

void setup() 
{
  pinMode(13, OUTPUT);
}

void loop() 
{
  unsigned int y = analogRead(A0);
  unsigned int timeDelay = map(y, 0, 1023, 0, 10000);//0 sec to 10 sec
  digitalWrite(13, HIGH);
  delay(timeDelay);
  digitalWrite(13, LOW);
  delay(timeDelay);
  //=====================
}

Turn the Pot CW to increase the On/Off time.

I have merged your topics due to them having too much overlap on the same subject matter @aojos.

In the future, please only create one topic for each distinct subject matter and be careful not to cause them to converge into parallel discussions.

The reason is that generating multiple threads on the same subject matter can waste the time of the people trying to help. Someone might spend a lot of time investigating and writing a detailed answer on one topic, without knowing that someone else already did the same in the other topic.

I left the first of the topics mentioned by @J-M-L separate as it seems to be fairly self-contained. However, it still would have been better for you to have linked to it in the vague first post of this topic if it provided context that would have been potentially useful to the forum helpers.

Thanks in advance for your cooperation.

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