Measuring current + voltage with a microcontroller  - Page 1
Forum section: electronics / beginners

author: MathCa
author_rank: Contributor
timestamp: March 06, 2023, 05:43:42 pm
content: Hello,I saw contradicting recommandations about the best ways to measure current and voltage with a microcontroller, one with 5v logic in my case.For the current, I need to be able to get a decent precision from 100mA to 150mA and a medium precision at least from there up to 25A max.The 100-150mA is to detect when I can stop the charge of a battery pack and up to 25A to record some stats about the usage of this pack.For the voltage, it has to have a good precision around 16.8v.If possible, it would be easier for me to use already made modules.Thanks for your recommendations! 🙏

author: Zero999
author_rank: Super Contributor
timestamp: March 06, 2023, 06:27:54 pm
content: A potential divider can be used to scale the voltage down to 0 to 5V.R1 = 24k & R2 = 10 would giver 5V out, with 17V in. If the ADC input isn't buffered, then a small capacitor, say 10nF, will need to be connected between Vout and 0V, so the sample and hold capacitor charge/discharging doesn't affect the reading.A Hall effect sensor can be used to sense the current. 100mA of 25A, is 0.4% so it's a push to get that level of accuracy. A 10bit ADC is essential and it will need to be zeroed, whilst the current isn't flowing.

author: MathCa
author_rank: Contributor
timestamp: March 06, 2023, 06:51:37 pm
content: Quote from: Zero999 on March 06, 2023, 06:27:54 pmIf I use a 16 bit ADC module, do you know if I should get a pretty accurate reading?" alt="" class="bbc_img" />

author: Zero999
author_rank: Super Contributor
timestamp: March 06, 2023, 07:38:14 pm
content: Quote from: MathCa on March 06, 2023, 06:51:37 pm

author: MathCa
author_rank: Contributor
timestamp: March 06, 2023, 07:47:24 pm
content: Quote from: Zero999 on March 06, 2023, 07:38:14 pmThanks I will read about it right now.Quote from: Zero999 on March 06, 2023, 07:38:14 pmYes, one when charging and the other when discharging.Quote from: Zero999 on March 06, 2023, 07:38:14 pmIs it mandatory to have -25A to +25A or I could have 0A to +25A? There are so many details to learn, it cannot be boringEdit: "If the output common mode voltage is set at mid-range, the full-scale current measurement range is equal in both directions." so now I understand why, it's to mesure in both directions? (Page 15https://www.mouser.ca/datasheet/2/389/tsc210-1852380.pdf)Thanks again!

author: Zero999
author_rank: Super Contributor
timestamp: March 06, 2023, 08:04:29 pm
content: Yes, current flowing in one direction gives a positive value and the other direction a negative value. This voltage is added to the voltage on the reference pin.Note the reference pin of the TSC214 has a low impedance, just 10k, so if you want to use a potential divider, it will need a unity gain buffer i..e a non-inverting op-amp with a gain of 1.

author: Terry Bites
author_rank: Super Contributor
timestamp: March 06, 2023, 08:23:30 pm
content: The volts bit is easy. A simple divider is fine. You will have to protect your ADC from over voltage accidents. You need to check on the makers recomendations, often a series resistor (part of your RC filter) is enough. Ask for further advice here if you need to.Current ranges.It depends on the accuraracy and linearity of the sensor you use. A typical 25A hall sensor has say 50mV/A sensitivity. At 150mA the output will be 37.5mV. You'll have at least 10mV of output noise from the sensor. Filtering will help. Go figure.You will be better off with a 4 wire shunt and a bidirectional "zero drift" current sense amplifier. eg TSC213These things are much (much) more accurate than hall or GMR sensors. They are diff amps made for this purpose, most regular diff maps can't sense down near 0V.. Low side sensing is applicable, a high side amp will die as the battery runs down. Power your sense amplifier from the micro's power.All shunts (and hall sensors) drift over temperature - they have a tempco you need to factor in. Choose you components with care.In the end a zero drift set up will have a few % error over 25A- 150mA. 10 bits will do.

author: MathCa
author_rank: Contributor
timestamp: March 06, 2023, 10:51:40 pm
content: Quote from: Terry Bites on March 06, 2023, 08:23:30 pmI think I will do exactly that. I ordered some samples to play and learn before going further.This is the best forum 🤝

