Monday, February 1, 2016

Arduino to Android Using bluetooth

Android as display for your Arduino sensors.

Until now I have demonstrated a few times how to send commands from your Android device (phone or tablet) to an Arduino or Attiny using Bluetooth. I showed you how to command a ledstrip and a relay with our phone. And in the apotheosis of that storyline I even showed you how to do that using spoken words.

However sometimes you need information send the other way round. You can have a sensor or a switch attached to an Arduino or Attiny and want to get the data displayed on the screen of your Android phone. That's just what we are going to do in this story, and we are again using the HC-06 Bluetooth module for the communication.


The hardware

For this project I am using an Arduino Nano. It can however easily be done with an Attiny85 as we are using just a few pins (3 to be exact). 

So what I am going to show you is how to attach the HC-06 module to an Arduino Nano and use an LDR attached to an analogue input for measuring illumination.

Lets start with the schematics:



As you can see I have attached the TX line from the Arduino to the RX line of the HC-06. This way they can communiacte with eachother. 
Then at the analogue input line A6 I put a voltage divider which consists of an LDR and a 10K resistor. And that's it. 

On a breadboard it looks like this:



Not much to it. Easy to implement. In a real world environment it looks like this:



So lets have a look at the Arduino code




Not much to it. You should easily understand this.
First in the setup we set the Baudrate for communication to 9600 baud.
Then in the loop we read the sensor value from pin A6 which will have a value between 500 en 1023 and translate that in a figure from 1 to 100.
Remember Serial.Print wil print a line and the cursor wil stay on that line. Serial.println wil print something and moves on to the next line.

An added bonus for testing purposes is that when you power the Arduino from USB out of your computer you can open the Serial monitor in the Arduino IDE and see the values the Nano will send to the Bluetooth module.

Next step is the Android code.



Again the APP for Android was devellopped in Mitt's App-Inventor. A great free tool for making APP's. Highly recommended.


First design the screen:




The top line does not display well on my computer screen when designing in App Inventor but on the Android screen it looks great. App inventor has a real-time vieuw. So you can see what's happening on your phone/tablet screen when you alter the code you are writing.


So the App has a Clock and a Bluetooth feature and just a Listpicker (which chooses your Bleutooth device) a Button and a Label. That's all. The Vertical Arrangements are used to put everything nicely lined up on your screen.


The codeblocks are even simpler:



In the listpicker we choose the Bluetooth device.

And then there is a trick. We use the Clock function to periodically check if any information is send over Bluetooth. If there is any information coming in we just simply alter the text from the Button in whatever we are receiving over Bluetooth.



So does it work. Yes it does indeed:




The text on the display is not very clear in thsi picture but it says that the value read over Bluetooth is 76. When you move your hand over the LDR the value will get lower. And when you shine a light on the LDR you will get a higher value.


Now this version of Bluetooth has a range of about 10 meter. Enough for a lot of purposes like outdoor temperature measurements etc. I am sure you will find purposes for this.


Rests only to give you the APP and the source codes so you can alter them for your own purposes.


Bluetooth receiver APP


Bluetooth receiver source for App Inventor


Arduino code


So till next time: have fun


Luc Volders