Noise filter for RPM counter of CNC

Thread Starter

JunkieNL

Joined Mar 19, 2020
54
The situation (working)
I have a CNC router, which I occasionally use to mill PCB’s.
1655563961526.png

I made an RPM counter for the spindle using a TCRT5000:
1655564017409.png

A temporary setup is holding the sensor above the fan, which rotates at the same speed as the spindle. The signal is read by an Attiny412 and the rpm is displayed on a TM1637 display. I tested it using my Dremel (not shown here) and it has no problems working at 600 Hz (36000 rpm). My spindle only goes up to 180 Hz.

The problem (not working)

The spindle is controlled separately from a box outside of the cabinet via a 100 Vdc PWM signal. I want to place the microcontroller and display in that same box. For this I bought a 7x1.5 mm2 (15AWG) automotive cable at a local hardware store and connected it as follows:

  • YELLOW – spindle 100 Vdc PWM
  • BLUE – spindle ground
  • GREEN – chassis ground
  • RED – sensor 5V
  • BLACK – sensor ground
  • WHITE – sensor signal
  • BROWN – NC

Only the sensor and the 2 resistors are inside the cabinet. The microcontroller and display are at the box outside the cabinet. The counter works as expected as long as the spindle is off. As soon as I turn on the spindle, the rpm output becomes erratic.

What I have tried so far

First, I looked at the 5V at the sensor. It showed spikes at a frequency of 16.7 kHz:
1655564125931.png

After placing a 10 uF capacitor in parallel at the sensor side it looked like this:
1655564138956.png

The output of the rpm counter was still erratic, so I placed a 33 nF capacitor between the sensor input and ground. The rpm counter is now working, although not very stable. The signal at the input of the microcontroller looks like this for maximum spindle speed:
1655564153211.png

and like this for minimum spindle speed:
1655564166789.png


The values of the capacitors were found by trial and error. It is now almost working as intended, but I don’t know how to improve this even further.

My Question

What can I do to improve? Do I need a different cable or better filtering perhaps? Any help is appreciated.
 

MaxHeadRoom

Joined Jul 18, 2013
28,617
With CNC it is usually very important to set up proper grounding and earth bonding of all metalic parts of the M/C as well as taking all power commons to earth star point GND.
 

AlbertHall

Joined Jun 4, 2014
12,344
Take a separate cable, preferably screened, for the sensor. That should eliminate the problem providing you follow the recommendations in post #2.
 

Thread Starter

JunkieNL

Joined Mar 19, 2020
54
With CNC it is usually very important to set up proper grounding and earth bonding of all metalic parts of the M/C as well as taking all power commons to earth star point GND.
The chassis is properly grounded to earth (except when I am using my oscilloscope).
The motor controller has a seperate ground and is floating.
The sensor has a seperate ground and is floating.
The CNC mainboard and steppers are not powered at the moment.

Are you suggesting to change this?
 

MaxHeadRoom

Joined Jul 18, 2013
28,617
The motor controller has a separate ground and is floating.
The sensor has a separate ground and is floating.
What I refer to as Ground, = Earth Ground.
So in this context, the (earth) Ground should not be 'floating'.
IOW, the common of each supply should be earth grounded where possible, usually in CNC circuits, a Star ground point is set up where all commons are bonded to.
 

Irving

Joined Jan 30, 2016
3,843
I concur with @MaxHeadRoom and @AlbertHall a second, ideally screened, cable is essential as is proper ground bonding. Your 10uF for the supply decoupling was a key step, but you should improve that by putting a 100nF ceramic in parallel to cover the faster transients.


How are you sampling the pulse in the ATTiny? Interrupt or just polling, or counting transitions in a given time period - can you show your code?
 

Thread Starter

JunkieNL

Joined Mar 19, 2020
54
I concur with @MaxHeadRoom and @AlbertHall a second, ideally screened, cable is essential as is proper ground bonding. Your 10uF for the supply decoupling was a key step, but you should improve that by putting a 100nF ceramic in parallel to cover the faster transients.


How are you sampling the pulse in the ATTiny? Interrupt or just polling, or counting transitions in a given time period - can you show your code?
I figured there was no need for interrupts. It just counts the pulses for 0.2 seconds and then updates the screen.

#include <TM1637Display.h>

// Module connection pins (Digital Pins)
#define CLK 2
#define DIO 3
TM1637Display display(CLK, DIO);

unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 0;
unsigned long updateDelay = 200;
unsigned long lastUpdate = 0;

int counter=0;
bool state = true;
bool lastState = true;

void setup()
{
display.setBrightness(0x0f);
pinMode(1,INPUT);
lastDebounceTime = millis();
lastUpdate = millis();
}

