This is an old revision of the document!
Necessary knowledge: [HW] Motor Module, [AVR] Digital Inputs/Outputs, [LIB] Motors, [LIB] Delay
Permanent magnet DC motors are very common in different applications, where small dimensions, high power and low price are essential. Due to their fairly high speed, they are used together with transmission (to output lower speed and higher torque).
Permanent magnet DC motors have quite simple construction and their controlling is quite elementary. Although controlling is easy, their speed is not precisely determined by the control signal because it depends on several factors, primarily of the torque applied on the shaft and feeding current. The relationship between torque and speed of a ideal DC motor is linear, which means: the higher is the load on the shaft the lower is the speed of the shaft and the higher is the current through the coil.
Brushed DC motors are using DC voltage and basically do not need special control electronics because all necessary communication is done inside the motor. When the motor is operating, two static brushes are sliding on the revolving commutator and holding the voltage on the coils. The direction of revolving of the motor is determined by the polarity of the current. If the motor must revolve in only one direction, then the current may come through relay or some other simple connection. If the motor has to revolve in both directions, then an electronic circuit called H-bridge is used.
In the H-bridge are four transistors (or four groups) directing the current for driving the motor. The electrical scheme of the H-bridge is similar to the letter H and that is where it gets its name. The peculiarity of the H-bridge is the possibility to apply both directional polarities to the motor. Picture on the side shows the principal scheme of the H-bridge based on the example of the switches. If two diagonal switches are closed, the motor starts operating. The direction of the revolving of the motor depends on in which diagonal the switches are closed. In the real H-bridge the switches are replaced with transistors which are selected according to the current of the motor and voltage.
H-bridge can also change the direction of rotation than the rotation speed of the motor. There exist also integrated H-bridges, for conducting smaller currents. For higher currents special power MOSFET-s are used. The H-bridge with other electronics is called motor controller or driver.
While the speed of the DC motor is easy to control, there is no guarantee that the desired speed is reached after all. The actual velocity depends on many factors, primarily torque on the output shaft of the motor, current and other motor characteristics. The speed and the output torque of the ideal motor is linearly dependent, i.e. the larger is the output torque, the lower is the speed of the motor, and it consumes more current. This depends on the exact type of motor in case of real motor.
A direct current (DC) motor can be controlled with analog as well as digital signals.
Normally, the motor speed is dependent on the applied voltage at the terminals of the motor. If the motor feed a nominal voltage, it rotates a nominal speed. If the voltage given to the motor is reduced, the motor speed and torque are reduced as well. This type of speed control is also called as analog control. This can be implemented, for example, using a transistor or a rheostat.
DC motors are controlled by microcontrollers, and because microcontrollers are digital devices, it is also reasonable to control the motors digitally. This is achieved by using pulse width modulation (PWM), by switching transistors quickly on - off. The total motor power is something in between standing and full speed. The time of the entire PWM period when transistor is opened, called duty cycle, which is denoted by percent. 0% means that transistor is constantly closed and not conduct, 100% means that transistor is opened and conducts. The PWM frequency should be high enough to prevent vibration of the motor shaft. At low frequencies the motor produces a noise and is therefore used modulating frequency above 20 kHz mostly. However, transistors efficiency is suffering from very high frequencies.
Compared to the analog control a digital control has a number of advantages. The main advantage of microcontroller-controlled systems is that it requires only a single digital output and there is no need for complicated digital-to-analog converter. The digital controlling is also more efficient because less energy is converted into heat.
A simplified control scheme is shown in the next drawing. The control voltage Vc is coming to the microcontroller output pin and switch the transistor Q on-off at a frequency of approximately 20 kHz. When the transistor Q is switched on, then the total current I is going through the motor M. In this case, the transistor behaves as a closed switch and a voltage drop Vq is near 0, and the entire input voltage Vdd remains the engine.
The total power which is passing the transistor can be calculated by the formula:
P = I * V
P = I * Vq, and when Vq ~ 0, then P ~ 0 W
The driver of DC motor in the HomeLab L293D includes 2 integrated H-bridges and circuit breaking diodes. The motor is controlled with three digital signals,one of them is operation enabling signal enable and the other two are determining the state of the transistors in the H-bridge. Never can occur that two vertical transistors are opened, because this would short-circuit the power source. This means that the driver is designed as foolproof and only option that can be chosen is which transistor (upper or bottom) of one side of the H-bridge (of “semi-bridge”) is opened. In other words the polarity is selected using two driving signals which is applied to the two ends of the coil of the motor.
The board of the motors of the HomeLab allows connecting up to four DC motors. The schemes and instructions for connection are found in the chapter “Motors module”. Basically, for every motor there is a H-bridge which is controlled with two digital output pins of the microcontroller, because the enable pin is constantly high. If both controlling pins have same value, then the motor is stopped if different then it revolves in the corresponding direction. The state of the H-bridge is described in the following table:
| Input A | Input B | Output A | Output B | Result |
|---|---|---|---|---|
| 0 | 0 | - | - | The motor is stopped |
| 1 | 1 | + | + | The motor is stopped |
| 1 | 0 | + | - | The motor revolves in direction 1 |
| 0 | 1 | - | + | The motor revolves in direction 2 |
DC motors can be controlled by manipulating directly corresponding driver pins with microcontroller. Though, special functions for driving the motor are in the library of the HomeLab.
// The setup of the pins driving pins. static pin dcmotor_pins[4][2] = { { PIN(B, 7), PIN(B, 4) }, { PIN(D, 1), PIN(D, 0) }, { PIN(D, 7), PIN(D, 6) }, { PIN(D, 5), PIN(D, 4) } }; // Allowing the control of the chosen DC motor. void dcmotor_init(unsigned char index) { pin_setup_output(dcmotor_pins[index][0]); pin_setup_output(dcmotor_pins[index][1]); } // Determining the operation and the direction of the chosen DC motor. void dcmotor_drive(unsigned char index, signed char direction) { pin_set_to(dcmotor_pins[index][0], direction < 0); pin_set_to(dcmotor_pins[index][1], direction > 0); }
With the array dcmotor_pins in the library, the controlling pins of four motor-controllers are determined. Before controlling the motors, function dcmotor_init with the number of the motor-controller (0 – 3) must be called out. It sets the pins as output. For controlling is the function dcmotor_drive, with it the negative direction parameter is used to give to the motor one revolving direction and other direction with positive parameter, and 0 if the motor is stopped.
The following is an example program which controls first and second DC motor so that they alter their revolving direction after every second. The speed could be controlled if one controlling pin were modulated with PWM signal.
// Robotic HomeLab DC motor driving example program #include <homelab/module/motors.h> #include <homelab/adc.h> // Main program int main(void) { // Variable of speed int speed; // Start of ADC adc_init(ADC_REF_AVCC, ADC_PRESCALE_8); // DC1 & DC2 motor initialization (without timer prescaler) // HomeLab II //dcmotor_drive_pwm_init(1, TIMER2_NO_PRESCALE); //dcmotor_drive_pwm_init(2, TIMER2_NO_PRESCALE); // HomeLab III dcmotor_drive_pwm_init(1, TIMER_NO_PRESCALE); dcmotor_drive_pwm_init(2, TIMER_NO_PRESCALE); // Endless loop while (true) { // Reading potentiometer value (average of 4) speed = adc_get_average_value(15, 4); // ADC value is 12-bit but DC motor input is 8-bit // conversion can be ether dividing the value with 8 or // make bit shifting to right 3 times (>>3) dcmotor_drive_pwm(1, 1, speed/8); dcmotor_drive_pwm(2, 1, 128); } }