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

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:
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).