Microcontrollers allow us to do things that would be difficult for us to do on our own. They can help with things like take a picture the instant lightning flashes in the sky or to photograph a balloon popping as soon as you hear it burst.
The Importance of Timing
Timing is an important part of working with microcontrollers. First, lets consider some units of time people are most familiar with. We measure time in terms of seconds, minutes, hours, days, months, and years. We use clocks and calendars to keep track of time.
Scientists think about the time to reach great distances in terms of the speed of light and measure distances in space in terms of light years.
What is the speed of light? Constant c = 299,792,458 meters per second in free space (vacuum) or 186,282.387 miles per second.
Circumference of earth at equator is 40,075,020 meters. At that rate the speed of light would circle the earth approx 7.48 times per second. To circle the earth only once it will take a fraction of a second to be more precise 1/7.48 th of a second or approximately 0.13 of a second.
From Very Large Time Units to Very Small
Microcontrollers work on the other end of a time spectrum – in very small fractions of a second. Although they are good at simply switching things on and off, microcontrollers are great as stop watches keeping track of very small time spans.
An electronic watch uses a crystal to help it keep time. This is an oscillator that vibrates at the rate of 32,768 times a second or 32.768 Kilohertz (kHz).
The Arduino Duemilanove uses a time keeping oscillator too. This one operates at 16 million times a second or 16 Mega Hertz (MHz). That’s over 488 times faster than a watch crystal. This clock speed helps us measure some very small time intervals.
Units of Time at Fractions of a Second
|Millisecond = 0.001 second||1 thousandth of a second.|
|Microsecond = 1.0 x 10-6
or 0.000001 second
|1 millionth of a second|
|Nanosecond = 1.0 x 10-9
or 0.000000001 second
|1 billionth of a second|
|Picosecond = 1.0 x 10-12
or 0.000000000001 second
|1 trillionth of a second|
Some Events Occurring at a Fraction of a Second.
- Car engine running at 1,000 RPM (revolutions per minute) is turning at a rate of 16.6 revolutions per second. This would be one revolution in 60 milliseconds (60 thousandths of a second).
- People on average are only able to see changes occur about thirty times a second. That is the reason movie film and videos often update at 30 frames per second. This would be one frame every 33.3 milliseconds.
- Household alternating current (AC) electricity in the United States switches polarity 60 times a second and 50 times a second in Europe. This would switch polarity one time every 16 milliseconds in the US and 20 milliseconds in Europe.
- High speed video cameras running at 325,000 frames per second can be played back in slow motion allowing us to see what happens with things like bullets hitting an apple or allowing us to see what a rain drop looks like when hitting a pool of water. This would be one frame for each 0.325 millisecond or 325 microseconds (325 millionths of a second).
- The speed of light travels about one foot in one nanosecond or one meter in approximately 3.3 nanoseconds (billionths of a second).
Arduino Time Functions
The Arduino programming language provides functions that handle timing features.
- millis() returns the number of milliseconds (thousands of a second) since the Arduino began running or since the last number overflow.
- micros() returns the number of microseconds (millionths of a second) since the Arduino’s program began running or the last number overflow.
- delay(ms) causes the program to delay the specified number of milliseconds.
- delayMicroseconds(us) causes the program to delay the specified number of microseconds.