gfxgfx
 
Please login or register.

Login with username, password and session length
logo
 
gfx gfx
gfx
19852 Posts in 1275 Topics by 5182 Members - Latest Member: charbuild March 28, 2024, 11:26:13 AM
*
gfx* Home | Help | Search | Login | Register | gfx
gfx
Breaktru Forum  |  eCigarette Forum  |  Modding  |  Topic: PWM arduino mod - measuring resistance
gfx
gfxgfx
 

Author Topic: PWM arduino mod - measuring resistance  (Read 10897 times)

0 Members and 1 Guest are viewing this topic.

Offline doobedoobedo

  • Super Member
  • *****
  • Joined: Dec 2013
  • Location: UK
  • Posts: 165
  • Karma: +36/-0
  • Gender: Male
PWM arduino mod - measuring resistance
« on: May 27, 2015, 01:59:10 PM »
It's more measuring anything to be honest. I currently have it breadboarded and all is well until I switch on PWM at which point I try to take readings to measure the resistance. Full duty cycle seems fine and I get pretty consistent readings. However as soon as I change the duty cycle everything goes to pot.

I'm reading the battery voltage via a resistor divider (currently 51K and 82K resistors to get from 12.6V to 5V range), the voltage the other side of the coil - same resistor divider values and measuring current with an acs713 hall effect current sensor. I'm driving a single mosfet with a gate driver controlled by a pin on the arduino. The PWM frequency is approximately 31KHz and the sampling frequency of the arduino is 9.6KHz. I get readings all over the place. I'm sampling each voltage ~100 times in a loop and I can get the mean, median and mode of the samples. Mode seems most reliable most of the time but will still throw up crazy values from time to time especially as the duty cycle goes down.

Is it likely to be the breadboard that's introducing all the noise? I'm loath to get PCBs made and wait for them just to find I have the same problem...

Any ideas at all would be most welcome. I'm happy to post code, data or anything else which someone may find useful to diagnose the problem.

Offline CraigHB

  • PV Master
  • *******
  • Joined: Nov 2011
  • Location: Reno, Nevada
  • Posts: 2023
  • Karma: +246/-1
  • Gender: Male
Re: PWM arduino mod - measuring resistance
« Reply #1 on: May 27, 2015, 03:56:41 PM »
To just average samples over a continuous full time stream is going to make it virtually impossible to get stable readings with any accuracy.

One way to handle it is to generate an interrupt in code based on the status of the PWM module that redirects to a sample routine.  Add a delay so the sample occurs at the end of the pulse providing stabilization time and use a hardware filter with a low enough time constant that ADC input voltage is stabilized before the sample is taken.

Otherwise, you can sample at max rate during the latter portion of the pulse and average.  It's trickier to do it this way but it eliminates the need for a hardware filter.  In that case you need code to start the sampling and end the sampling at the correct portion of the pulse.

Alternately you can use a poll method in lieu of an interrupt.  Use an enable-disable flag based on the status of the PWM module or state of the output pin that drives the switch.  It should be possible since max sample rate for the ADC is probably at least 100ksps and PWM frequency should be in the area of 100Hz.  With a typical MCU clock speed that leaves plenty of time for code execution.  However an interrupt method removes dependency on the amount of time it takes for code to execute.

Yet another option is to continuously sample and then set up up a window in code that accepts or rejects samples based on the status of the PWM module or output for the switch.  Like anything in electronics there's probably still more ways to do this task, but those are all the ones I can think of off-hand.

I'm not that familiar with the Atmel MCUs since I typically use the Microchip ones so I can't tell you specifically how to generate interrupts off the PWM module.  It's something you'll have to research in the data sheet or programmers reference.  I haven't actually done a PWM regulated mod so I can't tell you from experience, but a few people on the forum here have built them with all the required monitoring functions.  Possibly someone who's done it can you give you specifics on how they handled measuring things in code.

Offline CraigHB

  • PV Master
  • *******
  • Joined: Nov 2011
  • Location: Reno, Nevada
  • Posts: 2023
  • Karma: +246/-1
  • Gender: Male
Re: PWM arduino mod - measuring resistance
« Reply #2 on: May 27, 2015, 04:22:42 PM »
Had to split this up into multiple posts because ADC trouble is a pretty involved topic. 

Another issue you can run into involves layout and supply power.  ADCs can be a source of a lot of headaches in this regard.

There's a potential for electrical noise to interfere with the quality of your ADC readings.  Since a PWM regulated mod drives large currents from zero to some high level, there's a *lot* of electrical noise.  Electrical noise originates from changing currents.  The faster they change or the greater the magnitude of the change, the greater the noise.

You need to use good circuit design and good circuit board layout to mitigate electrical noise.  Building your circuit on a breadboard is not going to cut it.  You'll see problems you would not see otherwise.  Breadboards are fine for a lot of things, but anything that involves switching large currents or transmitting signals at high frequencies makes them useless.

To make a circuit noise proof you need a solid ground plane and filtered supply power for your MCU and peripheral chips.  You also need a good layout that is not going to induce voltages on sensitive ADC inputs.  This is such an expansive topic I can't get into all the details in a forum post, but it's something you should research and apply the concepts in your design.

