2
$\begingroup$

Is it possible to set up communication between an Arduino Uno and an Android phone using a wire that directly connects the Android phone and the Arduino?

$\endgroup$
  • $\begingroup$ I'm not going to go any more in depth than this, so I'll leave it as a comment, but my gut reaction was a 2 way modem on a headset connection. $\endgroup$ – Chuck Oct 20 '15 at 0:25
  • $\begingroup$ What do you want to achieve with this? You might choose for a bluetooth/wifi connection instead. $\endgroup$ – Paul Oct 20 '15 at 16:21
  • $\begingroup$ i wanna control this arduino with anothe mobile #FuaZe $\endgroup$ – user3699039 Oct 21 '15 at 13:15
  • $\begingroup$ You have to use the @ symbol, not the # symbol to talk to someone :-) $\endgroup$ – Greenonline Oct 22 '15 at 8:26
2
$\begingroup$

Whilst your question would be better suited to the Arduino StackExchange site, I shall attempt to answer it nevertheless. Below is the method that I have used in the past, however, there are, obviously, more than one way to skin a cat... For example the same question was asked a few years ago on EE.SE, Connect Android device to Arduino Uno via USB.

The answer to your question may be found here, Android USB host + Arduino: How to communicate without rooting our Android tablet or phone. However there are some pre-requisites for the Android device that you are intending to use:

You will require an Android device that supports USB host mode as well as the USB host API. Most Android 3.1+ tablets will suffice (some may require an USB OTG adapter). Also, the Galaxy Nexus has host mode enabled and matches the requirements (you will need an USB OTG adapter however).

I shall quote the information from the above page, in the case of link death.

This example consists of two parts:

  • The Android application that makes use of the USB API

A simple Android app that let’s you regulate the brightness of an LED on the Arduino using a slider. It also features a button to “enumerate” the USB device.

  • Firmware for the Arduino that does some serial I/O with the Android app

Very basic firmware for the Arduino. An interrupt is generated when a new byte is received. The received data controls the brightness of the Arduino’s on-board LED. (implemented via usleep-style software pwm in the main loop).

The Arduino Code:

int main(void) {
    //initialization
    initIO();
    uart_init();
    sei();

    uint8_t i = 0;
    volatile uint8_t pause;

    for(;;){//this is the main loop
        pause = data;
        PORTB |= (1 << LED);
        for(i = 0; i < pause; i++)
            _delay_us(10);
        PORTB &= ~(1 << LED);
        for(i = 0; i < 255-pause; i++)
            _delay_us(10);
    }
}

The Arduino Interrupt routine:

ISR(USART_RX_vect) {//attention to the name and argument here, won't work otherwise
    data = UDR0;//UDR0 needs to be read
}

The Android source code is available here: UsbController.tar.gz*.

  • You may need to change the PID value in UsbControllerActivity.java on line 38, if you have an Arduino Uno Rev3 or higher. You can check the VID/PID value with ‘lsusb’ after connecting the Arduino to your computer.

Most of the code is pretty standard, however, it is worth paying attention to the following UsbRunnable class:

private class UsbRunnable implements Runnable {
    private final UsbDevice mDevice;

    UsbRunnable(UsbDevice dev) {
        mDevice = dev;
    }

    @Override
    public void run() {//here the main USB functionality is implemented
        UsbDeviceConnection conn = mUsbManager.openDevice(mDevice);
        if (!conn.claimInterface(mDevice.getInterface(1), true)) {
            return;
        }
        // Arduino USB serial converter setup
        conn.controlTransfer(0x21, 34, 0, 0, null, 0, 0);
        conn.controlTransfer(0x21, 32, 0, 0, new byte[] { (byte) 0x80,
                0x25, 0x00, 0x00, 0x00, 0x00, 0x08 }, 7, 0);

        UsbEndpoint epIN = null;
        UsbEndpoint epOUT = null;

        UsbInterface usbIf = mDevice.getInterface(1);
        for (int i = 0; i < usbIf.getEndpointCount(); i++) {
            if (usbIf.getEndpoint(i).getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) {
                if (usbIf.getEndpoint(i).getDirection() == UsbConstants.USB_DIR_IN)
                    epIN = usbIf.getEndpoint(i);
                else
                    epOUT = usbIf.getEndpoint(i);
            }
        }

        for (;;) {// this is the main loop for transferring
            synchronized (sSendLock) {//ok there should be a OUT queue, no guarantee that the byte is sent actually
                try {
                    sSendLock.wait();
                } catch (InterruptedException e) {
                    if (mStop) {
                        mConnectionHandler.onUsbStopped();
                        return;
                    }
                    e.printStackTrace();
                }
            }
            conn.bulkTransfer(epOUT, new byte[] { mData }, 1, 0);

            if (mStop) {
                mConnectionHandler.onUsbStopped();
                return;
            }
        }
    }
}

After the USB interface has been claimed the Arduino USB serial converter is initialized by issuing the following control transfers:

conn.controlTransfer(0x21, 34, 0, 0, null, 0, 0);
conn.controlTransfer(0x21, 32, 0, 0, new byte[] { (byte) 0x80,
                0x25, 0x00, 0x00, 0x00, 0x00, 0x08 }, 7, 0);

The first call sets the control line state, the second call sets the line encoding (9600, 8N1). For communication, an additional thread is used to send data without blocking the Activity’s main UI thread. By notifying sSendLock of the UsbController the data will be transferred. After submission, the thread will go into “wait” again. This way, even if submission takes more time than expected, the Activity’s main thread will not be blocked and hence the app will not become unresponsive.

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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