PWM Current measurement: INA219 - Page 1
Forum section: electronics / projects

author: obiwanjacobi
author_rank: Super Contributor
timestamp: May 04, 2025, 12:25:10 pm
content: I have been at this for a while: trying to measure current draw with an INA219 on a PWM source.Recently I have started a new hobby: model railroads. Of course I want to automate stuff and the 'issue' I am currently working on is detecting loco's on isolated sections of track (blocks). I power the trains with a 12V switching power supply, going through a TB6612FNG (PWM H-bridge). That (motor) output is is connected to a shunt resistor that the INA219 monitors. I am using standard Aliexpress breakout boards and modules. The PWM signal is generated by a PCA9685. It is all controlled and driven by an Arduino Uno.The DC motor inside the loco has a LC snubber standard.My test track is an oval track split into 4 isolated sections / blocks. Here is a video (https://youtu.be/lceJXCfO7HY) of a proof of concept where two loco's are driving and the faster loco is stopping when the slower loco in front of it has not yet left the block (white arrows) in front of it. Each block has an individual PWM source and H-bridge and INA219.The primary goal that I am trying to achieve is detecting that current is being drawn in a block - that is it - it doesn't have to be an accurate value. But I do want an accurate 'trigger'. (A secondary goal is to detect shorts so I can cut power - but that is not what I am asking now.)The problem I am seeing is that the shunt-voltage values of the INA219 are all over the place, sometimes even read values when no loco is present in that block. When I remove all loco's from the track the false positives stop, so it may have something to do with the EMF? the DC motor is generating? I have added extra caps on both the 5V and 12V rails. A large buffer cap and a small 100nF cap.I am using continues sampling mode of the INA219 and have tried averaging the shunt-voltage (INA219 has modes for that, see page 20) - I think I have tried all settingsWhen the number of averaging samples is too great (like the 128 samples setting) its starting to miss changes to 'zero' (it is never really zero).I am starting to wonder if the INA219 is capable of measuring current on a PWM source or if it's the wrong choice.The code I have is a bit of a mess (lots of code in there that is not currently used), due to all the experimenting but basically the Arduino should send a small 'events' over serial whenever the state of these blocks change (loco's are transferring from one block to the other or stopping).https://github.com/obiwanjacobi/acdc/blob/main/acdc/src/Program.cppPlease ask, if you have questions about the code - I don't expect you to understand it right away.INA219:https://www.ti.com/lit/ds/symlink/ina219.pdfTB6612FNG:https://cdn.sparkfun.com/assets/0/1/b/b/3/TB6612FNG.pdfPCA9685:https://www.nxp.com/docs/en/data-sheet/PCA9685.pdfSo if anyone has any suggestions what I could try or change, it would be most welcome.Thanks.

author: DavidAlfa
author_rank: Super Contributor
timestamp: May 04, 2025, 12:50:49 pm
content: Of course the readings will be crazy, it's an ADC, it reads whatever has at the input.If you're hammering it with PWM (all/nothing) your readings will be the same, you need to filter it out in software.

author: WattsThat
author_rank: Frequent Contributor
timestamp: May 04, 2025, 02:01:32 pm
content: You will have problems trying to detect a zero current situation without somehow synchronizing the PWM output with when the INA219 samples the signal, otherwise, you’ll never be able to see anything other than the average voltage or current. That said, that still requires some synchronization or at least an understanding of the PWM timing.So - do you know what PWM frequency you’re commanding the PCA9865 to output in the setup? This is where you start if you want to set the INA219 number of samples to even have a chance of calculating the average over time.

author: ledtester
author_rank: Super Contributor
timestamp: May 04, 2025, 02:22:28 pm
content: Here is a discussion on adding a 10 Hz LPF on the INA219's input:https://forums.adafruit.com/viewtopic.php?t=130072There's also this discussion of the frequency response due to sampling frequency:https://e2e.ti.com/support/amplifiers-group/amplifiers/f/amplifiers-forum/984153/ina219-filters-and-frequency-response

