Reading 2 wire hall sensor

Hello.

I'm trying to read rpm from 2 pin hall sensor ATS642. I've succesfully managed to get the signal from it. Each time as metal object passes it generates a pulse.

Without metal touching it/passing by - 630mv
With metal touching it/passing by - 1280mv

Here is how it is hooked up.

The signal is stable and nice. BUT when I hook up wire to arduino - and use sketch with interrupt... it just does not work. Arduino does not detect FALLING/RISING.

What should I do?

Hello viktor6hp
Good day.

Please show us your code and give some rough overview about the HW Layout. Mean to which pin the Sensor is connected, how the powersource is made.

Thanks Mascho11

The output voltages either need to be modified by external circuitry so that digital inputs will recognize the transition, or, just use analogRead().

On the Arduino Uno, there is also a comparator that will discriminate the existing output voltage levels. See this tutorial

Here is rough schematic:

I used this generic code:

const int interruptPin = 2;     // Digital pin 2 for reading pulses
volatile unsigned long pulseCount = 0;
volatile unsigned long startTime = 0;
volatile unsigned long endTime = 0;
volatile bool pulseDetected = false;

void setup() {
  Serial.begin(9600);
  pinMode(interruptPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(interruptPin), handlePulse, RISING);
}

void loop() {
  // Your main loop code goes here
  // This loop will run continuously while handling the pulse interrupt

  if (pulseDetected) {
    endTime = micros();
    unsigned long pulseDuration = endTime - startTime;

    // Calculate RPM
    float rpm = 60000000.0 / pulseDuration;  // Assuming one pulse per revolution
    Serial.print("RPM: ");
    Serial.println(rpm);

    pulseDetected = false; // Reset for the next calculation
  }
}

void handlePulse() {
  if (pulseCount == 0) {
    startTime = micros();
  }

  pulseCount++;

  if (pulseCount == 2) {
    pulseDetected = true;
    pulseCount = 0;
  }
}

Hello Victor,
The level of the PWM signal is not matching with the TTL Level Standard:


Your high level at the Oszi is around 700mV. To get high level you need 2V minimum.
I suggest for the software to have a two state statemachine. One state for rising edge one for falling edge.
The interrupt sloap can be set with the EICRA Regsiter

#define EICRA_RISING_EDGE   0b00000011    // EICRA bit ISC01 and ISC00 are set, rising edge 
#define EICRA_FALLING_EDGE  0b00000010    // EICRA bit ISC01 is set, falling edge
#define EICRA_CHANGE_EDGE   0b00000001    // EICRA bit ISC00 is set, change edge

eg

EICRA = EICRA_FALLING_EDGE;

Then measure the time when rising edge was detected, adjust interrupt to falling edge and once falling was detected take the time again. Then calcualte the delta and adjust interrupt to next rising edge and so on..

If you had a higher supply voltage say 12v available you could increase the series resistor to get sufficient voltage to drive the Arduino?

Hello Victor
I suggest to use the Analog Comparator like jremington already has menthoned.
Check the Atmega 328 Handbook with chapter "Analog Comparator". Within the comparator there is already an interrupt generation included. So would perfectly fit.

This is correct, but the wiring diagram is not.
Try using the sensor between pin and ground (polarity matters).
The resistor then goes between pin and 5volt.

The FALLING pulse (produced by the sensor, not the resistor) could be sharper than the RISING pulse.
Leo..

Not only that, the Arduino doesn't recognize the full range of TTL standard voltages.

The lower limit for a HIGH on the Arduino Uno is 0.7Vcc or about 3.5V for a 5V Arduino.

Oh yes good hint, thank you. For UNO with ATmeg 328P


shall then
low max 0.3x5V = 1.5V
high min 0.6x5V = 3V

Use the MCP6541
8 pin DIP, 5V supply, $0.62 (USD)

Thank you!

So its clear that voltage is below factory threshold that arduino can detect.

So how do I increase signal voltage without increasing 5v that are sent to sensor? Can you give me schematic? I have LM393 comparators but don't know how to wire them properly (I thought that comparator can strengthen the signal).

This is an inverting comparator with hysterisis

393

Thank you!! This is schematic and resistor values for my exact voltages?

The output will go LOW when the input goes above about 970mV and the output will go HIGH when the input drops below 890mV
For the unused comparaors, connect the + input to ground through a 10K resistor and connect the - input to 5V through a 10K resistor

You could also put a 50K trimpot in for R2 instead of the 22K resistor and adjust the threshold to whatever you like

Thank you very much! Works perfectly! 393 converted signal perfectly. Everything works as intended! You saved my countless hous!!!

Great!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.