Reading a pwm signal pulsing at 1kHz with a pulse width varying between 3.88 usec and 998.06 usec

I have a hall encoder that can communicate its reading with a PWM. However, I'm having issues with accessing its full resolution, since the sensor is hardcoded with a PWM frequency of 1 kHz (+-10%) and has a resolution of 12 bits, meaning the wave width is between 3.8 and 998, with increments of 0.24 usec.

I'm using pulseIn to read the width of the signal. It's currently working, but the function return microseconds under the form of an unsigned long, rounding down every reading while doing so.

Are Arduino's even capable of acquiring such a fast signal? If yes, would there be a "pulseIn_short", or any solution easier than soldering tiny wire onto a tssop 14 chips with a cheap imported soldering iron and using SPI/I2C?

Hardware I used:
-Mega
-Sensor: AS5048A:
https://www.amazon.ca/dp/B08352RJGM?psc=1&ref=ppx_yo2ov_dt_b_product_details
datasheet: https://media.digikey.com/pdf/data%20sheets/austriamicrosystems%20pdfs/as5048a,b.pdf

code:

#define pin_sensor 2

unsigned long last_time = 0, time = 0;

void setup() {

  Serial.begin(115200);
  pinMode(pin_sensor, INPUT);
  delay(1000);
}

void loop() {
  time = micros();
  Serial.print(pulseIn(pin_sensor, HIGH));
  Serial.print(",");
  //Serial.print(pulseIn(pin_sensor, LOW));
  //Serial.print(",");
  //Serial.print(pulseIn(pin_sensor, HIGH) + pulseIn(pin_sensor, LOW));
  //Serial.print(",");
  Serial.println(time - last_time);
  last_time = time;
}

Blockquote

Get an adapter board to solder the chip on and connect wires or headers to the wide spaced pads. Then use SPI with the 'A' version or I2C with the 'B' version.

I already have a board with the required component to make a functional circuit. If you use the amazon link, you can see it. Using the pwm would be easy, but the rest of the pinout does not have a lot of surface area to solder onto.

Look for better suited boards.

There might be problems calling PulseIn from within a print statement? May be better to wait for the pulse measurement, then print it? I believe pulseln is a blocking function. Which Arduino are you using? I've had problems with PulseIn on the Due, I went back to a Uno. Also i think the answer it gives you has a granularity of 4 microseconds.

Why not use I2C or, preferably, SPI to talk to the chip? From the picture, you should be able to find an edge card connector to wire to.

With an Uno or Nano you can try to use the input capture unit.
(Take a look at 15.6 Input Capture Unit)

I'm using a mega. I'll try that, but I don't see how it would allow me to get measurement smaller than a microsecond.

Because the pcb the chip is soldered on doesn't allow a lot of space for soldering a connection to the SPI/I2C/Serial. After measuring the board, the 8 thin soldering area are only 0.75mm wide are separated by only 0.3mm.

If there is an edge connector with those dimension, it would probably solve the issue, but I'd like to find a software solution for the sake of learning.

That sound promising! However, I looked at the entirety of the "chapter" 15 and didn't find anything about the acquisition speed. Are you aware if this method would be able to digitalize the duty cycle within a precision of 0.25 usec (about the minimal increment of this chip output)?

If the PWM is constant, unrelated-unsynchronized to the processor, and you have "all day" you can take, say 1000, readings and then find the average. For example, at 50% PWM you'd expect about 500 high readings (and you don't need to count the low readings). It's probably better (more "random" and uncorrelated) the more slowly you read.

Or you can make a low-pass filter to get an analog-average and read the analog.

The description for the prescaler is in chapter 16. You can run the timer at cpu speed (16MHz)... should be 0.0625µs.

1 Like

Unfortunately, the code must be fast since I'm trying to control a mechanism and I'd rather have the loop run as fast as possible. With a single pulseIn, I'm already at 3 ms just to get my angle.

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