maanantai 27. kesäkuuta 2016

Basics: FET as switch, part 2


Last time I showed simple example where NFET was used to switch a load at negative (ground) side. Sometimes this won't do, and you'll want to switch something - like, say, external radio that uses lots of current but is needed for just a few seconds every hour or so - on positive supply side. In that case you'll need to use P-channel FET, like below.
 

Here I'm using a P-channel FET (Q1), for example BSH203. Like before, all FETs mentioned here refer to enchantment mode N- and P-channel FETs exclusively.

So what happens here? Without any control (control line floating) R2 pulls gate voltage up to VCC, making gate-source voltage (Vgs) zero and turning FET off. When control pulls gate down, Vgs grows and FET is turned on, allowing current to be passed to controlled chip.

R1 again is there to limit gate current somewhat (why? well, gate forms a small capacitor, and switching FET on or off involves either charging or discharging said capacitor, which may involve momentary high currents if not limited by gate resistor), and R2 is mainly there to make sure that gate is always in controlled state - i.e. high (off) when MCU isn't actively driving control. Again, you'll always want to have a pull-up or pull-down when using FETs to make sure it's always in well-defined state, be it off (like here) or on (with pull-down instead of pull-up in above circuit).

This works nicely when you only have one voltage to your circuit - i.e. when driven VCC is same as MCU's VCC. If voltage you want to drive is higher, however, a bit more complex circuit is needed as R2 (in off state) pulls gate voltage (and thus MCU pin) above MCU's operating voltage, which in worst case may even damage your MCU. So in that case you'll neeed something like this:

