Why remove a bit from pressure sensor reading? #42
Replies: 4 comments 2 replies
-
Hi Staffan, I'm looking at this code late at night and it makes no sense to me. ( I wrote it a long while back... 😬 ) In principle, it should be converting an unsigned 12-bit value read from the ADC (MCP3221) into a signed pressure value.
I'll look at this tomorrow with clear eyes, and re-run the pressure tests. I sure hope I find a reasonable answer, because at this point I'm surprised the pressure sensor works at all! (At least the good news is that it does... 😅 ) Cheers, |
Beta Was this translation helpful? Give feedback.
-
Thanks! :) And as you can see I do the same in my C++ code:
What we really get from the sensor over i2c is 3 bytes in the range of 52 000 (not blowing into the horn) to about 80 000 (blowing strongly). Sucking gives a value less than approx 52 000. So what I do is basically subtracting 52 000 and scale the interval to a 0.0 - 1.0 float. I spent several hours debugging my code as I first read it as a WORD (2 bytes), but when I blew strongly into the hax the return value definitely overflowed. So 3 bytes it is, strange as it is a 12 bit ADC, right? When I look at the MCP3221 datasheet it says it send four 0:s, the four highest bits, then the 8 lowest bits in the 2nd byte. Similar code online: https://raspberrypi.stackexchange.com/questions/132322/solved-can-ioctlfile-i2c-smbus-args-be-used-to-read-mcp3221. Two bytes. :) Cheers, Staffan BTW The 4194304 is 01000000 00000000 00000000 binary. |
Beta Was this translation helpful? Give feedback.
-
OK, mystery solved, and it's a bit embarrassing... I'm mistakenly reading 3 bytes when I should read just two. So instead of an error, in the third read we get the most significant byte one more time.
Things do work because of sheer luck 🍀: even with the extra byte, the result is mostly linear with pressure, and I used the 3-byte converted value to be the ground truth. I somehow made an "off-by-a-factor-of-two" error, if that even exists: the ADC returns 3 nibbles, not 3 bytes. Made that mistake early on, and never looked back! I'll be committing a fix for this shortly. In the meantime, accept my apologies... 😓 |
Beta Was this translation helpful? Give feedback.
-
Fixed by db868ba |
Beta Was this translation helpful? Give feedback.
-
Hi Javier! A small question about the code in pressure.rs:
Why are you clearing that bit from reg[0]?
Cheers,
Staffan
SWEDEN
Beta Was this translation helpful? Give feedback.
All reactions