element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Members
    Members
    • Benefits of Membership
    • Achievement Levels
    • Members Area
    • Personal Blogs
    • Feedback and Support
    • What's New on element14
  • Learn
    Learn
    • Learning Center
    • eBooks
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • Experts & Guidance
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Dev Tools
    • Manufacturers
    • Raspberry Pi
    • RoadTests & Reviews
    • Avnet Boards Community
    • Product Groups
  • Store
    Store
    • Visit Your Store
    • 'Choose another store...'
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
Personal Blogs
  • Members
  • More
Personal Blogs
Legacy Personal Blogs MIT App Inventor creating Android App to control Arduino via Bluetooth - Corrected 18 January 2015
  • Blog
  • Documents
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Blog Post Actions
  • Subscribe by email
  • More
  • Cancel
  • Share
  • Subscribe by email
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: neilk
  • Date Created: 14 Jul 2014 6:14 PM Date Created
  • Views 3031 views
  • Likes 3 likes
  • Comments 29 comments
  • android
  • bluetooth
  • hc06
  • mit
  • appinventor
  • led
  • arduino
Related
Recommended

MIT App Inventor creating Android App to control Arduino via Bluetooth - Corrected 18 January 2015

neilk
neilk
14 Jul 2014

App Inventor, Bluetooth and Arduino - Part 1

 

MISSING IMAGES RESTORED 5 FEBRUARY 2016

 

I was enthused by Pieter Bok’s post  involving an iPhone App connecting to an Arduino via WiFi:

 

http://www.element14.com/community/videos/12327/l/arduino-based-automatic-irrigation-system-for-pot-plants

 

I don’t have an iPhone, or a WiFi shield, but Bluetooth was mentioned in the thread,  so I Googled around for Android, Arduino and Bluetooth, finding several links which made use of an amazing, FREE resource from MIT called App Inventor, for developing Android Apps:

 

http://appinventor.mit.edu/explore

 

This link is to version 2 of App inventor. Several of the Arduino related links, were for App Inventor version 1, which is still available, although not for long, so you are advised not to start using it.

 

App Inventor 2 is entirely web-based, and includes the ability to connect to your target Android device via Wifi and run an emulation of your App in real time - the display on your device changes as you change the design in App Inventor! If you don’t even have an Android device, you can run an emulator on your PC and App Inventor will connect to it.

 

App Inventor 2 has two screens - Design and Blocks. In Design, you drag and drop components such as buttons, labels, text boxes, sliders, etc, on to a representation of your Android device screen and arrange them as you want them to appear.

 

In the Blocks screen, you drag and drop representations of code segments which relate the display components together and ultimately generate your app. Once you get into it, it’s actually very straightforward.

 

Initially, I got a little bogged down because I tried to follow a tutorial based on version 1 of App Inventor and I couldn’t get it to work. I thought it was problem with the code, but actually it was me! Because I was new to Bluetooth, I hadn’t realised that I had to pair the devices first! I’m learning fast!

 

Using this video tutorial  for version 2, to send data to the Arduino, I launched myself:

 

https://www.youtube.com/watch?v=6o_QVlltNgM

 

This Tutorial shows how to build an App which connects to an Arduino, via Bluetooth - I used a JY-MCU HC06 board. The Arduino sketch to receive commands from the App to turn an LED on and off is also supplied.

 

There seems to be some confusion about whether the JY-MCU HC06 Bluetooth board can accept a 5v Tx signal from the Arduino into its Rx pin. My JY-MCU board is clearly marked as expecting a 3.3v signal level, so I used a couple of resistors to make a simple voltage divider.

 

It was also said to be important that you disconnect the Bluetooth board from Arduino pins 0 and 1 when uploading a sketch. However, I forgot once and it didn’t  seem to make any difference!

 

You can have the IDE Serial Monitor open whilst the Bluetooth is working and see exactly what’s happening. In fact, you can test the sketch without the Bluetooth connected, by typing in the relevant characters on the keyboard and sending via the Serial Monitor.

 

In the Design screen, below, you can see the depiction of an Android device screen:

 

image

 

On the left of the screen is a list of the components which can be dragged and dropped onto the Android screen.

To the immediate right  of the Android screen is a list of the current screen components: a ListPicker, two Buttons in a Horizontal Arrangement and a “hidden” Label, hidden because it is initially blank. There is also a non-visible bluetooth client at the bottom of the screen.

 

To the far right of the Android screen are the properties of the currently selected component - in this case the Label, Label1 is selected.

 

