Toggling a button in App Inventor

toggleApp Inventor is an easy piece of software  that starts you off in developing Android apps. If instead of two buttons to switch a state ON or OFF you want to have a button that toggles, the image shows how that is done.
Suppose I have a button called “bThuisToggle” it carries an image called “huis.png”. If I click it I want it to send a byte (85) and the next time I click it I want it to send another byte (117). I also want the image of the button to change into “huis_uit.png”.
The block in the picture shows clearly how to do that

Advertisement

Using FET’s to Switch a load

Earlier I wrote a post on using MOSFET’s with TTL levels and on using MOSFET’s to drive Powerleds. Now I will write a bit more on using MOSFET’s to Switch a load.

The simplest way is the circuit below:

NFET1

It shows a basic circuit with an N-MOSFET that drives a relay. As a relay is an inductive load, the fly-back Diode D1 is necessary to protect the FET.
If we presume the input signal is on a TTL level, the FET needs to be a ‘logic FET’ that switches on these levels.
To avoid ‘floating’ of the input, we make sure that without any input signal the Gate of the FET has a defined low level by adding resistor R2. The value of this resistor is not very critical.
With regard to the gate resistor R1, there is a lot of discussion whether this is necesary or not, but as it limits the current surge when a load is applied I have chosen to add one.
There is also discussion on whether the gate resistor should be placed to the left of R2 instead of the right, but that is not necessary, and not critical. Should it be on the left, R1 and R2 would form a minimal voltage divider and as R2 is solely there to define a logic state and R1 only to protect the gate, they can stay as is.
The value of R1 is also not very critical: at 150 Ohm the current is limited to 5/150= 33mA which is still within the 40mA spec of the Arduino (mind you, this is an extreme case as the Gate resistance is very high). Upon increase of the value of R1, it takes longer to send the FET in saturation, which increases the RDS value, causing the MOSFET to get hotter.
Let’s take the IRL520NSP as example for our calculation: As the input capacity of MOSFET is 440pF (datasheet page 2/11), the time it would take for the FET to reach saturation would be:

t=2.2RC
t=2.2 * 440*10-12 * 150
t=145.200 *10-12
t=145 *10-9
t=145nS

Though the MOSFET, once in saturation is a pretty good switch, there still is a minimal resistance called RdsOn. To make sure the FET can handle the load, we have to calculate the power that will be dissipated in the FET. As a relay as in the example usually doesn’t use that much current, lets presume we want to switch a device that draws 4 Amps. For the logic FET we have chosen- the IRL520NSP- the specs are:
RdsOn=0.22 Ohm @VGS=5V @ Id=6Amp.
The power dissipated in the FET is then P=I²*R=4x4x0.22 =3.52 Watt.
That is well within the specs of the FET.thermalresistance

In order to calculate how hot the FET will be (and to see if we need a heatsink), we need the “Thermal Resistance Junction to Ambient” (RØJA). For our FET that is 40°/Watt, so that means a temperature rise of 3.52*40=140.8 degrees. Thus at an ambient temperature of 20 degrees that will be 161°C. The TjMax (maximum Junction temperature) for this FET is 175°C so that would be just within spec. A decent heatsink would not be a luxury in this case. Mind you though that many modern MOSFETS’s have a gate capacitance that is much lower, in the range of 10-15pF

In the above example the FET was sinking the current. This means that the Vcc was ‘sourcing’ the current to the load, whereas the FET was providing the connection to ground, i.e. ‘sinking’ the current.

Sometimes it may be necessary to ‘source’ the current, for instance if we need the grounds of two digital circuits to be galvanically connected, but we want to be able to switch off one of the circuits. A battery operated Attiny with an Rf transmitter attached might be a practical example: in order to save the battery, the transmitter needs to be switched off when it doesn’t need to be active.

In that case we ‘source’ the current to the ‘+’ of that circuit, that is galvanically connected to earth where it ‘sinks’ the current. A common cathode RGB is another example that would need this.
Sourcing current can be done with a p-FET. A Logic Level P channel MOSFET is e.g. the TPS1110 with an RdsOn of 65mOhm at -4.5Volt at 6 Amp. Another Logic Level MOSFET is e.g. The IRL6402 or the IRLR9343, but the latter is more suitable for audio amplifiers. The IRL6402 also does have a very low RdsOn of 65mOhm at 4.5Volt (and 3.7 Amp)

