Hi,
late summer this idea was born. Now in christmas holidays I have more time to follow up on this project. It sounds simple, but after searching and playing several days I am still stuck in the beginning completely.
I have a bike with a tachometer, which is running ahead approx 8,5%. The reading is faster, than the actual speed. The signal comes from a hall sensor giving e.g. 80.9Hz @10km/h or 1213.4Hz @ 150km/h.
The path:
- Read the signal from the hall sensor usind a Nano. I was thinking of pulseIn()
- define a correction factor, in my case 1.085 and
- give a square wave frequency out, that displays the correct bike speed
I am quite new to micro controller programming, so please let me learn.
I succeded on a simple code, that creates a nice output frequency as I need it.
int PulseModOut = 3;
int SpeedPulse = 7;
int state = 0;
void setup() {
pinMode(SpeedPulse, INPUT);
pinMode(PulseModOut, OUTPUT);
}
void loop() {
while(1) {
if(state == 0) {
digitalWrite(PulseModOut, LOW);
state = 1;
}
else {
digitalWrite(PulseModOut, HIGH);
state = 0;
}
delayMicroseconds(400);
}
}
The problem:
The accuracy is far below of what I expect. The osci reads 1217-1229Hz.
I need to improve on that, this is 1% error. I do not understand why.
@ delayMicroseconds(100) it increases to 2.8% error and
@ delay(0) I get 55.56-62.5kHz program frequeny. That is an amazing 12.5% error!!
What else do I need?
As I am also not satisfied with the reading of input frequency, I split this function code for testing. My osci has a test pin with 2V @ 50Hz, I could not succeed to get a good reading of this yet by using my Nano and pulseIn()
byte PWM_PIN = 3;
int pwm_value;
void setup() {
pinMode(PWM_PIN, INPUT);
Serial.begin(115200);
}
void loop() {
pwm_value = pulseIn(PWM_PIN, HIGH);
Serial.println(pwm_value);
}
Q:
Do I need a transistor, or should I rather use a opto cuppler as a signal converter into my Nano? Dirct connection is not appropriate?
And now the really tricky part in the programming logic:
So, I measure the time between two HIGH or two LOW, which equals to the time of oscillation (T). The frequency (f) correlates as f=1/T. If I shift the frequency by my correction factor, how often should I do it? If I do it every periode, there will not be any change or shift. I must find a logic, that has good update speed and still appropriate functionality. The relatively low input frequency is not really in my favor.
What else shall I consider in this project?