Fast square wave with arduino and mosfet - Page 1
Forum section: electronics / beginners

author: ManlishPotato
author_rank: Regular Contributor
timestamp: September 19, 2019, 07:41:13 pm
content: Hey y'allI'd like to make a tank circuit with a coil and capacitor in parallel. To power it i need a really frequent square wave. 1 mhz would be nice but maybe about a 500-600 mhz minimum. I was thinking of using a arduino directly interfacing the ports to produce the wave, using it to control a mosfet to power the circuit.Yet the signal pretty much stays the same no matter how much i increase the voltage. I cant't check exactly want the value was right now , but i thing it hade about 1-2v voltage drop across the mosfet which seems like a lot, and like i said i cant get the voltage top much higher than 4.5-4.9v. It had peaks of about 5.2 i think.Is this the wrong approach? How could you accomplish this for cheap?Thanks in advance//Benjihttps://imgur.com/a/JjfIlp2

author: MarkF
author_rank: Super Contributor
timestamp: September 19, 2019, 08:23:05 pm
content: The Arduino pin voltage is not high enough to turn on the MOSFET if you have the load on the low side as you have it.You want your tank circuit on the high side. Even so, you will need a logic-level MOSFET in order to fully turn it on.

author: ManlishPotato
author_rank: Regular Contributor
timestamp: September 19, 2019, 08:30:05 pm
content: Isn't FQP30N06L logic level? Also, with the voltages i measured i had no load connected, like in schematic, i had only the oscilloscope connected.

author: ManlishPotato
author_rank: Regular Contributor
timestamp: September 19, 2019, 08:32:50 pm
content: BTW, why do you use i resistor in series to the gate of the mosfet, isn't mosfets voltage driven unlike bjt's?

author: MarkF
author_rank: Super Contributor
timestamp: September 19, 2019, 08:47:53 pm
content: - I had not looked up your MOSFET. A quick look appears like it is.- In your circuit R1 is the load. Which when the MOSFET is turned on the voltage across R1 rises and will turn off the MOSFET unless you raise the gate voltage to compensate. (The load is on the low side.)- The series limits the gate current. A MOSFET gate has a capacitance which you charge when you apply a voltage. Also, you can have oscillations without the resistor.

author: ManlishPotato
author_rank: Regular Contributor
timestamp: September 19, 2019, 09:04:24 pm
content: Wow i didn't think of that! That makes sense, as i increase the voltage the more back flow i get. I'll have to try that tomorrow and post the result. Thanks for a great response!

author: MarkF
author_rank: Super Contributor
timestamp: September 19, 2019, 09:15:48 pm
content: Start with a series resistor of 100 Ω.I expect 50 to 100 ohm will work best.

author: djacobow
author_rank: Super Contributor
timestamp: September 20, 2019, 01:11:57 am
content: I think with a 16 MHz Arduino, your highest possible toggle speed is 2.67 MHz.cli();while (1) {PORTD |= 0x8;PORTD &= ~0x8;}Looks like that's 6 instructions.

author: MarkF
author_rank: Super Contributor
timestamp: September 20, 2019, 01:54:47 am
content: A quick Google search yielded:Entry #18 in this topichttps://forum.arduino.cc/index.php?topic=4324.0If you know the state of the other pins on the port, then you could do something like:while (1) {PORTD = 0b00001000;PORTD = 0b00000000;}for a 4Mhz 25% duty cycle signal (1 tick ON, 3 ticks OFF) orwhile (1) {PORTD = 0b00001000;asm("nop");asm("nop");PORTD = 0b00000000;}for a 2.6666Mhz 50% duty cycle (3 ticks ON, 3 ticks OFF)

author: ledtester
author_rank: Super Contributor
timestamp: September 20, 2019, 03:26:29 am
content: You can toggle an atmega GPIO with a single instruction by writing a 1 to the corresponding PIN bit:https://www.avrfreaks.net/forum/toggle-state-output-pin

author: ledtester
author_rank: Super Contributor
timestamp: September 20, 2019, 04:09:59 am
content: Quote from: ManlishPotato on September 19, 2019, 07:41:13 pmWhat are the values of your capacitor and inductor?And what exactly do you want to accomplish?To get a tank circuit to ring you don't need to continually excite it. Once excited it will ring on its own at its resonant frequency. Also, you don't have to directly couple it to your signal source. You can excite the tank by just wrapping a wire carrying your square wave around the inductor a few times. Have a look at this page in the section "Measuring LC tank circuits":http://www.giangrandi.ch/electronics/ringdownq/ringdownq.shtmlJust a fast rising edge is enough to transfer energy into the tank, so your signal source doesn't have to pulse at a fast frequency.

author: Mechatrommer
author_rank: Super Contributor
timestamp: September 20, 2019, 04:27:33 am
content: Quote from: djacobow on September 20, 2019, 01:11:57 am4 instructions minimum. 2 ops to toggle pin on and off, 2 ops for goto. hence 16MHz / 4 ops = 4MHz maximum, but only in asm...

author: ledtester
author_rank: Super Contributor
timestamp: September 20, 2019, 04:54:28 am
content: You could always unroll the loop and on a 16 Mhz atmega328 achieve 8 Mhz for about 2 milliseconds at a time with only a 100ns glitch between runs.On a atmega2560 the 8 Mhz pulse train would last for around 16 ms.

author: T3sl4co1l
author_rank: Super Contributor
timestamp: September 20, 2019, 07:30:24 am
content: Millihertz or megahertz? It matters, rather a lot.You can still connect an Arduino to something that can make 600MHz, but it's going to be a frequency synthesizer chip, something fit for purpose -- nothing the Arduino has direct participation in, only setting registers. It's also not going to drive as much power as one of those transistors can, and you need special RF transistors to amplify it if you need to.Tim

author: mikerj
author_rank: Super Contributor
timestamp: September 20, 2019, 07:48:38 am
content: Maybe I missed something, but why is the OP trying to toggle a port pin as fast as possible through bit bashing rather than using a PWM output?

author: radiolistener
author_rank: Super Contributor
timestamp: September 20, 2019, 10:20:08 am
content: Quote from: T3sl4co1l on September 20, 2019, 07:30:24 amHe talking about 500-600 milliHertz (0.5 - 0.6 Hz)

author: MarkF
author_rank: Super Contributor
timestamp: September 20, 2019, 04:40:58 pm
content: I believe you have all missed the boat here.If you read the words around his frequency numbers"To power it i need a really frequent square wave. 1 mhz would be nice but maybe about a 500-600 mhz minimum.",I believe he miss typed and would like to drive his circuit with a 1 MHz square wave and would settle for a 500-600 KHz frequency.Really frequent does not indicate milliHertz.And 500-600 mhz is not less than 1 mhz.

author: Mechatrommer
author_rank: Super Contributor
timestamp: September 20, 2019, 05:13:26 pm
content: frequent is a subjective term, almost useless in engineering term. one can say he bought a DSO every day (11uHz), but damn thats "frequent"

author: MarkF
author_rank: Super Contributor
timestamp: September 20, 2019, 05:18:42 pm
content: Quote from: Mechatrommer on September 20, 2019, 05:13:26 pmA little "common sense" goes a long way!

author: fourfathom
author_rank: Super Contributor
timestamp: September 20, 2019, 05:45:20 pm
content: Assuming we are talking about a square wave in the 0.5 to 1.0 MHz region, let's look at the LC tank circuit. I suppose the purpose is to get a clean, low-spurious output (jitter, harmonics, etc), signal. This will look somewhat like a sinewave at the filter output. Will this be used directly, or will it be sent to a logic stage for squaring-up? This affects the design of the tank circuit. In fact, if the tank load impedance is relatively low, for maximum efficiency and minimal power dissipation in the MOSFET driver, a simple LC tank may not be the best approach. When using a square wave drive you usually want an inductive input impedance to allow the MOSFET to spend as little time as possible in the linear region. Look at "Class E" amplifier networks, or perhaps a simple "Tee" filter. These details will depend on the spectral purity and the power levels desired.As for the Arduino loop, how many cycles do the bit-test and manipulation operations require? Something like this should give you a 50% duty-cycle:while(1){digitalWrite(OUTPIN, !digitalRead(OUTPIN);}

