gfurtadoalmeida / esp32-driver-mcp320x

ESP32 driver for Microchip MCP3204 and MCP3208 12-bit ADCs.
MIT License
12 stars 2 forks source link

Voltage not properly displayed #1

Closed Arata-Nakajima closed 1 year ago

Arata-Nakajima commented 1 year ago

When I run the code, the displayed voltage couldn't go greater than somewhere around 70 mV. The hex value in rx buffer was responding properly to the input voltage though, so I modified the code a little bit. Change diff.patch -> diff.html diff.patch

gfurtadoalmeida commented 1 year ago

Hi @Arata-Nakajima, I'll look at it later. Thank you!

gfurtadoalmeida commented 1 year ago

@Arata-Nakajima in your code, on the mcp320x_read_voltage function, you're just returning the digital code read.

The function mcp320x_read will only return a value from 0 to 4096 (as this ADC has 12 bits). This value is not the voltage, but a digital code that must be converted to voltage.

The conversion, handled by mcp320x_read_voltage, is done multiplying the code by the LSB voltage value. The LSB voltage is the reference voltage divided by 4096.

For a 5V reference voltage, the LSB would be 1.22 mV per bit (5000 / 4096). In this case, for a 2048 digital code, the voltage would be 2048 * 1.22 = 2500 mV = 2.5V.

Try downloading the library again, I've committed a fix on a cast, added documentation and simplified the conversion logic.

Arata-Nakajima commented 1 year ago

Thanks for the fix. Yea, the change for the return value of mcp320x_read_voltage was irrelevant. I've just uploaded the file as is for I could confirm the code was working. I will try the newest version as well.

gfurtadoalmeida commented 1 year ago

Hi @Arata-Nakajima, were you able to test the new code, did it work for you?

Arata-Nakajima commented 1 year ago

Perfeito!! Thanks. I changed the code little bit for the continuous ADC monitor. 5 V, 1 Hz sine wave is working fine. image