top of page
Writer's pictureShubham Ubale

DC Motor Interfacing with Arduino UNO

Updated: Mar 2


Overview of DC Motor





DC motor converts electrical energy in the form of Direct Current into mechanical energy in the form of rotational motion of the motor shaft.


The DC motor speed can be controlled by applying varying DC voltage; whereas the direction of rotation of the motor can be changed by reversing the direction of current through it.


For applying varying voltage, we can make use of PWM technique.


For reversing the current, we can make use of H-Bridge circuit or motor driver ICs that employ the H-Bridge technique.


For more information about DC motors and how to use them, H-Bridge circuit configurations, and PWM technique, refer the topic DC Motors in the sensors and modules section.

 

Connection Diagram of DC Motor with Arduino





Example

Here, we are going to control the speed and rotational direction of DC motor using Arduino Uno.

 

Here, a potentiometer is used as a means for speed control and an input from a tactile switch is used to change the direction of the motor.


L293D motor driver IC is used for controlling the direction of the motor.


PWM wave generated on the Arduino UNO is used to provide a variable voltage to the motor through L293D. In Arduino, analog Write function is used to generate PWM wave.

 

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 declare the digital pin as an interrupt pin.

  • Example: digitalPinToInterrupt(2) is used to declare digital pin 2 as an interrupt pin.

  • On UNO board, only pins 2 and 3 can be configured as interrupt pins. Hence, argument to this function can only be pin 2 or pin 3.

 

2.  attachInterrupt(digitalPinToInterrupt(pin), ISR, mode)
  • This function is used to configure the mode of interrupt event and declare the ISR for that interrupt. The interrupt event and ISR is for the interrupt pin declared by the function digitalPinToInterrupt(pin).

  • ISR in this function is the name of the ISR that will be used for this interrupt.

  • mode defines when the interrupt will be triggered. There are four modes available to choose from :

  • Example, attachInterrupt(digitalPinToInterrupt(2), motor, FALLING) configures digital pin 2 as an interrupt pin with ISR named motor and which generates interrupt for every falling edge event on pin 2.

 

3.  analogWrite(pin,value)
  • This function is used for generating PWM on PWM digital pins(pins 3,5,6,9,10,11 for Arduino UNO).

  • value can be any number between 0 to 255. 0 being 0% duty cycle and 255 being 100% duty cycle.

3 views0 comments

Commenti


bottom of page