Hall effect sensor PWM signal is not displaying on serial plotter correctly

Hello everyone,

I am using a hall-effect sensor which generates a PWM signal, to measure the speed of a rotating disk attached to the chuck of a lathe. This sensor is connected to pin A0 of the Arduino Uno Rev 3.

The sensor sends out pulses which have a different pulse width depending on the direction of rotation (180 μs for clockwise - 90 μs for anti-clockwise). When I turn the disk clockwise the signalm I see on the serial plotter is clear and constant with regularly spaced pulses, however when the disk is rotating anti-clockwise the pulses are irregularly spaced as if some of the pulses are being missed.

I assumed that this could be due to the main loop taking longer than 90μs to execute and/or the ADC conversion time was too long. So I am using the code detailed in this web page to increase the ADC conversion speed as well as reducing the need to use analogRead() which was taking longer than 90 μs (up to step 7 as i do not require the section about clipping):

(https://www.instructables.com/id/Arduino-Audio-Input/

When I test the code, the main loop takes 26 μs to execute and thus I should be able to read the signal correctly. However, the PWM signal still has irregular pulses when I test the code. I have also timed the time that the Serial.print() section takes within the void loop() and this fluctuates between 88 and 80μs. I am confused as to why the signal is not being displayed on the serial plotter correctly as the code can execute quick enough that the signal should be getting read.

I am relatively new to using Arduino so if I have not been as clear as possible I apologise, please ask me any questions required to make things clearer.

Here is my code please help:

byte x=0;

void setup()
{
  Serial.begin(2000000);

  ADCSRA = 0;             // clear ADCSRA register
  ADCSRB = 0;             // clear ADCSRB register
  ADMUX |= (0 & 0x07);    // set A0 analog input pin
  ADMUX |= (1 << REFS0);  // set reference voltage
  ADMUX |= (1 << ADLAR);  // left align ADC value to 8 bits from ADCH register

  // sampling rate is [ADC clock] / [prescaler] / [conversion clock cycles]
  // for Arduino Uno ADC clock is 16 MHz and a conversion takes 13 clock cycles
  //ADCSRA |= (1 << ADPS2) | (1 << ADPS0);    // 32 prescaler for 38.5 KHz
  //ADCSRA |= (1 << ADPS2);                     // 16 prescaler for 76.9 KHz
  ADCSRA |= (1 << ADPS1) | (1 << ADPS0);    // 8 prescaler for 153.8 KHz

  ADCSRA |= (1 << ADATE); // enable auto trigger
  ADCSRA |= (1 << ADIE);  // enable interrupts when measurement complete
  ADCSRA |= (1 << ADEN);  // enable ADC
  ADCSRA |= (1 << ADSC);  // start ADC measurements
}

ISR(ADC_vect)
{
  x = ADCH;  // read 8 bit value from ADC
}
  
void loop()
{
 Serial.print(x);
 Serial.println();
}

mrkiwe01: I am using a hall-effect sensor which generates a PWM signal, to measure the speed of a rotating disk attached to the chuck of a lathe. This sensor is connected to pin A0 of the Arduino Uno Rev 3.

I am relatively new to using Arduino so if I have not been as clear as possible I apologise, please ask me any questions required to make things clearer.

Part number of the sensor could be useful.