Connect with us

Current limiting using an Arduino microcontroller

Discussion in 'Microcontrollers, Programming and IoT' started by Michael Matrix, Jan 13, 2016.

Scroll to continue with content
  1. Michael Matrix

    Michael Matrix

    5
    3
    Jan 13, 2016
    I am trying to use an Arduino to limit the amount of current that can flow through an 18V circuit. Since the arduino uses 5V logic I can't use it directly in the 18V circuit (right?). I have been told that I need to use a logic-level MOSFET for this but I can't seem to find one that will work with very small currents (.5mA - 2mA). I may not understand correctly how the MOSFET should be used but I believe that it would act as a voltage controlled variable resistor? I want to control it using a PWM pin on the arduino.

    I can limit current an analog fashion using an LM334z adjustable current source and a 100ohm variable resistor, but I want to be able to change the current level programmatically. I cannot find a digipot that can work with 18V and has a 100ohm or less range.

    Can anyone suggest a MOSFET that would work, or tell me how else this can be done with readily available parts? I would deeply appreciate it!


    Background: I am building a DIY programmable TDCS device which must limit current to a maximum of 2mA but it requires an input of at least 12V to work as intended. I use 18V as my original analog devices used two 9V in series although I now use liPo batteries with boost converters. I have built four analog TDCS devices and I now want to make a much more capable, and compact, digital version. My analog devices use an LM334 and little LED ammeter panels to show the current level.

    I want my digital version to use an Arduino Nano with a tiny OLED I2C display and an I2C current sensor (an INA219). It should be able to show the current and a timer on screen, be able to shut off the current when a program ends and have digitally adjustable current levels, if I can figure that part out. :(
     
  2. Colin Mitchell

    Colin Mitchell

    1,417
    312
    Aug 31, 2014
    And you want someone to design this for you for $2.50.
     
  3. Michael Matrix

    Michael Matrix

    5
    3
    Jan 13, 2016
    No, I am not looking for someone to design it for $2.50, I was hoping that someone would know of the correct part I should use, or tell me that I have the wrong approach. I have been trying to figure out how to control current digitally for a few weeks but have not been able to on my own.

    Is this is an inappropriate question for this forum?
     
  4. Harald Kapp

    Harald Kapp Moderator Moderator

    10,090
    2,157
    Nov 17, 2011
    A MOSFET alone will not give you regulation without additional feedback.
    How about this circuit? Use the Arduino's analog output instead of the DAC connection.

    No, definitely not. You'll get to know Colin once you stay with us for a while. He doesn't mean to offend you :rolleyes:
     
  5. Michael Matrix

    Michael Matrix

    5
    3
    Jan 13, 2016
    Thanks for that circuit suggestion, but doesn't that part use a resistance to set the current level? If so it is very similar to the LM334z that I have been using but has the same problem; I can't set the resistance digitally. I have tried several digital potentiometers but none of them can source 18V.

    EDIT: Clarification

    Just to Clarify, I want to use voltage to set the current limit. This is so that it can be done using PWM from an Arduino.
     
    Last edited: Jan 13, 2016
  6. Harald Kapp

    Harald Kapp Moderator Moderator

    10,090
    2,157
    Nov 17, 2011
    Generallly: Yes.
    The appllcation "DAC Controlled Current Source" uses a DAC's output voltage instead. Unfortunately the info in the datasheet is admittedly scarce to non-existent. You can try to contact LTC to get more information. Or have a look at the other solutions offered on the right side of the web page.
     
  7. hevans1944

    hevans1944 Hop - AC8NS

    4,500
    2,095
    Jun 21, 2012
    @Michael Matrix please revisit the circuit that @Harald Kapp referenced. It uses a voltage-output digital-to-analog converter (DAC) to control a LT3092 three-terminal regulator connected in constant-current mode with a 0 to 1 V control signal from the DAC. One electrode (anode) of the TDCS device is connected to +18 V and the other electrode (cathode) is connected to the IN terminal of the regulator. The current that flows through the 10 Ω resistor, connected between OUT and the 18 V power supply return (common or ground), is equal to the current that flows into the IN terminal. The voltage drop across this resistor is the DAC voltage divided by that resistance, 100 mA for DAC output of 1 V. The minimum current of 0.5 mA is the "leakage current"required to operate the LT3092. This is probably uncomfortably high, being one fourth of the maximum current of 2 mA you desire. Clearly, another approach is required. See below.

    Just to clarify, what is your level of electronics expertise and experience? Also, do you have any medical qualifications for experimental therapies on humans not currently sanctioned or approved by the FDA? Do you have a technical support team?

    If you use a MOSFET to control current, it is best not to use it in a linear mode as a variable resistance. Your idea of using PWM from an Arduino is a practical way to control the current but probably not fast enough for closed-loop current control. An analog PWM circuit controlled by an analog voltage with analog feedback from a current-sensing resistor in the source lead of the MOSFET is simple and fast responding. The Arduino would need only provide the analog control signal from a voltage-output DAC, similar to the manner @Harald Kapp suggested.

    The MOSFET behaves as a simple, but very high speed, switch that is either open or closed. When closed, the closed-circuit resistance is very low and does not depend (much) on the current through the MOSFET. I would suggest you begin your investigation with a 2N7000 MOSFET and purchase a few dozen to play with. They are small and inexpensive. You will want your PWM frequency to be high enough (several kilohertz) to easily filter down to essentially DC for the electrodes. If you choose to experiment with pulsed DC, that can be easily incorporated into the PWM circuit by having an outer software loop that turns the pulses off and on independently of the PWM current control.

    If you are using the INA219 to measure stimulus voltage and current, you will need to run the PWM at a high frequency and filter the output current to obtain DC you can apply to the electrodes and measure reliably and accurately. However, the INA219 interfaced with I2C to the Arduino is probably too slow to incorporate into the feedback loop the Arduino will use to control the PWM and set the electrode current. You should look into that, but I suspect that you will have to measure the PWM current pulses "in real time" without filtering, using a separate current-sensing resistor and an analog circuit to generate pulses whose width is controlled by a DAC output from the Arduino.

    In other words, you would replace the LT3092 in @Harald Kapp 's circuit with a MOSFET and filter the high frequency PWM pulses going to the electrodes. This would basically be an analog PWM circuit controlled by a digital value sent to a voltage-output DAC in the Arduino. The response time should be considerably faster than what can be obtained from the INA219 sending data to the Arduino to implement the closed-loop current control. However, since you apparently have all the hardware you need, you could try an Arduino software feed-back loop using current-sense data from the INA219 first and fall back to an analog PWM control loop if that doesn't work for you.

    Sounds like an interesting project. Be careful not to fry your brain. Moowah hah hah hah!:eek:
     

    Attached Files:

    Last edited: Jan 14, 2016
  8. Michael Matrix

    Michael Matrix

    5
    3
    Jan 13, 2016
    Thank you for that detailed write up @hevans1944 ! I really appreciate that level of detail. And thank you @Harald Kapp for your continued help.

    I don't follow you on why the speed of a current regulating circuit such as the LT3092 would be a hindrance, or why a base of 0.5mA would be an issue as that is the minimum therapeutic level that is recommended. My analog, LM334 based devices have this same limit, they will not go below 0.5Mah no matter what resistance I place between the ADJUST and output. Does the switching speed of a MOSFET have to do with how quickly it can be adjusted? Is PWM not direct current? I didn't realize that. When a MOSFET is describe as being "Logic Level" doesn't that mean that PWM can be used with it at voltages below 5V?

    I may be misspeaking when I describe what I want to build as a "digital" current control. I assumed I would have to use PWM, which is analog but is controlled programmatically. So I mean digital in the sense that I want to set a current level, by any means, using a microcontroller and firmware.


    To answer your questions, I have a basic knowledge of electronics having been trained for it in the military (USAF), but my practical circuit design experience is limited and I have really only begun a serious effort in the past 6 months. I am a software engineer by trade but was seduced by single board computers (Raspberry Pi) into the world of DIY electronics and microcontrollers. So somewhere between absolute neophyte and talented amateur? I love creating things in the physical world that interact with my code. My only claim to fame so far is the custom design and fabrication of a device that beeps once at regular intervals to annoy people. See the attached images. As for safety my analog designs use a current limiting diode to keep to allow a maximum of 2.5mA. I should probably put two in series in case one shorts? My interest in TDCS is for the treatment of depression, which I understand the FDA has decided does not require regulation. I am very concerned with safety and have been told that the CRD is the best and safest method of ensuring that dangerous current levels are not accidentally passed through my brain.


    BTW, if you ever build an analog TDCS device use a wire-wound 100 Ohm potentiometer with multiple turns, single turn pots don't seem to have the "resolution" for lack of a better word to regulate tenths of a mA.

    Thank you again for your reply.

    -Mike


    1612.jpg IMG_20160113_143544_01.jpg
     
    hevans1944 and Gryd3 like this.
  9. Harald Kapp

    Harald Kapp Moderator Moderator

    10,090
    2,157
    Nov 17, 2011
    Dedfinitely not. It's a square wave with variable duty cycle to adjust the mean value. You neeed to loow-pass filter the PWM signal to get a DC signal.

    Adjusted is the wrong term. Turned on or turned off would be correct. With regard of the mean value of a PWM signal the swithcing speeed of the MOSFET in typical applications is irrelevant. The settling time of the mean value of the signal is almost entirely determined by the low pass filter.

    LOgic level means that the MOSFET can be turned on and off with typical logic level voltages (5VV or less) in contrast to "standard" MOSFETs (lacking a better word) which often need gate-source voltages of 10V or more to be turned on.
    Note that with PWM the MOSFET is operated as a switch, not as a linear element, therefore the term "adjust" is not applicable here.


    Is there a specific reason you need to adust the current on the high side (18 V rail) of your circuit?
    Would it be possible to insert the controlled currrent source on the low side (0V)? This may make matters much easier.
    Does you output signal need to be referenced to ground anyway? Or may it be floating?

    There are so many current source circuits, did you try to search for "voltage controlled current source"?
     
    hevans1944 likes this.
  10. hevans1944

    hevans1944 Hop - AC8NS

    4,500
    2,095
    Jun 21, 2012
    I said nothing about the speed of the LT3092. This is a strictly analog constant-current source operating from a fixed DC supply. Speed of response is usually not an issue. It was my impression that you were unsatisfied with its minimum current output of 0.5 mA and wanted to use PWM with a MOSFET to limit the current to a range of 0 to 2.5 mA. So, if you can live with the higher minimum current limit, @Harald Kapp's circuit (which is shown in the application notes on the LT3092 datasheet from Linear Technology) should do what you want. No MOSFET needed.

    It has to do with how fast the MOSFET can be turned on and off, which is also a function of the external circuitry. The switching speed limits the minimum pulse width (other than no pulse width, i.e., no pulses at all) you can achieve with a PWM and therefore the maximum switching frequency. As @Harald Kapp said in his response #9 above, PWM is never DC.

    In fact, PWM refers to a method (pulse width) of modulation, a means to add information to an otherwise steady-state signal which can be just about anything that carries energy from one place to another place: radio frequencies, light, sound, vibrations on a string, whatever. You turn the steady-state signal on and off, thereby adding information to it. What happens after that depends on what happens at the receiving end, or in this case the brain of the subject receiving therapy.

    If you perform PWM on a DC source, the percentage of time the source is allowed to be on determines its average value if the amplitude of the DC source remains constant. Thus, if it is on half the time then this is equivalent to half the voltage if averaged over a sufficient number of on/off cycles. The nice thing about PWM is it allows you to go to zero output without too much fuss: just turn the pulses off. Between "off all the time" and "on all the time" there is an infinite range of variation, but the smallest increment from zero will depend on how fast you can turn the pulse on and then off and how often this occurs.

    So, you really have two variables you can control: pulse repetition frequency (PRF) and pulse width. If the PRF is held constant and pulse width is varied, then you have a common pulse-width modulation or PWM control. To get the output back to a steady-state, but lower, DC output, you just past the PWM pulses through a low-pass filter. This can be as simple as a series resistor and a shunt capacitor. Depends on how "DC-like" you want the final result to be.

    So-called "logic level" MOSFETs are controlled on or off by applying "logic levels" zero or one between their gate and source leads. Of course logic level varies from one logic family to another, but I think it is customary to mean TTL logic levels or higher will switch the MOSFET from fully on to fully off. It has nothing to do with how much voltage you can apply between the drain and the gate or the drain and the source. Those are separate specifications having not much at all to do with the gate sensitivity. Best advice I can give is RTFI (read the fine instructions) and the datasheet on each device you contemplate using. Come back here with questions if it isn't clear what a particular spec means to you.

    I like to think of MOSFETs as a voltage-controlled resistors. Conduction occurs via majority carriers created in a channel formed by an electrical field produced by the insulated gate located over the silicon between the source and drain. There is no PN junction anywhere in sight to slow things down or add a power-dissipating voltage drop, so MOSFET drain-to-source voltage can be anything from zero to the rated maximum for the device you are using. The width of the conduction channel is varied by voltage applied to the gate, but except when the gate voltage is changing, there is no DC current drawn by the gate. There is a PN junction between the conduction channel and the silicon substrate body that is normally reverse-biased, an artifact of the manufacturing process. Do some research to find the conditions under which this diode becomes forward-biased and what happens if that occurs. The Wikipedia article is a pretty good start for understanding MOSFETs.

    I now think you should forget the complicated PWM approach and just go with @Harald Kapp's original suggestion.


    What the heck is the Evildoer "harmless prank" device? Is this an electronic version of the ancient mechanical wind-up hand-buzzer novelty from early in the previous century, as usually sold mail-order by the Johnson Smith Company along with itching powder, x-ray specs, and "throw your voice" devices? Except the 21st Century Evildoer product now actually delivers a small electrical shock and doesn't require winding... just a coin cell? Huh. Progress.
     
    Last edited: Jan 15, 2016
  11. Michael Matrix

    Michael Matrix

    5
    3
    Jan 13, 2016
    Thank you @Harald Kapp and @hevans1944, i am profoundly grateful for you taking the time to answer my questions, I realize now that I know just enough to be dangerous. :( Thanks for setting me straight on PWM.

    I like the idea of the LT3092 and I don't have any problem with the 0.5ma base current, but I don't understand how I would control it without using PWM? Isn't that the only way an Arduino can set a voltage on an analog pin? My other issue with that circuit is that it only seems to come in as a surface mount chip and I am unable to work at that level. :(

    Does that mean that I should be able to control a MOSFET to act as a varistor? When used in that way can they provide really low resistance, say 100 Ohm or less? If so then I could use one to "adjust" (TI's term for the set pin) an LM334, which is what I have been trying to accomplish since I have that circuit, other than the variable resistor, working well already.

    I apologize for my near total lack of understanding of the principles involved here, I thought I knew something about electronics but it seems my depth of knowledge is on the shallow end. Do you gentleman have an suggestions on a good book for getting up to speed with semiconductors? I have plenty of basic level books and I can find books that you need to be an Electrical Engineer to follow, but very little in between.

    No, an Evildoer is not a "joy buzzer" or anything like that; it doesn't shock anyone. They are my version of the Think Geek annoy-a-tron. That link is to their current version which does a lot more than mine, but basically it is just a small device that beeps or plays a recorded sound at some interval. The idea is that the source of the sound is very hard to locate and drives people crazy with frustration trying to find it. My Evildoers only beep and they have a fixed delay of about 3 minutes but it is the same idea. I put a rare-earth magnet on mine and place them under office chairs or above drop ceilings, it is just a prank and I typically give the device to the "victim" after a few hours of them looking. Most people want to prank someone else with it as soon as they know what it is. That was my first experience taking my own design all the way to custom PCBs. They aren't much but I am proud of them. :)
     
    hevans1944 likes this.
  12. hevans1944

    hevans1944 Hop - AC8NS

    4,500
    2,095
    Jun 21, 2012
    Wow! Serendipity strikes again. I spent years standing too close to the stage listening to rock music, and years after that blasting it into headphones. As a result I have suffered some hearing loss in both ears. Not enough to make me plunk down big bucks for hearing aids, but enough to almost completely destroy my previously excellent ability to localize sounds.

    This was demonstrated most recently after I cooked some rib-eye steaks under the broiler in my gas oven. There was a pan under the steaks to catch the fat drippings of course. After removing the steaks I thought nothing more about the fat, but it almost immediately ignited from the draft created by opening the oven door. So, I quickly closed the door and the fire immediately went out. Not soon enough to prevent the built-in oven smake detector going off and shutting down the oven. None of the controls worked until after I pulled the plug (for the electronics) and re-inserted it in the wall socket.

    But that wasn't the end of it. There were enough combustion products released to activate a smoke alarm somewhere in the house. I spent several minutes trying to locate the smoke alarm without success. The pitch is high enough that constructive and destructive sound interference occurs with the apparent source moving depending on which way my head was turned and where I was standing in the room.

    Eventually I gave up and opened the window in the room housing the smoke detector. The fresh air draft immediately silenced the alarm, so I gave up looking for it, closed the window, and went on to eat my steak dinner. I still don't know where that darned smoke alarm is. What I do know is it has another feature that is even more annoying. When the 9V battery becomes nearly exhausted, another circuit kicks in to make the Evilalarm issue a single beep about once every minute or so. Sounds about like a cricket. Almost impossible to locate by the sound. Who woulda thunk someone would turn this into an annoying toy!? But unlike a real smoke alarm, small enough to hide under a desk or chair with the aid of a small neodymium rare-earth magnet. Yikes!

    I've never actually tried to use a MOSFET as a variable resistor. I have this "gut" feeling that the transfer function of gate-to-source voltage versus channel resistance would not be all that easy to control. High-current MOSFETs can have very low "on" resistances, on the order of a tenth of an ohm, so they don't dissipate much power when fully on. The off resistance is very high because the conduction channel is pinched off. So you go from what is essentially an open-circuit (off) to essentially a short-circuit (on) with only a few volts change in gate-to-source voltage. Good luck making that behave like a linear variable resistor.

    Negative feedback can change all this by making the gate voltage a function of the source current. Also, some PWM integrated circuits do negative feedback, sampling the MOSFET source current through a low-valued resistor and using those samples to control the pulse-width. This seems to be a popular way to drive stepper motors, using a higher than steady-state rated voltage and controlling the average current through low-resistance motor coils. That allows you have your torque without dissipating a lot of power. If you were to apply the elevated voltage to the coils directly it would burn them out. PWM allows the motor coils to be excited with high-frequency pulses occurring much faster than the actual multi-phase (usually quadrature) step pulses. That, in turn, allows the integrated circuit to micro-step the motor. See for example this datasheet for the Texas Instruments DRV8711 stepper motor controller. I plan to use this puppy to remotely control the tuning capacitor mounted on my magnetic loop amateur radio antenna. Of course I could use a geared DC motor (as many amateurs do), but where's the fun in that?

    On controlling and using the LT3092: There are at least three ways to apply the 0 to 1 V control voltage. (1) Use a PWM signal from an Arduino output and scale and filter it with an RC network to provide 0 to 1 V DC. (2) Design an N-bit R-2R ladder network controlled by N-bits of digital output from the Arduino. There are R-2R ladder network components available for this. (3) Add an external Arduino "shield" containing a DAC, such as this Maxim MAX5214 which uses a SPI interface, so it is compatible with and can be daisy-chained with your LCD display. I would personally opt for the PWM approach since all you need are two resistors (one of which is variable) for scaling and a capacitor for DC filtering. You might even be able to get by with a single potentiometer and a capacitor.

    As for working with "itsy bitsy teeny weeny" microscopic integrated circuits... learn to work with them! Surface-mount is the 21st Century whereas through-hole mount is 20th Century technology.

    Yeah, I know, you need a lot of expensive tools... NOT! You need a tiny 1mm conical soldering iron tip for your 30 watt soldering pencil. Put that Weller 200 watt soldering gun back on the shelf. Get some 60/40 solder paste in a syringe with a hypodermic needle; a good 4 to 10 diopter magnifying head-set; some nice tweezers, a roll of very fine 60/40 solder; some solder flux in a syringe with a hypodermic needle; a cup-cake pan to keep all the tiny parts separated; some fine-braid solder-wick; and a somewhat steady hand (but not as steady as you might think), Except for the steady hand, all this might set you back fifty bucks. Oh, and you need a quiet place to work undisturbed without interruptions... a place you can walk away from and come back to and find nothing has been touched.

    Most surface-mount components can be re-flow soldered from solder previously placed on the board pads, either by the board manufacturer or by you. Once you have one pin, or two corner pins, tacked down it's a cinch to re-flow solder the remaining pins. For very fine pitches that are found on larger integrated circuits, you just wipe the solder across the row of pins with your soldering iron tip and let the surface tension of the molten solder do the rest. Sometimes you have to go back with some fine solder-wick to remove solder bridges, but that's not a big deal either. Helps to practice on junk boards before moving on to something serious. Rip open any modern works-until-it-doesn't, throw-away because it costs more to repair than to buy a new one, POS electronics for practice. If you shop Wally World you can probably afford to buy new junk to rip open and practice on.

    To place parts on a board, just apply a little liquid flux to the pad and stick the part into the flux. Re-heat the device terminal with your soldering iron tip to melt the solder already on the pad and allow it to re-flow up to the device terminal. Voila! Your first connection! The LT3092 comes in an SO-223 package that is pretty easy to solder because the output "pins" are connected together to a single tab on one side. The input and set pins are placed on opposite ends of the other side with another out connection in between. Easy-peasy to surface mount with just a little practice.

    I avoided SMD components until last year, when i took the plunge with some PIC10F200 type microprocessors. These also happen to be available in 8-pin DIP versions, but I didn't know that when I purchased them. So, I also ordered some 6-pin SO-23 to 8-pin DIP circuit boards that allowed me to re-flow solder the PIC to the circuit board, solder pin-terminals to the 8-pin DIP connections, and plug the whole thing into a bread-board as if it were a through-hole component. These "adapter boards" are readily available for prototyping, and you could even use them on a printed circuit board (PCB) with through-holes if you are willing to give up the size advantage afforded by the SMD package. However, once you get your feet wet, you will never want to go back to through-hole construction again. Well, almost never. Some parts need big wire connections.

    Hop
     
    Last edited: Jan 16, 2016
    Arouse1973 likes this.
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-