author: ManlishPotato
author_rank: Regular Contributor
timestamp: September 20, 2019, 06:50:40 pm
content: Sorry for the radio silence!To answer you questions:1. I indeed ment 1MHz and kHz500-kHz600 minimum (was writing late)2. I don't really need help with the coding bit, but thanks for the suggestions! (Although i believe you can achieve a 16MHz squarewave locked at 50% with a normal atmega328)Unfortunately i didn't really have much time to try it out today (so the way i actually wired it might be wrong) but i'll include a picture of the dso (with a low frequency of about 490Hz) and a schematic. I think i have somewhat of an idea whats wrong, but i'd rather not say because it could extremely stupid haha. I'll try to answer some of the other questions you had individually.PS. super impressed by the amount and quality of replies!

author: ManlishPotato
author_rank: Regular Contributor
timestamp: September 20, 2019, 06:53:51 pm
content: Accidentally used a 150ohm resistor, will a have to try a smaller value next time!Quote

author: ManlishPotato
author_rank: Regular Contributor
timestamp: September 20, 2019, 07:01:55 pm
content: QuoteSorry! Can't check what the value of the cap was right now, and I don't know the inductance of the coil, that was the start of the project, to observe the resonant frequency, to calculate the inductance. And that other stuff you said was pretty interesting, I'll have to try that out if i can't get this to work.

author: ManlishPotato
author_rank: Regular Contributor
timestamp: September 20, 2019, 07:06:51 pm
content: Quote from: fourfathom on September 20, 2019, 05:45:20 pmWow I'll have to read through your reply a couple of times to understand it. But really, i'm not looking for efficiency and exactness, i just want to get in the ballpark of the right resonant frequency, to calculate the inductance of the coil.

author: fourfathom
author_rank: Super Contributor
timestamp: September 20, 2019, 07:19:57 pm
content: Quote from: ManlishPotato on September 20, 2019, 07:06:51 pmIn that case most of what I said is pretty useless. But you may still want to only lightly couple the FET to the tank circuit, otherwise the drain capacitance will effect the resonant frequency.

author: ManlishPotato
author_rank: Regular Contributor
timestamp: September 20, 2019, 07:21:48 pm
content: Quote from: fourfathom on September 20, 2019, 07:19:57 pmOk! What do you mean by "lightly couple the FET"?

author: ledtester
author_rank: Super Contributor
timestamp: September 20, 2019, 07:28:55 pm
content: Quote from: ManlishPotato on September 20, 2019, 07:01:55 pmJust excite the tank with a sharp impulse and observe the frequency of the oscillations.Here's a circuit which will excite a tank at its resonant frequency:Then just measure the frequency and from knowing the cap value you can get the inductance.Discussion of how it works:https://electronics.stackexchange.com/questions/282451/how-is-this-oscillator-circuit-supposed-to-work

author: MarkF
author_rank: Super Contributor
timestamp: September 20, 2019, 07:56:16 pm
content: I made some annotations on your last circuit and have a few thoughts:- I assume you are powering the circuit by +6V through R1.- Remove R2 as it is not needed.- IF YOU CONTINUE TO CONNECT YOUR SCOPE AS INDICATED, YOU WILL NOT HAVE IT LONG.Or you will short out your circuit. Actually, I don't see how you managed so far unless you are powering the circuit from a battery.- You want to connect the scope GND clip to your circuit GND. Then probe across the MOSFET as shown.

author: MarkF
author_rank: Super Contributor
timestamp: September 20, 2019, 08:11:52 pm
content: A quick refresher:

author: ManlishPotato
author_rank: Regular Contributor
timestamp: September 20, 2019, 08:38:02 pm
content: That's a good pointer, but the power supply and arduino that i'm using are actually mains earth isolated, or floating

author: ManlishPotato
author_rank: Regular Contributor
timestamp: September 20, 2019, 08:41:18 pm
content: QuoteOh and this is true, forgot mark it

author: fourfathom
author_rank: Super Contributor
timestamp: September 21, 2019, 12:12:01 am
content: Quote from: ManlishPotato on September 20, 2019, 07:21:48 pmRather than connect the FET directly to the LC tank, use (perhaps) a small capacitor between the drain and the tank -- much smaller than the tank capacitor. You would need a resistor from drain to V+ since the tank inductor is no longer providing DC current to the FET. You do this to reduce the de-tuning effect of the FET drain capacitance, and to reduce the heavy damping of the FET during the "on" state.What are the values you intend for the tank inductor and capacitor? The resonant frequency? How will you be measuring the ringing tank signal frequency? What kind of accuracy are you trying to get? All these factors matter.

