Old car tachometer ...
 
Notifications
Clear all

Old car tachometer not working correctly with SimHub  

Page 1 / 2

domnulvlad
(@domnulvlad)
Eminent Member
Joined: 5 months ago
Posts: 26
Topic starter  

So after I figured out how the electronic tachometer (the speedometer is unfortunately mechanic) from the old dashboard that I have works (works exactly like an old VU meter), I wrote a simple Arduino sketch that takes an analog input from a potentiometer, maps it from 0 and 1023 to 0 and 255 and analogWrites it to the motor (with a diode to protect from reverse current from the motor). This works perfectly and I can point it to any value written on it.

 

Then I went to SimHub, set up an Arduino with a tachometer on pin 3 (the pin I used for the previous test) and uploaded. I went to the gauges menu, entered the max RPM of 8(000) and clicked "Test max RPM". It went to about 6100 RPM. Changing the number of cylinders made an extremely small difference, so I kept it at 4. Also the motor was humming differently and spasming when moving the needle up or down with my finger (which it didn't do in the previous test). In the advanced controls, clicking on "Test 1000 RPM" or any other option made it go the the exact same "max position" and setting the offset made it either turn off or go to that position.

 

Setting it as a water temperature gauge makes it work "correctly" but I have a feeling that's not the way to go. Would I have to write a custom protocol and let the Arduino position the tachometer depending on what RPM SimHub sends?

 

Thanks a lot.


Quote
Wotever
(@admin5435)
Member Admin
Joined: 3 years ago
Posts: 427
 

Hi ! That's really an unusual signal, standard tachometer signal is pulse based (half duty based pulse signal which always average to 2.5v with only a pulse frequency modulation) while yours seems to be voltage based, that's why using the standard support won't be able to move the needle (as average voltage is constant) but a basic pwm signal will. Indeed such hardware is out of the standard and you will need custom code to drive it.


ReplyQuote
domnulvlad
(@domnulvlad)
Eminent Member
Joined: 5 months ago
Posts: 26
Topic starter  

@admin5435 So the tachometer has a circuit board on its back (25 yr old PCB with a lot of passives and e few transistors) which has 3 input contacts (GND, 12V, signal) and, just to make sure I wasn't stupid by bypassing the circuit, I wired it up like in the wiki tutorial and checked that I had 12V on the input and ~2.5V at the transistor output when clicking "Test max RPM", but the needle wasn't budging.

 

So I think the only way with this tachometer is connecting straight up to the motor like I did before and driving it with PWM instead of the intended half duty cycle with different frequencies. I will try writing a custom protocol now.

 

I am very grateful for your help!


ReplyQuote
domnulvlad
(@domnulvlad)
Eminent Member
Joined: 5 months ago
Posts: 26
Topic starter  

IT LIVES! It seems I'm not as dumb as I thought I was 😆

For anyone who is in the same situation as me (has a gauge that works on PWM), the code (in SHCustomProtocol.h) is at the bottom of the reply, because the code tags are being buggy.

Please keep in mind that the code is tailored to my tachometer, you will have to change some values (minimum will be changed to the minimum PWM value at which the needle starts to move (for me it isn't 0 because there's some sort of a deadzone), maximum to the PWM value at which the needle is at the maximum RPM marking, tachPin to the PWM-enabled pin where the tach is connected, maxRpm is already commented and the value which the other values are multiplied by).

In SimHub, the custom protocol is just the property [GameData.Rpms].

Thank you, Wotever, for this amazing piece of software and for your help.

#ifndef __SHCUSTOMPROTOCOL_H__
#define __SHCUSTOMPROTOCOL_H__

#include <Arduino.h>

class SHCustomProtocol {
private:

public:

String message;

#define minimum 12
#define maximum 179

#define tachPin 9

#define maxRPM 8 //8 for 8000 RPM, 7 for 7000 RPM etc.

void setup() {

pinMode(tachPin, OUTPUT);

}

void read() {

message = FlowSerialReadStringUntil('\n');

}

void loop() {

float rpm = message.toFloat();
RPM(rpm);

}

void RPM (float rpm) {
float intervalPWM = maximum - minimum;
float intervalRPM = intervalPWM / maxRPM;
analogWrite(tachPin, (minimum + (intervalRPM * (rpm/1000)))*0.925);
}

void idle() {
}
};

#endif

Wotever liked
ReplyQuote
NINO
 NINO
(@nino)
Active Member
Joined: 5 months ago
Posts: 8
 

@domnulvlad hey man, glad to find someone with the same problem, and already having a solution... im really new at arduino, so... can you help me a little with the code you are sharing? i mean, thats not all of the code, where can i find the rest? cause simhub doesnt allow to modify anything right?


ReplyQuote
domnulvlad
(@domnulvlad)
Eminent Member
Joined: 5 months ago
Posts: 26
Topic starter  

@nino Ok, let me start writing a "tutorial" 😊 


ReplyQuote
domnulvlad
(@domnulvlad)
Eminent Member
Joined: 5 months ago
Posts: 26
Topic starter  

@nino Step 1:

Figure out at which PWM value the needle starts moving away from 0 and at which it is at the max RPM value.

Needed:

tachometer's input on pin 9

potentiometer's wiper on pin A0

 

Sketch:

#define tachometer 9
#define potentiometer A0

void setup() {
// put your setup code here, to run once:
pinMode(tachometer, OUTPUT);
Serial.begin(9600);
}

void loop() {
// put your main code here, to run repeatedly:
int val = map (analogRead(potentiometer), 0, 1023, 0, 255); //convert the analogRead of the pot from 0-1023 to 0-255 to be able to analogWrite it as a PWM value
analogWrite(tachometer, val); //write the PWM value set by the potentiometer to the tachometer
Serial.println(val); //print that PWM value to the Serial monitor so you know what it is exactly
}

After uploading it, open the Serial monitor, make sure the baud rate is set to 9600 and twist the potentiometer until it stops being able to turn and  you get the value "0" in the Serial monitor. Now, start twisting it the other way and watch the value increasing (slowly!). Keep an eye on the tachometer and note the value at which it starts moving from the "0 RPM" marking. If it starts moving right away (e.g. the value shown in the monitor is 1 or 2 and the needle is already moving, you write "0" as the minimum (on a piece of paper). If it starts moving at something like 56, you write 56 as the minimum.

Now that you know the minimum, we need to find the maximum. Twist the potentiometer until the needle is at the maximum RPM position on the tachometer. Get the needle as close to the max marking as you can. Now take a look in the Serial monitor. Take the value you are seeing and write it down. For example, if you see "234", you will write "234" as the max value (on a piece of paper).

If, while you are doing these tests, your Arduino keeps resetting without reason, close SimHub!

Ok, now we know the PWM values we must analogWrite to the tachometer in order to either get 0 RPM or max RPM.

(step 2 is coming in a next reply)


ReplyQuote
domnulvlad
(@domnulvlad)
Eminent Member
Joined: 5 months ago
Posts: 26
Topic starter  

@nino Alright. Step 2:

Go into the folder "[SimHub installation folder]\_Addons\Arduino\DisplayClientV2\" and open the file SHCustomProtocol.h with a text editor such as Notepad or Notepad++.

In there, delete everything and paste the code that is at the bottom of this reply.

Then, after pasting, change the following values:

"minimum", from 12, to the minimum value which you got from step 1

"maximum", from 179, to the maximum value which you got from step 1

"tachPin", to the pin on which the tachometer is connected (leave it at 9 if you are using pin 9, it HAS to be a PWM-enabled pin

"maxRPM" to the maximum RPM of your tach. For example, if it goes up to 9000 RPM, you will write "9".

Then comes a more "fiddly" part. You will have to change the value written a bit more to the bottom, that "*0.925". That value is tailored to my tachometer. If your is good, you will change that value to "*1". If the needle is undershooting (it has to indicate 2000 RPM, but it shows something like 1800), you will make that value bigger than 1 (something like "*1.2"). If it is overshooting, you will change it to something less than one (something like "*0.95"). But you will change this value after step 3, which is coming in the next reply.

ALSO, be sure to save the file after you changed its contents!

#ifndef __SHCUSTOMPROTOCOL_H__
#define __SHCUSTOMPROTOCOL_H__

#include <Arduino.h>

class SHCustomProtocol {
private:

public:

String message;

#define minimum 12
#define maximum 179

#define tachPin 9

#define maxRPM 8 //8 for 8000 RPM, 7 for 7000 RPM etc.

void setup() {

pinMode(tachPin, OUTPUT);

}

void read() {

message = FlowSerialReadStringUntil('\n');

}

void loop() {

float rpm = message.toFloat();
RPM(rpm);

}

void RPM (float rpm) {
float intervalPWM = maximum - minimum;
float intervalRPM = intervalPWM / maxRPM;
analogWrite(tachPin, (minimum + (intervalRPM * (rpm/1000)))*0.925);
}

void idle() {
}
};

#endif

ReplyQuote
domnulvlad
(@domnulvlad)
Eminent Member
Joined: 5 months ago
Posts: 26
Topic starter  

@nino And now, the last step, step 3:

Open SimHub, go to the "Arduino" tab, then to "My Hardware", click Multiple arduinos (even if you only have one, you have to use this option because "Single Arduino" doesn't allow custom protocols). Now, click on "Open arduino setup tool" (@admin5435, the word "Arduino" is not capitalized correctly, you might want to change that 😊). In the window that opens, do not select anything from the options on the right if you are only using this Arduino for a tachometer (but change the name to something like "SimHub tachometer"), but in the right part, select the Arduino board that you are using and the correct COM port and click "UPLOAD TO ARDUINO".

After it says "Upload successful", close the new windows until you get back to the "My Hardware" tab. After some time, you should see green text saying "Connected". Now, in that little window under the text "MULTIPLE ARDUINO SETTINGS", where it says "Custom protocol", click the small "EDIT" button. In that window, click at the top on "Computed value" and press the button "Insert property" at the bottom. In the new window, in the search bar, type "rpms" and double-click on the last property ("GameData.Rpms"). Now, the large text box should read "[Rpms]" in a light blue font. Click "OK" at the bottom.

Now, open your game and test. If the needle overshoots or undershoots, return to step 2. Change that value at the bottom of SHCustomProtocol.h from 0.925 to what I explained in step 2.

If you have any questions, ask me.


ReplyQuote
NINO
 NINO
(@nino)
Active Member
Joined: 5 months ago
Posts: 8
 

new info...i found the arduino IDE, and pasted the code in the SHCustomProtocol.h, but now i dont have any V getting out of the 9 pin when i activate the "TEST MAX", wich arduino are you using? im using a nano 328, and it never delivered over 2.35v... im lost...


ReplyQuote
NINO
 NINO
(@nino)
Active Member
Joined: 5 months ago
Posts: 8
 

@domnulvlad sorry just saw your reply, i´ll try that now, i made it some other way hahahah, i have another question, the diode i´m using (cause is the one i have) is not 12v, would that affect the device??


ReplyQuote
NINO
 NINO
(@nino)
Active Member
Joined: 5 months ago
Posts: 8
 

@domnulvlad thanks for your reply! will try that too, sorry for my "hard to read" english, i´m not from arround, but tryin to get this stuff working while noone speaking spanish has any ideas! hahah


ReplyQuote
domnulvlad
(@domnulvlad)
Eminent Member
Joined: 5 months ago
Posts: 26
Topic starter  
Posted by: @nino

@domnulvlad sorry just saw your reply, i´ll try that now, i made it some other way hahahah, i have another question, the diode i´m using (cause is the one i have) is not 12v, would that affect the device??

If you are doing exactly what I'm doing (ground of tachometer goes to ground, signal wire goes to pin 9), you are using the tachometer at 5V, so a 12V diode wouldn't do anything.

I am not sure the diode I used even makes a difference (I used some "signal diode"). It's technically there so the motor doesn't push current into the Arduino when it returns by the force of its built-in spring. I am pretty sure this small motor wouldn't damage an Arduino, but it's there for some safety.


ReplyQuote
domnulvlad
(@domnulvlad)
Eminent Member
Joined: 5 months ago
Posts: 26
Topic starter  

@nino

Also, make sure you are following my steps in the right order. Start from step 1 and end on step 3, so you don't miss important steps.

I hope you can understand my English as well, I am not a native speaker either.

Again, if you don't understand something, just ask.


ReplyQuote
NINO
 NINO
(@nino)
Active Member
Joined: 5 months ago
Posts: 8
 

@domnulvlad Man thanks in the first place, a lot, for taking the time to help me with this, took the code and paste it, replaced the values, checked the pin (used 9 also) but i had a big drop of tension, im reading 0.13v, don´t know why, but changing back to the original SHCUSTOMPROTOCOL the tension is 2.49, wich is correct, and the tacho works but undershoots a lot... like 4000RPMS below... any idea what could be wrong here? it´s a chevy 69 dashboard (irrelevant but just to know)... thanks in advance!


ReplyQuote
Page 1 / 2
Share: