Counter1 randomly increases its value

Hi, i made car trip computer. Everything works fine except one: Counter1 randomly increases its value.
Signal from vehicle speed sensor goes to Shmitt trigger gate C, and T1 input of ATMEGA 328p.
I observed that counter1 counts sometimes 2-10 pulses/s while voltage on T1 input does not change.
I’m not 100% sure but i think that problem exists only when T1 input is in high state.
I tested this on 4 diffrent ATMEGAS 328p and problem still exists.

Komputer_pokladowy.pdf (28.5 KB)

By your schematics is see you have no debounce to prevent switch bounces… Try adding that and see if it works better…

Did you check incoming signals with scope or multimeter? Multimeter are just bad when it comes indicting switch bounces…

The pull-down is only 10k for the speed input. With a noisy environment, like a car, it can be too high impedance, some spikes might get through. What do you mean, that the TI input is in high state ? The speed input ? The schmitt-trigger sets a strong digital signal on the TI input, nothing can change that I think.

I noticed that you don't have filters. Perhaps adding a small capacitance parallel to the 10k would help.

You do need some extra filter and protection for the 12V. A fuse and nothing else is not enough. A fuse + suppression coil + more high voltage capacitors are needed. Perhaps an diode or even a resistor to reduce the current. The LM1117 can have a maximum of 20V at the input. You can get that by turning on the motor for the first time.

How do you use the grounds ? Are the sensor inputs connected to ground somewhere, or just an isolated sensor directly to the Arduino ? Perhaps you need opto-couplers.

Is your battery voltage measured correctly ? After the readVcc, the internal mux and voltages needs to be set normally. You do a dummy read, that is good, but I also wait a few ms after the dummy read (maybe 10ms delay, something between 1 to 20ms).

You have many delays in your sketch. For example the debounce. The Bounce2 library does not use delays, but it relies on a fast running loop() without delays :

Peter_n: What do you mean, that the TI input is in high state ? The speed input ?

I mean that voltage on 11 pin of ATMEGA (T1) is constant, near 5V, without spikes - i measured that with oscilloscope.

Peter_n: resistor to reduce the current. How do you use the grounds ? Are the sensor inputs connected to ground somewhere, or just an isolated sensor directly to the Arduino ? Perhaps you need opto-couplers.

Ground of vehicle speed sensor (for dashboard and ECU) is connected to car ground. Ground of my circuit is also connected to car ground.

It is hard for me to check the sketch, I don't know if that could cause it. I think it is more likely that a spike is in the signal. Perhaps the spike is too short for the oscilloscope. Can you measure the speed signal before the 1k resistor, is that noisy ?

Grounding a circuit is an advanced topic itself. I'm not very fond of the idea that the sensor ground is connected to car ground near the sensor. I would use an opto-coupler for that signal.

What is the maximum frequency of the speed signal ? What is the impedance of the speed sensor ? You should add a capacitor parallel to the 10k anyway. Start with 4.7nF. I hope that will make a difference.

Since you use a 74HCT (with a 'T') the input switching voltage level is lower than normal. I don't know if that matches the sensor. The two 74HCT14 Schmitt-triggers could stretch a pulse of a few ns which could make it just long enough for the ATmega328 chip to detect. That means your circuit could be sensitive for pulses that are near the GHz range.

Speed signal on input (before resistor) - o7.jpg
o6.jpg - upper - signal on input (before resistor) 10V/div, lower - T1 input 5V/div.

Maximum frequency is about 230Hz at 160km/h, so frequency is quite low.
Speed sensor is hall-effect sensor.

I did edit my previous reply and added about the near GHz range, did you read that ? The frequency is indeed very low. Start with 10nF or 22nF parallel to the 10k. I suggest to use 10nF parallel to the 10k, and use it for a while. If the miscounts are reduced by half, use 22nF or 47nF.

The pictures show a clear signal, but that won't show any high frequency spikes.

I will try that, it will be difficult because PCB is SMT.
I have some other (weird) idea: maybe T1 is increased by OC0B?
But TCCR0A and TCCR0B both are 0x03.

Maybe if OCOB has wires along the speed wires, and that causes a pulse in the speed input signal. Or if OCOB causes a peak in the ground current, which raises the ground level of the Arduino board.

When it comes to grounding, shielding and decoupling, almost anything can happen.

OC0B is same pin that T1 in ATMEGA 328

O, sorry :-[ I didn't check the pinmapping, I was using my tablet when I wrote that... :-* I don't understand what your sketch is doing, there is too much going on. The registers are set in the setup(), and in the loop() I only see that you are reading the timer values, that should not change something.

Timer0 is used by delays, millis etc. in Arduino. And i use PD4 (T0) as digital input.

That is no problem. The Arduino timing for millis() is in software. Pin PD4 is a perfectly normal digital pin. It only becomes active when the register is set that is should be connected to TIMER0, but the Arduino default startup code doesn't do that.

Dear Peter_n, thanks a lot! I soldered 22nF capacitor parallel to 10k resistor and that solved my problem! It was quite easy to do, because i soldered it exactly on top of resistor.