If we click on the Blocks Tab and then click on the ListPicker1 component on the left hand side of the screen. A scrollable list of code blocks relevant to the ListPicker1 appears. Click  on “When ListPicker1 BeforePicking” and it will be placed onto the Blocks screen area.

 

 

image



We can now select further sub-Blocks to create the first full code Block, below..

 

This allows List Picker1 to display the available (already paired) Bluetooth devices: - This next code block has been corrected 18 January 2015


image

 

The next Block allows ListPicker1 to select and connect to the desired Bluetooth device and to set the “hidden” Label, Label1 to read “CONNECTED”:

 

image

 

I’ve then set the default Text of Label1 to be “NOT CONNECTED” and the colour of Label1 to be Red in the Design Screen; clearly, Label1 is now no longer “hidden”.

 

I’ve also added a further component into the code Block, to set the Text colour of Label1 to be Green when the Bluetooth client is connected.

 

The next 2 Blocks send the text string “on” when Button1 is pressed and the text string “off” when Button2 is pressed.



image
Finally, I’ve  added a third Button in the Design screen with a Text value of “DISCONNECT”and a fifth Block of code which disconnects the Bluetooth client, sets Label1 text colour to Red and Label1 text to read “DISCONNECTED”

 

 

 


image

The Arduino sketch is  straightforward:

 

int ledPin = 13;

String readString;

 

void setup()

{

  Serial.begin(9600);

  pinMode(ledPin, OUTPUT);

}

void loop()

{

  while ( Serial.available() )

{    // While there is data in the buffer

    delay( 3 ); 

    char c = Serial.read();

    readString += c;         // build the string - “on” or “off”

}

  if ( readString.length() >0 )

   {

    Serial.println( readString );

    if ( readString == "on" )

     {

      digitalWrite( ledPin, HIGH) ;

      }

    if ( readString == "off" )

     {

      digitalWrite( ledPin, LOW );

      }

    readString="";

   }

}

 

 

 

Obviously, the sketch could be simplified if the App merely sent, say ,“1” for ON and “0” for off, instead of “on” and “off”.

 

 



Attachments:
Led_Control.zip
  • Sign in to reply

Top Comments

  • dougw
    dougw over 9 years ago +1
    Thanks for posting this tutorial Neil. App Inventor is what I used to develop the Bluetooth android app that controls the Henrietta Project. I can vouch for the ease of use and ease of learning this development…
  • Former Member
    Former Member over 9 years ago +1
    A really nice initiative Neil! Congrats! I really appreciate the youtube tutorial it's very informative. The reason why I chose a Etnernet shield + nano router (equals "WIFI" shield ;-) ) is that I can…
  • Former Member
    Former Member over 8 years ago +1
    G'day, Nice project, and it will be my first app. Any chance of the source aia file. Grant
  • neilk
    neilk over 7 years ago in reply to Former Member

    Hi indrachandsah If you look at this blog post:

     

    https://www.element14.com/community/people/neilk/blog/2014/09/05/app-inventor-bluetooth-and-arduino-part-3-android-slider-controls-led-brightness

     

    You will see how to create an App which uses a slider to control the brightness of an Arduino connected LED over Bluetooth.

     

    As far as using an Arduino to control an AC Fan using the moc3021 is concerned, I'm afraid I have no expertise in this area. I suggest you post a NEW question on theis topic. I am sure there will be a number of colleagues who can help.

     

    Neil

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Former Member
    Former Member over 7 years ago

    dear sir , please help me how to control Ac fan regulation using zero crossing 

      detector. sir i'm using moc3021 optocoupler triac driver.please give

      me arduino code & slider app code.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • neilk
    neilk over 7 years ago in reply to Former Member

    Hi sarada

     

    If you look at this Blog:

    https://www.element14.com/community/people/neilk/blog/2014/07/18/app-inventor-and-arduino-part-2--send-data-from-arduino-to-android-app

     

    You will see that it shows how an android App can receive data from an Arduino, via Bluetooth module - HC06 in this case.

    This should point you in the correct direction.

     

    I hope this helps

     

    Neil

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Former Member
    Former Member over 7 years ago

    hi neil ,

    happy to see you helping the needy ones.noble work.

    i am in need of your help.

     

    my college project includes building of android app. and i don't know anything about it. Somehow i came to know about mit app inventer 2.

    can u help me.

    i was to receive 0 or 1 from hc 05 BT module and if app. receive 1 the app. must show "Alert" & if 0 is received "normal" must show.

     

    can you help me with build block of mit app inventer2

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • neilk
    neilk over 7 years ago in reply to gwideman

    Hi gwideman I have restored the missing images. Hope you find the post useful

     

    clem57 - thanks for the tip

     

    Neil

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2023 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube