Frequent viewers know that I like GreatScott's
channel because he greatly develops people’s interest in electronics. When he showed a
trigger board to keep a USB power bank on for small projects, I thought: Great idea, but this
can be done better! And it is a perfect excuse to tinker with the new ATtiny chips. He used a
50-year-old 555 timer IC, and I want to use a same size and a similar price modern chip because this
chip let’s us put some “artificial” intelligence into the project. During this journey, we
will learn about a very useful chip family, discover some unknown behavior of power banks
as well as a new fake sold on AliExpress.
Grüezi YouTubers. Here is the guy with the Swiss
accent. With a new episode and fresh ideas around sensors and microcontrollers. Remember: If you
subscribe, you will always sit in the first row.
We all know the Attiny13 and Attiny85
chips. They were perfect for small projects, particularly in the SMD version. But they had
an important flaw: The in-circuit programming. The new ATtinies overcame this issue and are
much easier to use. This is why I also want to add them to my toolbox for later projects.
But let's start with the project. Most power banks automatically switch off if you do not
draw enough current. So, they are not usable for most of our projects. GreatScott, amongst
other solutions, used a 555 timer to create short peaks with enough current to keep the
power bank on all the time without consuming too much energy. His device creates a short peak
just before the bank wants to cut off. Because the automatic cutoff time of power banks varies
a lot, he had to use a potentiometer to adjust the duration of the interval.
This solution has two flaws:
1. You have to measure the
behavior of your power banks and
2. Adjust the potentiometer accordingly
As mentioned, I wanted to replace the 555 timer with an MCU that automatically
does all the measurements and adjustments. So it should work with all power banks.
Oh boy, this was harder than I thought!
I started with a simple design similar to
GreatScott’s: An MCU, a FET instead of a transistor, and a 47Ω resistor to create the
100mA peak current. I could now program a fixed timing into the MCU and power the device
from the power bank. This would lead to a design with fewer components than the one with
the 555 timer. But as usual on this channel: We want more! As said before, the chip has to
characterize the behavior of the power bank and then automatically determine the needed
timing. Did I ask for too much? I think: No.
The typical power bank starts to deliver
5V once a device is plugged in. The current drawn by the device can be extremely small,
and still, it switches on. For small loads, my power banks switched off after 20-120 seconds.
This time also seems to depend on the make of the power bank and the load. So my plan was simple:
I power an ATtiny412 with a 3V coin cell that does all the measurements by switching the
FET. These small buggers only consume mA when they run and microamperes when they sleep. And
even less if you reduce the clock frequency.
Why did I choose the 412? The new ATtiny family
offers quite a few chips: From 8 pin to 24-pin packages and from 2kB to 32kB flash. Do not use
chips with a “0” in the middle. They are outdated. The 412 offers the biggest flash in an 8-pin
package and does not cost much more than the 212.
A voltage divider was needed to reduce the
maximal voltage to below 3V. As soon as the Attiny measures 5V at its analog input, it knows
the power bank is on, and it starts to count the time till it switches off. Now, we have the
parameter for the interval. If, in the future, we trigger a bit earlier, we should have an optimum.
But how do we determine the length of the trigger impulse? Easy: After the bank is switched off, we
“stimulate” it by switching the FET and counting how long it takes until it switches on again.
Unfortunately, the banks did not switch on. Why is that? Here, we get support from another feature of
the new Attiny chips: They support Serial.print, our favorite debugging tool. If you connect
the RX of a USB-to-serial adapter to pin 2, you can use Putty or better, MobaXterm to show the
output. Extremely cool! Remember that the memory of these chips is small. So, use only a few short
debugging messages. Or start your project with an Arduino Pro Mini, as I did, and wait till the end
of the video, where I show you a small trick.
I discovered that the voltage of the banks only
reduced to around 3 volts; never switched off, and refused to be triggered again! What would you
do to solve this problem? I introduced a second FET to disconnect the power bank completely.
Because this one has to be a P-channel type, I had to add an N-channel type to switch it with
the 3V of the ATtiny. This worked somehow, but it took long until the banks switched off. Obviously,
their detection circuit is very sensitive. After playing around for quite some time, the sketch
to switch all transistors and measure voltage and time became quite big. Still, it never
worked reliably. After losing a lot of time, I did what I always do when I am stuck: I
switched everything off and went to bed.
The next morning, with a fresh brain, I
changed the assumptions for the project: I no longer wanted to detect the time of the
impulse to switch the banks on. I decided to determine a good value and let it be. Still, I
wanted to measure the switch-off time. Do you see what opportunities this small change in the
specifications offered? The most important one is: Because I no longer want to switch the bank
on, all my measurements can be done with the 5V of the power bank. So, the coin cell was no longer
needed. Very good! And I replaced these two FETs with something much cheaper: The user. This is
similar to modern software, where the users do the testing, and supermarkets, where the shoppers
scan their purchases. You see, I learn fast!
What does this mean? If the user wants to
characterize a new power bank, he or she inserts the trigger device into the USB connector of the
power bank. Then, as we know, 5V is immediately available, and the ATtiny starts to run. It
counts the time till the bank switches off. This is exactly the time we are searching for. If
we create a pulse before this time is over, the power bank should keep on. But do you see the next
problem? Right when we know the result, the ATtiny is switched off, and we lose it. Not what we need!
Again, what would you do to solve this issue?
I use a simple method: After each second, I store
the elapsed time in EEPROM. The last value is exactly what we need! Cool. Now, we only have to
instruct the trigger device when to count and when to run. This can be done with a push button.
If I connect my device to a new power bank, I press the button while I insert it into the USB
connector. Now, it measures till the bank switches off. The result is stored in EEPROM. From now on,
I can insert it without pressing the button, and it starts triggering. Exactly like GreatScott’s
device. Just with fewer parts and more brains. Is this already Artificial intelligence? For
the marketing department, for sure. Because my brain obviously is transferred to this tiny
chip. Or at least parts of it. The EEPROMS are rated at least for 10’000 write cycles, BTW. If
this is insufficient for you, you can write a simple anti-wear function that uses a different
address every time it counts. So, our prototype is finished, and we could create a small PCB.
One thing of caution: The USB-C power banks discovered that I tried to fake them and
switched off after a few minutes. Obviously, they are more sophisticated. So here, we have
to use a standard USB-C trigger board or connect pulldown resistors as shown in my USB-C video.
But wait: I forgot something important: The programming of the ATtiny. The old ATTinies
used ISP for programming. This protocol needs four wires and, therefore, four pins of the 8-pin chip.
If you want to program the chips on your PCB, you are no longer free to connect these pins.
So, we only have two completely free pins left. The others are VCC and ground. Often, I put the
ATtiny on a socket and removed it for programming. Not easy for SMD parts as you can imagine.
The newer ATtinies use UPDI for programming. It only needs one pin. And this pin is also
the reset pin. The rest is freely usable. So, your soldered SMD chip can be programmed.
How cool is that! In addition, you do not need a new programmer. Just use an old Arduino
Nano, add a 10µF cap between RST and GND and a 47kΩ resistor to pin D6. After flashing the Nano
with this code, your UPDI programmer is ready, and you can select it in the Arduino IDE.
Next, you have to copy this line into your preferences tab in the Arduino IDE
and install the megaTinyCore. Now we can program the whole new ATtiny family.
The ATtiny412 only cost a few pennies. So, shipping is the bigger cost. This is
why I ordered a few from AliExpress.
Unfortunately, the programmer refused to
work because the Chinese chips exposed the wrong code. They pretended to be an ATtiny416,
which has 20 pins. Interestingly, they are not complete fakes because I could flash the blink
sketch using the 416 “board definition”. Still, I did not trust them and ordered others from a
more reliable source. Do you know how this very strange behavior is possible? I could understand
a complete fake. But creating a working chip with a wrong signature is over my head.
Anyway, the new chips work like a charm, and I added a few ATtiny1614 with 14 pins for
larger projects. As said, they do not cost much if you can combine them with something else
to get free shipping on Mouser or Digikey.
What did we learn today?
- If something looks simple and straightforward, it often is not
- Hacking devices is hard. Particularly those with quite elaborate chips inside
- Changing the requirements of your project sometimes leads to a much better design. This
is particularly important if somebody says: We did it always like that!
- The new ATtiny family is a big step forward because they can be programmed
using only one pin and support Serial.print without any problems
- We only need an old Arduino Nano as a programmer
- Open source and YouTube is the right way to foster innovation.
Somebody like GreatScott has a good idea, and an old guy like me adds his two cents
Maybe somebody creates a PCB. My version 1 was created when I thought I knew how it
worked. You see, it still contains a battery.
One last thing: You might have asked what
these debug() and debugln() commands are. I use them to switch the debugging messages
on and off. Here you find how I did it.
This was all for today. As always, you find
all the relevant links in the description.
I hope this video was useful or at
least interesting for you. If true, please consider supporting the channel to
secure its future existence. Thank you! Bye