[solved]Reading the state of an non-continuous 12V wire, find out it's frequency

What i want right now is to read whenever a feature (on my car) is On or OFF.
Using the multimeter i discovered the wire that gives ground when the feature is OFF and 12V when it's ON.
The problem here is that 12V is not continious. it's a wave or squere signal.

So, what i did was to put a diode, a 1000nF cap, a 100k capacitor discharge resistor, and 1k resistor to the arduino mega that i'm using already for another project, The result was a not stable signal, it varies between 100-900.

In my brain i wanted to somehow, reduce the voltage of the current and also to smooth the signal, but in the same time I want to be very sensible if the feature is OFF and the 12V drops to ground!

But then i realised that for doing this kind of complexity, i need to know the exact time between each 12V signal, and also, if exists(if is big), the rise time.

So, i searched in my boards and i found only an Attiny board and an ESP2866 board.
Is there a way to know the exactly time between every 12v Signal? maybe also show it as a graph? like an oscilloscope ?
Also, What are the limitation ? is it the clock speed? I know that the esp is much faster, he should perform better ?

I'm just trying to understand the question:
When the feature is on, there is a sine wave (or square wave) alternating from 0 V to 12 V, is that right?
And when the feature is off, it's 0 V all the time, right?
And all you want to do now, is to detect whether there's 0 V or the sine/square wave.

If that's what you are looking after, why do you need to measure the frequency? Why not just filter the signal to a continuous DC signal and go from there? A resistor and a capacitor should be all you need, shouldn't it? Or maybe just divide the signal to 5 V peak and read it directly with the Arduino. If no signal for, let's say 500 ms, then the feature is off, otherwise it's on.

prologikus:
What i want right now is to read whenever a feature (on my car) is On or OFF.
Using the multimeter i discovered the wire that gives ground when the feature is OFF and 12V when it's ON.
The problem here is that 12V is not continious. it's a wave or squere signal.

So, what i did was to put a diode, a 1000nF cap, a 100k capacitor discharge resistor, and 1k resistor to the arduino mega that i'm using already for another project, The result was a not stable signal, it varies between 100-900.

100–900 what? mV?

And the ”+12 V” in that drawing, is that the sine/square wave?

Did you actually look at the signal with oscilloscope? If not, why not and how did you find out it's a square/sine wave? And if you did, was it a sine or a square wave…? Can't be both, right…?

guraknugen:
I'm just trying to understand the question:
When the feature is on, there is a sine wave (or square wave) alternating from 0 V to 12 V, is that right?
And when the feature is off, it's 0 V all the time, right?
And all you want to do now, is to detect whether there's 0 V or the sine/square wave.

If that's what you are looking after, why do you need to measure the frequency? Why not just filter the signal to a continuous DC signal and go from there? A resistor and a capacitor should be all you need, shouldn't it? Or maybe just divide the signal to 5 V peak and read it directly with the Arduino. If no signal for, let's say 500 ms, then the feature is off, otherwise it's on.
100–900 what? mV?

And the ”+12 V” in that drawing, is that the sine/square wave?

Did you actually look at the signal with oscilloscope? If not, why not and how did you find out it's a square/sine wave? And if you did, was it a sine or a square wave…? Can't be both, right…?

Tnx for the answer!

Let's start from the beg:

guraknugen:
When the feature is on, there is a sine wave (or square wave) alternating from 0 V to 12 V, is that right?
And when the feature is off, it's 0 V all the time, right?
And all you want to do now, is to detect whether there's 0 V or the sine/square wave.

Right, but i want to know this as fast as possible (not like waiting 3 seconds and see if the analog read, read more then 1, if you know what i mean) And also, as efficient as possible ( not like using a capacitor discharge resistor lower then 100k, because it's a car battery and it's important not to waste energy)

guraknugen:
A resistor and a capacitor should be all you need, shouldn't it? Or

This is what i already done but it's not smooth because i don't know what values for the capacitor and resistor should i use, because i don't know the freqvency

guraknugen:
100–900 what? mV?

Reading from an arduino(5V)

guraknugen:
And the ”+12 V” in that drawing, is that the sine/square wave?

Did you actually look at the signal with oscilloscope? If not, why not and how did you find out it's a square/sine wave? And if you did, was it a sine or a square wave…? Can't be both, right…?

I only have a multimeter, not even one osciloscope in my whole city i think :smiley: ...
so i don't know nothing about the wave, i only know there is a wave and i'm wondering if i can use some of my available board to measure the frequency of this unknown wave.

If all you want to do is detect that there is a series of pulses then all you need is to save micros() when each pulse happens and if the time between pulses is too long then you know the device is off. Something like

