MTobjects's PWM
Event management for Uno, Nano, Mega
The PWM delivers a rectangular signal (a certain time in HIGH, a certain time to LOW) which starts endlessly. If it goes very slowly, it can run an engine for an hour, stop it for two, and start this cycle again. A little faster, it can blink an LED. If we exceed 50 times per second, an LED will flash so quickly that we will see an average lighting. It's a good way to vary the light intensity.
To vary the light intensity, you can use the PWM (hard) accessible by analogWrite. For these applications, one does not necessarily need a particular frequency and normally 256 levels are enough.
To order certain stepper motor, you can send regular inpulses to make it progress each time. But often we need to know the number of steps. Ultimately, we could use MTclock or MTsoftPWM which can count the number of inpulses, but that does not manage the final position or the sens. Rather, I recommend MTstepper who is dedicated to this task.
A servomotor is controlled by a PWM signal, generally 20ms period, with a inpulse width from 0.5ms to 2.5ms. We could use the MTobjet PWM to order it, but you have to manage the speed. It is better to use then PMservo which is more suitable.
MThardPWM and MTsoftPWMallow PWM by choosing the frequency and having a resolution of the average voltage often greater than 256 values.
Hard or soft?
There are two ways to have a PWM tension on a pin from the Arduino: by hard or soft.
Managing the PWM by Hard means using a specific part of the microcontroller that is designed for this. To make a signal, we calculate the order that we send to a "timer" that does the work. This requires very few resources, once launched, there is nothing more to do. You can have high speeds (max 8mhz for a square signal). But you cannot easily descend below 4Hz.
In addition we can only use certain pins associated with 16 bit timers (MThardPWM does not allow you to use 8 bit timers). On Uno/Nano, we can only enjoy the pins 8 and 9. On Mega the choice is on the pins 2, 3, 5, 6, 8, 11, 11, 13, 44, 45 and 46. In addition on Uno/Nano, pins 8 and 9 use the same timer and the frequency is the same. It is the same with the triplets of the Mega (2, 3, 5), (6, 7, 8), (11, 12, 13), (44, 45, 46).
Managing the PWM by soft means asking the program to look regularly and put an outing to HIGH or LOW. This therefore requires execution time. For a square signal, you can only go faster than 5khz (you can go a little faster by adjusting the MTobjects clock differently). On the other hand, we are less limited for slow speeds (5 days for a square signal).
But the big advantage of the soft PWM is that you can use all the pins (except A6 and A7 of a nano which are only inputs).
As the management of MTsoftPWM is software, there is the possibility of counting the levels, in order to give a number fixed by the user. There is also the possibility of triggering an action at the end of the count.
Choice guide
MThardPWM | MTsoftPWM | |
Pins usable Uno/Nano | 8,9 | All |
Pins usable Mega | 2, 3, 5, 6, 7, 8, 11, 12, 13, 44, 45, 46 | All |
Frequency on the pins | The same for 3 | Independent pins |
Minimum period | 1µs | 200µs (1) |
Maximum period | 4s | 5 days (1) |
Resolution | 62ns à 64µs | 100µs (1) |
Material resources | one timer | no |
Execution time | 0 | non zero |
(1) by default, but a factor 10 is possible |
AnalogWrite
The AnalogWrite() function generates a rectangular signal by hard. You can only use 6 pins on the Nano/Uno and 12 pins on the Mega. The period is fixed at 490Hz or 980Hz depending on the pin used. The resolution is 256 steps (8µs to 490Hz).
Detail of the functions on the Arduino reference page.
MThardPWM
A MThardPWM object generates a rectangular signal by Hard. We can only use 2 pins on the Nano/Uno and 12 pins on the MEGA. If we do not specify, the period is 20ms (frequency of 50Hz), square signal. The smallest resolution is 62ns, but it depends on the frequency. We cannot set up the PWM before the initialization of the Arduino card which would put the standard functions back; the PWM therefore starts around 16ms after the start of the setup.
MTsoftPWM
A MTsoftPWM object generates a rectangular signal by software. You can use all the pins available at output. The resolution is 100µs, it is that of a time base. You can have a smaller resolution (up to 16µs) by adjusting the MTobjects clock, see "Library adjustment".