Problem with reading signals from the HAL sensor - tachometer.

Hi. Hello.

I'm working on measuring the spindle speed in a small CNC machine.

  • The module - WiFi Lora 32 V2 (ESP 32),
  • HAL - Waveshare Hall AH49E,
  • IDE - Arduino.

For the tests I have prepared a very simple code: For one second, through interrupts, the signals from the digital output of the Hall AH49E are counted.

To check the operation of the code, I connect to the module input signal from a square wave generator. The signal has a frequency of several hundred Hz. This frequency is close to that of the sensor mounted on the spindle. The code works perfectly. Without a problem changing the frequency from the generator, I can "measure" the speed from several hundred to tens of thousands of RPM. The measurement result displayed by ESP 32 is stable, as well as the signal from the generator is stable.

Problems start when - instead of a generator - I connect a HAL sensor to the microcontroller. I did two tests

  1. the sensor is near the magnets mounted on the fan from the PC,
  2. the sensor is mounted on the CNC spindle.
    My toy oscilloscope shows that the signal builds up slowly but falls quickly.

The results shown by the code, change from second to second. The number of pulses (interrupts) counted in one second varies, for example, from 952 to 1040 despite the constant spindle speed.

I attach photos from my toy oscilloscope and the corresponding processor calculations.

I am not a world champion in the field of using HAL sensors :wink: If anyone can suggest where to look for the error (code? HAL module?), or what is wrong, I will be grateful.

If you need any more information - ask.

Regards, Tomasz

Sketch

int hallsensor = 17;

volatile int ticks = 0;

void pick() {
ticks++;
}

void setup() {
pinMode(hallsensor, INPUT);
Serial.begin(9600);
attachInterrupt(digitalPinToInterrupt(hallsensor), pick, FALLING);
}

void loop () {
ticks = 0;
sei()
delay (1000);
cli()

Serial.println(ticks);
}

This sensor?

You are using the analog output?
You appear to have added sei() and cli() to the code you have posted without ‘;’ terminators.
Does it work in practice when interrupts are suspended?
You should make a copy of ticks wrapped in cli() / sei() and print the copy. See “non-atomic operations”

This sensor? Yes.
You are using the analog output? No. Digital.

Right comments. I will correct the code. Thanks for advice.