author: obiwanjacobi
author_rank: Super Contributor
timestamp: May 04, 2025, 04:08:33 pm
content: Yes, I understand that I will get on/off readings, however in practice that does not happen a lot (I have stared at these values a lot!). I have tried to also read the bus-voltage and only take the shunt-voltage when it's above a certain threshold. That does not do very much. Most problematic are the negative readings (EMF?) that throw of the software. Also, it doesn't have to be ms accurate. So I will only report 'no-current' when I have a couple of readings that are off. I think the averaging the INA219 does will still allow this to work.I am not very knowledgeable in signal processing algorithms so I have only tried 'simple' things - like averaging or having a percentage above/below a threshold.It could very well be there is a simple algorithm that fixes this?I do know the PWM frequency (I program the PCA9685 at startup) but I have no time-base in the Arduino (where the code runs) that I can use to sync up taking INA219 readings. In the final design I intent to let the PCA9685 run from an external clock (all PWM for all blocks need to be in-sync), but the Ali module I got does not expose that pin.I have just rigged up an INA219 module that has a RC filter (as shown in page 11 of the datasheet) and will test that later on. Similar to what is being discussed on the adafruit forum, but I have used the 10 ohm resistors (and a 1uF cap) mentioned in the datasheet.

author: ledtester
author_rank: Super Contributor
timestamp: May 04, 2025, 04:44:50 pm
content: What is your PWM frequency?

author: DavidAlfa
author_rank: Super Contributor
timestamp: May 04, 2025, 04:52:42 pm
content: Just discard negative readings... is it that hard?

author: obiwanjacobi
author_rank: Super Contributor
timestamp: May 05, 2025, 01:21:58 pm
content: Quote from: ledtester on May 04, 2025, 04:44:50 pm70HzQuote from: DavidAlfa on May 04, 2025, 04:52:42 pmI am using the averaging mode on the INA219 so they're part of the mix. I did change the code to check for 'opposite' values but I don't think it did much good.(The polarity can be flipped (to let the train run backwards) so negative values is not the correct word)I tested with the INA219 module I modified to add the LPF. It ran fine during the first few tests, then I turned on PWM on that block (no load) and it started to 'detect' changes. While I was investigating that, one of the 10 ohm resistors burned through - I think the INA219 itself also got hot. I have no idea what happened thereI replaced it with a standard INA219 module and it works fine again.(I had cut two tracks and bared the copper trace to add two smd 10 ohm resistors and checked there were no shorts and everything was connected correctly etc.)

author: mtwieg
author_rank: Frequent Contributor
timestamp: May 05, 2025, 07:36:48 pm
content: Quote from: obiwanjacobi on May 05, 2025, 01:21:58 pmWith such a low PWM frequency you are going to want to use a great deal of averaging. Even with max averaging (68ms conversion time) you will probably see a lot of ripple in the measurements.As for the issues with burning the chip or getting strange values, a schematic showing how the shunt resistors connect, and how the INA219 itself is grounded, would help.

author: obiwanjacobi
author_rank: Super Contributor
timestamp: May 07, 2025, 07:15:52 am
content: I seem to remember that I lowered the PWM frequency because, higher frequencies generated a lot of noise when the train was actually running. So much even that it crashed the Arduino from time to time. I think the PCA9685 will only go up to 1800Hz. But I will test again with higher PWM frequency values, because I can't remember the details.The INA219 modules is this onehttps://learn.adafruit.com/adafruit-ina219-current-sensor-breakout/downloadsJust a breakout board with a shunt resistor.The motor/H-bridge output of the TB6612FNG goes to the track (block) where one lead goes through the shunt resistor of the INA219 board.The Arduino is powered by it's USB connection to my laptop and the track is powered with a separate 12V DC supply. All the grounds are connected (on the breadboard). I take the 5V from the Arduino to the breadboard and power all the breakout/modules. Both 5V and 12V rails have buffer caps and 100nF caps.