(yes, I'm mixing different FET symbols here, sorry about that - I just picked some suitable parts from the library quickly)
Here I'm using a PFET (Q2) to control the voltage to the load, and NFET (Q3) to control the PFET's gate. When control is low or floating the NFET is in OFF state, allowing R12 to pull PFET's gate high, turning it off. When NFET's gate is driven high, NFET turns on, pulling the gate of PFET down, turning in turn it on and allowing current to pass through.

Here also the size of PFET's pull-up starts to matter; since you can't drive the gate of PFET high directly (only via pull-up) the pull-up is absolutely required. Also large value of pull-up makes switcing PFET off slower, limiting maximum frequency you can achieve. In this case a value of 10kohm or so might be better, but my example load here is controlled slowly enough for 1Mohm to easily suffice.

In addition of ratings mentioned in last post you'll need to pay attention to following;
-maximum gate-source voltage of PFET: this must be at least maximum voltage in circuit, in this case 12v, preferably more.
-maximum drain-source voltage of NFET: when NFET is off, this is same as maximum voltage of circuit, 12v.

When working with relatively low voltages, like 12v here, these aren't that critical, but if you need to control higher voltages, past 25 volts or so, you'll find out that many (cheaper) FETs can't handle that high Vgs voltages, so you'll need to get more expensive parts or design circuit to limit maximum stresses. But those are topic for another time.












lauantai 25. kesäkuuta 2016

Lost the BT receiver


This small, thumb-sized module is what I'm talking about.

I was working outside, and since it was pretty warm summer day here there was a lot of "friends" around - mosquitoes, horseflies and I guess several other species of bite-y flying-y things. Funnily enough, when I have the hearing protector on those don't seem to be much of a problem, they become annoying only when I can hear them. They still bite, though, so I have to swat them away every now and then.

One seriously annoying horsefly (I think that is the name in english, those things that look like a fly but are bigger and hungry for a bite of your flesh) was constantly flying around my head, so I tried to swat it away. And suddely I lost the audio. Damn, loose connection.

Except this time the connection was even looser than usual. The damn module was completely gone. I managed to hit it trying to shoo the flies away, with sufficient force to knock it flying, to a direction unknown.

No idea where it went. Damn. Small, thumb-sized black thing isn't exactly the easiest thing to locate in long-ish grass.

I played around with Kali linux some time ago when trying to figure out unrelated bluetooth issue, and remembered that many of the tools I used with it exist on my phone too. Unfortunately I didn't remember exact commands I needed for searching the module so I walked inside to consult the net.

Shortly, armed with newly regained knowledge of arcane spells of blueranger (which doesn't exist on phone), hidtool and l2ping (as suitable emergency replacement for firstly mentioned) I went to look for the module - only to find out that it no longer responds to the phone.

I think that when it lost connection to phone (due to being out of range, when I consulted the net) it waited a while and turned itself off. Damn again, there went my best hope for ever finding it again - at least, in working condition. And now it's pouring rain outside. If it were salvageable before, if I'd happen to it of course, I suspect that it isn't no longer.

Time to invest to a new module - and some velcro tape, to attach it firmly on the side of those Zeklers (as I pretty much use it only with those these days).



sunnuntai 19. kesäkuuta 2016

Basics: FET as a switch


This time I'll talk about electronics basics, using transistors as switches.

Often you will encounter a situation where you want to control another device - be it I/O line, another chip, a high-power LED, a motor or whatever - with a MCU pin. Occasionally this can be done directly, without any external components, but often either the voltage of device driven exceeds the MCU's voltage (say you need to drive 12v device with 3.3v I/O) or current is too high (typical MCU pin can source about 10-30mA) or maybe you want the line to have a bit better protection (short circuit, ESD, component failure...) from failures in this device. In essence, you need a switch.

A mechanical relay might seem very tempting, as they are very easy to understand - they work like controllable mechanical switch. They however are bulky, slow(ish), expensive and power-hungry (that MCU current source limit again), so a transistor (these days a FET) is often the superior choice.

Transistors however require a bit more understanding to get them working properly. Here I concentrate on FETs - more specifically N and P channel enchantment mode FETs as they are common and fairly easy to use in your circuit, and whenever I mention FET or transistor below I mean these types specifically.

I won't go into details of FET operation - there are lots of resources available for that - and instead I give you just a few simple use cases and try to explain the reasons why design was done as it is.

FETs have (usually) three pins; Drain, Source and Gate. Gate is not directly connected to either Drain or Source and is used to control the amount of current passing through the transistor. When using N-channel FETs, current direction (in typical design) is from Drain to Source; in P-channel FET from Source to Drain.

Gate voltage is used to control resistance of the FET. When transistor is off, resistance between drain and source is very high and no current is passed through. When transistor is on, resistance is very small and current can pass through it. The resistance depends on exact gate voltage (to be more specific, gate-source voltage; the voltage difference between gate and source pins), but when used as switch you can mostly ignore this; in circuit transistor is either fully off or fully on.

In case of MOSFETs there typically is parasitic body diode that allows current to pass through the transistor in reverse direction (if, say, in case of NFET Source voltage is higher than Drain voltage). In some cases this may be a problem as it may allow your circuit to be powered in unexpected ways, but for simple cases it can be ignored.

Let's start with fairly common and simple case: toggling a string of LEDs on or off. LEDs need relatively large amount of current and high voltage to drive them; let's assume that LED string you have need +12v supply and has built-in current limiting (i.e. resistors) in them, so you don't have to worry about those details yourself.

This could be accomplished with following circuit: I've drawn LED string as resistor here.
BSS138 isn't greatest N-FET available as its current limit is relatively low, but for demonstration purposes it'll do (I just had it handily available in libraries).

Control signal is connected directly to MCU pin. R1 is series resistor used to limit current to gate when switching transistor on or off. Smallish value - like 1k - usually is acceptable. In some cases it can be omitted but I strongly recommend that you use one in your designs.

If FETs gate is unconnected (when MCU pin is in high impedance/input mode, as it usually is during reset and programming) FET is in essentially random state. For this reason you should always have a pull-down (NFETs) or pull-up (PFET) resistor; R2 in this case. Its value can be quite high; here I used 1M ohm. Don't count on MCU's pull-ups or pull-downs for this; they almost always are enabled with software and thus disabled when MCU is in reset.

Ratio of R2:R1 needs to be fairly large so that they won't act as voltage divider and bring the gate voltage down too much.

So what is happening in this circuit then?

Remember that I said that FETs resistance is controlled with gate-source voltage. Here source is connected to ground (naturally MCU must be connected to same ground here), so MCU pin's voltage is directly FET's gate-source voltage.

This is enchantment mode FET, so if gate voltage is zero, no current is passed through (resistance is high). In that case switch is essentially open/disconnected, no current is passed through LEDs and they're off.

When we bring control line high to say 3.3v, gate-source voltage grows past transistor's threshold voltage and current is allowed through, turning LEDs on.

Quite simple so far, yes?

Now, on FET selection then. When you open a FETs datasheet, there are huge amount of parameters. What of those are important when choosing one for this application?

One very important parameter is found in graphs; Drain current vs Gate to Source voltage (Vgs) . Some (older) FETs require higher Vgs, like 4 volts before they even start to turn on. If your MCU provides just 3.3v out, this obviously is impossible to reach. BSS138 however turns on below 2 volts and at 3.3v it is very close to maximum current already.

FETs may have fairly low maximum drain-source voltage, and if that is exceeded the FET can  be damaged. For this circuit maximum Drain-Source voltage in our circuit here can be assumed to be 12v when FET is off. Actual voltage is a bit smaller due to LEDs voltage drop but it's always safest to assume maximum voltage found on your circuit. So just make sure FETs Drain-Source voltage is larger than your circuits maximum voltage.

Maximum gate-source voltage of a FET can be surprisingly low. In this circuit Gate-Source voltage is (assuming 3.3v MCU) 3.3v, which just about any FET can handle easily.

Drain current for BSS138 (in SOT-23 package) is just 0.22A; like I said, not best transistor there is, but it can drive some LEDs. If you were dealing with high-current LEDs, you'd want a FET with large current capacity.


At the next part; what if you want the switch to be on the positive side of switched circuit?



perjantai 17. kesäkuuta 2016

When google doesn't help


Have you ever had a problem where google couldn't help you?

This question of course implies that in general your google seaches are helpful and provide the answers you seek for, i.e. your google-fu is at least somewhat strong.

I use internet searches very often when working, mostly to look up some details that haven't been burned into my mind (yet) - like checking out usage of some API call, or looking for a function or feature I'm not familiar with, or details on some obscure chip register in case data sheet isn't exactly clear. However it seems that with increasing frequency I fail to find results to what I'm looking for, even after extensive search query rephrasing.

Part of the reason might be that I simply work with stuff that isn't that common. I like working at a very low level, with nothing but a C compiler (and standard libraries) between me and the embedded chip. Libraries provided by chip manufacturers just seem to be nothing but horribly bloated mess. Or sometimes I want to do something that isn't really supported with a chip or libraries.

If your search has ever resulted just five links, four of which are nothing but links to some (completely useless) source (or even header) and one is otherwise useless (tangential at best) you know what I am talking about.

It can be frustrating to find a fragment of information at one place and other fragment elsewhere, with large part still missing - but damn isn't it satisfying when you eventually solve the puzzle and things actually start working! That feeling, it is one of the reasons why I love working with the less common topics and methods.




tiistai 14. kesäkuuta 2016

Removal of headphone jack


Well, this has been sitting in my publishing queue for a while, and then someone wrote pretty much same thing themselves. Figures... But I've been a bit lazy lately, so I'll just publish this anyway now.

Lately there's been a lot of rumors about how manufacturers are looking to remove the good old 3,5mm headphone jack. I think one rumor about Apple was just that, a rumor, although I am absolutely certain that they are very, very seriously thinking about that, but now Intel proposed the same too.

It is true that the physical dimensions of this jack limit the thickness of the device to minimum of about 5mm, or 4mm if you are really pushing it. The thing there however is that the size, the bulk, of the connector allows it to take some abuse. How many 3,5mm connectors have you heard of that were pulled off the PCB? And how many USB connectors? Yeah, I thought so. So they want to move one more function to already fragile USB connector. Bad idea. Very, very bad idea.

But no, manufacturers want to go digital, with optional (and soon-to-be-removed) analog path as stop-gap measure. Hell no, I definitely don't want that!
 
When outside I like to use cheapest possible earbud-type headphones I can find. Lately that has meant about 10€ ones from any local store every six moths or so when previous pair breaks or wears out. Reasons are pretty much same every time; flimsy wires get bent and break at connector, or the earbud itself or the rubber wears out and doesn't stay in anymore. As I pretty much listen to only audiobooks there is no reason to pay for supreme audio quality, and so far more expensive earbuds have been equally short-lived. And no, while I appreciate bigger, over-the-ear headphones at home, I don't want to use them outside.

Now, adding active DAC and amplifier to headphones? I predict that the price of more affordable headphones will jump by (at least) 100%, with my suspicions about cheap DAC/amplifier combination's sound quality. And I expect that certain brands would be ecstatic to strictly enforce "brand name accessories only" policy. No more cheap headphones for you, better cough up 95€ for those simple, plain white ones!

One could claim that you could use an adapter module, but that is simply adding one more fragile thing to the chain. And the connectors already are the weakest link with current headphones - adding two more connectors will certainly will only mean one more thing constantly breaking.

Bluetooth headphones are nice, but they, too, are fairly expensive. Cheapest analog headphone around here is (like I already said) around 10€. Cheapest Bluetooth ones are about 40€. That's some hefty premium to pay every time again when old ones break! Or I could buy some directly from China, but there you'll never know what you get.

The bluetooth receiver I've been using has been nice, and it definitely takes one physical stress out from the phone end (which is very good when moving a lot). But this, like the dedicated BT headphones, have very limited battery life - I'd hate to have to charge them after every single use. My phone can go from four to five days between recharges, so thought of recharging damn headphones every four to five hours isn't exactly tempting.

But hey, since we are talking about dropping ubiquitous 3,5mm plug, how about a bit different analog one, say 1,5mm? That would sacrifice some of the connector sturdiness, and cause compatibility issues (yay for another adapter until you get new headphones), but at least it would be thinner, analog and still cheap.

But, alas, I fear that we the people are at the mercy of the corporations. They have spoken, and they want to sell us more fragile and more expensive things. So enjoy your current analog headphones now - while you still can...



torstai 9. kesäkuuta 2016

Travelling for work


I remember reading an opinion piece in a local newspaper when I was in my teens, saying something to the tune of following (the whole text was way longer);

"So because they get to travel for work, traveling should be taxable benefit."

I remember chuckling a bit for that, although I didn't have any experience about that kind of traveling (aside seeing my dad occasionally traveling around the Finland and world).


Since then I've traveled a lot. My longest air travel (from leaving hotel, to stepping out from a plane at Oulu) was 36 hours (with two plane changes), second longest around 24 (one change).  And then there's been so many shorter flights I've lost count. And of course numerous train and car travels.


And one thing I've learned is that traveling sucks, big time. Being there (whereever that is) can be great (vacation), okay-ish (with full day's work, assuming you have energy left for sightseeing afterwards) or just plain chore (work from dawn till dusk). But the traveling - moving from point A to distant point B - sucks, almost with no exceptions.

I don't remember even having talked with my dad about travels when I was younger, but apparently I did, or possibly somehow was able to read his moods when leaving or coming back, possibly somewhat subconsciously, as that is just about the only reason I can think why I'd have though about that opinion piece like I did: "This person clearly never has traveled for work. Let's see how he feels after few trips.."

Because really, if work travel were considered a taxable benefit, absolutely no sane person would ever consider of going anywhere as schedule even now is (almost) always too tight (think of red-eye flight there, important meeting and another red-eye back) and often there's no personal time (or energy) left to see what's around there.

Of course, these days I have a bit more liberty to set up my trips to have some personal time, too. But even with that it's still a chore that needs to be done, time away from my family.

But still, there are things that make it all worth the pain and trouble of enduring (air) travel.


Zell am See, Austria, June 2008. Not a work trip, but nevertheless.






perjantai 3. kesäkuuta 2016

Inadequate tools


Ever get the feeling that your tools might not be up for the job?



Yeah... I might need a bit bigger saw here...