Arduino and motorcycle tachometer

Hi, I’m new to the forum but I have been working with arduino for a while now.

I’m really having problems with my project since I need to read a signal but i have not access to a oscilloscope.

I’m trying to read a 12v signal from my bike to my phone via bluetooth, write this program to make it work

#include <SoftwareSerial.h>

SoftwareSerial BT1(10, 11); // RX | TX
const byte engineRPMSignal = 2;
volatile unsigned int counter;
unsigned long passedTime;
unsigned int rpm;

void countRPM()
{
  counter++;
}

void setup()
{
  Serial.begin(9600);
  BT1.begin(9600);
  pinMode(engineRPMSignal, INPUT);
  attachInterrupt(digitalPinToInterrupt(engineRPMSignal), countRPM, RISING);
  counter = 0;
  passedTime = 0;
  rpm = 0;
}

void loop()
{
  delay(1000);

  detachInterrupt(digitalPinToInterrupt(engineRPMSignal)); //Interrupts are disabled
  
  rpm = 60 * 1000 / (millis() - passedTime) * counter;
  counter = 0;
  passedTime = millis();
  
  Serial.print("RPM=");
  Serial.println(rpm); //Print out result to monitor
  BT1.println("RPM= " + String(rpm));
  
  attachInterrupt(digitalPinToInterrupt(engineRPMSignal), countRPM, RISING);   //Restart the interrupt processing
  }

It’s really simple just count interruptions on the pin 2 where its connected the signal of the tachometer

The problems is that as soon as i connect the orange cable with my motorcycle it star sending weird digits like 52839 (not even possible the motorcycle idles at 1250 rpm aprox) and then it stop sending at all and as soon as I disconect the orange cable it comes back and the bluetooth module starts working again sending 0 RPM

Does it getting starved of energy with the battery? (It shouldn’t i charged it and it makes 6.8v 3400mah)

Why do I receive such big numbers?

Why does it disconnect?

Is the CD4050BE a good choice for this application?

Thanks in advice :slight_smile:

rpm = 60 * 1000 / (millis() - passedTime) * counter;

Integer overflow. 60 * 1000 will evaluate to some negative number. You need to tell the compiler to use long here.

rpm = 60UL * 1000UL / (millis() - passedTime) * counter;

SteveMann:
Here's your problem (I think):

void loop()

{
  delay(1000);





Delay() is blocking code, but, I assume, again, the hardware interrupts keep occurring, so during that one-second where nothing else happens, the count is still incrementing.

He is OK with the delay because it is all interrupt driven. Won’t be good if the code has to do anything else. But for now it’s ok.

The other issue is that he has interrupts turned off for too long. Turn off interrupts then make a copy of counter and reset to zero then turn interrupts back on and use your copy for calculations.

Without a scope, you don't know how clean is the signal that you are counting. It may be very noisy- especially if the tachometer on the vehicle is a simple meter movement. It could also be higher than the input limits of your Arduino.

Here's a thread on another forum that discusses this subject.

Delta_G:
He is OK with the delay because it is all interrupt driven. Won’t be good if the code has to do anything else. But for now it’s ok.

The other issue is that he has interrupts turned off for too long. Turn off interrupts then make a copy of counter and reset to zero then turn interrupts back on and use your copy for calculations.

I saw that and deleted my post.
He doesn't need to turn interrupts off and on in loop at all. Just zero the count before the delay() function.

SteveMann:
I saw that and deleted my post.
He doesn't need to turn interrupts off and on in loop at all. Just zero the count before the delay() function.

Oh yes he does need to turn interrupts off when reading or modifying counter. It’s a multi-byte type. Things would be really bad if he went to get the value and it got updated between reading the high byte and the low byte.

Remember, just because you only wrote one line doesn’t mean that the processor did it all in one instruction.

Thanks for the answers.

I saw that project but at least I should get some input but instead my arduino closes connection with bluetooth or restart I don’t really get the problem.

Is the cd4050 an issue?

I'm no longer participating in this thread.

chuso_41:
Hi, I’m new to the forum but I have been working with arduino for a while now.

I’m really having problems with my project since I need to read a signal but i have not access to a oscilloscope.

I’m trying to read a 12v signal from my bike to my phone via bluetooth, write this program to make it work

#include <SoftwareSerial.h>

SoftwareSerial BT1(10, 11); // RX | TX
const byte engineRPMSignal = 2;
volatile unsigned int counter;
unsigned long passedTime;
unsigned int rpm;

void countRPM()
{
  counter++;
}

void setup()
{
  Serial.begin(9600);
  BT1.begin(9600);
  pinMode(engineRPMSignal, INPUT);
  attachInterrupt(digitalPinToInterrupt(engineRPMSignal), countRPM, RISING);
  counter = 0;
  passedTime = 0;
  rpm = 0;
}

void loop()
{
  delay(1000);

detachInterrupt(digitalPinToInterrupt(engineRPMSignal)); //Interrupts are disabled
 
  rpm = 60 * 1000 / (millis() - passedTime) * counter;
  counter = 0;
  passedTime = millis();
 
  Serial.print(“RPM=”);
  Serial.println(rpm); //Print out result to monitor
  BT1.println("RPM= " + String(rpm));
 
  attachInterrupt(digitalPinToInterrupt(engineRPMSignal), countRPM, RISING);  //Restart the interrupt processing
  }




It's really simple just count interruptions on the pin 2 where its connected the signal of the tachometer

![|500x249](https://i.ibb.co/jbkKzdM/arduino.jpg)

The problems is that as soon as i connect the orange cable with my motorcycle it star sending weird digits like 52839 (not even possible the motorcycle idles at 1250 rpm aprox) and then it stop sending at all and as soon as I disconect the orange cable it comes back and the bluetooth module starts working again sending 0 RPM

Does it getting starved of energy with the battery? (It shouldn't i charged it and it makes 6.8v 3400mah)

Why do I receive such big numbers?

Why does it disconnect?

Is the CD4050BE a good choice for this application?

Thanks in advice :)

Perhaps take a look at how they do it here…
https://www.sportdevices.com/rpm_readings.php