Arduino Uno with Android

Hi guys,

I've been tasked with writing an Android application to control a Ardunio-connected LED board and I was wondering what would be the best way to do this. I researched a lot on Google, but it only made me more confused.

Ideally, an app that I write would allow the user to click a button on the android app and the following would occur:

  1. one set of LEDs on the board are lit for a limited amount of time(~0.5 sec)
  2. Phone takes pictures
  3. Repeat this for another set of LEDs

I'm already familiar with Android but I know practically nothing about Arduino. My main questions are:

  1. How to connect Android device to Arduino? (From google I found http://www.amarino-toolkit.net/ but I'm not sure if it meets my requirements in terms of programming)
  2. How to send Arduino code(instructions) from Android (what to write in Eclipse)? I've already inherited some arduino code to try out.
  3. Is there some kind of API or SDK on the android side that streamlines this whole process?
    4)I have an Arduino Uno right now. Will this be sufficient or should I buy another type of board?

Thanks in Advance!

The Arduino Uno should be fine.

You could use Bluetooth for communication with a HC05 bluetooth device for the Arduino. See this.

...R

Start from here: http://www.circuitsathome.com/

*Declaimer: *

BareVisions:
Ideally, an app that I write would allow the user to click a button on the android app and the following would occur:

  1. one set of LEDs on the board are lit for a limited amount of time(~0.5 sec)
  2. Phone takes pictures
  3. Repeat this for another set of LEDs

I'm already familiar with Android but I know practically nothing about Arduino. My main questions are:

  1. How to connect Android device to Arduino? (From google I found http://www.amarino-toolkit.net/ but I'm not sure if it meets my requirements in terms of programming)
  2. How to send Arduino code(instructions) from Android (what to write in Eclipse)? I've already inherited some arduino code to try out.
  3. Is there some kind of API or SDK on the android side that streamlines this whole process?
    4)I have an Arduino Uno right now. Will this be sufficient or should I buy another type of board?

This is almost entirely an Android problem. You need to get/write an app that controls Arduino and takes pictures, all inside 0.5 secs. It may be possible. I would put it in the too hard basket. I think it would be a hell of a lot simpler to use Android to control the Arduino and another camera. If you can flash the LEDs for longer, it might save you a lot of grief.

1.) whatever your problems, communications isn't one of them. The simplest approach is bluetooth, as suggested. here is some background

http://homepages.ihug.com.au/~npyner/Arduino/GUIDE_2BT.pdf
http://homepages.ihug.com.au/~npyner/Arduino/BT_2_WAY.ino

  1. The commands from Android probably need be no more than single alphanumeric, one for each LED pattern. Pretty easy stuff

  2. Who knows? Probably not needed

  3. A Uno should be fine.

Thanks guys for all the advice.

Nick - Ideally the LED should only be on for .005 seconds but since this is a prototyping effort, time isn't really a big deal but it's a goal. Thanks again for answering all my questions.

BareVisions:
Nick - Ideally the LED should only be on for .005 seconds but since this is a prototyping effort, time isn't really a big deal but it's a goal. Thanks again for answering all my questions.

Nobody will be able to see that - why bother ?

...R

BareVisions:
LED should only be on for .005 seconds

Well, I recognise that you are not actually talking about people seeing the LEDs, and you are talking about a camera seeing them, which is not unreasonable, but I think it might be time to be more forthcoming on what the objectives are, and why you want to go about it this way.

At the moment, all your problems are with Android, and they are probably getting worse, but all might be fixed by the simple expedient of changing the camera to one under Arduino's control.

Nick_Pyner:
changing the camera to one under Arduino's control.

???????

The original post says

  1. Phone takes pictures

...R

I suggest that you do two things.

  1. Determine if there is any variable delay between the app telling the API to take a photo, and the photo actually being taken
  2. Determine if there is any variable delay between the app sending a bluetooth serial command and the command being sent.

You'd also need to determine any variability at the bluetooth receiver as well, however I suspect as this is a dedicated module that its much such an issue.

The Android is going to by your main problem as its a multi tasking system, and things like the request to the camera API to take a picture

See How to reduce shutter lag in camera for android? - Stack Overflow
But even those solutions may not work for you, because your leds are only on for 5ms, which is equivalent to a frame rate of 200fps, which AFIK is well beyond what most smartphones will do.

I strongly suspect that what you have been asked to do, is not technically possible with a phone camera. You'd be better off doing the same thing with an SLR

Thanks guys for the help. I think I should have been more clear about the objectives of the project:

  1. Phone sends signal to Arduino to light up a section of LED Board for a short amount of time(short as possible)
  2. Phone takes picture (Automatically)
  3. Phone moves onto next section of LED and repeats the process.

