I was pointed to a fairly interesting type of christmas light string/curtain that uses only two wires to control a large number of RGB LEDs. Of course I had to investigate it and found that it was quite a clever design.
After outlining an idea how to implement powerline control with an ATtiny10 in the previous log, I did of course have to try it. Especially since a rather nasty hack is involved.
The Hardware
The objective is to create a two terminal device with an LED that receives both power and control information via a single line.
My test setup is shown above. The top is a breakout board with an ATMega328P and USB to serial converter (Sometime also known as "Arduino Nano Clone"). The bottom breadboard contains the target circuit with an ATtiny10, a capacitor, LED and series resistor.
Note that there are only two connections to the lower board: VLED (connected to PB0/D8 of the ATMega328) and GND.
The full circuit of the lower board is shown above. Notice the mistake? There is no connection to the power input pin VCC!
Well, of course this is the "Nasty Hack". The ATtiny10 has an internal diode connecting PB2 to VCC (shown in red). The purpose of this diode is to short ESD pulses to the power rail. It is also the reason why you are not supposed to apply voltages higher than VCC+0.5V to any input pin. Operating the MCU like this can have nasty side effects, and I am pretty certain that Atmel Microchip will not endorse it at all.
When VLED is larger than VCC plus the diode voltage, it will charge the parallel capacitor. When VLED is pulled low, the diode will be in reverse direction and the microcontroller can be supplied from the capacitor.
Apart from abusing PB2 to power the MCU, it will also be used to sense the logic level of VLED. PB1 is used as output to control the LED. Note that the LED is supplied from VLED and not the capacitor and hence will only light up when VLED is high.
The Software
To keep things simple we will limit the functionality to only turning the LED on and off. During normal operation, VLED will be "High", pulling it down for a short time shall toggle the LED.
/*
* Attiny10 Powerline LED
* Created: 30.01.2022 18:49:11
* Author : tim
*/#include<avr/io.h>#include<avr/interrupt.h>intmain(void){
DDRB |= _BV(PB1); // Set PB1 as output. LED is connected to PB1
EICRA |= _BV(ISC00) | _BV(ISC01); // Activate INT0 on rising edge
EIMSK |= _BV(INT0); // Enable INT0 in mask register
sei();
while (1)
{
}
}
ISR(INT0_vect, ISR_NAKED)
{
PINB |=_BV(PB1); // Toggle LED on PB1
reti();
}
The ATtiny10 firmware is shown above. I am using INT0 to detect a rising edge on PB2 (which INT0 is connected to) and call the interrupt handler. The interrupt handler uses the pin toggle function of the ATtiny10 to turn the LED on or off. Note that I did not change the clock setting at all, meaning that the ATtiny10 is clocked at its default 1 Mhz.
This is a simple approach how to emulate the behavior of the LEDs with a microcontroller.
Data is sent by pulling VLED to GND for a few µs. While VLED is at GND, the diode is in revese direction and the remaining charge from the capacitor will power the microcontroller. The capacitor is recharged after VLED is pulled to positive voltage again.
The Sense input on PB0 can directly sense the signal from VLED. In case there are concerns because of V(PB)>VCC, another diode may be inserted. But the voltage differential is only ~0.6V, which should still be acceptable.
The LED is driven by PB2. When VLED is pulled to GND it will turn off. A resistor for current limiting should be added as well.
If you are very adventerous, you could also omit the diode. In that case, the MCU will be powered by the ESD diode on PB0, which should also charge the capacitor via the VCC pin.