© 2022 EasyEDA Some rights reserved
1.Easy to use and quick to get started
2.The process supports design scales of 300 devices or 1000 pads
3.Supports simple circuit simulation
4.For students, teachers, creators
1.Brand new interactions and interfaces
2.Smooth support for design sizes of over 30,000 devices or 100,000 pads
3.More rigorous design constraints, more standardized processes
4.For enterprises, more professional users
Std EditionATtiny85 NeoCandle DIP
Profile:Candle Simulation using Neopixels, IR Remote Receiver and Ambient Light Sensor.
License: CC-BY-SA 3.0
NeoCandle is a simple tea light candle simulation based on ATtiny25/45/85 using 5mm NeoPixels (WS2812), LDR light sensor (GL5528) and IR remote receiver (TSOP4838). It can be controlled by an IR remote control with two buttons and has a timer mode for automatic shutdown after a set time as well as an LDR mode in which the candle is automatically switched on and off depending on the intensity of the ambient light.
The simulation code is based on the great work of Mark Sherman. With most LED candles, the random flickering is rather silly and lacks the spooky element of candlelight: shadows cast by the candle move because the flame is in motion. With NeoCandle, four NeoPixels are arranged in a square, the intensity of the four LEDs forms a "center" of light balance that can be moved around smoothly.
This 'physics-based' candlelight has the following properties:
The control of NeoPixels with 8-bit microcontrollers is usually done with software bit-banging. However, this is particularly difficult at low clock rates due to the relatively high data rate of the protocol and the strict timing requirements. The essential protocol parameters for controlling the WS2812 NeoPixels (or similar 800kHz addressable LEDs) can be found in the datasheet.
Fortunately, the timing is nowhere near as strict as the data sheet suggests. The following timing rules can be derived from the excellent articles by Josh Levine and Tim and should work with all 800kHz addressable LEDs:
Pulse | Parameter | Min | Typical | Max |
---|---|---|---|---|
T0H | "0"-Bit, HIGH time | 65 ns | 350 ns | 500 ns |
T1H | "1"-Bit, HIGH time | 625 ns | 700 ns | 8500 ns |
T0L | "0"-Bit, LOW time | 450 ns | 800 ns | 8500 ns |
T1L | "1"-Bit, LOW time | 450 ns | 600 ns | 8500 ns |
TCT | Total Cycle Time | 1150 ns | 1250 ns | 9000 ns |
RES | Latch, LOW time | 9 µs | 50 µs | 280 µs |
Apart from T0H, the maximum values can be even higher, depending on when the NeoPixels actually latch the sent data (with some types only after 280µs!). This also makes it possible to work without a buffer and thus without the use of SRAM. The software essentially only has to ensure that T0H is a maximum of 500ns and T1H is at least 625ns, so that the pixels can reliably differentiate "0" from "1" and that the time between sending two bytes is less than the latch time. Assuming that the microcontroller runs with a clock frequency of 8 MHz, the following simple bit-banging function for the transmission of a data byte to the NeoPixels string was implemented:
// Send a byte to the pixels string
void NEO_sendByte(uint8_t byte) { // CLK comment
uint8_t count = 8; // 8 bits, MSB first
asm volatile (
"sbi %[port], %[pin] \n\t" // 2 DATA HIGH
"sbrs %[byte], 7 \n\t" // 1-2 if "1"-bit skip next instruction
"cbi %[port], %[pin] \n\t" // 2 "0"-bit: DATA LOW after 3 cycles
"add %[byte], %[byte] \n\t" // 1 byte <<= 1
"subi %[bit], 0x01 \n\t" // 1 count--
"cbi %[port], %[pin] \n\t" // 2 "1"-bit: DATA LOW after 6 cycles
"brne .-14 \n\t" // 2 while(count)
::
[port] "I" (_SFR_IO_ADDR(PORTB)),
[pin] "I" (NEO_PIN),
[byte] "w" (byte),
[bit] "w" (count)
);
}
When compiled, the function for bit-banging a data byte requires only 18 bytes of flash. The resulting timing values are shown in the following table:
Pulse | Parameter | Clock Cycles | Time |
---|---|---|---|
T0H | "0"-Bit, HIGH time | 3 Cycles | 375 ns |
T1H | "1"-Bit, HIGH time | 6 Cycles | 750 ns |
T0L | "0"-Bit, LOW time | 8 Cycles | 1000 ns |
T1L | "1"-Bit, LOW time | 4 Cycles | 500 ns |
TCT | Total Cycle Time | 11/10 Cycles | 1375/1250 ns |
This results in a transfer rate of 762 kbps, at least for a single data byte. The implementation can certainly still be optimized in terms of speed, but this is already close to the maximum and more than sufficient for controlling only four NeoPixels. Remember that interrupts should be disabled during transmission, otherwise the timing requirements cannot be met.
There are three data bytes for each NeoPixel. These are transmitted in the order green, red and blue (this can be different for other types of NeoPixels) with the most significant bit first. The data for the NeoPixel, which is closest to the microcontroller, is output first, then for the next up to the outermost pixel. So this doesn't work like an ordinary shift register! After all color data have been sent, the data line must be kept LOW for at least 9 to 280 µs (depending on the type of NeoPixel) so that the transferred data is latched and the new colors are displayed.
The implementation of the candle simulation requires random numbers for a realistic flickering of the candle. However, the usual libraries for generating random numbers require a relatively large amount of memory. Fortunately, Łukasz Podkalicki has developed a lightweight random number generator based on Galois linear feedback shift register for the ATtiny13A, which is also used here, slightly adapted. When compiled, this function only requires 86 bytes of flash.
// Start state (any nonzero value will work)
uint16_t rn = 0xACE1;
// Pseudo random number generator
uint16_t prng(uint16_t maxvalue) {
rn = (rn >> 0x01) ^ (-(rn & 0x01) & 0xB400);
return(rn % maxvalue);
}
The IR receiver implementation is based on TinyDecoder and requires 234 bytes of flash. Only the NEC protocol is supported, but this is used by almost all cheap IR remote controls. Alternatively, you can build such a remote control yourself with TinyRemote.
The IR NEC decoding function utilizes timer1 to measure the burst and pause lengths of the signal. The timer is automatically started and stopped or reset by the IR receiver via a pin change interrupt. The measured lengths are interpreted according to the NEC protocol and the transmitted code is calculated accordingly. The program was tested with the TSOP4838, but it should also work with other 38kHz IR receivers (note different pinout if necessary).
The output of the IR reciever is inverted (active LOW), a burst is indicated by a LOW signal, a pause by a HIGH signal. IR message starts with a 9ms leading burst followed by a 4.5ms pause. Afterwards 4 data bytes are transmitted, least significant bit first. A "0" bit is a 562.5µs burst followed by a 562.5µs pause, a "1" bit is a 562.5µs burst followed by a 1687.5µs pause. A final 562.5µs burst signifies the end of the transmission. According to the data sheet of the TSOP4838, the length of the output signal differs from the transmitted signal by up to 158µs, which the code must take into account. The four data bytes are in order:
The Extended NEC protocol uses 16-bit addresses. Instead of sending an 8-bit address and its logically inverse, first the low byte and then the high byte of the address is transmitted. For a more detailed explanation on the NEC protocol refer to TinyRemote. Don't forget to define the used IR codes in the sketch!
// IR codes
#define IR_ADDR 0xEF00 // IR device address
#define IR_PWRON 0x03 // IR code for power on
#define IR_PWROFF 0x02 // IR code for power off
Open the NeoCandle Sketch and adjust the IR codes so that they match your remote control. Remember that only the NEC protocol is supported. If necessary, adjust the values for the LDR light sensor and the switch-off timer.
avrdude -c usbasp -p t85 -U lfuse:w:0xe2:m -U hfuse:w:0xd7:m -U efuse:w:0xff:m -U flash:w:neocandle.hex
This work is licensed under Creative Commons Attribution-ShareAlike 3.0 Unported License. (http://creativecommons.org/licenses/by-sa/3.0/)
ID | Name | Designator | Footprint | Quantity | BOM_Manufacturer | BOM_Manufacturer Part |
---|---|---|---|---|---|---|
1 | VIN | VIN | HDR-1X2/2.54 | 1 | BOOMELE | 2.54-1*2PFemale |
2 | VOUT | VOUT | HDR-1X2/2.54 | 1 | BOOMELE | 2.54-1*2PFemale |
3 | 8k2 | R3 | AXIAL-0.3 | 1 | UniOhm | MFR0W4F8201A50 |
4 | 10k | R1 | AXIAL-0.3 | 1 | UniOhm | MFR0W4F1002A50 |
5 | 330 | R2 | AXIAL-0.3 | 1 | UniOhm | MFR0W4F3300A50 |
6 | PinHeader | S | HDR-3X1/2.54 | 1 | BOOMELE | Header2.54mm 1*3P |
7 | TSOP4838 | U1 | TSOP18XX | 1 | VISHAY | TSOP4838 |
8 | ISP | ICSP | HDR-3X2/2.54 | 1 | CONNFLY | DS1023-2*3SF11 |
9 | GL5528 | LDR | RES-PHOTO_CDS | 1 | Senba | GL5528 |
10 | WS2812 NEOPIXEL 5mm LED | LED3,LED1,LED2,LED4 | RGB-5MM | 4 | Worldsemi | WS2812D-F5 |
11 | 100n | C1 | RAD-0.1 | 1 | Dersonic | CC1H104ZA1PD3F5P3003 |
12 | MICRO USB 5S B | USB1 | MICRO-USB-1 | 1 | ValuePro | micro USBFemale |
13 | ATTINY85 | ATTINY | DIP-8 | 1 | Microchip | ATTINY85-20PU |
14 | 47u | C2 | CP_5X11MM | 1 | ValuePro | 47uF 16V 5*5Black high frequency |
Unfold