Simple Timing Code for Motors

In a similar post as our “Are your motors wireless?” article, here we’ll look at a common question we receive from those unfamiliar with electronics. We’re often asked if our motors are capable of providing vibrations for, say, 1 second then resting for 5 seconds.

Again, the simplest answer is ‘yes’. This is because the timing function is not a feature of the motor, they turn when provided with a DC voltage and stop when the drive signal stops. In other words, the timing aspect is handled by other intelligent components, who in turn tell the motor when to vibrate:

In most cases, this will be a microcontroller. There are many different types available, one of the most well known is the PIC microcontroller. However, a drawback for most hobbyists or developers is the unfamiliarity with programming. The world of coding and development boards can be daunting for some.

We normally recommend customers to investigate the Arduino platform, based on Atmel AVR microcontrollers. It is inexpensive, easy to learn, and has an excellent community for support. It also handles the power by through USB or an external DC supply. Look at the code below to see just how easy it is to turn the motor on for one second and off for 5:

After reading up on the basic syntax and structure the above will make sense. Even if at first it looks foreign, there are only 3 functions (pinModedigitalWritedelay) and one declaration (int) to understand, plus the declaration is optional!

It is worth noting that a motor driver is included because microcontrollers cannot provide enough current for DC motors, and they can be easily damaged by the motor. See Application Bulletin-001 for a simple, low-cost solution or Application Bulletin-017 for a more advanced motor driver.

All this simplicity comes at a cost, that is the accuracy of the timing circuit is not very good. Perhaps not noticeable if you are only concerned with turning the motor on/off, but if you were to use this as the basis for a watch you may find yourself a couple of minutes faster or slower each day. This can be improved using an external timing circuit:

This is certainly out the scope of this post, but for more information, you may want to consider investigating 555 timers or quartz crystal clocks with binary counters or contact us and one of our engineers will be able to help you.