Nexus 4 – Arduino Communication

I was one of the lucky people who managed to get my hands on a Nexus 4 shortly after its release and it has served me incredibly well since then. Whilst it now might be classed as mid to low end, I have next to no complaints about it. However, lately I wanted to use my phone as the brain inside a quadrocopter, but unfortunately there is no simple solution for interfacing an android device with an Arduino (which would relay the commands to the Electronics Speed Controllers). This post describes the three possible methods I found, a brief description of how they work and what their advantages and disadvantages are.

The first and most obvious solution was to use the ability of android devices to act as a USB host, commonly referred to as USB OTG. The Nexus 4, however, doesn’t natively support this and so some messing about is needed to make this work. Whilst it does have the necessary hardware to support USB OTG, it is unable to provide the 5V required to power the attached device. This hardware problem can simply be solved by purchasing a USB OTG Y-cable to provide the power from an external source, but the problem still remains that the stock Nexus 4 kernel has the functionality disabled.

This can be resolved by using a custom kernel made by Ziddey which enables this functionality. The download links to the kernel as well as more information about this method can be found on Ziddey’s thread here.

The problem with this method is that the phone will charge from this USB connection, which on a quadrocopter, where battery power is already at a premium, was a game breaker. There are ways to disable charging on a rooted Nexus 4 which I will discuss later, but doing so breaks USB OTG support. This is because of the slightly hackish way that the custom kernel detects the USB OTG cable. Most devices detect a USB OTG cable by means of the ID pin being tied to ground, however, this ID pin detection is broken on the Nexus 4 for whatever reasons. However, manufacturers create proprietary chargers by placing custom resistances between ground and this ID pin. This therefore leads the powered USB OTG Y-Cable to be detected as a proprietary charger. It is this change which the custom kernel looks for to enable USB OTG and therefore disabling charging breaks this detection mechanism.

The second method to connect a Nexus 4 to an Arduino is to use the fairly new Android Accessory Development Kit. Whilst you can purchase the Arduino Mega ADK (here), I already had an Arduino I wished to use and so I purchased a USB Host shield (here) off eBay for next to nothing. It was then a simple case of plugging the two things together and following the documentation Google provides. I did, however, have the problem that the phone was trying to charge off the Arduino.

Fixing this requires a rooted device, but once rooted it is fairly simple to disable charging. From a root terminal window run the following command to disable charging:

echo 1 > /sys/module/pm8921_charger/parameters/disabled

To re-enable charging run the same command with a 0 instead of a 1. To make it slightly less of a hassle I created scripts to do this for me. However, the shebang will need to be “#!/system/bin/sh” not the normal “#!/bin/sh”.

I have no idea what happens if your phone runs flat with charging disabled. I imagine it probably will start charging the moment it shuts down, but it is probably best not to find out.

The final method, which is probably the most dangerous to your phone but also the one that made me the most excited, is to use the UART debugging connection hidden within the headphone jack. This method is described in great detail here. It is great because it doesn’t void your warranty (as unlocking the bootloader does) and also unlocks a huge amount of potential for modding one’s phone: It is like the Android Debug Bridge but even more awesome. It is also useful for people writing custom kernels as it provides debugging of kernel startup, before the Android debugger has started. The only downside is you have to provide a 3V signal to trigger the UART mode and then create a level converter to convert between the 1.8V Nexus 4 and the 5V Arduino (or USB if connected to your computer) which is a little fiddly. The potential for severely damaging your phone is also fairly high.

Of all these methods I had the most success with the second one, whilst having to disable charging every time was annoying, the connection was reliable. The first method was never really an option because of the problem of the device charging and I never actually used the third option in my quadrocopter. This was because the slightly cobbled together nature of my circuit made me unwilling to pin the fate of my quadrocopter on it.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>