PFET1

The circuit doesnt differ that much from the ‘sinking’ circuit with the N-FET. It is in fact everything flipped over its horizontal axis (but the direction of polarity for the diode remains). The circuit is OFF when the Gate is the same voltage as the Source (VGS = 0), which means that we have to output 5V (HIGH) from the Arduino to turn the MOSFET off. To turn it on we need -5V at the Gate (relative to the Source) so we need to output 0V (LOW) from the Arduino to turn the MOSFET on.

If we briefly go back to the ‘sinking’ circuit with the N-FET, it is clear that it doesnt really matter what the required Voltage of the Load is: 5V, 12 Volt, 30 Volt: as long as the Mosfet is able to handle it it doesnt make any difference for the arduino (provided that has its own 5 Volt supply).
The sourcing/P-FET circuit however does face a problem if the supply voltage goes up.
Suppose the supply Voltage is 12 Volt. Then our 5Volt TTL level will never be high enough to make VGS equal to 0 (relative to the source, so 12 Volt). It will 0Volt (Arduino pin LOW) or 5 Volt (Arduino pin HIGH), ergo the MOSFET will never be OFF.
The next circuit will solve that:

PFET2

In this circuit we use a simple signal transistor to Switch the VGS between 5 and 0 Volt (relative to the source so 7 Volt or 12 Volt relative to ground). If the Output from the Arduino is HIGH, the transistor will conduct and the voltage on the collector will become LOW, causing the VGS to increase (remember, it increases in relation to the Vcc). If the Output from the Arduino is LOW, the transistor will not conduct, pushing the Collector voltage towards Vcc, or in other words: lower the VGS, thus closing the FET and switching the Load OFF. This also has the advantage that in comparison with the prior circuit, the signal is no longer inverted: Arduino pin HIGH => load ON. Arduino pin LOW => Load OFF. Another ‘advantage’ is that at a higher voltage also non-TTL level MOSFETs can be used.
The calculations on this circuit are as follows:
The base transistor limits the current to

5V-Vbe/1000
=5-0.7/1000
=4.03/1000
=4 mA

As the Arduino pin will have no trouble sourcing that, the BC547 will be in full saturation.The Collector Emittor saturation voltage is 0.2 Volts. Therefore the Vcollector and thus the Vgate will be 0.2 Volts, in other words the VGS will be 11.8 Volt, which is more than enough to send the MOSFET in saturation.
As a double check: The current through the resistor will be 11.8/1000=11.8mA
lets presume the gain of the transistor is around 100 (it depends a bit on the type of BC547 and the current range in which we are working), the Ib will then need to be minimally 118uA, which is sufficiently met by the current of 4mA.

If we want to calculate the power and heat development in the transistor we do that as follows:

P = (VBE * IBE) + (VCE * ICE)
  =0.7*4+0.2*40
  =2.8+8=10.8mW
RØJA = 200 °C/W (from datasheet)
Temperature increase= (10.8*200)/1000=2.16°C

This works well, untill the Supply Voltage goes up to above 12 Volt. That is not some magical number, but it is in general the max VGS the FET’s can take. The TPS1110 for instance has already maximum rating of 7 Volts.

The circuit below gives a solution:PFET3

We calculate the circuit as follows: If the Arduino goed HIGH. there will be 5 Volt on the base of T2. Therefore there will be 5-0.7=4.3 Volt on the emitter. Therefore the current through R2 is:

4.3 / 330 = 13 mA

With an hFE of 100, about 99% of that current will flow through the collector as well and thus through R1. This would normally mean a voltage drop of 13 Volt over R1. Because we have a zenerdiode, much of that current will flow through the zener, when it tries to maintain the voltage drop to 10 Volt over VGS.

The function of the 1 k resistor (R1) is then mainly to pull the gate quickly back up to the supply voltage and thus VGS = 0 if the transistor is off. R2 is there mainly to create a current that can activate the Zener without the zener having to dissipate the full Vcc