This whole process would be automated by an application on the phone, so the user wouldn't himself be taking any pictures.

This project has already been done at my university with expensive high speed cameras and an Arduino LED board, but they want me to port it to an Android phone while retaining as much of its functionality as possible. Of course the Android's shutter speed won't be able to match the speed of an expensive high speed camera, but I would like to maximize the Android's capabilities.

HI BareVisions

It doesn't matter who pressed the button on the phone.

Even if some code presses the button, its an API call to a multi tasking operating system, its not a "Real Time Operating System".

Hence there may be a variable lag between the call to the API and the OS telling the camera hardware to take a picture and the camera actually taking the picture.

The minimum I'd do is to write some code that at random times, makes the Android API call to take a photo, then measure the time before you get the callback to say that a photo has been taken.
If this varies by more than 5mS (i.e the duration of the LED on time), I can't see how you can make this work, as it would be impossible to determine when you tell the Arduino to turn on the LED.

If you find that this time is totally repeatable to within 5mS, then you'd still need to do the same test on the Arduino latency, ie write code to send a command via bluetooth serial to the Arduino (or hard wired serial).
You'd need to record the random times on the phone, then correlate those times with the Arduino side, where you write some code that uses the microsecond timer to record the period between the commands.

i.e Neither side needs to know the real time, you just need to know how long between the random times at which you are sending the commands to the Arduino.

If you correlate these figures and see that there is far less than 5mS of variation, then all well and good, go ahead and continue with the project,

But if you continue without doing this research you'd probably be wasting your time.

And the test code is not wasted, as you can enhance it into your main code.

However if you get a fail on either of these, you'd need to go back to the drawing board until you can address the problem.

Robin2:

Nick_Pyner:
changing the camera to one under Arduino's control.

???????

The original post says

  1. Phone takes pictures

...R

Your dead right, and now is your chance to tell OP how to do that, and what a great idea it is.

BareVisions:
Thanks guys for the help. I think I should have been more clear about the objectives of the project:

  1. Phone sends signal to Arduino to light up a section of LED Board for a short amount of time(short as possible)
  2. Phone takes picture (Automatically)
  3. Phone moves onto next section of LED and repeats the process.

No chance of hearing about the purpose of this, or why the light is so brief?

they want me to port it to an Android phone

I would be a bit careful about this. It is just possible that "they" have no idea what they are talking about, or they are having a lend of you. Indeed, if they did know what they were talking about, they are more likely to send to the Android programming mafia first.

Of course the Android's shutter speed won't be able to match the speed of an expensive high speed camera, but I would like to maximize the Android's capabilities.

Android's shutter speed is not the problem unless the LEDs are moving, and I don't think you have mentioned that. In the remote event that you can get it to send commands and take pictures, the problem is much more likely to be shutter delay. My phone scratches its bum and thinks about it before it takes the picture in a manner far worse than any digital camera I have ever owned, and I imagine all phones are much the same. This to the point that taking the picture and then sending the command is probably a better option, since the delay is probably calculable. Further, practically all cameras have a delay. This to the point where you are likely to find that you have a better chance of capturing the event by shooting first, and using a slow shutter speed. One second is a shutter speed that quickly spring to mind.

Hi Roger - Thanks for the help. In the upcoming few days, I will write some dummy apps that measures this latency as you have advised. Agreed, it would be a huge waste of time to go further and figure out later the delays of the phones after programming and assembling the arduino setup.

Hi Nick - I'm a bit murky myself on why the LED array is blinking so fast on the original set up with the high speed camera, but as I said before it's not a requirement that it should be this fast, more of a goal. The LED array isn't moving so you're right, it's more about the shutter delay and not the shutter speed( I have limited knowledge of cameras sorry about that). The original project basically involved shining different portions of the LED board onto a sample and taking pictures with a camera. Basically each picture contained the sample being illuminated by a different set of LEDs on the same LED board. I'm trying to replicate as much of this with an android device.

One last question though - will communication between android and arduino be fastest with a bluetooth module or with a USB cable between the two? Or would it be irrelevant considering that the android's camera will likely lag a bit anyway before shooting the picture?

BareVisions:
Hi Nick - I'm a bit murky myself on why the LED array is blinking so fast on the original set up with the high speed camera, but as I said before it's not a requirement that it should be this fast, more of a goal. The original project basically involved shining different portions of the LED board onto a sample and taking pictures with a camera.

