Hope this is the right category, otherwise please move it.
I try to make my Arduino read combustion engine RPM by connecting it to the engines ignition coil. Coil negative is connected straight to optocoupler. Cars 12v positive is connected thru 20Kohm resistor to the other leg of the optocoupler. Arduino is powered from my laptop USB. All works fine, except: Arduino Uno looses USB serial connection to my laptop when its connected and engine running. I have to disconnect USB, close down Arduino software, change USB port and try again. Sometimes it takes up to 10 minutes to establish the connection again.
It works fine on the kitchen table giving it 20v input at about 4 hz. It keeps the USB connection fine. But when connected to the engine, data logging stops and I cant upload any new code. If I turn on some serial.print to see what is happening, it stops printing to serial as soon as I start the engine. Arduino is powered from my laptop USB. The Arduino keeps running its code, I can see that on my LED's. The red LED flashes on idle, the green one turns on over 1500 rpm.
Any input on this would be highly appreciated!
#include <Servo.h>
const int LedPinRed1 = 10;
const int LedPinRed2 = 4;
const int LedPinGreen1 = 9;
const int RpmPin = 3; // RPM input on pin A0.
int RpmPinState = 0;
volatile int tachCount = 0;
long tachCounttime;
long tachCountrpm = 0;
void setup() {
pinMode(RpmPin, INPUT);
digitalWrite(RpmPin, HIGH);
attachInterrupt(1, tachPulse, FALLING);
tachCounttime = millis();
pinMode(LedPinRed1, OUTPUT);
pinMode(LedPinRed2, OUTPUT);
pinMode(LedPinGreen1, OUTPUT);
digitalWrite(LedPinRed1, LOW);
digitalWrite(LedPinRed2, LOW);
digitalWrite(LedPinGreen1, LOW);
}
void loop() {
if (millis() - tachCounttime >= 250)
{
// rpm = number of interrupts counted in 250ms
// *4 to make per second
// *60 to make per minute
// /2 for 2 pulses per rotation (4 cylinder)
// /10 for some reason???
tachCountrpm = ((tachCount*4)*60)/10;
//Do things with the RPM value here (update display etc)...
tachCount = 0;
tachCounttime = millis();
}
RpmPinState = digitalRead(RpmPin);
// LED OUTPOUT // Turn on LED when state changes.
if (RpmPinState == 0) {
digitalWrite(LedPinRed1, HIGH);
}
else
{
digitalWrite(LedPinRed1, LOW);
}
if (tachCountrpm > 1500) { // If RPM is above 1500rpm, then turn on LED.
digitalWrite(LedPinGreen1, HIGH);
}
else
{
digitalWrite(LedPinGreen1, LOW);
}
}
void tachPulse()
{
++tachCount;
}