I want to light a miniature house …not “doll house” that can be played with , but the models like the period miniatures found in museums.

The lights in all rooms should come on simultaneously but slowly, over a period of say, 2-5 seconds when a viewer comes within 12-15 inches of the model. I don’t want the lights going on simply when someone walks past the model at 3 feet away.

Lights should stay on as long as the person is within that 12-15” distance, then fade out slowly, again, over about 2- 5 seconds as they move away. If another viewer comes up while the first moves away, and the lights dim a bit before lighting up again, or stay on, either way is OK.

Now for some background. I don’t know much about circuits. My dad (deceased) was a EE and I sometimes watched him tinker with stuff and asked questions. Then I took a basic Electronic course in junior college, but that was many moons ago. I do remember some principles and terminology.

I came across a design called a “Fade-in, fade out “ ckt for LEDS. ( Link attached)

It’s the first ckt shown, with the “slight flaw”

I bought some breadboard stuff and the parts on the parts list and used a 12v charger to power it. But I used a 2N2222 because my dad had a lot of those around.

The fade out works better, but the “fade in” is a little fast, but at least not like the typical LED sudden turn on (and off) .

Then I googled a lot on sensors and came across this Sharp sensor GP2Y0A02YK which seemed what I wanted. It’s weak at 3ft but seems to put out enough voltage at about 12-15 inches.

What I don’t know (and finally have come to my problem) is how to make this turn on and off the fader ckt.

I decided to try this: make the Sensor output turn on a second transistor (also NPN 2N2222-- which I will call NPN-2 ) and put the entire fader ckt as the load in the NPN-2 collector . I really had/ have no idea if this is correct, but I figured a load is a load if there is enough current to make it work.

The 12 v charger is the Vcc ( I took the push button switch out of the Fader ckt and just made the 12v go straight to the resistor R1) , and made all the common points of the fader ckt go to the NPN-2 collector.

The NPN-2 emitter goes to 12v charger low side The Sensor module itself is powered by 4 AA’s and its output goes to the NPN-2 base to turn it on (or not). And surprise of surprises, the whole thing actually works! Well, not great, but pretty well. So my questions are these.

1) Do I need resistor(s) in the base of NPN-2? It seems to work with one and without one. If I should have one/some there, what do I need?

2) How do I power the sensor module off the charger since it only needs 6v and not 12v?

3) How can I slow down the fade-in a little more ?

4) ) Is there a better way to do this? I.e. a better design, better sensor, just scrap the whole thing and start over?.

And finally 5) You all can stop rolling your eyes now.

Thank you


  • \$\begingroup\$ Use PWM with a small MCU. \$\endgroup\$ Mar 24, 2013 at 8:17
  • \$\begingroup\$ Please add a link to the datasheet for the GP2Y0A02YK and your final circuit diagram. (There is a circuit diagram editor button when you edit your question). \$\endgroup\$
    – jippie
    Mar 24, 2013 at 10:59

2 Answers 2


1) Your approach of switching the whole circuit is a bit strange, though it works in this case.

The base resistor should be small enough that the transistor switches on, but ensure that the transistor base isn't destroyed by excessive current.

(In other words, you are lucky that the output of the sensor is too weak to destroy the transistor.)

2) The sensor wants 5V for its power supply (see "Recommended operating conditions" in the data sheet). The easiest way to get that from 12V is with a 7805 voltage regulator. See the data sheet for suitable input and output capacitors.

3) You can increase the value of R1, but only so much that it still can switch the transistor on. Or you can increase C1, but then the fade out will also get longer. You can counter that by decreasing R2.

4) It would be better use an Arduino with PWM to dim the lights, then it would be easier get exact control of the timing. The sensor is a good choice for this application, you could hook it to an analog input and program the threshold.


This isn't an exact answer to your question, but it's a suggestion of a different way to control the fade-in and fade out.

Leon Heller suggested in a comment that you use a small microcontroller with Pulse-Width Modulation to drive the LED. This is a good suggestion, because it means that you have a tiny computer controlling the brightness of the LEDs, and you can program it to fade them in and out any way and at any speed you choose.