author: MrAl
author_rank: Super Contributor
timestamp: March 07, 2023, 05:30:20 am
content: Hi,I would shoot for dual range, low and not low. You can get better accuracy that way.Maybe 0 to 1 amp, and 1 amp to 30 amps.You probably don't need great accuracy on the higher range setting. 24.5A vs 25A isn't usually a big deal.

author: Veketti
author_rank: Regular Contributor
timestamp: March 07, 2023, 05:47:36 am
content: Quote from: Zero999 on March 06, 2023, 08:04:29 pmSorry, not trying to hijack the thread but is the OP-amp purpose here to protect the ADC input?

author: Zero999
author_rank: Super Contributor
timestamp: March 07, 2023, 08:29:36 am
content: Quote from: Veketti on March 07, 2023, 05:47:36 amThe TSC214 is not just a general purpose op-amp, but a differential amplifier, specifically designed for current sensing. The idea is to use it to measure the voltage across the current sense resistor, which will be 1mV/1A for a 1mR, scale it to -2.5V to 2.5V and level shift it to 0 to 5V.It is possible to use a plain op-amp and some other components, but it's easier to use a dedicated IC.Here are some application notes.https://www.ti.com/lit/an/sboa347a/sboa347a.pdfhttps://www.renesas.com/sg/en/document/apn/current-sensing-low-voltage-precision-op-ampshttps://www.analog.com/en/app-notes/an-105fa.html

author: paulca
author_rank: Super Contributor
timestamp: March 07, 2023, 09:52:23 am
content: Maybe a little over the top but...https://www.ti.com/lit/ds/symlink/ina3221.pdfAs I am literally working on a similar project I can recommend the above. It comes in pre-made module form from Amazon/Aliexpress.3 channels or current and voltage. Current resolution of 400uA and voltage of 8mV. Max voltage is 30V, either -30V or 30V, not both. The current and voltage channels can be completely independant to the INA3221 or MCU power supply, except one thing. They have to share a ground. This can and does create issues with accuracy depending on what is on the circuit and what the ground is.The current sense resistors can be removed on the module easily and replaced with something more appropriate for 25A, if you still want to go that high. You'll maybe find picking the components (RSense) to measure at 10mA resolution at 100-150mA and still measure 25 amps! The default 0.1R sense resistors on the modules, are good for 163mV VShunt, which gives 1.63 amps max? If you replace them with 0.01R 1% resistors it will measure 16.3A with a 4mA resolution. If you can find/afford 0.001R sense resistors it should measure 163A at a 40mA resolution. However, I expect the noise levels on the shunt resistor will become a concern on 0.01R and make 0.001R impractical.For the higher current sensing consider hall effect sensors rather than shunt resistors.

author: MathCa
author_rank: Contributor
timestamp: March 07, 2023, 04:29:50 pm
content: Quote from: MrAl on March 07, 2023, 05:30:20 amI was also thinking about doing that 👌 In this case, I really don't need reliable data outside 2 small ranges. I could do like this... Simple and effective!" alt="" class="bbc_img" />

author: MathCa
author_rank: Contributor
timestamp: March 07, 2023, 04:51:17 pm
content: Quote from: paulca on March 07, 2023, 09:52:23 amThanks for this suggestion! I will definitely want to make my own PCBs, but in a year or two. At the moment, pre-made modules are much easier for me. I see a lot of lesser known parts that I would love to use but they are so small and hard to solder without the right tools. I was able to use some adapter boards with small ICs but I'm still a beginner 🤭Quote from: paulca on March 07, 2023, 09:52:23 amThis is interesting, I can at least try 👌Quote from: paulca on March 07, 2023, 09:52:23 amAnother good idea, and I can "upgrade" everything anytime while learning more.Thank you!

author: paulca
author_rank: Super Contributor
timestamp: March 07, 2023, 05:05:24 pm
content: On the INA3221 modules. I found out last night I got lucky. The black boards are correct. The purple boards join all the shunt resistors in parallel.These are the working ones.https://www.amazon.co.uk/HiLetgo-INA3221-High-Side-Measurement-Interface/dp/B09KGYY6QW/