author: ManlishPotato
author_rank: Regular Contributor
timestamp: September 21, 2019, 12:46:38 pm
content: QuoteCan't check the values right now since i'm not in the office. I'll post an update on Monday once i get a chance to check it out. But i can say that the resonant frequency is or more or less unknown and that's the purpose of the experiment, to use the resonant frequency to calculate the inductance. I'm not really looking for accuracy, only to get in the ballpark of the right value.Oh and i'm using a digital oscilloscope to view the experiment.

author: ledtester
author_rank: Super Contributor
timestamp: September 21, 2019, 01:05:00 pm
content: The default circuit for the Falstad simulator shows how to excite a tank with just a switch:https://www.falstad.com/circuit/The 10R - 1H - 15 uF side is your tank, the 10R representing the internal resistance of the tank.The 100R is there to limit the current through the inductor when you hold down the switch.

author: ManlishPotato
author_rank: Regular Contributor
timestamp: September 21, 2019, 02:23:42 pm
content: QuoteThat's pretty cool!I guess what basically i'm suggesting is to replace the button operated switch with a mosfet, and turn it off and on at just the right time to achieve stable oscillation.

author: fourfathom
author_rank: Super Contributor
timestamp: September 21, 2019, 04:11:02 pm
content: Quote from: ManlishPotato on September 21, 2019, 02:23:42 pmThat "stable oscillation" part is going to be quite tricky. Driving the LC tank with a mosfet, 50% duty-cycle, at anywhere the natural resonant frequency of the tank will just show you the frequency of the driving squarewave. Even lightly-coupled to the tank as I have mentioned, the frequency will be forced by the driving signal.Your best bet may be to set the driving frequency to be much lower than the tank resonant frequency (say 10x or 100x lower), and with your scope observe the ringing that follows the "off" portion of the driving waveform. You should initially see a high voltage transient ringing at the tank resonant frequency that decays over time. The amplitude and duration depends on mainly the "Q" of the inductor and the speed of the mosfet cutoff. You will still want to compensate for the mosfet drain capacitance and scope probe capacitance. You will want a current-limiting resistor in the drain circuit, and do be careful that the transient voltage doesn't exceed the mosfet specs.

author: ledtester
author_rank: Super Contributor
timestamp: September 21, 2019, 05:58:58 pm
content: Quote from: ManlishPotato on September 21, 2019, 02:23:42 pmIf your goal is just to get a stable trace on your DSO so that you can measure the period, as fourfathom said, you only need to pulse the tank once in a while -- like once a second or perhaps once every 1/10th of a second.If your goal is to produce a continuous signal at the resonant frequency of the tank then I would recommend the LM311 circuit I referred to above. It naturally finds the right time to inject a new impulse. This is important as to not affect the frequency of the oscillation.You have the same situation when pushing someone on a swing set... if you push them at the bottom of the swing trajectory you will make them go higher without affecting the period and frequency of their motion. If you push them at any other point you will change the amount of time it takes to complete the next swing.

author: alsetalokin4017
author_rank: Super Contributor
timestamp: September 22, 2019, 02:43:01 am
content: Screenshots below show a tank circuit ringing in response to a square pulse from a mosfet, and a tank circuit excited by "scratching" the terminals of a 9v battery to produce a spiky pulse. The frequency of the inductive ringdown is easily measured in each case, and it's a good exercise in scoposcopy, especially using the single shot mode to capture the "scratch" ringdown. This frequency, along with the known capacitance or inductance, can then be used in your favourite tank circuit calculator to determine the unknown value.http://www.1728.org/resfreq.htmThis video may also be of interest to the OP:

author: JustMeHere
author_rank: Frequent Contributor
timestamp: September 22, 2019, 05:35:05 pm
content: Quote from: ManlishPotato on September 19, 2019, 08:32:50 pmThis is to stop the mosfet pin from drawing/sinking too much current when it changes state. It looks like a small cap when it does this. So it behaves as a straight short for a (very) brief time.There should also be a high value pull down between this resistor (100k to 1M) and the pin. This will stop the pin from floating during programming and chip start up.

