My motivations to make this tweak in such an ordinary item were two: have fun and make it useful.
I normally enjoyed reading a book when commuting between work and home, a trip that takes around 40 minutes one way depending on traffic. It happened that during parts of autumn and winter the days are dark enough not to give conditions for reading. So, I came up with the idea of purchasing a book light to go on reading.
So, I bought a 10bucks well-rated reading lamp(4.6 out 5) on Amazon, and although it seemed to be good, I had an unpleasant surprise at first use: It was too strong to be used in the car ( later, I discovered that also at home ). Then, what to do now? Return?
Nooo...
I thought of returning the lamp and finding another one, but then I could run into the same situation again. Then, at first, I guessed that I could just easily increase the value of the resistors in series with the LEDs, and then the job was done. But it happens that after thinking a bit more, I realized I would miss changing the brightness because sometimes my eyes ask for different light intensities depending on the day.
Anyways, I needed first to break into the hardware and see how things are and then decide what to do. So, here we start and I hope you enjoy reading about this short trip.
What is about the original lamp?
As the video below shows, it has no luminosity control and the LEDs are then turned on at the maximum always. Even, less cool is that it has three light types: white, warm, and mixed ( which is the strongest with all 6 LEDs ON).
What did I expect then?
As these features didn't suffice my needs at all, I started thinking about how I would like this lamp to be and I basically had the answer promptly in my head, and here is what I wanted:
- Control of light intensity;
- and, more variations in the temperature of the light ( different combinations between white and warm).
The idea seems good, but at that point, I still needed to know the hardware and see if this modification would be possible or not.
Hardware teardown
The image below shows the lamp itself ( Those marks over the rubber were caused by me before opening). Also, In order to make it easier for the description, I called the parts: head and base.
Base PCB
The base PCB basically provides regulated 3V3 for the head PCB. The circuit will not be detailed but it is in short a regulator and a Li-Ion charger controller.
Head PCB
Finally, here is where the job really needs to be done. As you can see, the circuit is as simple as expected.
Drawing the SCH
Handed with a multimeter, it didn't take long until I could figure out how the connections were made. The figure below shows how these connections explicitly. note: although I wrote uC over the IC, it's was not necessarily an MCU.
Now, the circuit is completely known and something can be done about it. In a quick look, I realized that I could find some MCU that has a pinout compatible ( power pins and GPIO functionality for the LED and the button ) with the uC and then add some firmware and have it working with everything I wanted. After a quick search, I found the PIC12F1840 (see below) which matched the pinout (also functionality) perfectly.
The Dev setup
As the MCU was delivered I just needed help from a hot air gun to remove the old controller and put my MCU in there. I also needed to solder a cable so that I can connect the programmer. The picture also shows an Analog Discovery 2 (AD2) which I used to simulate a button, this way I didn't need to press the physical button every time I wanted to test something, and actually, the real button was mapped to a pin used by the debugger which makes it useless in debugging mode. The AD2 was also used for checking the PWM generated to control the LEDs. Lastly, regarding the firmware development, I used the MPLABX IDE.
Firmware
Since we already know the hardware, we can then think about the code structure, and in this regard, the manner I found to overcome the limitation of having only one button to control the whole system was to detect more than a single button click event. So I defined three events for the button: One-Click, Long Press, Double Click. Those are the events commonly captured by a smartphone as well.
About the environment
I made use of the MCC ( Microchip Code Configurator ) to configure the hardware as I needed for this project, the timers, clock, GPIOs, and so on.
The Button behavior
- One-click: Changes the light temperature. If the light is off it simply turns it on.
- Long Press: keeps changing the Brightness until the button is released
- Double click: Turns off the light
The light temperature
The head PCB has 6 LEDs: 3 white and 3 yellow. As two independents PWM control the LEDs, I defined 5 different sets of white and yellow intensity to perform the role of the temperature of the light. The combination is shown in the table below:
SET | YELLOW (%) | WHITE(%) |
---|---|---|
1 | 100 | 0 |
2 | 75 | 25 |
3 | 50 | 50 |
4 | 25 | 75 |
5 | 0 | 100 |
The percentage corresponds to the amount of influence of that light in the total brightness.
The Brightness
For the brightness, there are five 5 levels available which I chose according to some experimental tests varying the Duty Cycle of the PWMs that controls the light. the formulas below show the relationship between the brightness and the light temperature. light_ratio refers to the values in the previous table.
(I) Brightness.white = (Brighteness) * light_ratio.white/ ( light_ratio.white + light_ratio.yellow)
(II) Brightness.yellow = (Brighteness) * light_ratio.yellow/ ( light_ratio.white + light_ratio.yellow)
The Implementation
Now that we know how everything from the perspective of usability, we can now think in terms of code. In order to make it easy to implement the code, let's make some representation for parts of the system. First, as the button is going to generate particular events, we can think of a state machine like the one drawn below:
Then, we need another state machine from each we can have the events generated by the button clicks. The drawing below represents the button subsystem
The real thing
After the implementation, I recorded separately in the following video the result of the whole implementation.
Conclusion
I had the fun I wanted from this project for sure and although I was not working on it consistently, let's say it was going often to the drawer of "things I wanna do", I managed to finish it and have my book lamp working properly. Also, what made me happy was that I applied effort to have this project written down. I'm aware that I have some difficulties in bringing what is in my head into words, but I am sure that I can improve with practice. Lastly, if anyone wants more information about it or would like to share suggestions, I would be glad to know.