Now, I have a real thing about LEDs, and how to make them behave really nicely. One Two things I can't stand about LEDs are the flicker caused by PWM (I'm looking at you car lights), and the linear fade curved people use, which makes it looks like the brightness is changing very fast when the LED is dim, and changing slowly when the LED is bright (I'm looking at you Apple). I'll try to address both of these problems in this answer.


It might be just me, but I find flickering LEDs annoying. In a room illuminated by LEDs, if you flick your eyes around, you'll easily notice that the LED lights don't leave a continuous streak of light on your retina, but instead leave a series of flashes. Look at the two different LEDs spinning in this image:

PWM LED lights

The green one is continuously on, while the blue one is flashing. To be fair, it's flashing fast enough that you won't notice it until it's moving (or your eye is moving).


The strange thing about humans is that we don't perceive physical phenomena linearly. I.E. a light that's twice as bright doesn't seem to us to be twice as bright. To seem twice as bright to us, a light actually has to be 8 times brighter! The study of this phenomenon is known as Psychophysics.

power law exponents graph

For this reason, to make a light look like it's fading on and off linearly, you need to actually make it fade on and off very non-linearly.

So what's the solution? Here's what I did for my own LED fading on and off project.

Flickering: First, let's get rid of the flickering. Instead of adjusting the brightness of the LED by pulsing it, let's smoothly control the flow of current through it. We can do this using a transistor as a valve to control the current, a resistor to measure the current, an Op-Amp to control the transistor and a Digital to Analogue converter (DAC) to tell the Op-Amp how much current we want:

PSoC controlling the current through an LED

There's an excellent microcontroller made by Cypress called the PSoC (Programmable System On Chip). These chips incorporate the Op-Amp and the DAC, so all we need to add is the transistor and a couple of resistors. I haven't drawn the whole circuit here, just the parts we're interested in.

The DAC tells the Op-Amp how much current we want. Then the Op-Amp controls the transistor to give us that much current.

The DAC on the PSoC chip gives us 128 output steps, meaning we can have 128 brightness values of the LED from off to fully bright. By linearly ramping the DAC output linearly from 0 to 127, we can make the LED fade on very smoothly. However, due to the psychophysics, the fade-in won't look linear. It will seem to start fast, then slow down as the LED gets brighter.


To make the fade in more linear, we can simply apply a cube function to the fade-in curve to compensate for the 0.33 power of our brightness perception:

int i, dacValue;
for (i=0; i<128; i++)
    dacValue = (i*i*i) >> 14;        // Apply a cube function
    SetDac(dacValue);                // Set the output
    MillisecondDelay(23);            // Fade in over 3 seconds (0.023 * 128 = 3.0)

Now this is a huge improvement over PWM, but it's still not perfect. Due to the psychophysics, the human eye is still very sensitive to changes in brightness while the LED is still dim. As the LED fades in and out, you can actually see the discrete changes in brightness of the LED, as the DAC goes from 1 to 2 to 3 etc.

We can compensate for this by doing some PWM on the DAC. I.E. we can simulate a DAC value of 1.5 by rapidly switching the DAC between 1 and 2. Then we can very smoothly bring the brightness up from zero to maximum.

Here we can see a demonstration of the output of the DAC:

Error diffusion

Each digit represents 0.1ms of time. This image is 100 digits wide, so one line represents 10ms of time. For the first 11.2ms, nothing happens, then the DAC goes to 1, then back to 0. Then 4ms later, back on and off again. Then again only 2.5ms later. The pulses become more and more frequent, until at 56.0ms, the DAC is spending half its time at 0 and half at 1.0. To the eye, this will look like 1/256 maximum brightness. As time goes on, the amount of time spent at 0 decreases until at 81ms, it's at 1 constantly. Now it's time to fade towards 2.

This pulsing is so difficult to see because the changes is brightness are very small, and the speed is fast. To the eye, it looks like a perfectly smooth, flicker-free, and linear fade in.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.