author: ManlishPotato
author_rank: Regular Contributor
timestamp: September 25, 2019, 01:45:53 pm
content: Update! 25-09-2019Hey! I have finally had som time to work on the project!I was able to excite the tank by using a push button like in the schematic suggested by ledtesterThat gave me a stable ish frequency that when converted comes reasonably close to the actual value. The values were 0.15 mH and 10mH and the cap was a 2.2nF ceramic capacitor. I have tried to use a mosfet in place of the P Button, but that gave me an inaccurate frequency. I tried to "lightly" couple the fet like fourfathom suggested, but i had no luck with this, could you suggest which values of coupling capacitor and resistor from drain to V+ should be used?I included a schematic with the mosfet and the produced waveform after the push button is released.Would it be possible to get close to the actual resonant frequency, or should i use something like a bjt or igbt instead?Lots of thanks!//Benji

author: fourfathom
author_rank: Super Contributor
timestamp: September 25, 2019, 07:40:40 pm
content: Here's a very simple circuit and simulation waveform. I'm using a 2n3904 bipolar transistor and a 10K resistor for the LC tank stimulus coupling. Actually, with the inductor and capacitor values in your LC tank, the output capacitance of the transistor (either bipolar or mosfet) is fairly inconsequential, but the 10K resistor does serve to limit the transistor current and reduce the voltage peaks on the ringing tank circuit. If I drop this resistor to 100 Ohms (for example), when the transistor switches off the collector voltage spikes up to 140V -- well past the collector breakdown voltage -- and then swings down negative and forward-biases the base-collector junction. Neither of these is a good thing, and show how you need to watch the voltage spikes when dealing with an inductive load.I have a 1KHz square wave driving the transistor, but you could go with a much narrower "on" pulse if you like. You could replace the bipolar transistor with a mosfet if you like.You can also insert a small diode (1n4148 or similar, cathode towards the collector) between the transistor collector and the series resistor. This will block the reverse voltage and avoid collector-base-diode clamping. Doing this lets you substitute a 1K resistor for the 10K to increase the stimulus current and give you a bigger tank waveform voltage to measure.The tank voltages and duration of ringing will somewhat depend on the inductor series resistance. I assumed a resistance of 10 Ohms in my simulations. Lower resistance will give you a slightly bigger ringing voltage and a much longer ringing duration. Looking at the damping on your waveform, I would guess that the inductor resistance is closer to 100 Ohm.

author: ManlishPotato
author_rank: Regular Contributor
timestamp: September 26, 2019, 01:27:42 pm
content: Ok i've tried the schematic you sent me, it gave a nice oscillation, but the frequency was wrong! It had a frequency of about 4.90kHz, whereas it should be closer to 33.93 kHz. Any ideas what the reason of this could be?I was using a BC337 npn bjt transistor and a 1n5255b zener diode (which i hope is a good stand-in to the diode you were using since i didn't hava any on hand).The voltage was still +7v and the cap and coil values are the same. The base resistor was the same you were using, 1k.The images are respectively using the following resistors:10k1k560220

author: fourfathom
author_rank: Super Contributor
timestamp: September 26, 2019, 04:25:09 pm
content: Quote from: ManlishPotato on September 26, 2019, 01:27:42 pmAre you sure about the component values? I agree, your 10mH / 2.2nF tank should resonate around 33.9 KHz.The frequency of the ringing tank circuit will only be affected by the values of L and C, and any stray reactance in the circuit. The zener you used has a reasonable low capacitance when reverse-biased, and even if you removed it from the circuit the tank ringing frequency will be virtually unchanged. Your transistor is a fine choice for this circuit (as long as the ringing voltage doesn't exceed the Vce breakdown voltage, which it doesn't). The transistor output capacitance is about 15pF, which is trivial here given the 2.2nF tank capacitor.I notice in your previous post you also show a 4.9 KHz tank oscillation frequency. I suspect that your inductor is not really 10mH, or possibly the capacitor is not 2.2nF (or both).Another interesting thing is that your ringing tank voltage is much smaller than I get in my simulation (with a 1K series resistor I get 20V P-P but you get about 3.7V P-P). Can you measure the DC resistance of your inductor? In my simulation I have the inductor resistance at 100 Ohms, which is pretty high for a 10mH part.Finally, in your previous schematics you show "DSO", which I assume is your digital storage oscilloscope connected directly across the tank circuit. Is this a differential measurement? The top of the tank is at +7V so I hope your scope ground isn't connected there. The fact that the tank voltage goes negative when the transistor switches off tells me that the +7V rail is your ground reference. I don't see how this will affect the resonant frequency, but it might be simpler to measure the tank signal relative to DC ground. There will of course be a +7V offset.