if (digitalRead(pulsePin == HIGH) {
   latestPulseMicros = micros();
}

if (micros() - latestPulseMicros >= maxInterval) {
  // the pulses have stopped
}

It might be neater to use an interrupt to detect the rising edge of the pulse - but probably no necessary.

...R

The micros() function can also be used to determine the frequency or the time between the peaks.

I would just let the Arduino read the signal and measure the time between each peak and copy the result into an array. After maybe a hundred peaks or so, I would print the array data with Serial.print() and read the result on my screen. If the data is rather consistent, that is the time between each peak is just about the same every time, then I could calculate the frequency, if the time isn't enough information, and go from there.
I would probably be happy with the time alone and then I would probably use Robin2's suggested method and use my data to define maxInterval properly.

it took me 1 to measure the time
i wrote this code:

  if (onetime == 0)
    if (digitalRead(pin) == HIGH) {
      millis1 = millis();
      onetime = 1;
    }

  if (onetime == 1)
    if (digitalRead(pin) == LOW) {
      millis2 = millis();
      duration = millis() - millis1;
      onetime = 2;
    }

  if (onetime == 2)
    if (digitalRead(pin) == HIGH) {
      duration2 = millis() - millis2;
      onetime = 3;
    }

  if (onetime == 3) {
    mySerial.println("Start:");
    mySerial.println(duration);
    mySerial.println(duration2);
    mySerial.println("Stop");
    onetime = 0;
  }

I used an attiny board ( digispark ) with software serial and a USB to TTL converter (pl-2303) with a OTG connected to my android with a serial monitor from the playstore compatible with my usb to tll,
I also used 13k,13k resistors to divide the voltage

The result from the monitor:
3- duration
99-duration2

So what this means (if you skip the code) is that for 3 ms the voltage = battery voltage (12v) and for 99 ms the voltage is low

Of course those values are not accurate, it may be 1-6ms for the time when it's 12V instead of only 3ms, or maybe more. idk...

So now that i lose my time for learning how to measure this time, :smiley:
How can i choose the capacitor and the discharge resistor so the voltage will be smoothed ?

EDIT:
I simulated a circuit using http://www.falstad.com/circuit/circuitjs.html
with a 12V PWM signal with 3% duty and i just find out that the frecvency also affects what capacitor and resistors should i use.

I also don't know what is the resistor used inside the arduino to discharge the capacitor ( is it 100M ohm ? )

Is the freqvency 9.8Hz ?

If the car is producing a square wave why are you trying to smooth it? Arduinos love square waves.

...R

Robin2:
If the car is producing a square wave why are you trying to smooth it? Arduinos love square waves.

...R

I don't know if is a square wave, how do you suggest to read the state of the function using this RAW signal waves with 3% duty and 10Hz (correct my if i'm wrong)?

I have my doubts if your circuit is safe for your Arduino.

Just feed a steady 12V into it and measure the output with a multimeter. 12V out?

Robin2:
If the car is producing a square wave why are you trying to smooth it? Arduinos love square waves.

...R

int doorlock_wave;
int doorlock_onetime;
int doorlock_timer;

void doorlock_listen()
{
  if (digitalRead(doorlockPin) == HIGH) {
    doorlock_wave++;
  }
}

void doorlock_decide()
{
  if (doorlock_wave == 1) { //if a wave is detected
    if (doorlock_onetime == 0) { //and this step is not done already
      doorlock_onetime = 1; //set this step as done
      doorlock_timer = 0; //count to the next signal
    }
  }
  else if (doorlock_wave >= 2) { //if the second wave is detected
    if (doorlock_timer <= 150) { //if the time between the first and the second wave is less then 150ms (10Hz frequecy)
      doorlock = false; //set the locked state of the car AS UNLOCKED
      doorlock_wave = 0; //reset the wave counter /and/ set this step as DONE
    }
    else { //if the time between the waves is more then 150ms
      doorlock = true; //set the locked state of the car AS LOCKED
      doorlock_wave = 1; //set the waves at 1
    }
    doorlock_onetime = 0; //prepare for the first step again
    doorlock_timer = 0; //reset the timer
  }
}

just did this code, i'm about to test it and see what's happening.

sterretje:
I have my doubts if your circuit is safe for your Arduino.

Just feed a steady 12V into it and measure the output with a multimeter. 12V out?

I was using a peak detector circuit to see the maximum voltage and it was the same as the battery, 12-14V

EDIT: [solved]
The code works, i didn't thought about using the raw signal. i only wanted to smooth it and that make me stuck.
....R :smiley: