Read my thoughts

Experimenting with Android and Arduino

Experimenting with Android and Arduino

Ever since I got into the world of mobile development, I always wanted to do more than just writing apps. I always wanted to interact with the physical world with my mobile. So lately, I have discovered an awesome open source electronics i/o board called Arduino. I ordered one from Sparkfun, and it took around 20 days to get to me, it would’ve gotten to me a little earlier, if the carrier company didn’t think it was an explosive unit.


As I got my Arduino, I had to dust out my electronics knowledge that I got back when I was at college. Arduino really makes it easy, even for the non electronics geek, to get into electronics really quickly.

My First Real Arduino Experiment

After a quick refresh into the world of electronics, I wanted to do a little experiment that would allow me to interface my newly bought Nexus S phone with Arduino. Since I didn’t connect my bluesmirf bluetooth module yet, I had to interface with Arduino using serial connection. There’s one problem though, I didn’t want to connect a “mobile device” with wires to a board.

The solution was simple:

  1. Connect the Arduino board to a host computer
  2. Run a TCP server on the host computer, that waits for commands from a client
  3. The TCP server translates those commands, and sends them to the Arduino’s serial port

Circuit Design

The components setup is easy, you would control the 10 LED graph bar, as individual LEDs with current limiting resistors. Looking at the datasheet for the LED graph bar, it says that I would need 20mA of current to operate the LEDs. Unfortunately, I don’t have lower resistors than my 330Ω, so I used 10 330Ω resistors to provide around 15mA to each LED, which was enough.

LED Graph Bar Arduino Sketch

Arduino comes with a powerful language called the Arduino programming language, which is based on C/C++. So I had no problem understanding the language quickly, and start interacting with the board.

I connected the LEDs to ports 2 to 11 on the Arduino board, and sat them to output either HIGH (5v) to light on, or LOW (0v) to light off. Also in the setup function, I initiate the serial communication with baud rate = 9600.

As the loop function starts, I wait for serial bytes to arrive, as the bytes arrive, they arrive in ASCII encoded stream. I had to do a little conversion from ASCII to integer, and if the integer is between 0 and 9, I light the LEDs that correspond to that number plus 1, So if I receive 3, I would light 4 LEDs. I count from 0, because I wanted to keep my communication under 1 byte, plus that’s how machines count.

TCP To Serial Communication Server

To let the Nexus S communicate wirelessly with Arduino, I had to write something that would allow me to send serial commands from the mobile phone to Arduino. So I wrote a simple TCP server in Python. The Python server would receive the desired commands over WiFi, and translates those commands to Arduino using the serial port. I wouldn’t have used this server, if I connected the bluetooth module, since I can send those commands directly from Android phone to Arduino.

The TCP server is simple, I use the awesome library pySerial to open the serial port on my Arduino, with the same baud rate as defined on the sketch. If the connection has been established, I loop indefinitely waiting for commands, which in our case would be digits from 0 to 9. As I receive the digits, I convert them to ASCII, and send them to Arduino. If the letter ‘Q’ was received, the communication ends.

The Android App

The TCP server waits for commands to be sent from any device that it connects to, I had the server running on port 50000 of the same machine that is connected to the Arduino. and all I had to do is send the numbers of LEDs that I want to light. Using the accelerometer sensor on the Nexus S, it was really easy. I just had to filter out the jitter, as the accelerometer sensor is very sensitive, so I could send normal integers over the air to light how many LEDs I want.

The full source code of the app is available under GNU GPLv3 license on my github repository, please feel free to use it.

Resources

4 Responses to Experimenting with Android and Arduino

  1. June 20, 2011 at 3:06 am
    arun neeraj:

    Its nice…but using a bluetooth module will be more awsome…using the pc in between will have some trouble(portability) wil lose…i have done the same in python by jst three lines in system and by using a nokia phone which support python to send commands…and am glad to know android version which support wifi…any way good work……

  2. July 6, 2011 at 10:53 pm
    Mwad3:

    Excellent my cousin .. keep going ,, :)

Leave a Reply

Your email address will not be published.

*

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>