Tuesday, 4 June 2013

Electronics: Moving the train (Part I) - PWM Signal

Hi again you all guys, how are you doing?

When I first began thinking of the electronics design, I understood that my first challenge was going to be “moving the train”. Thus, this post is aimed at those who don’t know how to move DC motors using a microcontroller. Anyway, my purpose is not explaining it in depth, but to provide links where you can find out more details.

So far there are two things related to DC Motors that need to be controlled; velocity and direction. The velocity is controlled by a transistor which receives a PWM signal and the direction is controlled by four transistors disposed in H-bridge.

This post expects to give some notions of PWM signals. In addition, the next one will detail the H-Bridge issues.
  • PWM signals and DC Motors

PWM signals can be used in several applications. For instance, they can offer a digital way of driving DC motors. As you know, DC motors behavior is as described below.
  1. When the motor receives no power, it just stays stopped. In contrast, if the motor is fed with 24V DC, the train travels at the fastest movement speed.
  2.  In the range between 0V and 24V: The higher is the applied voltage, the faster the motor spins.
Note that motors are fed with 24V DC in my case.



In contrast, transistors can be just in two different states: opened or closed. It means that transistors provide the voltage given by the power supply when it is connected or just zero when it is disconnected.

The key concept of using PWM signals is that motors can’t vary their velocity instantly, so the provided voltage is exactly the average voltage. In my case, the working frequency of this signal is 4 KHz.

In addition, the motor speed is given by the time which the signal is 24V versus the time which it is 0V. The time which the motor is provided with 24V in each period is called the duty cycle.




For more information related to PWMs, here is a link: http://en.wikipedia.org/wiki/Pulse-width_modulation
  • Generating a PWM signal
PWM signals can be generated basically in two ways; by software and by hardware. The former is not the best way, because microcontrollers are not powerful processors and we therefore need them to be free in order to execute other tasks.  In contrast, the latter is a very good way of generating PWM signals, because the software just spends the time needed to configure the hardware and then it stays free to execute whatever is needed.

On the other hand, sometimes there aren't any available hardware resources and then the PWM signal has to be generated by software. 

In my case, which I will explain later on this blog, I use Arduino and a STM8S microcontrollers. Both microcontrollers are able to generate PWM signals by hardware, which in my case is being 4 KHz.

Specifically in the case of Arduino, a basic high-level library is provided and therefore generating PWM signals is really easy. The only thing to be considered is that the default PWM signal works around 512Hz, which is a very low frequency and induces noises to the motor.

For those who are using Arduino, you will find an explanation on how to change its timer configuration in order to vary the PWM signal frequency: http://arduino.cc/en/Tutorial/SecretsOfArduinoPWM

Here are exposed all the possible combinations of settings which can be set to the timers frequency: 

Pins 5 and 6: controlled by Timer 0
etting Divisor Frequency
S0x01 1 62500
0x03 64 976.5
0x02 8 7812.5 625
5 0x05 1024 61.0351
0x04 256 244.1406 25625 TCCR0B = TCCR0B & 0b11111000 | <setting>;
Pins 9 and 10: controlled by timer 1
etting Divisor Frequency
S0x01 1 31250
0x03 64 488.2
0x02 8 3906.2 58125 0x04 256 122.0703125
R1B = TCCR1B & 0b11111000 |
0x05 1024 30.517578125 TC C<setting>;
Pins 11 and 3: controlled by timer 2
etting Divisor Frequency
S0x01 1 31250
0x03 32 976.5
0x02 8 3906.2 5625 0x04 64 488.28125
6 256 122.0703125 0
0x05 128 244.140625 0x
0x07 1024 30.517578125

For instance, the next line sets the Timer2 to 4KHz.


TCCR2B = TCCR2B & 0b11111000 | 0x02; //4KHz

I'll see you on my next post

No comments:

Post a Comment