DC Motor Interfacing with MSP-EXP430G2 TI Launchpad
Introduction
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.
Interfacing Diagram
Interfacing DC Motor with MSP-EXP430G2 TI Launchpad
Example
Here, we are going to control the speed and rotational direction of DC motor using MSP-EXP430G2 TI Launchpad.
Here, we are going to control the speed and rotational direction of DC motor using MSP-EXP430G2 TI Launchpad.
Here, potentiometer is used as a means for speed control and an input from 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 MSP-EXP430G2 TI Launchpad is used to provide a variable voltage to the motor through L293D. In Energia, analogWrite function is used to generate PWM wave.
Tread Carefully : MSP-EXP430G2 TI Launchpad board has a RAM of 512 bytes which is easily filled, especially while using different libraries. There are times when you need the Serial buffer to be large enough to contain the data you want and you will have to modify the buffer size for the Serial library. While doing such things, we must ensure that the code does not utilize more than 70% RAM. This could lead to the code working in an erratic manner, working well at times and failing miserably at others.
There are times when the RAM usage may exceed 70% and the codes will work absolutely fine, and times when the code will not work even when the RAM usage is 65%.
In such cases, a bit of trial and error with the buffer size and/or variables may be necessary.
Sketch for Direction and Speed Control of DC Motor
const int pot_input = A4;
bool d1 = HIGH;
bool d2 = LOW;
void setup() {
pinMode(12, OUTPUT); /* Motor control pin 1 */
pinMode(13, OUTPUT); /* Motor control pin 2 */
pinMode(14, OUTPUT); /* PWM pin for Speed Control */
pinMode(5, INPUT_PULLUP); /* Interrupt pin for direction control */
attachInterrupt(5, motor, FALLING); /* Interrupt on falling edge on pin 5 */
}
void loop() {
int pwm_adc;
pwm_adc = analogRead(pot_input); /* Input from Potentiometer for speed control */
digitalWrite(12,d1);
digitalWrite(13,d2);
analogWrite(14, pwm_adc / 4);
}
void motor(){
d1 = !d1;
d2 = !d2;
for(int i = 0; i<10000; i++)
for(int j = 0; j<10; j++);
}
Function Used
1. attachInterrupt(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 in the function.
- 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 :
- LOW : trigger the interrupt whenever the pin is low.
- CHANGE : trigger the interrupt whenever the pin changes value.
- RISING : trigger when the pin goes from low to high.
- FALLING : trigger when the pin goes from high to low. - Example, attachInterrupt(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.
No comments:
Post a Comment