Introduction: Using MIT App Inventor to Control Arduino - the Basics

We all have a smartphone. Now, with that statement of the obvious, let me ask you this. Why is your Arduino hardly ever connected to your smartphone. Bluetooth costs about $8 to implement on an arduino. The main issue is that some people get hung-up on the issue of programming bluetooth, so they just keep the usb cord. So here is the absolutely most basic things you need to use bluetooth with android and arduino.

This tutorial will go through the bare minimum you need to create an connection between a custom android app you make with MIT App Inventor and the Arduino.

All code and references provided are based on the code from the LittleArm Arduino robot arm.

Step 1: The Bluetooth Module

The most common bluetooth module today is the HC-06 or HC-05. It costs about 7-10 dollars online. Look at the image below to see how to hook it to the Arduino.

That is pretty easy right? Just four jumper wires and you now have wireless serial connectivity to your arduino. Now, if you have a program where you have been sending commands to the arduino with the Serial Monitor you can now do that wirelessly with a bluetooth device. (Note: You can upload programs to the arduino via bluetooth, but that is significantly more complicated than what we want to achieve here.)

Step 2: Arduino Code

Some people think that that when you replace the cord with bluetooth that you have to change your code. THEY ARE WRONG. Relax, if your code works with the USB cord than you do not have to change a thing. As I said, bluetooth is basically the same thing as USB in code syntax. The Arduino's Serial.read is just fine with the same code.

Here is the code that is used with the LittleArm Arduino robot Arm.

//////Other Intiations here</p><p>void setup(){ 
  Serial.begin(9600);
  baseServo.attach(5);        // attaches the servo on pin 5 to the servo object 
  shoulderServo.attach(4);
  elbowServo.attach(3);
  gripperServo.attach(2);
  
  Serial.setTimeout(50);      //ensures the the arduino does not read serial for too long
  Serial.println("started");
  baseServo.write(90);        //intial positions of servos
  shoulderServo.write(100);
  elbowServo.write(110);
  ready = 0;
}//primary arduino loop<br>void loop() 
{ 
  if (Serial.available()){
    ready = 1;
    desiredAngle.base = Serial.parseInt();
    desiredAngle.shoulder = Serial.parseInt();
    desiredAngle.elbow = Serial.parseInt();
    desiredGrip = Serial.parseInt();
    desiredDelay = Serial.parseInt();</p><p>    if(Serial.read() == '\n'){              // if the last byte is 'd' then stop reading and execute command 'd' stands for 'done'
        Serial.flush();                     //clear all other commands piled in the buffer
        Serial.print('d');                  //send completion of the command
    }
  }

This snippet of code is the read and return code. Notice that it reads information from the Serial port just as if there were a usb cable connected. There is no need to initiate ports for the Bluetooth, they are naturally activated.

Step 3: App Inventor

The basics of app inventor are easy. If you know how to code you can struggle through the basic set-up and operation. So just fiddle around until you have a button or slider bar that you want to use then you can add in this bluetooth code. In the context of the tutorial the app is that of the LittleArm Arduino Robot Arm, which has multiple sliders. But we will focus on just one.

Step 4: App Inventor: Add Bluetooth to Your App

Just click and drag the bluetooth icon into your app.

Step 5: App Inventor: Create a ListPicker

Create a listpicker so that you can use to find and select the bluetooth devices that are paired with the smartphone.

When the listpicker is open, then a selection should trigger some event. In this case, a Bluetooth connection to the device listed. (Ignore the last three green blocks those are specific "trees" in the forest that you don't have to have.)

Step 6: Send Data to Arduino

Assuming that the connection is successful the light on the HC-06 bluetooth module will become solid. That is good, your device can now talk to the arduino. So to send something to the arduino, just select the type of data you want to send. Is it numbers, letters, bits, bytes, etc. Then select the .SendXXXX operation from the bluetooth menu in AppInventor and stick your data in. In this case for the LittleArm we use .SendText because we have text characters, in this case commas, in the data. If it was numerical we would use SendNumbers and so on.

You may notice that the function that the send command is inside of, is linked to one of the slider bars in the app. This means that the the Send is linked to an "event." i.e. when the slider is moved it automatically calls this function. Not to bad, right?

Step 7: Recieve Data From Arduino

Now, you are not always sending data to the Arduino. Sometimes you want to get something back. In fact, if you want to have reliable connections, you should require something back. A parity character, if you will. Something that signals that all of the data was received and the arduino is ready to get more. In this case we use the letter 'd.' D is for Done. Now you could change your arduino code to send the value of a sensor or anything else. But it is good practice and much more reliable to send every data set with an signal character to show that all the data was sent and received.

To read this data being sent ('d') just again go through the bluetooth options until you find the type of data that you want to wait for. The character 'd' in this case is simply text in the form of a single byte, so we allow the scanning of only one Byte. If it is a "d" then we know that the arduino read the signal we sent and that we can send another.

Step 8: Enjoy Your App

And that is all there is to it. You now have an app, that will connect with a bluetooth device and then send some kind of data to the Arduino and receive some kind of data via serial communications. Now you can control almost any arduino project with your android smartphone. Enjoy.

You can see a demo of the app that all of this code was taken from in this video.