Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:iot-open:practical:hardware:itt:avr:dc [2025/08/26 08:21] – [DC motor] ingmar05en:iot-open:practical:hardware:itt:avr:dc [2025/09/02 11:27] (current) raivo.sell
Line 3: Line 3:
 ===== Theory ===== ===== Theory =====
  
-[{{  :examples:motor:dc:motor_dc_picture.jpg?220|DC motor}}]+[{{  :examples:motor:dc:motor_dc_picture.jpg?200|DC motor}}]
  
-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 are very common in different applications, where small dimensions, high powerand 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 a relatively simple construction, and their control is straightforward. Although controlling is easy, their speed is not precisely determined by the control signal because it depends on several factors, primarily on the torque applied to the shaft and the feeding current. The relationship between torque and speed of an ideal DC motor is linear, which means: the higher the load on the shaft, the lower the speed of the shaft, and the higher the current through the coil.
  
-[{{  :examples:motor:dc:motor_dc_performance.png?220|The perfect graph of relationship between speed (V), current (I), power (P), efficiency (η)and torque (T)of a DC motor. }}] +Brushed DC motors use 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 rotation 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 an H-bridge is used.
- +
-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.  +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 Hand that is where it gets its name. The peculiarity of the H-bridge is the possibility of applying both directional polarities to the motor. An H-bridge can also change the direction of rotation and the rotation speed of the motor. There also exist integrated H-bridges for conducting smaller currents, for higher currentsspecial power MOSFETs are used. The H-bridge with other electronics is called motor controller or driver. 
- +
-[{{  :examples:motor:dc:motor_h_bridge_principle.png?220|The working principle of H-bridge used on switches.}}] +
- +
-H-bridge can also change the direction of rotation than the rotation speed of the motor. There exist also integrated H-bridgesfor 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 +
- +
-This means that the transistor spend almost no energy in the open state. Similar situation is also the case when the transistor is in the closed state. In this case, there is no current flow through the transistor or the motor. Now the power which is going through the transistor, is calculated as follows: +
- +
-P = I * Vq, and when I = 0, then P = 0 W+
  
-In conclusionwe can say that if the transistor is a switch element on the scheme, then the system efficiency is high and the power used by transistors is low. Compared with linear (analog) system, where the transistor consumes of the half-open state the same amount of power than the motor, it is a very big energy savingsIn practice, there is no lossless system and in fact, the losses occur when the transistor switch one state to otherTherefore, higher losses are occurring when the transistors are switched at higher frequencies.+DC motors are controlled by microcontrollersand 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 and off. The total motor power is something in between standing and full speed. The time of the entire PWM period when the transistor is opened, called the duty cycle, is denoted by a percentage. 0% means that the transistor is constantly closed and does not conduct, 100% means that the transistor is open and conducts. The PWM frequency should be high enough to prevent vibration of the motor shaft. At low frequenciesthe motor produces noise and is therefore used with modulating frequency above 20 kHz. However, the transistors' efficiency is suffering from very high frequenciesCompared to the analog controla digital control has a number of advantages. The main advantage of microcontroller-controlled systems is that they require only a single digital output, and there is no need for a complicated digital-to-analog converterThe digital control is also more efficient because less energy is converted into heat.
  
 ===== Practice ===== ===== Practice =====
  
-The HomLab uses a combined ships to drive DC motors, which includes 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 HomLab uses a combined chip to drive DC motors, which includes two integrated H-bridges and circuit-breaking diodes. The motor is controlled with three digital signals, one of them is the operation enabling signal //enable//and the other two are determining the state of the transistors in the H-bridge. It can never occur that two vertical transistors are opened, because this would short-circuit the power source. This means that the driver is designed as foolproofand the only option that can be chosen is which transistor (upper or lower) of one side of the H-bridge (of “semi-bridge”) is opened. In other wordsthe polarity is selected using two driving signalswhich are applied to the two ends of the coil of the motor.
  
-The Combo Board of the HomeLab allows connecting up to four DC motors. Basically, for every motor there is 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:+The Combo Board of the HomeLab allows connecting up to four DC motors. Basically, for every motorthere is an 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 the same value, then the motor is stoppedif they have different values, 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                   ^ ^ Input A ^ Input B ^ Output A ^ Output B ^ Result                   ^
Line 55: Line 25:
 |  0        1        -        |  +        | The motor revolves in direction 2 | |  0        1        -        |  +        | The motor revolves in direction 2 |
  