author: Jeroen3
author_rank: Super Contributor
timestamp: May 07, 2025, 07:37:36 am
content: You have a challenge with the Nyquist rate. In theory you need at least twice the samplerate of the input signal.The drawback here is that you have a square wave input (infinite frequency) and a low frequency samplerate.INA219 doing at most 12 kHz on 9 bit mode, means you could measure "up to 6 kHz" in theory.However, when you have 70 Hz for it should be able, you just need to do some DSP in your software.I would recommend seeing if you hardware supports DMA transfers for I2C, such that you don't need to be polling the I2C in software at several kHz.Do not use averaging in the INA219, because that will make your samplestream discontinuous. The INA219 does not interleave with averaging enabled.This part is made to measure DC power usage inside systems for health monitoring, such as voltages and current in computers over SM-bus. (see applications on front page)You're kind of abusing it for sensing DC motor currents. I'd move the part to the supply side of you H-bridge if you want to stay within the intended purpose.Or get an AMC1200/INA199 or similar and a use the arduino's ADC.

author: PCB.Wiz
author_rank: Super Contributor
timestamp: May 07, 2025, 08:53:37 am
content: Quote from: obiwanjacobi on May 07, 2025, 07:15:52 amYou may like to trade off some power loss of slower edges, for lower RFI and hash.What is the rise time of the average current of the train as it accelerates?A decent multipole filter would be needed to reject PWM and track noise, from the average current drain.

author: obiwanjacobi
author_rank: Super Contributor
timestamp: May 07, 2025, 03:13:32 pm
content: I don't understand half of what is saidBut a couple of things triggered me:Quote from: Jeroen3 on May 07, 2025, 07:37:36 amI don't mind that I am missing a sample here or there (if I interpret this correctly) - my code currently only considers value above a certain threshold.Quote from: Jeroen3 on May 07, 2025, 07:37:36 amI am using an Arduino Uno (atmega328P), so no DMAthe code uses a cooperative tasking system (co-routines) in the main loop, but currently this is about the only task (besides blinking a led to let me know the main loop is still running). So polling it is, which is fine - for now at least.Quote from: Jeroen3 on May 07, 2025, 07:37:36 amSince my initial implementation was also a sort of averaging solution, I though it was a good idea to let the chip do it. What algorithm to use for the raw samples?Quote from: Jeroen3 on May 07, 2025, 07:37:36 amYes, I thought of that too and did try to use it on the supply side of the H-bridge. Downside is that the TB6612FNG has a dual h-bridge, but I thought I would get rid of the PWM undetermineness (is that a word?). It didn't help much, I remember. I can't remember at what PWM frequency I tried this, though...Quote from: PCB.Wiz on May 07, 2025, 08:53:37 amThe current rise-time as the train accelerates? I have no idea. I don't think the system is fast enough to measure that. I am pumping my data over usart/serial which takes a while.I have put a small Cap (to GND) on the PWM input into the H-bridge to smooth out the rising and falling edges. That block now has tons of ghost-readings at very high levels when no train is running, just the PWM is on.Putting it on the h-bridge output seems at least to do no harm and the edges are a little less steep now...Quote from: PCB.Wiz on May 07, 2025, 08:53:37 amI don't have the knowledge to make something like that, but it sounds like something that supresses specific values / value ranges...?

