Add an Arduino-based Optical Tachometer to a CNC Router

13,300

137

29

Posted

Introduction: Add an Arduino-based Optical Tachometer to a CNC Router

Build an optical RPM indicator for your CNC router with an Arduino Nano, an IR LED/IR Photodiode sensor and an OLED display for less than $30. I was inspired by eletro18's Measure RPM - Optical Tachometer Instructable and wanted to add a tachometer to my CNC router. I simplified the sensor circuit, designed a custom 3D-printed bracket for my Sienci CNC router. Then I wrote an Arduino sketch to display both a digital and analog dial on an OLED display

A few simple parts and a couple of hours of your time, and you can add a digital and analog RPM display to your CNC router.

Here's the parts list available for 2-day shipping. You can probably source the parts for less if you're willing to wait longer.

Parts List:

$6.99 Arduino Nano

$5.99 IR LED/IR Photodiode (5 pairs)

$7.99 OLED display 0.96 yellow/blue I2C

$4.99 Jumper wires

$1.00 30 inches (75 cm) 3-conductor-stranded wire. Can be purchased from your local home supply store (Home Depot, Lowes) in the buy-by-the-foot section

$0.05 220 ohm resistor ($6.99 if you want 750 assorted resistors)

$0.50 Heat shrink tubing ($5.99 if you want a complete assortment)

3D Printed brackets

Arduino IDE (free)

Note: I initially added a .01μF capacitor after I secured all the wires and notices some erratic RPM values when the CNC was moving. The capacitor worked fine for lower RPMs < 20K but it smoothed out the signal too much for anything higher. I tracked the noise down to powering the Nano and display directly from the CNC shield. A separate supply works for all RPM. I left the steps in for now, but you should use a separate USB power source.

Step 1: Print the 3D Bracket

Print the 3D bracket to hold the IR LED and IR Photodiodes. The 3D files are here and on Thingiverse.

https://www.thingiverse.com/thing:2765271

For the Sienci Mill, the angle mount is used to mount the sensor to the aluminum angle bars, but the flat mount may be better for your project.

Step 2: Optionally 3D Print the OLED Display Holder and Electronic Enclosure

I choose to attach the OLED to an angled display holder that I screwed on the top of a Sienci Electronics Enclosure.

Here are the links to the 3D printed parts that I used.

Sienci Electronics Enclosure 3D Part

0.96" OLED Display Mounting Bracket

The enclosure was a nice place to mount the OLED display bracket and it holds the Arduino Nano nicely, plus it fits on the back of the Sienci Mill. I drilled a couple of holes on the top of the enclosure to attach the OLED bracket.

I also drilled a couple of holes in the bottom to run a small zip tie through to firmly attach the wire harness

Step 3: Build the IR Sensor Wire Assembly

The 3-conductor wire will be used to wire up the sensor. One wire will be a common ground for both the IR LED and the IR Photodiode, with each of the other two going to their respective component.

Step 4: Add a Current Limiting Resistor for the IR LED

The IR LED requires a current limiting resistor. The easiest way, is to incorporate the resistor into the wire assembly.

Bend the tips of each into a U-shape and interlock them. Crimp with a pair of pliers and then solder them together.

Step 5: Splice Jumper Wires

You can splice jumper wires to connect them on Arduino header pins.

Cut a piece of heat shrink tubing and slide over the wire before connecting them.

Slide the heat shrink tubing back over the connection (or entire Resistor) and shrink the tubing by using a heat gun or running a flame quickly over the tube until it shrinks. If using a flame, keep it moving quickly or it can start to melt.

Step 6: Determine the IR LED and Photodiode Leads

Both the IR LED and the IR Photodiode look similar, each having a long (anode or positive) lead and a short (cathode or negative) lead.

Step 7: Insert Diodes Into Holder

Take the IR LED (clear diode) and insert it into one of the LED holder holes. Rotate the LED so that the long lead is on the outside. In the photo, you can see the clear LED in the top hole with its long lead at the very top.