Offline doobedoobedo

  • Super Member
  • *****
  • Joined: Dec 2013
  • Location: UK
  • Posts: 165
  • Karma: +36/-0
  • Gender: Male
Re: PWM arduino mod - measuring resistance
« Reply #3 on: May 27, 2015, 05:08:30 PM »
Thanks Craig. I'll lower the PWM frequency to 122Hz. There are registers I can read to check where I am in the PWM cycle (I'm sure I do need to do more reading).

I have a circuit board designed and am keeping the components associated with the PWM as far away from everything else as I can. Is it worth trying to isolate the ground plane around the PWM pin/gate driver/mosfet as much as possible with just a small bridge to the rest of the ground? (I did physics and am thinking wave propagation through a slit :)). The rest of the components only draw about 30mA max.

Thanks, you've given me more to think about. :)

Offline doobedoobedo

  • Super Member
  • *****
  • Joined: Dec 2013
  • Location: UK
  • Posts: 165
  • Karma: +36/-0
  • Gender: Male
Re: PWM arduino mod - measuring resistance
« Reply #4 on: May 27, 2015, 05:49:40 PM »
Thinking more about it, the reason I wanted to measure during a PWM cycle is the 30A limit of the current sensor. A fully charged 3S battery maxes that out at about 0.4 ohms. If I switch to a shunt I can give a short burst at full power without having to worry about going over that limit - problem solved! Although I think I'll need to get my differential amplifiers back out :).

Offline CraigHB

  • PV Master
  • *******
  • Joined: Nov 2011
  • Location: Reno, Nevada
  • Posts: 2023
  • Karma: +246/-1
  • Gender: Male
Re: PWM arduino mod - measuring resistance
« Reply #5 on: May 27, 2015, 11:19:40 PM »
Yes you should isolate the ground plane for high currents.  It's not a complicated thing to do, just put a narrow separation in the ground plane where high currents flow isolating them from the ground plane where your digital circuits are located.  Tie it together at the negative battery connection.  Real easy thing is to lay down a strip of solder mask in the high current plane then solder a bare copper wire along the length of it.  That way you can keep the high current plane narrow and save valuable space for the digital stuff.

One thing that will filter your power supply nicely for the MCU is to use a linear regulator with high value input and output caps, should be the low ESR type like MLCC or aluminum polymer electrolytic.  A value of 10uF will do, but higher is better.  If you have room, the 22uF TDK C3225X5R1C226K250AA is a good one to use.  The caps should be as close as possible to the linear regulator.

Also put a 1uF MLCC cap as close as possible to the power and ground pin pairs on your MCU.  Do the same for any other digital chips you may employ.  That can help quite a bit in noise proofing your circuit.  Use a larger form factor MLCC cap like 0805 (2012 metric) to minimize ESR and the DC bias effect.  If space is an issue a 1uF 0603 (1608 metric) MLCC cap will work well if it's a good one like the TDK C1608X7R1C105K080AC.

Power for PWM is simply duty cycle times peak voltage times peak current.  So if you you use a shunt with a 3 Watt rating, for example, you can put 6W instantaneous through it at 50% duty cycle.  Also, the ratings for shunts are continuous and an e-cig does not power an atomizer continuously.  You should be able to exceed the power rating by double without overheating the shunt (an increase in current of 50%).

Offline doobedoobedo

  • Super Member
  • *****
  • Joined: Dec 2013
  • Location: UK
  • Posts: 165
  • Karma: +36/-0
  • Gender: Male
Re: PWM arduino mod - measuring resistance
« Reply #6 on: May 28, 2015, 11:16:56 AM »
Isolation sounds easy enough to do, cheers :).

For my first iteration of the PCB I'm just going to solder a pro-mini to the PCB which has a 5V regulator on-board. I doubt if it's the best available as the whole thing is just over $3. But saying that there are a couple of improvements over the reference design, so...

I'm quite happy to use 0805 caps. I was looking at both 3W and 2W 500uohm shunts as I figured ratings would be continuous as the larger package shunts were rated for 5x overcurrent for 5 seconds. No overcurrent info on the smaller package size resistors though, which would lead me to think they're not as good maybe? (package size for the ones with overcurrent info 3921/4120 and without 2512).

I've found the documentation on the AVR timers. I'm already using an interrupt to wake it up from power conserving sleep, but plenty of reading and digesting to do still.

Offline Ian444

  • Jr. Member
  • **
  • Joined: Sep 2014
  • Location: QLD, Australia
  • Posts: 23
  • Karma: +4/-0
  • Gender: Male
Re: PWM arduino mod - measuring resistance
« Reply #7 on: May 30, 2015, 06:31:19 AM »
With the Atmel you can use one timer to do the pwm, and set an interrupt on the 2nd timer to start the ADC. The interrupt on the 2nd timer (output compare B) can be set with a value that is shorter than the length of the pulse firing the atty. Or maybe you could get fancy and calculate a value relative to the pwm pulse length. I've been using the former method (on a simple project) and it seems to be working well. If you have a scope you can connect it to an output pin (that say goes high with the 2nd timer interrupt) and observe the timing between the pwm signal and the start of an ADC read.

Regarding power consumption a common trick to lessen power usage is to ground the tail of a voltage divider with one of the micro output pins just when you need to sample the voltage. Other measures to reduce power I've found is to disable the analogue comparator, turn off input pin pullups, set unused pins as outputs, and make it sleep as much as possible. This is on an Atmel 8-pin ATtiny13 but I think many principles would be similar on the Arduino micro. The datasheet for the micro is definitely your best friend. For me, its painfully slow doing a new project as I have to read the datasheet 20 times for it to sink in, but it can be rewarding too.

Another tip is if you are using a breadboard, keep all the high current stuff totally off-board, the mosfet, battery wires, atty wires, everything. Just run some small wires for power, ground, and mosfet gate to the breadboard, and the power and ground wires should attach directly to the battery terminals, not some place "downstream" where there could be voltage drops due to the current.

Offline doobedoobedo

  • Super Member
  • *****
  • Joined: Dec 2013
  • Location: UK
  • Posts: 165
  • Karma: +36/-0
  • Gender: Male
Re: PWM arduino mod - measuring resistance
« Reply #8 on: May 30, 2015, 08:33:21 AM »
I'm thinking I can use the timer overflow interrupt to catch the start of a cycle (still reading and digesting to do though).

I'm already using a pin to power any components which I switch off when it goes to sleep, hadn't thought about doing it ground side though.

Offline doobedoobedo

  • Super Member
  • *****
  • Joined: Dec 2013
  • Location: UK
  • Posts: 165
  • Karma: +36/-0
  • Gender: Male
Re: PWM arduino mod - measuring resistance
« Reply #9 on: May 30, 2015, 05:49:10 PM »
This is more like it!

Using the timer overflow interrupt I can take all the readings I need with the duty cycle at 15 or above, PWM running at 122.5Hz. Even on the breadboard I'm getting very consistent readings (within 1/1024).

Thanks everyone!  :beer-toast:

Offline CraigHB

  • PV Master
  • *******
  • Joined: Nov 2011
  • Location: Reno, Nevada
  • Posts: 2023
  • Karma: +246/-1
  • Gender: Male
Re: PWM arduino mod - measuring resistance
« Reply #10 on: May 30, 2015, 06:04:05 PM »
If you have a scope you can connect it to an output pin (that say goes high with the 2nd timer interrupt) and observe the timing between the pwm signal and the start of an ADC read.

A PWM regulator would be one of those projects where a scope is pretty much a necessity.  In fact just about any project involving an MCU is going to be in need of a scope.  Since frequencies are low for the most part, one of those USB computer scopes can probably do the job (if it's a good one).  Though I would highly recommend a proper DSO.  I have a good Rigol DSO, but I'm tempted to get one with a larger screen, this one specifically;

http://www.testequipmentdepot.com/owon/oscilloscopes/sds-series/sds7102-portable-digital-oscilloscope.htm

If you go down a bit in bandwidth and sample rate you can go cheaper like this one;

http://www.testequipmentdepot.com/owon/sds-series/portable-digital-oscilloscope-sds6062e.htm

Quote
The datasheet for the micro is definitely your best friend. For me, its painfully slow doing a new project as I have to read the datasheet 20 times for it to sink in, but it can be rewarding too.

For sure that's one document that gets a lot of mileage put on it whenever I start a new project.

Quote
Another tip is if you are using a breadboard, keep all the high current stuff totally off-board, the mosfet, battery wires, atty wires, everything.

Yes it's possible to use a breadboard for high current or high frequency stuff if you take the sensitive stuff off the breadboard.  In other words  use the breadboard to connect one or more sub-circuits on home made PCBs.  I do that myself quite a bit.  Sometimes you want to test stuff before you actually spend the money for fabricated PCBs (and the time waiting).

Offline Dejay

  • Jr. Member
  • **
  • Joined: Jun 2015
  • Location: Netherlands
  • Posts: 2
  • Karma: +0/-0
Re: PWM arduino mod - measuring resistance
« Reply #11 on: October 06, 2015, 09:40:32 PM »
Thank for the questions and answers in this thread! I'm learning a lot just by reading here :)

This might be a potentially stupid question: But can you read resistance with a voltage divider WHILE firing? (I figure it should work if you also know the battery voltage to take the voltage drop out of the equation)

Offline doobedoobedo

  • Super Member
  • *****
  • Joined: Dec 2013
  • Location: UK
  • Posts: 165
  • Karma: +36/-0
  • Gender: Male
Re: PWM arduino mod - measuring resistance
« Reply #12 on: October 07, 2015, 04:23:51 AM »
Yes you can. I constantly monitor all sensors when it is, or would be, firing so long as the board is awake.

Breaktru Forum  |  eCigarette Forum  |  Modding  |  Topic: PWM arduino mod - measuring resistance
 

gfxgfx
gfx gfx
Powered by MySQL Powered by PHP Valid XHTML 1.0! Valid CSS!