-For each motor that is connected to the H-bridge is operated by two of the digital output of the microcontroller. The motor speed is is controlled by timers that generate a continuous PWM signals to the H-bridge, the direction of rotation of the motor is controlled to the second terminal. Motor speed is controlled relative values from 0 to 255, where means that the motor is standing and 255 is the maximum moving speed of the motor. The following code describes a function’s, which are described in the HomeLab II (ATmega2561) library to control DC motors.+Each motor that is connected to the H-bridge is operated by two of the digital outputs of the microcontroller. The motor speed is controlled by timers that generate a continuous PWM signal to the H-bridge, and the direction of rotation of the motor is controlled by the second terminal. Motor speed is controlled by relative values from 0 to 255, where zero means that the motor is standing and 255 is the maximum moving speed of the motor. The following code describes a function which are described in the HomeLab II library to control DC motors.
  
 <code c> <code c>
Line 73: Line 43:
  { 6, 7 }  { 6, 7 }
 }; };
-// Initializing a PWM to chosen motor+// Initializing a PWM to the chosen motor
 void dcmotor_drive_pwm_init(unsigned char index, timer2_prescale prescaler) void dcmotor_drive_pwm_init(unsigned char index, timer2_prescale prescaler)
 { {
Line 100: Line 70:
   sei();   sei();
 } }
-// Generating a PWM for chosen motor+// Generating a PWM for the chosen motor
 void dcmotor_drive_pwm(unsigned char index, signed char direction,  void dcmotor_drive_pwm(unsigned char index, signed char direction, 
 unsigned char speed)  unsigned char speed) 
Line 117: Line 87:
 </code> </code>
  
-The controlling pins of four motor-controllers are determined with the array dcmotor_pins in the library. Before controlling the motors, function dcmotor_drive_pwm_init with the number of the motor-controller (0 – 3) must be called out. It sets the pins as output. It should also set the timer prescaler, for HomeLab II timer2_prescale and for HomeLab III timer_prescale, which determines the frequency of the PWM signal. In case of HomeLab II, as the program does not have functions which are using timer, it is appropriate for the value TIMER2_NO_PRESCALE. When for example an ultrasound sensor are used, then should be chosen TIMER2_PRESCALE 8, otherwise the controller performance may not be sufficient and the sensor readings may be corrupted. This is not applying in the HomeLab III. Higher values of the prescaler are not recommended, because it makes the motor rotation intermittent, and generates vibration.+The controlling pins of four motor-controllers are determined with the array dcmotor_pins in the library. Before controlling the motors, the function dcmotor_drive_pwm_init with the number of the motor-controller (0 – 3) must be called out. It sets the pins as output. It should also set the timer prescaler, which determines the frequency of the PWM signal.
  
-Function dcmotor_drive_pwm is for control motor speed. This function need three input values: motor number, direction (-1, 0, +1), where -1 is the rotation in one direction, +1 other direction and 0 for stop and thirdly, the speed range of 0-255. The speed value is not linked to a specific rotational speedit is the relative value between minimal and maximal motor speed. Motor actual speed depends on the motor type, load and the supply voltage. Motor speed accuracy is 8-bits, which means that the minimum control accuracy is 1/255 of the maximum engine speed.+The dcmotor_drive_pwm function is used for controlling motor speed. This function needs three input values: motor number, direction (-1, 0, +1), where -1 is the rotation in one direction, +1 is the other directionand 0 is for stopand thirdly, the speed range of 0-255. The speed value is not linked to a specific rotational speedit is the relative value between the minimum and maximum motor speed. Motor actual speed depends on the motor type, loadand the supply voltage. Motor speed accuracy is 8 bits, which means that the minimum control accuracy is 1/255 of the maximum engine speed.
  
-The following is an example program which controls first and second DC motor so that first motor rotates half of the speed and the second motor speed is controlled by a potentiometer.+The following is an example program that controls the DC motor.
  
 <code c> <code c>
 // Robotic HomeLab DC motor driving example program // Robotic HomeLab DC motor driving example program
 #include <homelab/module/motors.h> #include <homelab/module/motors.h>
-#include <homelab/adc.h> 
  
 // Main program // Main program
 int main(void) int main(void)
 { {
- // Variable of speed + dcmotor_drive_pwm_init(1, TIMER2_NO_PRESCALE); 
- int speed; + dcmotor_drive_pwm(2, 1, 128);
- +
- // 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); +
- }+
 } }
 </code> </code>
  
 +==== Task to be implemented ====
 +  - Make a smooth start of the DC motor and after 10 seconds of rotating at maximum speed, slow down to a full stop.
  
en/iot-open/practical/hardware/itt/avr/dc.1756196494.txt.gz · Last modified: 2025/08/26 08:21 by ingmar05
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0