author: Jeroen3
author_rank: Super Contributor
timestamp: May 08, 2025, 07:33:03 am
content: Quote from: obiwanjacobi on May 07, 2025, 03:13:32 pmYou may not care about discontinuous or irregular sampling, but the math does.I would try to create a polling system using the i2c transfer complete interrupt, with the i2c configured at a speed that this consistently produces samples to throw in a simple qnumber based exponential moving average. This is relatively lighteweigth.You will then get a average current value filtered to the given factor of the average.Quote from: obiwanjacobi on May 07, 2025, 03:13:32 pmPerhaps designing a filter in that way goes some beyond the target audience for an arduino, but you can keep it much simpler with the moving average filters i mentioned.Something like this (psuedo):Code:volatile int16_t current;void i2c_transfer_complete_irq(){unsigned int ina219_current = i2c.data;i2c.StartRead(address);// static, so that the value persistent for the next time this routine is calledstatic int16_t ema;int16_t filtered_current = calculate_ema_q15(ina219_current, ema);// copy to public variablefiltered_current = current;// no access to serial in irq!}Ask chatgpt for the content of `calculate_ema_q15`. Or how you do the interrupt for your arduino, I do not know right now.

author: mtwieg
author_rank: Frequent Contributor
timestamp: May 08, 2025, 11:23:49 am
content: Quote from: Jeroen3 on May 07, 2025, 07:37:36 amNo idea what you're referring to here... what is the difference between how the INA219 does filtering/averaging and what the OP could do in the Arduino?QuoteThis might be helpful. If the sense resistor is in series with the output of an H-bridge, that means the INA219's common mode input voltage will swing all the way down near zero volts. The INA219 datasheet gives an Absolute Maximum common mode input voltage range of -0.3V to +26V. It doesn't seem to specify any functional minimum common mode input voltage (very bizarre to not include that). So it's possible that this is the reason you see strange measurements. Depending on how the INA219 is grounded relative to the power circuitry, it might even be experiencing negative common mode voltages.Sensing current at the positive supply of the H-bridge would avoid this issue. But be aware the average current you're measuring wouldn't actually be the average load current. The average supply current will be different. But if you know the H-bridge duty cycle you can calculate the load current.QuoteYeah that would offer flexibility. But I'm wondering if the OP chose an I2C-based chip because they want to have many duplicates of this circuit (the INA219 supports up to 16 slave addresses, while the Arduino has less ADC channels). Same for why they chose to use the PCA9685 to generate PWM instead of the Arduino's own PWM peripherals.

author: Jeroen3
author_rank: Super Contributor
timestamp: May 08, 2025, 12:21:49 pm
content: The INA219 does internal computations if you enable the averaging. It will do n samples for current, x samples for voltage, n samples for current....Where in reality you want, 1 sample for current, 1 sample for voltage, recurring. It doesn't do that.This is crippling for the requested function.QuoteDo people not RTFM?Those INA series, if they say the range is -0.3 to 26 common mode, than they do that. I've used them before.Maybe this problem can be looked at from a different viewpoint. There are also H-bridges with integrated current sensing.Also look, TI made an appnote with details for sensing current in h-bridges:https://www.ti.com/lit/ab/sboa174d/sboa174d.pdf?ts=1746669841095What can also be attempted is to configure the INA219 in "Shunt voltage, continuous" with averaging, the current register may present correct data.The other registers remain empty. But careful, in continuous you do not know when a sample was taken. So viewing it is subject to aliasing due to time offset.

author: obiwanjacobi
author_rank: Super Contributor
timestamp: May 08, 2025, 04:57:55 pm
content: A couple of points for clarification:- I do not use current and power measurement of the INA219. For that you need to program the calibration register, which I do not do. I only use the shunt and bus voltage. I use continuous sampling with averaging (still experimenting with these modes).- I do not need accurate values. I only need to detect if *any* current is being drawn in a block/track. Is the block 'occupied' or not - is the question I want to answer. As a secondary goal I want to detect a short (on the track), but that is not this issue.- It does not have to be fast. Something like 10 values per second (per block) -just guessing here- is more than enough to make decisions on. I estimate that 16 blocks (see next point) should be manageable for the Arduino, perhaps even more.- The idea is to make a PCB that can manage 16 blocks (PCA9685 has 16 PWM outputs) all addressable over I2C. My preliminary schematic for this PCB brought a problem with I2C addressing to light - but we'll fix that later (a KiCad project is also in the repo).- I have also considered measuring the shunt voltage with an opamp and an ADC (separate chip, not the Arduino) and use a I2C io expander to interface with it - as an alternative to using INA-like chips. Have not tried this yet because I guess it'll suffer from the same set of problems.Thanks for the link to the current sensing in H-bridges article.> My earlier claim that adding a cap on the pwm caused ghost readings is false, I also get ghost readings without the cap.

author: mtwieg
author_rank: Frequent Contributor
timestamp: May 08, 2025, 07:31:36 pm
content: Quote from: Jeroen3 on May 08, 2025, 12:21:49 pmI see what you mean, but I think the OP only was interested in measuring current, in which case this isn't a problem?QuoteI've used other INA devices and IME their datasheets are accurate, but in this case it seems to only provide electrical characteristics at a common mode voltage of 12V. Recommended operating conditions also only says 12V typical, nothing given for min and max. The -0.3-26V spec is for absolute maximum ratings, which doesn't mean the part will operate normally across that range. I'm guessing it does, the datasheet just doesn't mention it...QuoteYes, this is what I had in mind.QuoteYeah, seems like the averaged result is not continuously updated, and it doesn't give a clear way of indicating when a new average is available. Not sure if that's an issue for the OP's application.

author: mtwieg
author_rank: Frequent Contributor
timestamp: May 08, 2025, 07:38:05 pm
content: Quote from: obiwanjacobi on May 08, 2025, 04:57:55 pmCurrent measurement and shunt voltage measurement refer to the same thing.If you also want to measure bus voltage, you could probably get away with much less averaging there. Looks like the INA219 allows both measurements to be configured independently, which is nice.QuoteFrom what I understand, it sounds like this approach should meet your requirements. You'll just need to be careful to keep your worst-case measurement delays in mind.

author: obiwanjacobi
author_rank: Super Contributor
timestamp: May 11, 2025, 08:05:19 am
content: Sorry for the late response but I've been busy trouble-shooting my crashing/freezing Arduino... again...It would randomly freeze up. I discovered it was related to reading the INA219 over I2C - when I stopped doing that it stopped crashing. I reworked my TWI code (I don't use any Arduino libraries) -and making sure it doesn't wait forever- before querying the registers again, but that didn't help.I then removed the shunt resistors from the track supply and that fixed it. My program still queries the four INA219s in the main loop (not getting any values of course) -so I2C is working- and my train is driving across the track and the Arduino doesn't hang.So the PWM, together with the EMI (?) of the DC motor of the train cause these INA219's to misbehave -I think- on the I2C side of things...?Problem now is that I don't have any ideas on how to deal with this...Seems to me I need to do something about the DC motor 'emission' of the train (the final track layout will have multiple loco's running at the same time) - that seems to be the root of the problem. And I wouldn't be surprised that when that is fixed, the current reading will be a lot less flaky as well...

