This is the last post in a series of four on the days-accident-free counter I made for Nottingham Hackspace. You can read the other three here:
The electronics for the counting of days and digit display was relatively simple. I wanted to make something a bit more complicated as well.
I decided it would be a good idea to have the LEDs auto-dimming with the ambient light level. There are many sensible ways to achieve this:
- Using an LDR to control the duty cycle of a 555 timer producing PWM.
- Using an LDR to directly control the LED current via a transistor.
- Using an LDR to measure the light level via a microcontroller and output PWM from that.
What isn’t sensible is to re-create an “output-compare” timer entirely in logic in order to produce PWM. But that’s what happened.
My starting point was to produce an 8-bit timer. A 555 timer produces a 36kHz clock that feeds a 12-bit 4040 counter. The 9th bit of this counter is used as a “period complete” signal. The PWM frequency is 36kHz/256 = 140Hz.
An LDR is read by a ADC0820 IC, which is a single-channel ADC designed with 8-bit parallel output designed for input to a microcontroller, before the days of high-level integration and high-speed serial made such things redundant. This IC has a RD pin which starts a conversion when bought LOW, and an INT output that goes LOW when conversion is complete. There are also a few other control pins (MODE/CS/OFL) that were tied low as I didn’t need them.
The 8-bit output of the ADC and the 8-bit timer output feed a 74688 8-bit comparator, which has an output that goes LOW when it’s two inputs are equal. Therefore, given a free-running clock and a static ADC input, the comparator will output LOW for a single clock period. This timer operation is pretty much how PWM is produced by a microcontroller: a free-running counter and a compare register.
The ADC and timer need some synchronising around the clock rollover. A few logic gates are used to glue together the ADC and timing signals. I needed a NOT and an AND, these can be produced by three NAND gates, so I could use a single 4011 quad NAND chip.
Hopefully these timing and logic diagrams will make it clearer.
So, the counter counts to 256, which clears the ADC read signal (HIGH). On count 257, the time reset output (via NAND gates) goes high, which sets the count to 0. This sets the ADC read signal (LOW), and a new conversion is performed. At end-of-conversion, the INT signal sets the LED flip-flop (via another NAND gate)
The comparator output was clears the LED flip-flop at a point in the cycle corresponding to the light level. In this way, light-dependant duty cycle PWM is produced. The PWM feeds a power transistor output stage for actually driving the LEDs.
So, that’s how you over-engineer an auto-dimming circuit. Never actually do this.
Finally, here’s the PCB with all the components mounted. The LDR plugs in next to the potentiometer, which is used with the LDR to form a potential divider.