Introduction: Arduino Controlled Phone Dock With Lamps
The idea was simple enough; create a phone charging dock that would turn on a lamp only when the phone was charging. However, as is often the case, things that seem initially simple can end up getting a bit more complex in their execution. This is the story of how I created a dual phone charging dock that accomplishes my simple task.
Step 1: What I Used
This is by no means an exhaustive list of everything I used, but I wanted to give a general idea of the major components I used. I've included Amazon links for most of these components. (Note that I get a small commission from Amazon if you use these links. Thanks!)
Arduino Uno: http://amzn.to/2c2onfe
Adafruit 5V DC Current Sensor (x2): http://amzn.to/2citA0S
2-Channel Solid State Relay: http://amzn.to/2cmKfkA
4-Port USB Box: http://amzn.to/2cmKfkA
1' Panel Mount USB Cable (x2): http://amzn.to/2cmKfkA
6" A-B USB Cable: http://amzn.to/2cmKfkA
I also used the following supplies that I picked up at the hardware store:
4"x4" Plastic Conduit Boxes (x2)
40W Edison Light Bulbs (x2)
Light Bulb Socket
Track Light Bracket
Assorted Black Iron Pipe (3/8")
Assorted Brass Pipe Fittings
3' Extension Cord
Step 2: Experimentation, Design & Wiring
In order to determine when the phone was charging, the current flow to the phone would need to be constantly monitored. Although I'm sure there are circuit designs that can measure current and control a relay based on the current level, I'm by no means an electrical expert and didn't want to tackle building a custom circuit. From some experience, I knew that a small microcontroller (Arduino) could be used to measure current and then control a relay to switch lights on and off. After finding a small DC current sensor by Adafruit, I began experimenting with connecting it to a USB cable to measure the current flowing through it as it charged a phone. A typical USB 2.0 cable contains 4 wires: white, black, green, and red. Since the black and red wires carry power through the cable, either one of these can be used to measure the current flow - I used the red wires. A typical current sensor needs to be placed inline with the current flow (the current needs to flow through the sensor), and the Adafruit sensor is not an exception to this rule. The red wire was cut with the two cut ends being attached to the two screw terminals on the current sensor. The Adafruit sensor was connected to an Arduino and I wrote some simple code to report the current flow through the sensor. This simple experiment showed me that a charging phone drew between 100 and 400 mA. After the phone was completely charged, the current flow would fall below 100 mA, but wouldn't reach 0.
With my experiment successfully demonstrating that I could measure the current flow with an Arduino, I designed the circuit shown above. Two 1' panel mount USB extension cables would be connected to a 4-port charging box. The phone charging cables would be connected to these extension cables, making the system able to accommodate any sort of USB charging cable - and hopefully making it "future phone proof." The red wires of the extension cables would be cut and connected to the current sensors. The current sensors supply information to the Arduino, which in turn controls a two-channel solid state relay. The relay is used to switch the 110V power to the light bulbs. Power to the USB box and light bulbs can be tied together allowing the system to use a single outlet. I particularly like how power to the Arduino can be supplied by one of the extra USB ports in the charging box.
Step 3: The Phone Dock
The phone dock was built from 3/8" black pipe. I used two male-female elbows, a T, a short section that was fully threaded, and a round flange. For the brass parts at the top of the dock, I cut a 1 1/2" long brass pipe in half and used one half for each part. A small hole was drilled in the T, which was large enough to accommodate the ends of the lighting cables. The cables were worked through the elbows and were JB Welded into the brass pipes. This ended up being a lot harder than it seems as the elbows were not large enough inside to fit the lighting cable end through. I ended up reaming the insides of the elbows until they would fit.
If I had to make this dock again, I would give it more support for the phone. As you may expect, if the phone is pushed at all when it is on the dock, the lightning cable ends can be bent very easily. I find it strange that Apple actually sells a dock with a similar non-supported configuration.
Step 4: The Lamps
I wanted the lamps to have a similar industrial look to that of the dock. For the first lamp, I used a generic bulb socket set on top of a 3/8" pipe flange. Some small brass pipes connect the base to the socket and complement the brass accents on the dock. A 40W Edison bulb really is the star of this lamp. I wanted to use Edison bulbs as they fit perfectly with the design of this dock and they allow you to create a beautiful exposed-bulb lamp.
While at Lowe's I found a track light bracket on clearance that I thought was interesting. I turned the bracket upside-down and added a pipe flange to make the base. The socket in the track light mount was not attached to it as it was designed to be held in place by a flat-faced bulb. Since I was using an Edison bulb, I made a small aluminum bracket to hold the socket inside the circular housing of the track light bracket. Small brass knobs were added to complement the rest of the system.
Once the dock and lights were completed, they were painted matte black - except for the brass bits.
Step 5: The Arduino Enclosure
I used two 4" x 4" PVC enclosures for the Arduino housing. I cut ventilation slots into one side and the cover of each enclosure. On the side of the one enclosure, I cut two rectangular holes for the panel mount USB cables. Holes spaced 1 1/8" on center were drilled on both sides of these rectangular holes and were used to attach the cables to the enclosure. One side of both enclosures was cut away so that the two boxes would form a single box when they were set side by side. A 3/4" thick wood block was used to hold the boxes in this side by side configuration and also forms a convenient base for them to sit on.
Step 6: Attach USB Box
The first component to add to the enclosure is the 4-Port USB charging box. I simply fixed this in place with double-sided tape.
Step 7: Mount Arduino in Enclosure
I like to use electric box faceplate spacers to mount electronic components as they are made of plastic and can be adapted to work as hold downs or standoffs. I simply cut them up with my knife and then push screws through them. The Arduino was mounted into the one enclosure box with small flat head screws with the faceplate spacers mounted between the Arduino and the box.
Once the Arduino was mounted, a short (6") A-B type USB cable was connected between the USB port of the Arduino and the closest port of the charging box. This was a really tight fit for the cord and I actually had to trim back the bendy plastic bits surrounding the wire at the end of the cable so that it would fit.
Step 8: Wiring and Mounting the Relay
The cords to the lamps were fed through holes in the enclosure. One wire from each cord was connected to the outputs (the switched 120V side) of both channels of the solid state relay. Short (4") sections of wire were connected to the remaining screw terminals adjacent to where these lamp wires were connected. These wires will be used to supply power to the 120V side of the relay.
On the DC side of the relay, 4 wires were attached according to the configuration shown. Two of the wires supply the + and - DC voltage necessary for the operation of the relay, while the remaining two wires carry the digital signals, which tell the channels to turn on or off.
These 4 wires were then attached to the Arduino as follows:
The red wire (DC+) is connected to the 5V pin.
The black wire (DC-) is connected to the GND pin.
The brown wire (CH1) is connected to the digital output pin 7
The orange wire (CH2) is connected to the digital output pin 8
Once all of the wires were connected to the relay, it was mounted in the enclosure using small flat head screws.
Step 9: Wiring and Mounting the Current Sensors
Communication and power wires were created for the two current sensors by splicing the two sets of wires leading from the sensors to the Arduino. As before, the red and black wires are used to power the sensors. These wires are connected to the Vin (red wire) and GND (black wire) pins of the Arduino. Surprisingly, even the communication wires (the SDA and SDL wires) can be spliced together. This is because the Adafruit current sensors can each be given a unique address depending on how their address pins are soldered together. If the board does not have any of the address pins soldered together, the board is addressed as board 0x40 and will be referenced as such in the Arduino code. By soldering the A0 address pins together, as seen in the diagram, the board's address becomes 0x41. If only the A1 address pins are connected the board would be 0x44, and if both the A0 and A1 pins were connected the address would be 0x45. Since we are only using two current sensors, I only had to solder the address pins on board 1 as shown.
Once the boards were addressed correctly, they were attached to the enclosure using small brass screws.
The SDA (blue) and SCL (yellow) wires from the sensors are connected to the SDA and SCL pins on the Arduino. These pins were not labeled on my Arduino, but they are the last two pins after the AREF pin on the digital side of the board.
Step 10: Connect the USB Extension Cables
As previously mentioned, the USB extension cables need to pass current through the current sensors. This was facilitated by splicing wires into the cables' red wires. Once the USB cables are mounted in the enclosure, these wires from the splices are connected to the current sensors. For each USB cable, the current flowing through it will flow down these wires, through the sensor, and then return to continue through the cable to the charging phone. The male ends of the USB cables were plugged into two of the open ports of the USB charging box.
Step 11: Connect the Power
The final step in the electronics box is to connect the power cord to the USB box and lamps (aka. the 120V side of the relay). The black wires leading directly to the lamps are connected to the one wire of the power cord together with the brown wire from the charging box. The power cable to the charging box was simply cut with the two wires inside (they are the blue and brown wires) being stripped back. Finally, the two white wires from the relay are wire nutted to the other wire of the power cord together with the blue wire from the USB charging box.
Step 12: The Completed System
Once the box is completely assembled, the enclosure covers can be replaced. Now that the hardware for this system is complete, it's time to move onto the software.
Step 13: The Arduino Code
Development of the Arduino code was fairly straightforward, although it took a few tests to get it just right. In its simplest form, the code sends a signal to power the appropriate relay channel whenever it reads a current flow that is greater than or equal to 90mA. While this simple code was a good starting point, cell phones don't charge to 100% and then sit there drawing very little current. Rather, I found that once the phone was charged it would draw several hundred mA for a short time every few minutes. It is as if the phone is a leaky bucket that needs to be topped off every few minutes.
To solve this issue, I developed a strategy where each channel could be in one of three states. State 0 is defined as when the phone has been removed from the charging dock. In practice I found that virtually no current was flowing when the phone was removed, but I set the upper current limit of this state to 10mA. State 1 is the state where the phone is fully charged, but still on the dock. If the current flow falls below 90mA and is above 10mA, the system is in state 1. State 2 is the charging state, where the phone is drawing 90mA or more.
When the phone is placed on the dock, state 2 is initiated and continues during charging. Once charging concludes and the current falls below 90mA, the system is in state 1. A conditional statement was made at this point so that the system cannot proceed directly from state 1 to state 2. This keeps the system in state 1 until the phone is removed, at which point it enters state 0. Since the system can proceed from state 0 to state 2, when the phone is placed back on the charger and the current flow rises above 90mA, state 2 is started again. Only when the system is in state 2, is the signal sent to the relay to turn on the light.
One other issue I ran into is that the current would sometimes briefly fall below 90mA before the phone was fully charged. This would put the system into state 1 before it should have. To fix this, I average the current data over 10 seconds and only if the average current value falls below 90mA will the system enter state 1.
If you're interested in this code, I've attached an Arduino .ino file with some more descriptions in it. Overall, it works pretty well, but I've noticed that sometimes the system seems to proceed to state 0 when the phone is still attached and fully charged. This means that every now and then the light will come on for a few seconds (when it progresses into state 2) and then go out. Something to work on for the future I guess.
Step 14: The Finished System
I installed the charging dock on our bookshelf, with the Arduino box located behind some books. If you simply glance at it you would never realize the work that went into it - and even to see it in operation doesn't do it justice. Then again, it makes me happy to see the lights come on and go out, and I've even come to rely on them to see if the phone is charging.
We have a be nice policy.
Please be positive and constructive.
Could you possibly pass the fritzing part of the solid state relay? Thank you very much. Very nice project.
Actually, could you possibly pass the fritzing file?