Table of Contents


DC motor


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).

The perfect graph of relationship between speed (V), current (I), power (P), efficiency (η)and torque (T)of a DC motor.

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.

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-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

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 conclusion, we 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 a 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 savings. In practice, there is no lossless system and in fact, the losses occur when the transistor switch one state to other. Therefore, higher losses are occurring when the transistors are switched at higher frequencies.


The HomLab uses a combined ships to drive DC motors, which 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 Combo Board of the HomeLab allows connecting up to four DC motors. 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

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 a relative values from 0 to 255, where 0 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.

// 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) }
static int motorindex[4][2] =
	{ 0, 1 },
	{ 2, 3 },
	{ 4, 5 },
	{ 6, 7 }
// Initializing a PWM to chosen motor
void dcmotor_drive_pwm_init(unsigned char index, timer2_prescale prescaler)
  	unsigned char i, pwm;
	motor[index] = 1;
  	pwm = PWMDEFAULT;
  	// Starting all channels
  	for(i=0 ; i<CHMAX ; i++)
    	  // PWM state variable initialization
    	  compare[i] = pwm;           
    	  compbuff[i] = pwm;          
  	// Starting Timer 2 to normal mode
  	// Allow Timer 2 interrupt
  	// Enable global interrupts
// Generating a PWM for chosen motor
void dcmotor_drive_pwm(unsigned char index, signed char direction, 
unsigned char speed) 
	if(direction == -1)
		compbuff[motorindex[index][0]] = 0x00;
		compbuff[motorindex[index][1]] = speed;
	if(direction == 1)
		compbuff[motorindex[index][0]] = speed;
		compbuff[motorindex[index][1]] = 0x00;

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.

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 speed, it 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 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.

// 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
	// 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);
en/examples/motor/dc.txt · Last modified: 2018/12/27 12:53 (external edit) Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0