It is important that the MOSFET be switched on and off quickly, as it has low power dissipation when fully off (ie. no current flowing) or fully on (when there is a low resistance between gate and drain). At other times the power dissipation will be higher, and the part may get hot.

This won’t be an issue for low-switching speeds (eg. turning garden lights on and off, or a fish feeder) but becomes an issue when doing PWM (pulse-width modulation) where you may be switching on and off many times a second.

The analogWrite function on the Atmega328-based Arduinos runs by default at 490 Hz. Tests indicate that the circuit above will take about 1 µS to switch on and 8 µS to switch off (if using the high-side driver) and 1 µS to switch both on and off if using the low-side driver. (High side means your switch/FET is between the Vcc and the load; low side means the switch/FET is between the load and ground.)

This is one of the reasons for choosing a reasonably low value for R1 in the high-side driver. We rely on R1 to pull the gate of the MOSFET high when the transistor is turned off, and we need that done as quickly as reasonably possible. We could potentially use a lower value resistor for R1 (eg. 470 ohms) to increase the switching speed, but then Q1 (the driver transistor) has to switch higher currents, and we need to watch that this transistor (Q1) does not get too hot.

Another point which can be initially confusing is expecting the Arduino board to provide enough power to run a motor, or lots of LEDs. It won’t, basically. The power supply on the Arduino is rated with enough capacity to drive the chips on the board itself, with a little over for extra chips like clocks, SD cards, etc.

It is not intended to drive a 3 amp electric motor.

In this case you have a separate power supply (maybe a battery, maybe a plug-pack) which is the “+5V” or “+12V” on the circuits above.

However it is important to connect the grounds. That is, connect the ground wire from the battery or plug-pack to the ground wire on the Arduino. That way they have a common “reference voltage”.

Driving an IR LED: constant current source or not

If one uses an IR LED for remote control, it is generally unwise to hang that LED directly (with a series resistor ofcourse) on the Output pin of your Arduino (or other microcontroller). The I/O pin of an Arduino can deliver 20mA, which is generally enough for a regular LED, but IR LED’s usually have a taste for more current as they have to bridge a distance to the receiver (such as a TV).
There are basically two ways to drive an IR LED then: with a transistor (or a FET for that matter) that has the LED plus series resistor in the collector (or drain) line or with a constant current source.

Now ‘constant current source’ in this aspect is a bit odd as the LED will be switched on and off so the current is not constant, but it refers to the current that flows to the LED in ‘open’ state.

Both the regular ttansistor driver  and the constant current source have their pro’s and con’s sp lets discuss both.

IR-leddriverSuppose we want to drive 2 IR LED’s -say the IR204- from a 5 Volt source and we do that with a simple transistor driver as pictured to the right. As we want to drive then with 200mA a simple BC547 isnt enough so we have chosen for the  BC337. Around a collector current of 200mA the hFe is 80 and the Vcc is 0.7V.
That leads to the component values as pictured to the right, but the only value we really need to be concerned about is R1 that has a value of 6.5 Ohm at 0.26 Watt. One could use a 1/4 Watt transistor for that as the LED is not always on.

 

IR stroombronSuppose we want to do the same with a constant current source, as pictured in the  bottom figure to the right. Basically this needs 2 extra components (the 2 diodes) and a resistor in the emitter line rather than in the collector line.
The principle of a constant current source  probably is known: The two diodes keep the base of the transistor at 1.2V. Consequently the Emittor of the transistor is at 1.2-0.7=0.5Volt. Therefore the Current  through the transistor is defined by Ve/R1 and at a value of 2.5 that makes 200mA. The dissipation in the resistor is 0.5*0.2=0.1 Watt.

So what are the advantages of  the constant current source over the  simple transistor driver?
Well, there are a few:

  • The resistor can be of a lower wattage: 100mW vs 260mW. That might not seem such a big issue, but if you are driving a bigger current, that could be an issue.
  • Suppose you want to change the voltage? With the constant current source you just up the voltage and that is it. With the simple transistor driver you will need to re-calculate the series resistor, both in resistance as well in Wattage.
  • Same goes for adding more IR LED’s (as long as the supplied voltage allows it): no need to recalculate the value of R1
    So doesnt the simple transistor driver have any advantages?
  • It lacks two components
  • With the two IRLED’s it can still work with a voltage as low as 3.7 Volt, whereas The Constant Current source needs at least 4.2 Volt (if you use 2 IR LEDs).
  • If you are using 3.3 Volts both circuits can be used if you drop one IR LED. However should you still need 2 IR LED’s you are out of luck with the constant current source as it is  risky to put two LED’s  directly parallel if they are not exactly the same. With the simple transistor driver it is always possible to put two or more different IR LED’s parallel when they each have their own  series resistor.