author: tooki
author_rank: Super Contributor
timestamp: May 11, 2025, 11:06:18 am
content: Quote from: mtwieg on May 08, 2025, 07:38:05 pmNo, they do not.In the INA219, INA226, etc, there are separate registers for current and shunt voltage. The shunt voltage gives you the raw voltage across the shunt. The current register gives you an actual current value in amps, but as obiwanjacobi said, this requires the calibration register to be populated with the correct value. Similarly, the power register gives you the power in watts, provided the calibration register is populated.

author: SiliconWizard
author_rank: Super Contributor
timestamp: May 11, 2025, 02:19:43 pm
content: Quote from: obiwanjacobi on May 11, 2025, 08:05:19 amOne first common measure is to add a capacitor in parallel with the motor. You can further make the motor wires go through a ferrite ring.But...maybe time to have a look at your "grounding scheme" too.

author: ledtester
author_rank: Super Contributor
timestamp: May 11, 2025, 02:41:46 pm
content: Quote from: obiwanjacobi on May 11, 2025, 08:05:19 amI would try powering the MCU+INA219 from a separate supply - like batteries.In this video at 4:35 the presenter explains that he used a separate power supply for the motors because of glitches appearing on the I2C lines:Functional Test Project: INA219, PWM Motor Control, ESP8266 -- GadgetReboothttps://youtu.be/KmjOLjQqjkE?t=4m35sIn this case he is powering the Arduino from the USB 5V and the motors from batteries - but the main point is that the different supplies are being used.

author: obiwanjacobi
author_rank: Super Contributor
timestamp: May 11, 2025, 04:16:25 pm
content: I use two supplies: The Arduino and the breakout boards are all powered of USB and I have a separate 12V supply that runs the trains.All grounds are connected together, though.I have tried some beads on both track supply wires and a couple of caps (100nF was the largest non-polarized I have, so 200nF in total) which doesn't do much - I think I need larger caps. Could I use two electrolytics back to back?At least the Arduino is not hanging anymore, but I can see on the I2C clock that when the train is running in a block that have a INA219 connected a couple of reads are failing during that time. As soon as it exits the block I can see I2C reading all 4 INA219's. There is definitely something the INA219 does not like about the 'current' (shunt-voltage) it is sensing...I also tried putting a cap over the shunt resistor, that didn't do much either.I have also tried making the output of the H-bridge less steep by putting big caps on the outputs of the H-bridge to ground. The rising edge looked nice and smooth (C-curve) but the falling edge had some nasty ringing (?) that only went away when I removed both caps.

author: mtwieg
author_rank: Frequent Contributor
timestamp: May 12, 2025, 12:18:08 am
content: Quote from: obiwanjacobi on May 11, 2025, 08:05:19 amMCU I2C code can be quite frustrating to deal with. Especially with Arduino with its very limited debug capability and undocumented code. When things freeze, are either SCL or SDA stuck low?QuoteThis is definitely possible.Quote from: obiwanjacobi on May 11, 2025, 04:16:25 pmUnfortunately this description doesn't really say much. Exactly how things are connected together can matter quite a bit when sensitive amplifiers are combined with switching power circuits.It's possible that the INA219 common mode voltage is swinging outside its the absolute maximum range. Without seeing your detailed setup, I can offer a few general tips:1. Connect the GND of each full bridge directly to the GND of the INA219 monitoring it.2. Add an RC filter between the current sense shunt and the INA219. This should filter the common mode part of the shunt voltage, not just the differential part. This means you want capacitors to GND on both IN+ and IN- pins, and resistors between the shunt and IN+ and IN-. Unfortunately the adafruit module doesn't include such a circuit, but you could hack one on by cutting a couple traces. Like 10ohm and 1uF might be sufficient.3. Make sure you have sufficient decoupling caps on the H-brdige. Like a 1uF MLCC and a 100uF electrolytic, both as close to each bridge as possible.4. Adding caps directly on the output of the bridges might help, but it's a good solution. It's just going to cause current spikes elsewhere in the circuit, possible doing more harm than good.5. You haven't explained how the PWM signals are applied to the H-bridge (sign-magnitude, locked antiphase, etc), but changing that might help. For example, you could apply pwm to just one half of the bridge (the side not connected to the shunt resistor), and keep the other side static (either high or low, depending on the sign you want to drive). This would result in the shunt side only seeing large common mode transients when the direction changes, rather than constantly at 70Hz.

author: PCB.Wiz
author_rank: Super Contributor
timestamp: May 12, 2025, 02:10:34 am
content: Quote from: obiwanjacobi on May 08, 2025, 04:57:55 pmQuote from: obiwanjacobi on May 11, 2025, 04:16:25 pmIf you only want a presence detection, you could always use that failure to read 100% as an indication.Think of it as a noise-sensor.

author: Jeroen3
author_rank: Super Contributor
timestamp: May 12, 2025, 06:24:11 am
content: The lockup is probably due to i2c stuck bus scenario. The slightest glitch can cause this. Or even a reset or improper power-on of your master chip.I2C is aretardedlimited protocol that needs many workarounds.https://www.ti.com/lit/an/scpa069/scpa069.pdf