author: alsetalokin4017
author_rank: Super Contributor
timestamp: September 26, 2019, 10:58:43 pm
content: Here's where we would like to see photos of the components.Sure enough the scope trace, measured by the horizontal graticule markers, agrees with the "numbers in boxes" 4.9 kHz displayed by the scope (although there is some error there due to the way the Rigol measures frequency when the screen isn't completely filled with the waveform). And sure enough your favorite tank calculator says, given the stated tank component values, that the ring frequency should be about 33.9 kHz. And as we have seen the simulation also agrees. And we believe that the schematic of the probing connection, while "unorthodox" should still yield the correct resonant frequency.So please let's have a nice look at the components in question so we can verify their values, as that seems to be the only explanation for the discrepancy.

author: fourfathom
author_rank: Super Contributor
timestamp: September 26, 2019, 11:10:15 pm
content: Quote from: fourfathom on September 26, 2019, 04:25:09 pmWhen I wrote the above comment I got my reference points turned upside down. It looks like your 'scope "ground" reference is on the driven side of the tank circuit, and the probe point is on the +7V rail. If this is a true high-impedance differential measurement this shouldn't matter. If somehow your actual scope ground is connected to the driven side of the tank, then this could dramatically affect the oscillation amplitude and frequency.And yes, some photos of the components might give us a better idea of what's going on.

author: ManlishPotato
author_rank: Regular Contributor
timestamp: September 27, 2019, 03:33:36 pm
content: Aaaaand apparently i'm an idiot!Yes i had the oscilloscope hooked up in parallel to the tank circuit, exactly like in the schematic with normal 10x probe. I'm pretty new to oscilloscope's, so i'm still kinda learning where to place the probes for accurate readings. I know how to do it safely, i'm just not familiar to all the intricacies of the practice just yet. The circuit is virtually the same, this time i was using a 560ohm resistor. I included some pictures of the waveform down below, one with 10mH inductor, and one with 0.15mH. Using a resonant freq calculator gives us reasonable values of 11.105mH and 0.1332mH.In these i had the probe ground connected to the lowest ground in the circuit and the other end connected to negative side of tank circuit.I also included some pictures of the circuit of anyone is still curiousThe conversation might go on, but i would like to thank fourfathom and anyone who helped me find a solution!//Benji

author: fourfathom
author_rank: Super Contributor
timestamp: September 27, 2019, 03:57:39 pm
content: Quote from: ManlishPotato on September 27, 2019, 03:33:36 pmIt's working!!! Great news!To be honest, your probing "technique" is a problem that that most of us old-timers would't have been considering when troubleshooting this circuit. You must be using a floating (non-grounded) power supply, or (shudder) not have your 'scope grounded, because otherwise you would have short-circuited the power supply through the inductor. Usually we clip the scope probe ground to "circuit ground". If we want to measure a signal relative to some other voltage (directly across the tank circuit in your case) we will use two probes and subtract the signals.Could you measure the inductor resistance for me? (might as well measure both of them.) I'm still curious about the amplitude and "damping" differences between your circuit and my simulation. It's probably due to resistance.And thank you for sticking with this and reporting back. I'm here to a large extent for the "puzzles". My wife likes crosswords, and I like circuits!

author: ManlishPotato
author_rank: Regular Contributor
timestamp: October 07, 2019, 01:44:20 pm
content: For anyone still interested, here is the final schematic i used to achieve my goal!I also included a picture of the final soldered project

author: ledtester
author_rank: Super Contributor
timestamp: October 07, 2019, 07:04:50 pm
content: Using the ring-down method:http://www.giangrandi.ch/electronics/ringdownq/ringdownq.shtmlthe Q of your tank is around 19.