It won’t surprise you to find out that timers are really useful for stuff involving time. They can generate a regular, consistent signal that’s less like a timer and more like a metronome, only much faster. Instead of beats per minute, their signal is measured in Hertz, and usually in mega-Hertz at that.
So what exactly does this mean, and what applications are timers good for?
Personally, I got interested in timer interrupts after playing around with stepper motors. In my Pmod STEP tutorial, I use a technique known as “bit-banging” to control the time between each step. With bit-banging, the output signal is handled inside the main loop of the microcontroller code. In particularly lazy versions (like mine), a delay function is used to pause the code until a certain amount of timer has passed.
This isn’t the best technique for two reasons. If you’re not careful when you mix signal generation with your other code, you can accidentally write code that takes too long to execute, causing your signal to hang. To demonstrate this, I’ve written the following code. This is possibly the worst example of bit-banging I could come up with!
void loop() { digitalWrite(PIN_LED1, HIGH); delay(1000); digitalWrite(PIN_LED1, LOW); delay(1000); // Go through one full rotation // Step 1 digitalWrite(coil4, LOW); digitalWrite(coil1, HIGH); delay(10); // Give the motor time to move // Step 2 digitalWrite(coil1, LOW); digitalWrite(coil2, HIGH); delay(10); // Give the motor time to move // Step 3 digitalWrite(coil2, LOW); digitalWrite(coil3, HIGH); delay(10); // Give the motor time to move // Step 4 digitalWrite(coil3, LOW); digitalWrite(coil4, HIGH); delay(10); // Give the motor time to move }
(This is just the loop() segment)
If that code made you cringe, that’s just a sign that you are a sensible person. Here’s a GIF of it in action:
Notice how the motor can only move when the LED isn’t blinking? That’s because bit-banging can only do one thing at a time. For microcontrollers, that’s not good. This is where timers come in!
At the bottom, I’ve included the text for an example that uses timer interrupts to control the stepper, instead of bit-banging. Here’s a GIF of it in action:
As you can see, now the motor can spin freely, regardless of what the loop() segment is doing! The timer interrupt, just like other interrupts, allows us to break out of our normal code and take care of important side functions, but timers allow us to be very precise as well.
Check out this tutorial by Shaz on the ChipKIT site. She will show you just how inaccurate the timing of the delay() function really is! It works by using the timer to measure how long the delay function really lasts, and these counts can vary wildly!
I used this code as the basis for my Timer Interrupts tutorial on Instructables. In it, I dive deeper into what timers are and how they work, and I explain each step of the code used to control those timers. If you’re interested in adding timers to your next problem, you should check it out!
What will you use timers for?