author: obiwanjacobi
author_rank: Super Contributor
timestamp: May 12, 2025, 02:49:01 pm
content: Quote from: mtwieg on May 12, 2025, 12:18:08 amQuote from: Jeroen3 on May 12, 2025, 06:24:11 amI've got my code to work around it (it can timeout and abort). It hasn't been stuck since.I do see that when the train runs on a block/track that an INA219 is measuring, that a couple of I2C transaction fail - but my code recovers from it.Quote from: mtwieg on May 12, 2025, 12:18:08 amHow 'exactly' does it need to be? Schematic or wiring diagram? Get a glimpse of the breadboard in the video on the first post.Quote from: mtwieg on May 12, 2025, 12:18:08 am1) The TB6612FNG is mounted on a breakout board and it's a breadboard setup so: wires, wires, wires...2) I will give this another shot. I have tried to modify an INA219 module with what the datasheet suggested, but that released the magic smoke - totally unsure what happened there.3) I don't have any 1uF MLCC caps... I did put in the 100uF on the 'motor' supply.4) When I did that, I saw a whole bunch of 'ringing' on the falling edge. I don't have a good feeling about that.Quote from: mtwieg on May 12, 2025, 12:18:08 am5) The PCA9685 generates the PWM (currently at 100Hz) - first post contains links to datasheets. All these block-supplies need to be in-sync for when the train transitions between them. I turn on the PWM at count zero and turn it off at some point later depending on the speed I have set. The first 4 PWM outputs are fed into two TB6612FNG (dual H-bridge) ICs that each control two sections of track (blocks) - which are isolated from eachother. The TB6612FNG does not have any options to drive half a bridge...?I currently have 4 INA219 connected to I2C but only one is connected to the track. I observe that when the train hit the block the one INA219 is measuring, that the mayhem begins and I2C communication suffers. When the train is running on the other blocks, I can see on my scope 4 sets of I2C clock pulses for reading the for INA219 ICs - no problems.

author: MarkF
author_rank: Super Contributor
timestamp: May 12, 2025, 04:34:21 pm
content: I have done occupancy detection on my railroad with a simple diode and a comparator. It has been very reliable for me at detecting both PWM (my hardware capable of 10Hz to 400Hz) and DC levels. I use a mix of both (PWM for slow speed response and pure DC for higher speeds) and I've settled for PWM freq of 80Hz. However, I use a power transistor and relay instead of a H-bridge to provide the necessary current.https://www.eevblog.com/forum/chat/model-trains/msg5905910/#msg5905910My overall hardware design is outlined at the beginning of that topic.Update:I should add that I generate the PWM waveform in software from a fixed timer interrupt. I solved the issue with sampling PWM by only performing the detection at the end of frame 0 of the 128 PWM period right after the output is set high. Therefore, I'm always reading a constant DC level. If you're generating the PWM via hardware, staying in sync would be an issue.This is my current speed profile using 50 speed steps:https://www.eevblog.com/forum/chat/model-trains/msg5697617/#msg5697617

author: obiwanjacobi
author_rank: Super Contributor
timestamp: May 18, 2025, 01:24:54 pm
content: I am investigating current sensors that are 'isolated' from the circuit they measure.I've looked at hall-effect current sensors but I feel they are not sensitive enough (not enough swing in the output for a specific delta-A) and would require additional circuitry (opamp) to adjust the output to something usable.I am currently looking into another Ali module that uses a coil where you run the wire through you want to measure the current from. Here's the schematic.I've attached my scope to the output and see that the output voltage dips down to about -2V. I am hesitant to connect this to the ADC input of the Arduino Uno - although I see lots of YT videos where they do just that. C6 removes the DC offset voltage which I think is causing this. When I short C6, I get a voltage at rest of about 2.5V and signal moves above and below that when the train is in the block I am measuring.So question is: can I connect it as-is to my Arduino or is it better to short out C6?I am testing with putting a Cap at the output to smooth out some of the spikes and ringing I see. Thus far I only see it making the edges less fast but not fixing any spikes.I have some larger-value MLCC caps on the way so I have some more choices to test with.

author: Jeroen3
author_rank: Super Contributor
timestamp: May 19, 2025, 05:40:56 am
content: Above circuit only works for AC. (the hints are the three series capacitors)Why aren't you using ACS712 or similar chips. They fit your current range and are super easy to use.