Take the IR photodiode (dark diode) and insert it in the other hole. Rotate the photodiode so that its long lead is in the center.

As shown in the photo, the short lead of the LED and the long lead of the photodiode will both be in the center. These two leads will be spliced to a common wire back to the arduino. (See the tech notes at the end if you want more details)

Take a small piece of 1.75 filament and insert it behind the diodes. This will lock the diodes in place and prevent them from rotating or coming out.

I went through several iterations of designs before settling on this one. Having the diodes stick out a bit greatly improved the tolerance when aligning it with the collet nut.

Step 8: Fuse the Locking Filament to the Holder

You'll want to trim the locking piece of filament to just a little longer than the width of the holder.

Heat a nail for a few seconds in vise or holding it with pliers.

Step 9: Press the Filament Ends Against the Heated Nail Head

Keep your finger on the opposite end of the filament and press to melt and fuse the locking pin in the holder.

Step 10: Finished Diode Holder

Flush and neat

Step 11: Attach the Wiring Harness to the Diodes

Trim the wire to length for your application. For the Sienci Mill, you'll need about 30 inches (~75cm) in total (wire + jumpers) and have slack for the router to move.

Bend the wire and lead tips into a U-shape to interlock them and make soldering easier.

Take some thin heat-shrink tubing and trim two short pieces and two slightly longer pieces. Slip the shorter pieces over the outside diode leads. Slip the longer pieces over the two center leads.

Having two different lengths offsets the splice joints and offsets the thicker joints from each other so that the diameter of the wiring is reduced. It also prevents any shorts between the different wire splices

Cut three pieces of slightly larger diameter heat-shrink tubing and place them over each of the three wires in the wiring harness.

It's important to make sure that there's a little gap between the ends of the heat shrink tubing on the wires and the splice point. The wires will get hot, and if the heat shrink tubing is too close, they'll begin to shrink at the end, potentially making them too small to slide over the joint.

Step 12: Ensure the Wire With the Resistor Is Attached to the Long Lead of the IR LED

The current limiting resistor (220 ohm) built into the wiring harness, needs to be connected to the long (anode) lead of the clear IR LED. The wire connecting the two common leads will be connected to ground, so you may want to use a black or bare wire for that connection.

Solder the connections to make them permanent.

Step 13: Shrink the Heat-shrink Tubing

After the joints have been soldered, use a match or lighter to shrink the tubing on the diode leads first. First move the heat-shrink tubing on the wires as far away from the heat as possible.

Keep the flame moving quickly as it shrinks and rotate to get all sides equally. Don't linger or the tubing will melt instead of shrink.

After the diode leads have been shrunk, slide the slightly larger heat-shrink tubing from the wires, over the joints and repeat the shrinking.

Step 14: Prepare the Mounting Block

Depending on your application, choose the mounting block that suits your application. For the Since Mill, select the angle mounting block.

Take a M2 nut and a M2 screw. Screw the nut just barely on to the end of the screw.

Turn the mounting block over and test fit the M2 nut into the hole.

Remove and heat the nut slightly with a match or flame and then quickly insert it into the back of the mounting block.

Unscrew the screw, leaving the nut embedded in the plastic mounting block. For some added strength, apply a drop of super glue to the edge of the nut to securely attach the nut to the block.

Step 15: Ensure the M2 Screw Is the Proper Length

Make sure that the screw is not too long or the sensor will not tighten against the mounting block. For the angle mounting block, ensure that the M2 screw is 9mm or a bit shorter.

Step 16: Attach the Mounting Block to the CNC Router

For the Sienci Mill, attach the angle mounting block to the bottom of the inside of the Z Rail with a couple of drops of super glue.

Step 17: Attach the Sensor to the Mounting Block

Place the adjustable arm into the mounting block

Insert the M2 screw with a washer through the slot in the adjustable mounting arm and screw it into the nut.

Slide the adjustable arm until the LED and Photodiodes are even with the router collet nut

Tighten the screw

Step 18: Add Reflective Tape to One Side of Collet Nut

