Pantech.AI

How to Interface DC Motor with Arduino UNO

Overview of DC Motor

DC Motor

A DC motor converts electrical energy, supplied as Direct Current (DC), into mechanical energy, producing rotational motion in the motor shaft.

The speed of a DC motor can be controlled by varying the DC voltage applied, while the direction of rotation can be changed by reversing the current flow through the motor.

To vary the applied voltage, the Pulse Width Modulation (PWM) technique is commonly used.

To reverse the current, an H-Bridge circuit or motor driver ICs using the H-Bridge method are effective solutions.

For more detailed information on DC motors, H-Bridge circuit configurations, and PWM techniques, refer to the “DC Motors” section in the sensors and modules guide.

Connection Diagram of DC Motor with Arduino

Interfacing DC Motor with Arduino UNO

Example

In this setup, we will control the speed and rotational direction of a DC motor using an Arduino Uno.

A potentiometer is employed to control the speed of the motor, while a tactile switch is used to change the motor’s direction.

The L293D motor driver IC is utilized to manage the motor’s direction.

The PWM signal generated by the Arduino Uno is used to provide a variable voltage to the motor through the L293D. In Arduino, the analogWrite function is used to generate the PWM signal.

Direction and Speed Control of DC Motor Code for Arduino

const int POT_input = A1;   /* assign ADC Channel */
bool d1 = HIGH;
bool d2 = LOW;

void setup() {
  pinMode(4, OUTPUT);  /* Motor control pin 1 */
  pinMode(7, OUTPUT);  /* Motor control pin 2 */
  pinMode(3, OUTPUT);  /* PWM pin for Speed Control */
  pinMode(2, INPUT_PULLUP);  /* Interrupt pin for direction control */
  attachInterrupt(digitalPinToInterrupt(2), motor, FALLING);	/* Interrupt on falling edge on pin 2 */
}

void loop() {
  int pwm_adc;
  pwm_adc = analogRead(POT_input); /* Input from Potentiometer for speed control */
  digitalWrite(4,d1);
  digitalWrite(7,d2);
  analogWrite(3, pwm_adc / 4);    
}

void motor(){
  d1 = !d1;
  d2 = !d2;
  _delay_ms(200);
}

Functions Used

1.  digitalPinToInterrupt(pin)

This function is used to designate a specific digital pin as an interrupt pin.
For instance, digitalPinToInterrupt(2) assigns digital pin 2 as an interrupt pin.
On the UNO board, only pins 2 and 3 can be used as interrupt pins, so the argument for this function must be either pin 2 or pin 3.

2.  attachInterrupt(digitalPinToInterrupt(pin), ISR, mode)
  • This function is used to configure the interrupt mode and assign the ISR (Interrupt Service Routine) for the specified interrupt pin, which is declared using the digitalPinToInterrupt(pin) function.
  • The ISR is the name of the function that will be called when the interrupt occurs.
  • The mode specifies when the interrupt will be triggered, and there are four available options:
  • LOW: Trigger the interrupt whenever the pin is low.
  • CHANGE: Trigger the interrupt whenever the pin changes its value.
  • RISING: Trigger the interrupt when the pin transitions from low to high.
  • FALLING: Trigger the interrupt when the pin transitions from high to low.
  • For example, attachInterrupt(digitalPinToInterrupt(2), motor, FALLING) configures digital pin 2 as an interrupt pin, associates it with the ISR named motor, and triggers the interrupt on every falling edge event on pin 2.
3.  analogWrite(pin,value)
  • This function is used to generate a PWM signal on PWM-capable digital pins (pins 3, 5, 6, 9, 10, and 11 on the Arduino UNO).
  • The value parameter can be any number between 0 and 255. A value of 0 corresponds to a 0% duty cycle, while a value of 255 represents a 100% duty cycle.

Leave a Comment

Your email address will not be published. Required fields are marked *