I think it would pay to ask. It would also pay to ask what is and why the high speed camera. There is nothing to suggest you need one and nothing high speed about 1/200 sec. It now sounds like you are not taking pictures of LED but taking pictures of something secret that is illuminated by them in different ways. It is just possible that the secret something is in an active state and the LEDs are used to get a stop-motion exposure just like an ordinary flashgun - a slow flashgun that has different light patterns.

I'm afraid none of this will actually solve the real problem, which is to get Android to work as a camera and a remote control device simultaneously.

One last question though - will communication between android and arduino be fastest with a bluetooth module or with a USB cable between the two? Or would it be irrelevant considering that the android's camera will likely lag a bit anyway before shooting the picture?

It would be irrelevant in the light of that but it occurred to me yesterday that, while I don't think you have any chance of succeeding with this, that is because I have only been thinking bluetooth, and I don't know anything about using Android via USB. Your problem is entirely Android, and you might find that USB provides better access to the inner workings thereof, and perhaps the path to success!

One last question though - will communication between android and arduino be fastest with a bluetooth module or with a USB cable between the two? Or would it be irrelevant considering that the android's camera will likely lag a bit anyway before shooting the picture?

The existing USB libraries between Android and Arduino are just Serial libraries, so will not be any faster than Bluetooth.
However it may saving you having to pair the device each time (I'm not sure how well automatic bluetooth paring works, it seems to vary depending on what BT module you are using etc )

However the latency may be lower and less variable than with BT, and you may have more control over the time it takes to send the data

See GitHub - mik3y/usb-serial-for-android: Android USB host serial driver library for CDC, FTDI, Arduino and other devices.

I've recently been playing with the USB stuff and it has a few issues getting it to compile and it didn't support the ATMega32U4 style boards, but it generally works, and you can modify the code to support 32U4 (I did)

The plus side with hard wired USB is that you can draw power for the Arduino via the USB (well its worked OK for me on the few devices I have tested). But I guess as you need to trigger banks of external LED's you will need other PSU's etc, so the powering of the Arduino probably isn't an issue.

I reckon the biggest obstacle here is the time it takes between the phone being told to take the picture and actually doing so. If the LED is being used as a substitute for a flash gun it will be very difficult to synchronize with the camera shutter.

I suggest you forget all about the Arduino for a moment and just write a short Android program to make the phone take and save pictures and study its (poor?) performance.

Why not use the phone's flash?

Maybe you could rig up the Arduino to detect the phone's flash and use that to trigger the Arduino controlled flash?

...R

Robin - I'll do that as soon as possible and find out the limitations of Android's automatic photo-taking capabilities. I don't think we could do this particular type of imaging with the phone flash.

Roger - Thanks for the advice. I think we'll just stick with bluetooth for now, and hopefully purchase a module that has automatic pairing capabilities.

Nick - I'll get back to you to find out the cause of the super quick lighting. I suspect right now that they made it that quick because the high speed camera could take pictures at that rate so they wanted to make it as quick as possible so they can image as many samples as possible.

BareVisions:
Nick - I'll get back to you to find out the cause of the super quick lighting. I suspect right now that they made it that quick because the high speed camera could take pictures at that rate so they wanted to make it as quick as possible so they can image as many samples as possible.

OK, on the strength of that and, as I warned, it could be that the "they" are either stupid, or they think you are. You can test this and/or get your own back by blinding them with science. Just ask whether by "high speed" they mean exposure, frame rate, or both and just what the numbers are. If they can't answer, they are stupid. If they can, they will know you are just as smart as they are, which is a good way to get some better and more forthcoming dialogue.

I have no idea how you control the exposure on an Android camera to take photos. I thought you just pointed it and pushed the button. However, the Android is capable of taking movies. I have no idea what the frame rate is, but somebody surely does.. Furthermore, you might find the frame rate is adjustable under certain circumstances. I doubt this can be set by the user but it may be controllable if the user can set the circumstances. You will be amazed how slow the exposure can sometimes be, but a movie camera is a high speed still camera, and electronic ones can easily deliver 60 frames a second. The slow frame exposure may be immaterial anyway.

You may find that all you need do is synchronise the Arduino with Android's movie. It is likely to be all you can do, but don't ask me how.

It occurred to my while riding over the Narrabeen bridge this afternoon that the synchronising might be quite easy if live adjustment is done with Android controlling Arduino as you should be able to see what's going on on the screen - visual feedback.

Nick_Pyner:
but a movie camera is a high speed still camera,

I seem to remember reading that (back in the day) movie cameras had relatively low shutter speeds because any blur was not noticeable in the movie. That meant that individual frames were not good for still images of moving objects.

But you do bring to mind the question of just using a "proper" camera rather than the toys that are in phones.

...R