Use a small strip of aluminum tape (used for furnace ducts) and attach it to one facet of the collet nut. This reflective tape will allow the IR optical sensor to pick up a single revolution of the spindle.

Step 19: Ensure the Reflective Tape Does Not Go Over the Edge to Adjacent Facets

The tape has to be on one side of the collet nut only. The tape is thin and light enough that it does not interfere with the wrench to change end mills or affect spindle balance.

Step 20: Run the Sensor Wire Along the Inside of the Z Rail.

Using strips of the aluminum duct tape, attach the wire to the inside of the Z Rail. It's best to run the tape near the edge of the angle rail to clear the lead screw nut assembly.

Step 21: Attach the Sensor to the Arduino Nano

Connect the wires to the Arduino as follows:

  • IR LED (with integrated resistor) -> Pin D3
  • IR Photodiode -> Pin D2
  • Common wire -> Pin GND

Step 22: Attach Jumper Wires to the OLED Display

Pull off a 4-wire set of jumper cables

Plug the wires into the 4 pins for the I2C interface:

  • VCC
  • GND
  • SCL
  • SDA

Step 23: Attach the OLED Display to the Arduino

Attach the jumper wires to the following pins. Note: These wire do not all attach to adjacent pins, nor in the same order.

  • VCC -> Pin 5V
  • GND -> Pin GND
  • SCL -> Pin A5
  • SDA -> Pin A4

Step 24: Attach the OLED Display to Its Holder

Using the brackets you printed earlier, attach the OLED display to its holder

Then attach the display to the CNC frame.

Step 25: Prepare the Arduino IDE for Loading the Arduino Sketch

A program for an Arduino is called a sketch. The Integrated Development Environment (IDE) for Arduinos is free and must be used to load the program to detect the sensor and display the RPM.

If you don't already have it, here's a link to download the Arduino IDE. Choose the downloadable version 1.8.5 or above.

Step 26: Add the Required OLED Libraries

To run the OLED display, you'll need a couple of additional libraries, the Adafruit_SSD1306 library and the Adafruit-GFX-Library. Both libraries are free and available through the links provided. Follow the Adafruit tutorial on how to install the libraries for your computer.

Once the libraries are installed, they're available for any Arduino sketch you create.

The Wire.h and Math.h libraries are standard and are automatically included in your IDE installation.

Step 27: Connect the Arduino to Your Computer

Using a standard USB cable, connect the Arduino Nano to your computer with the Arduino IDE.

  1. Launch the IDE
  2. From the Tools menu, select Board | Arduino Nano
  3. From the Tools menu, select Port |

Now you are ready to load the sketch, compile it and upload it to the Nano

Step 28: Download the Arduino Sketch

The Arduino Sketch code is attached and is also available on my GitHub page where any future improvements will be posted.

Download the OpticalTachometerOledDisplay.ino file and place it into a work directory with the same name (minus the .ino).

From the Arduino IDE, choose File | Open...

Navigate to your work directory

Open the OpticalTachometerOledDisplay.ino.ino file.

Step 29: Compile the Sketch

Click the 'Check' button or choose Sketch | Verify/Compile from the menu to compile the sketch.

You should see the compile area in the bottom, with a status bar. In a few seconds the message "Done Compiling" and some statistic on how much memory the sketch takes up will be displayed. Don't worry about the "Low Memory Available" message, it does not affect anything. Most of the memory is used by the GFX library needed to draw the fonts on the OLED display and not the actual sketch itself.

If you see some errors, they are most likely the result of missing libraries, or a configuration issues. Double check that the libraries have been copied into the correct directory for the IDE.

If that doesn't fix the problem check the instructions on how to install a library and try again.

Step 30: Upload to the Nano

Press the 'Arrow' button or choose Sketch | Upload from the menu to compile and upload the sketch.

You'll see the same 'Compiling..' message, followed by an 'Uploading..' message and finally a 'Done Uploading' message. The Arduino starts running the program as soon as the Upload is complete or as soon as power is applied afterwards.

At this point, the OLED display should come alive with a RPM: 0 display with the dial at zero.