void loop()
{
state=digitalRead(1);
// if (((millis() - lastDebounceTime) > debounceDelay) && (state!=lastState )){
if (state!=lastState) {
counter++;
lastState=state;
lastDebounceTime = millis();
}

if ((millis() - lastUpdate) > updateDelay){
display.showNumberDec(60*5*counter/2);
counter=0;
lastUpdate = millis();
}
}
 

Thread Starter

JunkieNL

Joined Mar 19, 2020
54
What I refer to as Ground, = Earth Ground.
So in this context, the (earth) Ground should not be 'floating'.
IOW, the common of each supply should be earth grounded where possible, usually in CNC circuits, a Star ground point is set up where all commons are bonded to.
How important would you say it is to have common grounded to earth? (In this situation.)

Let me explain why I ask such a question.

Currently I am using a separate isolation transformer for my CNC, since the spindle power suppy is not galvanically isolated. (The rightmost socket has an earth connection.) If I would connect the commen ground to earth and someone would plug the CNC into one of our non-polarized European outlets without the isolation transformer... there is a 50% chance of KABOOM. KABOOM is not safe. :)

1655568822254.jpeg
 

MaxHeadRoom

Joined Jul 18, 2013
28,617
You would need to show by drawing exactly what you have,.
If you refer to such sites as the CNCforum, there are repeated complaints of problems of seemingly spurious activation of limit switches etc.
These are generally cured when equi-potential bonding of the various systems to earth GND is done.
 

crutschow

Joined Mar 14, 2008
34,280
A small resistor (e.g. 1kΩ) in series with the signal from the sensor and before the capacitor across the lines, should help in filtering.
 

Thread Starter

JunkieNL

Joined Mar 19, 2020
54
A small resistor (e.g. 1kΩ) in series with the signal from the sensor and before the capacitor across the lines, should help in filtering.
I have tried this, but it actually made it worse. I did however put a 2k pot after the capacitor, just before the input of the Attiny. That is helping the stability, but there is still occasional over counting. (showing too high rpm)
 

Thread Starter

JunkieNL

Joined Mar 19, 2020
54
Correct,, with hysteresis of about 1.2V @ 5v VDD

I still think an interrupt driven approach using the falling edge would be more reliable rather than looking for a state change.
That is still over counting, even with debounce. Here's my code:


#include <TM1637Display.h>

// Module connection pins (Digital Pins)
#define CLK 2
#define DIO 3
TM1637Display display(CLK, DIO);

unsigned long updateDelay = 1000; // ms
unsigned long lastUpdate = 0;

int counter=0;

ISR(PORTA_PORT_vect) {
PORTA.INTFLAGS = PIN7_bm ;
counter++;
// delay(10); // for debouncing
}

void setup()
{
PORTA.DIR &= ~(PIN7_bm);
PORTA.PIN7CTRL |= PORT_PULLUPEN_bm | PORT_ISC_FALLING_gc;
display.setBrightness(0x0f);
lastUpdate = millis();
}

void loop()
{
if ((millis() - lastUpdate) > updateDelay){
display.showNumberDec(counter);
counter=0;
lastUpdate = millis();
}
}
 

Thread Starter

JunkieNL

Joined Mar 19, 2020
54
My experience, attempts at suppression and other methods are a stop gap, when ground bonding usually cures them all.
This is the same signal with (top) and without (bottom) the common connected to earth ground. There is a clear improvent visible, but not enough.
NewFile2c.png
NewFile2d.png
 

Irving

Joined Jan 30, 2016
3,843
That is still over counting, even with debounce. Here's my code:


...

ISR(PORTA_PORT_vect) {
PORTA.INTFLAGS = PIN7_bm ;
counter++;
// delay(10); // for debouncing
}

...
That debouncing won't work... Delay(x) becomes a no op inside an ISR

Your ISR is being triggered by noise spikes too fast/intermittent to be seen on your scope in the normal way. You might try increasing the (virtual) persistence or using the eye diagram feature (if available). Both will show the waveform gradually 'filling' with otherwise invisible low-going spikes during the high portion of your signal.

You need to accept only low-going pulses greater than a minimum width

Some preconditioning with a 555 timer and some logic could be an option, or simulate this in software: Within the falling-edge ISR set a timer running then return. When it expires use it to trigger a second ISR that tests the state of the interrupt pin. If it's still low then count it as a true pulse. The timer should be just a little shorter than your expected shortest low pulse duration.

It's not 100% foolproof but it should deal with the worst of it.
 
Last edited:

MisterBill2

Joined Jan 23, 2018
18,167
I have fought the noise monster on many occasions and what works is to use both adequately screened cabling AND adequate isolation between the signal loop and everything else. So the connections at the opto device will indeed include "ground" but it will not be the local ground at the sensor. All three lines will be inside the screened cable and the screen will be isolated from every conductor in the noisy area. This will avoid noise currents flowing in any of the three leads. And only capacitivly coupled noise current will flow in the screen. The supply common and the sensor common, (one wire in the drawing), along with the signal common, all can terminate near the signal common point,
 
Top