Arduino auto gauge project

I’m trying to build a set of digital gauges for an old Dodge pickup with a donor engine that does not have ODB II. I’ve been wanting to learn how to program Arduinos, and this seemed like the perfect project for it. I’ve ordered a couple of Arduinos and some parts, but they are all on a slow boat from China (literally). I did get one of the Arduinos the other day, along with some buttons and LEDs, so I decided to start playing around.

Phase one is the speedometer. The plan is to use a Hall effect sensor to detect a magnet attached to the drive line. If I know exactly how far the vehicle moves every time the drive line completes a revolution, then I can measure the number of revolutions in a fixed time and calculate the MPH from that.

I don’t yet have the Hall sensor, so I hooked up a circuit with a momentary switch. Each press of the button will fill in for the signal from the Hall sensor. Here’s a look at the circuit:

20160529_195455

My plan is to enter a loop for exactly one second and measure the number of times the button is pressed. Multiply that by the distance the vehicle will move for each revolution of the drive line, and you’ve got inches per second. Multiply the inches per second by .0568 and you’ve got miles per hour. Display the result, then reset the pulse counter and jump back to the loop again. As you can see in the picture above, I don’t yet have a display hooked up, so I am dumping the number of pulses and the MPH out to the serial monitor so I can see them on my PC.

In my initial testing I noticed that for a slow computer, the Arduino actually whips through the loop really fast! Even my shortest button presses were registering 10 -12 hits. After a bit of tinkering, I finally worked out how to check the button state so that I was only registering a hit when the state changed from LOW to HIGH. I think this programming thing is starting to come back to me… 🙂

I eventually ended up with the bit of code below, but I still need to find out how far the vehicle travels per drive line revolution. The number I have in there now is probably way too high, as it actually came from a previous thought exercise and was based on putting the magnet on a brake rotor or drum, and is for a full revolution of a 25″ tire. The actual number should be much lower. If it’s not, I may need to put a pair of magnets on the drive line so the MPH is more granular. The ideal number would be 17″ of forward movement per drive line revolution, as that would translate to 1 MPH for every pulse that is detected during the one second measuring period.


/*
  ButtonCount

Uses a pushbutton on pin 2 to simulate a Hall Sensor that will be attached to a driveshaft 
Program loops for one second and counts the number of high readings from the simulated Hall sensor
and then calculates miles per hour from inches per second and outputs it to the serial monitor.

 The circuit:
 * pushbutton attached to pin 2 from +5V
 * 10K resistor attached to pin 2 from ground

 */

// constants 
// set pin number:
const int buttonPin = 2;     // the number of the pushbutton pin
//set other constants:
const int distance = 78.5;   // distance covered by the vehicle for every hit on the Hall sensor

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
int old_buttonState = 0;     // variable for previous pushbutton status
int pulse = 0;               // pulse
int mph = 0;                 // miles per hour

void setup() {
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  Serial.begin(9600);
  while (! Serial); // Wait untilSerial is ready 
  Serial.println("Ready");
}

void loop() {

  unsigned long startTime = millis();  //get the millisecond count at the start of the loop 
  unsigned long interval = 1000UL;     // set the loop interva1 to 1 second
  
  pulse=0;                             // reset the number of pulses before the next count
  while(millis() - startTime < interval)  //loop for the amount of time interval is set to
{
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if ((buttonState == HIGH) && (old_buttonState == LOW)) {
    pulse = pulse + 1; //count a pulse only when the state changes from low to high
    delay(10);
  }
   old_buttonState=buttonState; //set the old buttonstate to the current buttonstate
}  
  

    mph = ((pulse * distance)/(interval/1000)) * .0568;  //calculate MPH
    Serial.print("Pulses = ");
    Serial.println(pulse);
    Serial.print("MPH = ");
    Serial.println(mph);
    

}

Leave a Reply