Arduino Uno - ADC code for sensor?

Hi there,

I'm seeking some guidance for analogue to digital conversion of a speed signal from a inductive type sensor with two wires. It outputs a varied voltage & frequency sine wave signal depending on the speed of the vehicle.

I need this signal converted to a digital output of 0-5v with the right frequency.

The gearbox mounted analogue two wire sensor SP2+ / SP2- runs 4 pulses per rev of the tailshaft of the gearbox.

I need this singal to output as a digital 4 pulse per rev signal.

Can anyone share their programming ideas for such a requirement?

Cheers Mike from NZ.

What happens if you just connect the signal to a digital I/O pin - perhaps pin 2 or pin 3 which can be used with attachInterrupt(). My guess is that the pin will switch from LOW to HIGH when the voltage rises far enough.

You could use the code in an ISR to increment a counter for each pulse

void myPulseCounter() {
   pulseCount ++;
}

If connecting directly to a digital pin does not work perhaps you could use the Atmel chip's analog comparator to detect the pulses.

...R

Thanks for the suggestion Rob, but not sure it will work for my requirements.

I've come across this simple code, the serial has the exact 0-5v output I need, but I need that on a digital output as a 0-5v oltage reading - any suggestions on what additional coding will acheive this?

*/

// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (5.0 / 1023.0);
// print out the value you read:
Serial.println(voltage);
}

UZZ32:
Thanks for the suggestion Rob, but not sure it will work for my requirements.

Why not? It seems to me worth trying the simple stuff first.

I’ve come across this simple code (I’m assuming all the variables are global)

Your simple code uses the ADC to read the voltage at different moments in your sine wave. As I understand it you want to identify whenever (say) the voltage changes from rising to falling

You can do that with code like this

prevSensor = sensorVal;
sensorValue = analogRead(A0);
if (sensorVal < prevSensor) {
     if (newPulseDue == true) {  // so we only read a pulse once
         pulseCount ++;
         newPulseDue = false;
     }
}
else {
    newPulseDue = true;  // reset this when the voltage is rising
}

However the analog comparator will be much more effective at that sort of thing than using analogRead().

…R

Hey Rob, your attachInterrupt() idea, now think you're on the money with that setup.

Could you care to share some more insight on that kind of code sequence?

UZZ32:
Hey Rob, your attachInterrupt() idea, now think you're on the money with that setup.

Could you care to share some more insight on that kind of code sequence?

I prefer "Robin".

I don't understand your question. I suggested some code in Reply #1.
I was assuming that you would record the time for each pulse using millis() or micros() and figure out the frequency from the time between pulses - or the time between (say) 10 pulses - or the number of pulses in a given time.

Please post the code you have tried and ALSO post a photo of a pencil drawing showing how you have everything connected.

...R