So, what is best for you, depends on your situation. If you are exactly sure what you are building, you know what your number of LED’s needs to be and what Vcc they will be getting, the simple transistor driver is probably best.
If you just want a more universal module that you can use in various situations, then the constant current source is probably better.

“You forgot to mention the constant current circuit uses less energy”
Now there are  a few fairy tales about the constant current source vs the simple driver. One of them is particularly persistent and it claims that the constant current source ‘uses less energy’ than the transistor driver.
That of course is complete bogus. It is probably based on the perception that  -as shown above-  a lot of energy is dissipated in the series resistor R1 and less in the emittor resistor (0.26 Watt vs 0.1 Watt in our example). That is a complete misunderstanding of the formula P=I*V.
If you just look at the Supply voltage (5 Volt) and the current that flows (200mA), the power usage of both circuits is 1 Watt. Sure, it is true that of that 1 Watt in one case 0.26 Watt is used by the series resistor as opposed to  0.1 Watt in the emittor resistor, but that doesnt change the overall use of 1 Watt  by both circuits.
So there is more power available for the IR LED !”   Nope, we have constructed both circuits  such that the LED’s get 200mA. Over 3 Volts (as they are in Series)  that means that the LED’s dissipate 0.6 Watt in both circuits.
Ah see 0.6 Watt + 0.26 Watt = 0.86 Watt while 0.6 Watt + 0.1 Watt =0.7 Watt, so the constant current source IS using less energy
Nope, as said before  both circuits  use 5*0.2 = 1 Watt. The difference of the 0.26-0.1=0.16 Watt is in the transistor. In the Simple driver the transistor dissipates (1-0.86=) 0.14 Watt  but in the constant current source the transistor dissipates (1-0.7=) 0.3 Watt.
The 0.14 Watt dissipation in the transistor also follows from Vce*Ic=0.7*0.2=0.14mW. Now ofcourse the believers in the fairy tale will say:
“But the same goes for the Vce*Ic in the constant current circuit that also uses 0.14 Watt and not 0.3 Watt

Again wrong and it illustrates the complete misunderstanding of what a constant current source does. It keeps the current constant and it does that by changing the resistance of the Vce pathway. So the Vce of 0.7 Volt is for that transistor to be in full saturation and that is exactly that that transistor is not

 

Determine Number of Days into the year

For a project I needed to read data from EEPROM for every different day of the year. Using the day of the year from 1-365 as an index to read the memory seemed the easiest way. My RTClib library (from Adafruit) sadly didnt know a ‘day of the year’ function So I set out to make or find one. I came across a site where this was undertaken, but sadly that had a flaw that was probably not noticed by the owner because he only used it to determine if a day was even or odd. It gave me a good start though.
It entails altering the RTClib.h and RTClib.cpp files from the Adafruit RTC library.
First take the RTClib.h file and add the line as shown below:
rtclibh
Then open the RTClin.cpp file and add the function as shown below.
rtclibcppThis function cycles through an Array of ‘number of days per month’ stored in Flashram with PROGMEM. It starts by determining the current day and then add the days in the months from january till the current month to it.
Then it determines if february already passed and checks if it is a leapyear. If so it adds an extra day.
To use the function: define, initialize and start the RTC as usual. The call to “now.dayOfYear()” returns the number of days into the year.
e.g. Serial.print(now.dayOfYear();

The function is  m.m.somewhat  akin to the ‘days since 1-1-2000’ in the lib.

This function will work up to 2100 because that year is not a leap year (centuries as arule are not leap years). I could correct for that but then I also would have to correct for the year 2400 as that ís a leap year again (centuries are no leap years… unless the century is a multitude of 400).