[solved] 12v DC motor slot car interrupts problem

Hi there, I am trying to make a project using arduino UNO and some slot cars. I am using a tachometer to change the motor speed of the cars. I am using pwm to do that using a circuit with a TIP120. Here are the squematics :

I’m using external dc 12V . I added a reed switch that goes to digital pin2 and gnd.
So I calculate rpm with the reed switch, and depending the rpm; I map that value to pwm value for the motor.
Everything works great when I connect that to the motor, so everything works as excepted. BUT , when I put the motor (the slot car) in the circuit and starts spinning the interrupts seems to not work anymore as intended. Times given in interrupts are going so high and therefore rpms are not being well calculated…
Is that a problem with noise? Am I doing something wrong with the interrupts?
thanks in advance.

I paste the code:

int sensor = 2;
volatile float time= 0;
volatile float time_last = 0;
volatile int rpm_array[5] = {0,0,0,0,0};
long debouncing = 15;
int led = 9;           // the pin that the MOTOR is attached to
volatile int pwm_value = 0;    // pwm_value
void setup()
  //Digital Pin 2 Set As An Interrupt
  pinMode(led, OUTPUT);
  pinMode(sensor, INPUT);
  digitalWrite(sensor, HIGH);
 attachInterrupt(0, reed_interrupt, FALLING);

//Main Loop To Calculate RPM and Update LCD Display
void loop()
  int rpm = 0;
  if(rpm < 500){
  brightness = map(rpm,0,400,40,190); 
  analogWrite(led, pwm_value); 
  if(time > 0)
    //5 Sample Moving Average To Smooth Out The Data
      rpm_array[0] = rpm_array[1];
      rpm_array[1] = rpm_array[2];
      rpm_array[2] = rpm_array[3];
      rpm_array[3] = rpm_array[4];
      rpm_array[4] = 60*(1000000/(time)); 
    //Last 5 Average RPM Counts Equals....
      rpm = (rpm_array[0] + rpm_array[1] + rpm_array[2] + rpm_array[3] + rpm_array[4]) / 5;
  Serial.print(" pwm:");
  // time for serial
//Capture reed switch Interrupt
void reed_interrupt()
  if((long)(micros() - time_last) >= debouncing *1000){
    time = (micros() - time_last); 
   time_last = micros();

Reed switches are mechanical and therefore tend to bounce. You should try a Hall-effect switch instead.

Reed switches are mechanical and therefore tend to bounce. You should try a Hall-effect switch instead.

I'd agree, with a reed switch you'll very likely need a low-pass RC filter and a
schmitt-trigger circuit - such as 74HC14 inverter - to clean up the signal and lose the
bounces, all rather complex. Software-debouncing (not using interrupts) could be much

I'm not very clear about the power wiring either - motors can put out a lot of e-m noise that
can interfere with your sensors and signals if you don't take care to separate them.

The power supply quality to a slot-car is horrendous, you'll need to pay careful attention to powering logic devices from that.
logic circuits from that

I don't see any filter caps on your +5V ps. You need large (1000uF ) or greater and some 0.1 and 1uF as well.

Hi, can you post a picture of your project please to help understand your problem.
What are you trying to achieve, constant speed or what?
Mapping speed back to PWM to control speed sounds like an ever increasing positive circle or a seesawing up and down speed.
I thought slot cars were about speed control by the driver.
Don't forget these motors spin at thousands of rpm, you may be better measuring wheel speed not motor shaft speed.

Tom....... :slight_smile:

As shown in the code, it is supposed to do software de-bouncing , anyway I'm going to order some hall effect sensors just to compare how it works.
I forgot to say , that this motor has a 100nF in parallel. Where am I supposed to put those filter caps?
I'll post some picture of the project, and how it works when I get back home.

Finally I solved the issue, finally it was an electro-magnetic issue, there was noise in my signal, so that was why I was getting unwanted interrupts, I solved it separating the ground wire of the pwm signal as is quoted here:

thank you everybody !