pcbway

android and arduino usb communication, arduino android usb data sending, usb data communication android arduino,send data from android to arduinoHello friends, hope you all are fine and having fun with your lives. In today’s post, I am going to share How to do  USB Communication between Android and Arduino. I have designed many projects before in which I have interfaced Android and Arduino and communicated between them but in those projects I have used either Wifi or Bluetooth as a mode for communication. But recently I got a project in which I have to do USB Communication between Android and Arduino, they have this restriction of using USB. So, I have to work on it and I got it working successfully. You should also have a look at How to Install Android Studio. and Getting Started with Android.

So, today I thought to share it with you guys. It isn’t much difficult but will need a little patient. When I started working on it I couldn’t find any data on it online and I have to work a lot to make it work. So that’s why I am sharing it so that others can get it work easily. When I was starting this project then I have a bit confusion that whether to use the USB Host shield of Arduino or to use the usb cable through which we connect Arduino with computer. After a little research I got the idea that simple USB will work fine so I go with it. I have explained this tutorial in detail. First I have done the Arduino side because that one is quite easy and after that I touched the Android side that’s a bit complex and you must have the prior knowledge of it if you wanna make it work. So, let’s start this project. 🙂

Circuit designing for Monitoring Incoming Data

  • First of all, I have used the OTG cable for interfacing Android with Arduino, which is shown in below figure:

android and arduino usb communication, arduino android usb data sending, usb data communication android arduino,send data from android to arduino

  • From the above figure, you have clearly got the idea that why I have used this cable, one side of this cable, which is mini usb, will be inserted in the Android mobile while the other side, which is the female usb port, will be plugged with the usb cable of Arduino, as shown below:

android and arduino usb communication, arduino android usb data sending, usb data communication android arduino,send data from android to arduino

  • I have connected the two cables in the above figure, now one end of this cable is gonna inserted in the Android mobile while the other side will be plugged in Arduino and in this way we will do the USB communication between Android and Arduino.
  • But there’s some problem, we need some way to display the data in Arduino, which we are getting from Android. So for that reason I have interfaced another Arduino with this Arduino and I am doing serial communication between these two Arduinos.
  • So, in simple words, Android is plugged with first Arduino via USB and the first Arduino is connected with second Arduino via serial port. so, when Android will send the data to first Arduino then first Arduino will send that data to second Arduino, which we can easily see on the Serial Terminal. So here’s the circuit diagram of two Arduinos.

android and arduino usb communication, arduino android usb data sending, usb data communication android arduino,send data from android to arduino

  • Now you can have the idea in above figure that the two arduinos are communicated via pin # 2 and 3 which I have made Software Serial, now with the pins they are sending data from first Arduino to second Arduino. Now let’s have a look at the coding of both Arduinos.

Programming Code for Arduino

  • As in the above section, we have connected two Arduinos via Serial communication in which we used software serial.
  • Next thing we need to do is to write the code for both of them, so simply copy the below code and upload it in both of your Arduino boards.

  • Above code is quite simple and I am simply sending any data I am receiving on the Serial port to software serial, so when I get the data from Android via USB, I will receive that data and then will forward it to Software Serial on which my second Arduino is connected, which will receive that data and then show it over to Serial Terminal.
  • Now let’s have a look at Android side code, which is a bit complicated.

Programming Code for Android

  • You must have the prior knowledge of Android, if you wanna make it work so if you know the basics of Android then you know that there are two main files designed in Android, which are Java file and XML file.
  • So, first of all create a project in Android and place the below code into its XML File:

  • Next thing we need to do is to add the USB permission in the manifest file, so paste this  code in your manifest file:

  • Now the last thing, you need to do is to add the code in java file so copy the below code and paste it in your jave file:

  • Now hit the Enter button on your Android Studio and if everything goes fine then you will get a screen as shown below on your mobile:

android and arduino usb communication, arduino android usb data sending, usb data communication android arduino,send data from android to arduino

  • Here’s a simple button, when you press this button, it will show ON and will send the data which you wanna send for example, in my case I am sending character “1” and “2” on button press.

USB Communication Between Android and Arduino

  • Now let’s do the usb communication between Android and Arduino, I hope till now you have configured your Android Mobile and both the Arduinos so their overall picture will look something like:

android and arduino usb communication, arduino android usb data sending, usb data communication android arduino,send data from android to arduino

  • You can clearly see in the above figure that the Android Mobile is connected with Arduino, which is further connected with second Arduino and US of Second Arduino is connected with computer, where I am checking the coming data from Android in Serial Terminal.
  • Now when I press the button on Android app, it will send 1 or 2 to Arduino, which is shown in below figure:

USBCommunication between Arduino and Android

  • I have pressed the toggle button on Android many times thats why I have got many 1 and 2 in Serial Terminal.
  • So, that’s how I am receving data from Android to Arduino. Now instead of sending 1 and 2, you can send any kind of data from Android to Arduino. I will post more tutorials on it in which I will be sending some data like GPS coordinates from Android to Arduino plus I will also post the 2 way communication i.e. receving data in Android from Arduino.

The Arduino part is easy but the Android part is bit difficult so if you need help then ask in comments and I will help you out. That’s all for today, will meet you guys in coming tutorials. Take care !!! 🙂

7 Comments

  1. helloo hope ur fine. its realy awesome communication 🙂 but its a 1 way communication can u send a code which allow arduino sensor data to display on android device plx

  2. Can you provide me the arduino-android communication. So how to read data from arduino in android? I’m trying it out and I’m stuck. Is for an univseristy project.

    Thanks

    • Yes it is possible but its quite complicated and the hex file won’t be sent to Arduino but instead you can send it to Atmega but you will also need the programmer to program your hex file.

  3. Hi,
    Here you didn’t use any usb codes for first Arduino, instead of this you just use serial codes. So i think your cable was a usb to serial (uart) converter. So all complex interactions between the Android and Arduino was done by the converter, not Arduino.

    My question is , is it possible to communicate an Android device (host) with any Microcontroller (client) via a cable(cable available with the phone charger) with mini male usb on Android side and normal male usb on Microcontroller side.

Leave a Reply

Your email address will not be published. Required fields are marked *

Post comment