If you've put the router back together, you can turn on the switch and see the display read out the RPM as you adjust the speed.

Congratulations!

Step 31: Use a Dedicated Power Source

NOTE: This was the source of the the signal noise that caused the erratic RPM displays. I'm investigating putting some filter caps on the power jumpers, but for now you'll need to power it via a separate USB cable.

--------

You can run the display connected to your computer with the USB cable, but eventually you'll want a dedicated power source.

You have a couple of options, you can get a standard USB wall charger and run the Arduino from it.

Or you can run the Arduino directly from your CNC router electronics. The Arduino/OLED display only draws 0.04 amps, so it's not going to overload your existing electronics.

If you have Arduino/CNC Router Shield electronics (like the Sienci Mill), then you can use a couple of unused pins to tap into the needed 5 volts of power.

On the upper left side of the CNC router shield, you can see that there are a couple of unused pins labeled 5V/GND. Attach a pair of jumper cables to these two pins.

Step 32: Connect the Arduino to the Power Jumpers

This one's easy, but not as nicely labeled.

On the Arduino Nano, there are a set of 6 pins at the end of the board. They aren't labeled, but I've included the pin out diagram and you can see that the two outside pins that are closest to the indicator LEDs are labeled GND and 5V on the diagram.

Connect the jumper from the 5V pin on the CNC shield to the pin nearest to the one labeled VIN (don't connect it to VIN, but to the inside corner pin of the 6 pin group). VIN is for powering the Nano with 7V-12V power.

Connect the jumper from the GND pin on the CNC shield to the pin nearest the TX1 pin.

Now when you turn on the CNC router electronics, the OLED RPM display will come on as well.

Step 33: Technical Notes on the Circuit

The sensor circuit uses an IR LED/IR Photodiode pair.

The IR LED works like any regular LED. The positive lead (the longer or anode) is connected to positive voltage. On an Arduino Nano, it's an output pin set to HIGH. The negative lead (shorter or cathode) is connected to ground to complete the circuit. Since LEDs are sensitive to too much current, a small resistor is placed in series with the LED to limit the amount of current. This resistor can be anywhere in the circuit, but it makes the most sense to place it on the positive side of the circuit, since the negative lead shares a connection to ground with the photodiode.

The IR Photodiode behaves like any other diode (including Light Emitting Diodes LEDs) in that they only conduct electricity in one direction, blocking electricity in the opposite direction. That's why it's important to get the polarity correct for LEDs to work.

The important difference with Photodiodes, is when they detect light, the photodiodes will allow electricity to flow either way. This property is used to make a light detector (in this case Infrared light or IR). The IR Photodiode is connected in an opposite polarity (called reverse bias) with the positive 5V on the Arduino pin connected to the negative lead of the photodiode and the positive lead is connected through a common wire along with the IR LED to ground.

With no IR light, the IR photodiode blocks electricity, allowing the Arduino pin with its internal pull-up resistor be at the HIGH state. When the IR photodiode detects IR light, it allows electricity to flow, grounding the pin and causing the HIGH value on the photodiode pin to drop down towards ground causing a FALLING edge that the Arduino can detect.

This change of state on the Arduino pin is used in the sketch to count the revolutions.

The strip of aluminum tape on the collet nut, reflects the IR light from the always-on IR LED back to the IR photodiode every time it rotates past the sensor.

Step 34: Technical Notes on the Arduino Sketch

The Arduino sketch drives the OLED display and simultaneous reacts to IR LED/IR Photodiode sensor.

The Sketch initializes the OLED display throughout he I2C (Inter-integrated Circuit) protocol. This protocol allows multiple displays/sensors to share a connection and can read or write to a specific connected device with a minimum of wires (4). This connection reduces the number of connections between the Arduino and the OLED display.

It then turns on the IR LED by setting that pin HIGH providing the 5V needed for the LED.

It attaches an interrupt function to a pin that is called when it detects a change that pin's state. In this case the incrementRevolution() function is called whenever a FALLING edge is detected on Pin 2.

An interrupt function does just what it implies, it interrupts anything that is currently being done, executes the function and then resumes the action exactly where it was interrupted. Interrupt functions should be as short as possible, in this case it just adds one to a counter variable. The little Arduino Nano runs at 16Mhz - 16 million cycles per second - plenty fast enough to handle the interrupt of 30,000 RPM, which is only 500 revolutions per second.

The Loop() function is the primary action function for any Arduino sketch. It is continuously called, over and over again as long as the Arduino has power. It gets the current time, checks to see if a specified interval has elapsed (1/4 second = 250 milliseconds). If so, it calls the updateDisplay() function to display the new RPM value.

The loop function will also dim the display after 1 minute and turn off the display after 2 minutes - fully configurable in the code.

The updateDisplay() functions calls the calculateRpm() function. That function takes the count of revolutions the interrupt function has been steadily incrementing and calculates the RPM by determining the rate of revolutions per time interval and extrapolating that to the number of Revolutions per Minute.

It displays the numerical value and uses some High School trig to draw an analog dial and the indicator arm to reflect the same values.

The constants at the top of the sketch can be modified, if you would like a RPM dial with different major and minor values.

The update interval and average interval can also be modified.

2 People Made This Project!

Recommendations

  • Microcontroller Contest

    Microcontroller Contest
  • Science of Cooking

    Science of Cooking
  • Pocket-Sized Contest

    Pocket-Sized Contest
user

We have a be nice policy.
Please be positive and constructive.

Tips

once you have downloaded the libraries go into the adafruit_SSD1306 folder open the Adafruit_SSD1306.h with a text editor go down the page until you see
-----------------------------------------------------------------------*/
// #define SSD1306_128_64
#define SSD1306_128_32
// #define SSD1306_96_16
/*=========================================================================*/

and change to this
-----------------------------------------------------------------------*/
#define SSD1306_128_64
// #define SSD1306_128_32
// #define SSD1306_96_16
/*=========================================================================*/

6 Questions

Did you ever figure out a solve for the 5v power from the power supply as opposed to needing a 5v USB wall wart?

0

Thanks for replying to my questions about TFT display and slow revs. I guess I am trying to run before learning to walk on this, but your sketch is using I2C (which I have tried on an OLED screen) and the screen I would like to use is SPI. I was hoping you may have some experience of this type of screen to help me get started as the code will surely be quite different. Also, I don't really understand why dealing with the slow revolutions by increasing the pulses is better than in the sketch. Looking forward to your answer.

0

A couple of questions.

I am having trouble working out if and how to get this to work at speeds of 0 - 1000

Would this work with a 1.8 SPI TFT screen on the nano and how

Yes, it should work with the 1.8 TFT screen. You'll need the correct driver, which Adafruit has available on their site. You'll need to adjust the constants that define the OLED resolution, so you'll need to change OLED_HEIGHT = 64, OLED_WIDTH = 128; to the 128x160 resolution of the 1.8 TFT. You may need to adjust the YELLOW_SEGMENT_HEIGHT = 16 statement that defines the height of the RPM: ###### banner at the top, and you may need to adjust the TEXT_SIZE_LARGE = 2 constant to a different number that correlates with the font size you choose for that top RPM:### banner.

The other critical thing you will need to do is to increase the number of sensor pulses per revolution to more accurately display slow RPM. The easiest way is to add additional reflective strips to the spindle. On the Router, you could easily add from 1 to 5 more strips to the collet nut to add additional sensor pulses to detect. In the caculateRpm() function you'll need to create a new constant and divide the revolutions by the total number of sensor pulses per complete revolution. ie. const int PULSES_PER_REVOLUTION = 4; current_revolutions = revolutions / PULSES_PER_REVOLUTION;

Then adjust the arrays to that will generate the dial. Change MAJOR_TICKS[] to MAJOR_TICKS[] = { 0, 1000 }; and MINOR_TICKS to MINOR_TICKS[] = {250, 500, 750};

Hi! I'm quite interested in this little project. I have a q: can I use a single color display instead a two color one? I suppose I have to modify the programming right?

A single color display is supported without any changes needed. The two-color OLED displays dedicate 16 pixels for the top color. On a single color display, those same number of pixels are used as the top banner, but it's all one color.

This instructable is almost exactly what I want to do with my band saw but cannot because, being well past 70 my ability to learn Arduino is practically zero even though I am still capable of assembling and soldering the kit. Doubly so when guided by such a well written set of instructions

My thought is to put six equally spaced reflectors on the bandsaw top wheel (I expect that at very low speeds a single reflector on a wheel doing only a few rpm when cutting metal would not get enough signals to be reliable) and I need to read out the blade speed in feet per minute and/or metres per second. I have butchered up a treadmill to provide a widely variable speed as no amount of pulleys and belts that I could come up with would bring the speed down to 80 feet per minute needed for some metals.

If you have a band saw in your workshop can I please suggest you design a version of this project suitable for use on band saws and make an instructable on that as well?

That sounded like a reasonable question, so I modified the the arduino code and constructed the display and sensor holder out of wood this time. I ended up using 24 reflective strips to provide a reasonably smooth display and enough precision for the slower speeds. I just published the instructable, so let me know if you have any questions. https://www.instructables.com/id/Add-a-Linear-Speed-Display-to-Your-Bandsaw

I don't know for sure, but I'm skeptical that it would work. The sensor in the project needs to respond quickly to the reflective input. At 30K RPM, the sensor is sending 500 pulses a second to be detected by the Arduino. I would be concerned that the proximity sensor might not be optimized to respond so quickly and may even be buffered so that 'smart car robots' don't twitch when the sensor is triggered.

9 Comments

While I quite "butchered" the code to only get results via the serial (don't have a screen here for now), I used the instructions and diode mount files to create the "sensing" part. Thanks a lot, it's REALLY super clean/clear.

Also, while I had at first a lot of troubles to get reliable readings (especially at slow speed), by adding a .75nF capacitor between the "signal" pin and ground, it resolved everything. I now get good measures from almost zero to the 10000RPM max my spindle goes to.

I'll use the measures as an input for a PID algorithm to try and keep the spindle spinning at the requested speed even under load changes...

I'd love to build one of these for one of my old motorcycles that never had a Tach, however, this display is too small! Would this same circuity work with a larger display, maybe 3" or so? And if so, what display would work?

I have the full color 3" touch screen for the Arduino that could be used. It needs a full-size Arduino to drive it. I wouldn't know how to properly connect the sensor up to the engine to have a proper tach. I believe it would be cheaper (and more reliable) to purchase an after market tach. You could cobble together a combination, use an aftermarket tach and have it drive a 3" Arduino graphics display.

First, I really like the way you used the collet nut for the reflector. I'm working on a project where i am having issues with reflections off a cylinder at the top of my router... this could solve those issues for me.

Next... you may want to consider this sensor... pee-manufactured, and because of the angle, it has a clearly defined focal area. (There are many similar ones... this is just an example.)
https://www.digikey.com/product-detail/en/tt-electronics-optek-technology/OPB703/365-1675-ND/1638026

I hadn't really considered any of those, but looking at the the pages of Digikey sensors listed, they have an angled sensing field and are tuned to a distance of 0.15". The data sheets show a massive drop-off in sensitivity past 1" The sensor in the project works extremely well at the needed distance of 1.5".

Well, I would have voted, if this Instructable was in a contest. Great job.

great project glad you used the small uno, shows you gave some partial thought to the project

This is excellent work. Your article is very complete, solves a real problem, and gives credit where it's due. Brings tears to my eyes. I'm so glad you took the time to write the article. Doing those step-by-step photos isn't easy because sometimes you just get going and before you know it you're saying, "Aggggh! I should have taken photos of that" but its too late. The LED mount fabrication was most fun to observe. I'm not a fan of 3D printing but for your project it seems to have worked well.

This was a great article and is right on with the spirit of the Instructables site.

NetZener