Introduction
This project is to build a handheld test tool, to measure how fast things are turning. It could have uses in vehicle diagnostics, or for checking how fast a drill is spinning. It could also be used in math/physics experiments to see how fast wheels or other parts are revolving. Basically if any machine is spinning you can measure its rotational speed in revolutions per minute (RPM) or revolutions per second (Hz).
The short (60 second) video here shows it in action:
It is low-cost (<$25) and easy-to-build, so it could be ideal as a beginner 'learning electronics/learning to solder' project. All parts and details are published in this blog post.
How does it work?
The principle is really simple; an infrared LED shines light on the target object. Reflections get sensed by a single photodiode 'eye'. Provided there is a reflective portion of the target object and a non-reflective portion (as an example, a piece of white tape could be stuck on a dark colored object, or a piece of black tape on a light colored object) then the reflected light occurs in pulses directly related to the number of revolutions per minute. The pulses are counted by a microcontroller every second and shown on a LCD display.
Working with the Microcontroller
This project uses a Texas Instruments MSP430 series microcontrollerMSP430 series microcontroller. A microcontroller in a nutshell is a computer-on-a-chip. it contains a microprocessor, memory, storage (often in the form of Flash memory) and lots of input/output (I/O) pins. Software is written and compiled/built into an executable file on a PC, and then programmed into the Flash memory.
In order to program the microcontroller integrated circuit (IC), a cheap development board development board is used. It has a 20-pin DIP socket so it is perfect for plugging in a microcontroller, programming it, removing it and then fitting it into your own projects.
To get started, plug in the development board into the PC using the supplied USB cable. Next, google ‘CCS Cloud’ and register at the TI website and the web browser will show a development environment. All coding and compiling can be done in the web browser, and there is no need to download and install any development software.
Go to File->New CCS Project and give the project any name (e.g. tachometer) and for the Device field select MSP430Gxxx Family and MSP430G2553.
You’ll see that the CCS Cloud environment has created a file for you called main.c and it has a basic main function already inside in the editor pane. It will look like this:
As a test, click File->Save and then click on the Hammer icon which is used to build the code into an executable file. The text in the Output pane will indicate Finished building: tachometer.This proves that the compiler is functioning. In order to download the code into the microcontroller plugged on the development board, all you need to do is click on the Run icon. The first time you do this it may download and install a little helper program to perform the transfer, and subsequently you don’t need to install anything to the PC.
The program doesn’t do anything, so you may wish to experiment and blink the LED on the development board. To do this, type the following code into the Editor pane:
#include <msp430.h> #define LED1_ON P1OUT |= BIT0 #define LED1_OFF P1OUT &= ~BIT0 /* * main.c */ int main(void) { WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer P1DIR |= BIT0; while(1) { LED1_ON; _delay_cycles(60000); LED1_OFF; _delay_cycles(30000); } return 0; }
It should look like this:
Select File->Save then click the Hammer icon, confirm the Output pane successfully says Finished building: tachometer.hex, and then click the Run icon.
After a short pause the code gets transferred into the development board microcontroller, and the red LED should flash rapidly as the code was written to do. The executable file is written into non-volatile Flash memory, so if you unplug the USB cable and then re-insert it, the LED will start flashing again.
Now that hopefully you are comfortable with writing code and building it and installing it onto the microcontroller, the tachometer code can be installed. Go to github and copy the code there and paste it into the CCS Cloud browser editor pane so that it replaces the original code. Same as before, click on File->Save and then the Hammer icon, and again confirm that the output says Finished Building: tachometer.hex. Note that if there are any errors, there will be red error messages in the Output pane and they will need to be corrected. There should be no issue with the current code however.
Click on Run and the code will be installed into the microcontroller. Unplug the USB cable and then using a flat blade screwdriver carefully lever out the programmed microcontroller from the development board. It will be installed in the Cyclops-1000 circuit board!
Building the Circuit
There are different ways to build the circuit; for example using isolated pad proto board, or using a printed circuit board (PCB). To build it on a PCB, the computer aided design (CAD) files are sent to a PCB manufacturer; the typical cost is $20 for 10 boards. One PCB manufacturer is iTead, but others offer similar prices and results too. To use iTead, search for their PCB prototyping service, and purchase their 2-layer 10x10cm board option. Then go into your iTead account order and click on 'View Order' and there will be a button to upload the CAD files. The PCB CAD zip file for this project can be downloaded from github and uploaded to iTead as a zip file. That’s it! The PCBs should arrive within a week or two.
I initially built mine on isolated pad proto board. To start off, the power supply circuit is built. It can be verified by plugging in a battery and using a multimeter to confirm 5V and 3.3V outputs are functioning.
Next, the optical portion of the circuit is constructed. It consists of an infrared LED, and a photodiode circuit that amplifies the received signal and then provides a high/low logic output. The output is inverted and converted to a 3.3V logic signal using the Q1 transistor.
To test this circuit with a multimeter, placing white card to cause a reflection should result in the signal output from Q1 to change from high (3.3V) to low (0V). Careful adjustment of the angle of the LED should allow for a reliable distance of about 10cm from the target using a white card target.
The output from Q1 is connected to the microcontroller. The microcontroller circuit consists of a 20-pin DIP socket for the MSP430 series microcontroller, a reset circuit (R6 and C6), a 32.768kHz crystal, a supply decoupling capacitor (C7) and two pull-up resistors (R4 and R5) which together establish an I2C serial bus.
The microcontroller that was programmed earlier can be plugged into the 20-pin socket once the entire circuit has been soldered.
The final part of the circuit is to attach the LCD display.
And that’s it! The circuit is very simple. Plug the microcontroller in, connect a 9V battery and everything should work. The LCD display will prompt you to point it at a rotating object and the display will automatically indicate the RPM value and the number of rotations per second too (in Hz).
Any questions, please feel free to discuss in the comments section below. Also, if you do improve the hardware or software it would be great to hear about it.
PCB Version
The PCB has a couple of optional features, in particular there is space for a push-button. It could be useful to implement new functions for example min/max display of speed where the speed is varying over time, or a ‘high-res’ mode where very high granularity is needed for the RPM measurement, or a 'counter' mode which just counts number of rotations forever until it is reset.
The PCB version is quicker to build and there is less chance of error during assembly.
Final Test and Assembly
The project functions to speeds beyond practical limits for rotating objects. To prove this, I pulsed an LED at known frequencies and pointed it at the sensor. It worked to beyond 15,000 pulses per second which is close to 1 million RPM. The entire project fits in a handheld casehandheld case; a window will need to be cut for the display, the optical parts and the power switch. Alternatively a 3D printer could be used to create something suitable.
The window was cut to be larger than the text display area, so that the text would be visible from an angle too.
The window was fitted with a neutral density filter. This was glued on the inside of the case. For the push-button, I deliberately drilled a bigger hole so that a plastic cap that I had lying around (it wasn't designed for the specific push-button that I used) could fit. Other cylinder shaped plastic pieces could be used.
It turned out that I didn't need to glue the cap to the push-button; it just sits on top loosely, and is fitted with a lip (I used a grommet) around it so that it won't drop out of the case. Since there is a lot of light bleed from the sides of the LCD, I cut a black piece of card to act as a surround and lightly glued it down (I used 'glue dots' so that it can be easily removed if necessary).
Here are some final photos of the assembled device:
How does the Software work?
The software is straightforward. It is examined in detail here.
To examine it, look at the function called main(). The software sets up the crystal oscillator, and then sets a hardware timer inside the MSP430 chip to repeat every second. When the timer reaches one second the hardware will automatically call an interrupt routine. The main function also sets up another different interrupt to occur each time a falling edge signal occurs on the microcontroller. The edge interrupt pin is connected to the optical circuit.
The rest of the main function just loops forever, waiting for a fresh edge sum to be available, and printing it to the LCD.
Whenever the edge interrupt occurs, a counter is incremented. Whenever the 1-second event occurs the counter value is published in a global variable called edge_sum and then the counter value is reset.
Therefore the global variable edge_sum contains the total number of transitions that occurred every second. This value is multiplied by 60 to get an RPM value.
Summary
This project has shown how to build an RPM measurement device (tachometer) at low cost, and how to program the microcontroller and make modifications to the code. With the PCB this is a really easy beginners project and provides a tool for measuring the rotational speed of all sorts of machines.
Parts List
Note: A quantity of two parts is needed for some line items! Refer to the first column to see how many references there are to that part, to determine the quantity.
Ref |
Value | Description |
---|---|---|
C1, C7 | 100n100n | 100nF ceramic capacitor |
C2, C3 | 1u1u | 1uF ceramic capacitor |
C4, C5 | 10u10u | 10uF electrolytic capacitor |
C6 | 1n1n | 1nF ceramic capacitor |
D1 | SD5620-001SD5620-001 | Honeywell SD5620 optoschmitt detector |
D2 | IR LEDIR LED | 850nm infrared LED Vishay VSLY5850VSLY5850 |
J2 | DIL headerDIL header | optional - SMD 10WAY DIL header 1.27mm pitch |
LCD1 | 2x16 Text LCD2x16 Text LCD | MCCOG21605B6W-BNMLWIMCCOG21605B6W-BNMLWILCD |
Q1 | BC547BC547 | BC547 transistor NPN |
R1 | 1k1k | 1k resistor |
R2 | 22k22k | 22k resistor |
R3 | 47R47R | 47 ohm resistor |
R4, R5 | 2.2k2.2k | 2.2k resistor |
R6 | 47k47k | 47k resistor |
R7 | 180R180R | 180 ohm resistor |
R8 | 330R330R | optional - 330 ohm resistor |
SW1 | Slide SwitchSlide Switch | slide switch OS102011MA1QN1OS102011MA1QN1 |
SW2 | Push SwitchPush Switch | tactile switch 6.5mm x 4.5mm pin spacing Omron B3F-1070B3F-1070 |
U1 | R-78E5.0-0.5R-78E5.0-0.5 | Recom R-78E series 5V 500mA DC-DC converter |
U2 | MCP1702-3302EMCP1702-3302E | 3.3V LDO voltage regulator |
U3 | MSP430G2553MSP430G2553 | MSP430 series microcontroller MSP430G2553IN20MSP430G2553IN20 |
X1 | 32.768kHz32.768kHz | 32.768kHz crystal wire ended, 12.5 pF |
MSP-EXP430G2MSP-EXP430G2 | MSP430 development board | |
DIP socketDIP socket | DIP socket 20-way | |
EnclosureEnclosure | 117x79x24mm case with battery holder | |
PP3 battery clipPP3 battery clip | 9V battery clip | |
Proto boardProto board | optional - 160x100mm perf pad board |
August 2019 Update - Revision 2 Code
Background
The original code was not very granular, it was adequate for (say) power tool speed measurement as a typical use-case.
However, for more granular and more accurate measurements, newer code is now available. The rev2 code is available on github. It is not well tested, there may be bugs.
Initial results look promising: it is way more accurate (perhaps to +-0.1 RPM at speeds of around 600 RPM) and now displays measurements with two decimal places, so it is 100 times more granular.
Revision 2 Algorithm
For those interested (not essential to know for using the device), for the new algorithm, it uses a multiplied-up 8MHz clock in the chip, and counts to establish the transition period. Since it is a 16-bit counter, it will soon run out, so the overflows are counted, and all converted to a 'unsigned long' with not pretty coding, but that can be an "opportunity for later improvement" : ). It gets further uglier to deal with the 1% or so error from the multiplied-up clock: it was all integer arithmetic in the older revision 1 code, but the revision 2 code has some floats to multiply by a calibration value which will have to be user-determined. It could be easily determined with an LED connected to a transformer (i.e. since that will guarantee a 50Hz or 60Hz source), pointed at the sensor.
Also, there's no print library to save on space, so the integer printing routine is also modified to incorporate a decimal place (i.e. scale the value to be printed by 100, and then deliberately always put a dot before the two rightmost digits).
Building the code
To use it, CCS cloud cannot be used as I understand, because it seems to only generate debug code, I could not find how to add or change the build configuration. The code is large enough that Release code is needed to be generated, not Debug code.
So, to use the newer rev 2 code, CCS needs to be installed on your PC.
Once it is installed, create a new project (e.g. called tachometer-new) with the processor set to MSP430G2553, and copy and paste the code from github into the main.c file.
Right-click on the project name in the left pane, and select Build Configurations->Manage and set the Release configuration as active, instead of Debug which is the default and click OK.
Right-click on the project name again, select Properties, and then select on the left side Build->MSP430 Hex Utility and then check the box labelled Enable MSP430 Hex Utility and click OK.
Now build the project (click on the hammer icon). If it is successful then in your work folder, in the tachometer-new\Release subdirectory, there should be several files including one called tachometer-new.hex
That's the file to program into the chip as discussed earlier above in the blog post.
Calibration
The new code will need calibration, because the chip is being used to produce a high-speed clock from the 32.768kHz crystal, and it can have an error of (say) 1%, which is significant. To do the correction, you could use an LED connected to a transformer, to get it to flash at the mains frequency (50 or 60Hz depending on region) and pointing it at the sensor. The mains frequency is very accurate so can be used as a reference in this case. When you do that, the uncorrected output from this project may be (say) 51.4Hz if your're in a 50Hz mains region.
The correction factor is the value measured, divided by the expected value. So, the factor would be 51.4/50 = 1.028
The correction factor is then entered into the code, near line 48 (it is a constant called CORRECTION_FACTOR).
Then, rebuild the code and reprogram the chip as before. It should be a lot more accurate after that!
The rev 2 code appears to work from below 1Hz, to about 48kHz. As another test, it can be used to determine the frequency that infra-red remote controls operate at - just point it at the sensor and press any button.
The display continues to show the measured value for a few seconds.
MSP-FET Debugger Wiring
Although it is not essential for this project, optionally if you have an MSP-FET programmer, then it is possible to use that to program or to debug the code. The MSP-FET is Texas Instrument's programmer/debugger tool for these MSP430 chips.
The diagram here shows how to wire it to the MSP-FET programmer. There are four connections to be made, and these names are labelled on the back of the programmer:
To make the connections, a 1.27mm cable and connector could be used, but it is easier and cheaper to directly solder to the pads. I soldered jumper wire sockets, so that they could be extended with male jumper wires, to connect to the MSP-FET.
I used some epoxy glue to make sure the wires don't break off. When putting it all in the enclosure, the wires will tuck out of the way.
Top Comments