fbpixel
Create an app with App inventor 2

Create an app with App inventor 2

Learn how to create an application for Android very simply using App Inventor 2.

In the fields of robotics and connected objects, one of the most interesting features to implement is the remote control of an electronic project. Whether to display measurements from his home or to drive a drone, a possible solution is to develop a smartphone application and communicate with a microcontroller using a Bluetooth connection. This App can be quickly created using MIT’s online software, App Inventor 2 (AI2).

Material

  • Computer
  • Android smartphone with a QR code reader app
  • Arduino UNO
  • Bluetooth HC-06
  • Dupont F / M Cable

Creating AI2 application

In this example, we create an application to enable a function (ON / OFF) and retrieve a sensor measurement from an Arduino board. This tutorial can be applied for many other applications.

Register on the App Inventor 2 website .

Create a new project named myApp

The designer screen of the project appears

AI2 Application Configuration

To create the design of our application, we use the objects available under the tabs in the drop-down menu on the left of the screen and modify their parameters that appear on the right of the screen.

In User interface, add a label object and rename it titleLbl. It will contain the title of the application.

In layout, add a VerticalArrangement object that allows you to align objects vertically.

In VerticalArrangement, place a label btStatusLbl and then a ListPicker btBtn that will be used to manage the Bluetooth connection.

Then add a label named sensorLbl that will contain the value received from the Arduino board.

Add a HorizontalArrangement that allows you to align objects horizontally and that will contain the buttons to activate the Arduino function.

In the HorizontalArrangement, add two buttons named onBtn and offBtn.

Finally, in Connectivity, add a BluetoothClient object that will allow us to interact with the Bluetooth device.

And in Sensor, add two Clock objects which will help receive and send data at a regular time interval.

You can customize your application as you like by using different colours, fonts or images.

AI2 Application Programming

AI2 uses Scratch to program application which makes it possible to create a program in the form of block diagram. It is very easy to use but requires some programming logic.

All functions are available by clicking on the objects in the drop-down menu on the left of the screen. Browse the functions of the objects added to the app designer as well as those of the objects Variables, Text, Math and Control that we will use.

Create a list of available Bluetooth modules on the device.

Manage establishment of the connection and change the label according to the connection status.

A global variable “command” is defined in order to keep in memory the value given by the last button pressed (ON or OFF).

The value of command is sent via Bluetooth, if it is connected, at a frequency defined by the object Clock1.

To recover the value of the sensor sent from Arduino, we check if data is available in the buffer with a frequency defined by Clock2. We then display this value in sensorLbl.

Complete Code

Once the blocks are placed and configure properly, the code should look loke this

Compile and share your Application

Once this program in Scratch is correctly configured and displays no errors, install a QR code reader application and compile the program using the menu Build> App program (provide QR code for .apk)

Once the QR code has been generated, you can download and install the application on your Smartphone.

You can share the installer .APK so that other can install your App on an Android phone. You can share your project as a AIA file so that other can use and modify your application.

Export to create the AIA file on your computer

Import to open a AIA file fro your computer to the online software

Arduino Code

Here is the code to upload to the Arduino board to receive and send data to the serial port.

#include <SoftwareSerial.h>

SoftwareSerial hc06(2,3);

String cmd="";
float sensor_val=0;

void setup(){
  //Initialize Serial Monitor
  Serial.begin(9600);
  //Initialize Bluetooth Serial Port
  hc06.begin(9600);
}

void loop(){
  //Read data from HC06
  while(hc06.available()>0){
    cmd+=(char)hc06.read();
  }

  //Select function with cmd
  if(cmd!=""){
    Serial.print("Command recieved : ");
    Serial.println(cmd);
    // We expect ON or OFF from bluetooth
    if(cmd=="ON"){
        Serial.println("Function is on");
    }else if(cmd=="OFF"){
        Serial.println("Function is off");
    }else{
        Serial.println("Function is off by default");
    }
    cmd=""; //reset cmd
  }
  // Simulate sensor measurement
  sensor_val=(float)random(256); // random number between 0 and 255
    
  //Write sensor data to HC06
  hc06.print(sensor_val);
  delay(100);
}


Result

We can activate and deactivate the function in the Arduino code and receive data from a sensor.

Do not hesitate to leave a comment or to send us a message if you need more information on how to create a Smartphone Application.

Applications

Sources

Measure your home temperature on your smartphone

Measure your home temperature on your smartphone

One interesting branch of robotics is making existing objects smarter which helps in our everyday life. This domain is known as domotics (especially for home) and IoT (Internet of Things)

Here is a small domotic project that consists in measuring your home temperature and collect the data on your smartphone. It is a simple project in which we use Bluetooth module, serial communication with a way to deal with multidata and a smartphone Android application made withh App Inventor 2.

Material

  • Smartphone
  • Arduino UNO
  • 5V Battery
  • Temperature sensor LM35
  • Bluetooth module HC-06

Wiring

home temperature measurement schematics

N.B.: The module displayed in Fritzing is a HC-05 but it has the same pinout as HC-06.

Code

We search the datasheet of the sensor LM35 to find the temperature range and the conversion rule between voltage and temperature. To be able to convert the measured voltage into a physical value. Once the temperature is measured it is sent to the smartphone via the Bluetooth module. To deal with multi-data, the data are concatenated into a String with a separator which make it easier to decipher.

#include <SoftwareSerial.h>                          
SoftwareSerial HC06(2,3); 

// Constants
#define DELAY 1000 // Delay between two measurements in ms

// Parameters
const int sensorPin = A0; // Pin connected to sensor

// Variables
float voltage, temperature;

void setup(void) {
  Serial.begin(9600);
  HC06.begin(9600);
}

void loop(void) {
  voltage= analogRead(sensorPin) * (5.0 / 1023.0); // Convert digital value to voltage
  temperature=100*voltage; // conversion from V to °C
  
  Serial.print("Sensor reading = ");
  Serial.println(temperature); // the temperature reading

  // Send voltage and temperature value to app
  HC06.print(voltage);
  HC06.print("x");
  HC06.print(temperature);
  
  delay(DELAY);  
}

Home Measurement App

We created a simple application using App inventor 2.The data from the Arduino is sent as a text to the Android application. To obtain the values from the sensor, the text need to be split according to the separator “x”. The resulting list items are then displayed in the corresponding labels.

android app inventor 2 for home measurement
app inventor 2 home measurement scratch

The Bluetooth module must be stored in the list of Bluetooth devices in your phone. The Bluetooth module HC-05 or HC-06 should paired once before using the app.

You can find the aia project in the ZIP file here.

If you want more information on this project or if you think of one related project that should be on this site, do not hesitate to leave a comment or to send us a message.

Source

Learn how to read sensor value

Communicate with HC-06 Bluetooth module

Create an Android App using App inventor 2

Remote controlled robot

Remote controlled robot

One of the coolest feature in a robot is to be able to control it with your smartphone. Building a remote controlled robot can be easily achieved using Arduino and a Bluetooth module.

 

Material

  • Mobile Robot Rovy
  • 4x TTGM
  • DC Motor driver (here we use a Arduino Mega prototyping shield and 2xHBridge SN754410)
  • Battery 7,4V
  • Bluetooth module HC-06
  • Arduino Mega
  • Smartphone Android

 

Structure

In this project, we used a 4-wheeled mobile robot Rovy but this project can be applied to any programmable mobile robot.

Hardware

  • Board

The board need to have enough inputs/outputs to drive 4 DC motors with H-bridge. Here we used an Arduino Mega but an UNO would be enough.

  • DC motor driver

To be able to control the speed and direction of a DC motor, H-bridges are usually used such as SN754410.

  • HC-06

To communicate with the robot we need a serial port. We could use a USB cable to control the robot from the computer but as the robot is mobile it is much nicer to use a wireless device.

Schematics

rovy arduino bluetooth schematics

Interface

A simple smartphone interface can be created using Processing or App Inventor 2. The interface has four buttons that represent directions of the robot and one button to select the Bluetooth device. You can download the App project on this link.

When pushing on a button a corresponding command is sent via Bluetooth.

Button

Command

Movement

Upper Arrow

“F”

Forward

Left Arrow

“L”

Turn left

Right Arrow

“R”

Turn right

Lower Arrow

“B”

Backward

“S”

Stop

If no button is pressed then the interface send the command “S” (Stop).

Software

The software can be devided in simpler steps: Reading command from bluetooth, select robot functionnality depending on the command and apply motors control depending on the functionnality.

Read the command from the bluetooth module

To learn how to connect and to use HC-06 check this page.

  if (Serial3.available()>=MESSAGE_BYTES)
   { // If data is available to read
    delay(3);
    if( Serial3.read() == HEADER)
    {
     state = (char)Serial3.read(); // read it and store it in val
    }
   }
   Serial.print("Recieved : ");
   Serial.println(state);

 

Select robot function depending on received command

Note on Arduino code and keayword switch.

   movementEnable();
   switch(state){
     case 'F':
      GoForward(Power);
      Serial.println("Robot walking");
      break;
    case 'B':
      GoBackward(Power);
      Serial.println("Robot backing");
      break;
    case 'R':
      TurnRight(Power);
      Serial.println("turn right");
      break;  
    case 'L':
      TurnLeft(Power);
      Serial.println("turn left");
      break;  
    default://S
      Stop();
      Serial.println("resting");
    break;
  }

 

Control DC Motors to perform the selected movement

Learn how to drive a DC motor here.

void movementEnable(){
    digitalWrite(enableBridge1,HIGH); 
    digitalWrite(enableBridge2,HIGH);
    digitalWrite(enableBridge3,HIGH);
    digitalWrite(enableBridge4,HIGH);
}

void movementDisable(){
    digitalWrite(enableBridge1,LOW); 
    digitalWrite(enableBridge2,LOW);
    digitalWrite(enableBridge3,LOW);
    digitalWrite(enableBridge4,LOW);
}


void GoForward(int Power){
  analogWrite(MotorForward1,Power);
  analogWrite(MotorReverse1,0);
  analogWrite(MotorForward2,Power);
  analogWrite(MotorReverse2,0);
  analogWrite(MotorForward3,Power);
  analogWrite(MotorReverse3,0);
  analogWrite(MotorForward4,Power);
  analogWrite(MotorReverse4,0);
}

void GoBackward(int Power){
  analogWrite(MotorForward1,0);
  analogWrite(MotorReverse1,Power);
  analogWrite(MotorForward2,0);
  analogWrite(MotorReverse2,Power);
  analogWrite(MotorForward3,0);
  analogWrite(MotorReverse3,Power);
  analogWrite(MotorForward4,0);
  analogWrite(MotorReverse4,Power);
}

void TurnRight(int Power){
  analogWrite(MotorForward1,Power);
  analogWrite(MotorReverse1,0);
  analogWrite(MotorForward2,0);
  analogWrite(MotorReverse2,Power);
  analogWrite(MotorForward3,Power);
  analogWrite(MotorReverse3,0);
  analogWrite(MotorForward4,0);
  analogWrite(MotorReverse4,Power);
}

void TurnLeft(int Power){
  analogWrite(MotorForward1,0);
  analogWrite(MotorReverse1,Power);
  analogWrite(MotorForward2,Power);
  analogWrite(MotorReverse2,0);
  analogWrite(MotorForward3,0);
  analogWrite(MotorReverse3,Power);
  analogWrite(MotorForward4,Power);
  analogWrite(MotorReverse4,0);
}

void Stop(){
  analogWrite(MotorForward1,0);
  analogWrite(MotorReverse1,0);
  analogWrite(MotorForward2,0);
  analogWrite(MotorReverse2,0);
  analogWrite(MotorForward3,0);
  analogWrite(MotorReverse3,0);
  analogWrite(MotorForward4,0);
  analogWrite(MotorReverse4,0);
  movementDisable();
}

Complete code

 

#include <Servo.h>
#include <SoftwareSerial.h>                              
//SoftwareSerial HC06(2,3);                              // If board different from Mega
// Motors definition
const int enableBridge1 = 22;
const int enableBridge2 = 23;
const int enableBridge3 = 24;
const int enableBridge4 = 25;

const int MotorForward1 = 11;
const int MotorReverse1 = 10;
const int MotorForward2 = 8;
const int MotorReverse2 = 9;
const int MotorForward3 = 7;
const int MotorReverse3 = 6;
const int MotorForward4 = 4;
const int MotorReverse4 = 5;

//Serial port
#define HEADER        '|'
#define MESSAGE_BYTES  2  // the total bytes in a message
char state='S';

// Parameters
int Power = 80; //Motor velocity


/******************************************************************\
* PRIVATE FUNCTION: setup
*
* PARAMETERS:
* ~ void
*
* RETURN:
* ~ void
*
* DESCRIPTIONS:
* Initiate inputs/outputs
*
\******************************************************************/

void setup(){
  Serial3.begin(9600);
  Serial.begin(9600);
  delay(500);
}

/******************************************************************\
* PRIVATE FUNCTION: loop
*
* PARAMETERS:
* ~ void
*
* RETURN:
* ~ void
*
* DESCRIPTIONS:
* Main Function of the code 
\******************************************************************/

void loop(){
 
  if (Serial3.available()>=MESSAGE_BYTES)
   { // If data is available to read
    delay(3);
    if( Serial3.read() == HEADER)
    {
     state = (char)Serial3.read(); // read it and store it in val
    }
   }
   Serial.print("Recieved : ");
   Serial.println(state);

   movementEnable();
   switch(state){
     case 'F':
      GoForward(Power);
      Serial.println("Robot walking");
      break;
    case 'B':
      GoBackward(Power);
      Serial.println("Robot backing");
      break;
    case 'R':
      TurnRight(Power);
      Serial.println("turn right");
      break;  
    case 'L':
      TurnLeft(Power);
      Serial.println("turn left");
      break;  
    default://S
      Stop();
      Serial.println("resting");
    break;
  }
     
}


/******************************************************************\
* PRIVATE FUNCTION: movementEnable
*
* PARAMETERS:
* ~ void
*
* RETURN:
* ~ void
*
* DESCRIPTIONS:
*   Enable motor control   
\*****************************************************************/
void movementEnable(){
    digitalWrite(enableBridge1,HIGH); 
    digitalWrite(enableBridge2,HIGH);
    digitalWrite(enableBridge3,HIGH);
    digitalWrite(enableBridge4,HIGH);
}

void movementDisable(){
    digitalWrite(enableBridge1,LOW); 
    digitalWrite(enableBridge2,LOW);
    digitalWrite(enableBridge3,LOW);
    digitalWrite(enableBridge4,LOW);
}

/******************************************************************\
* PRIVATE FUNCTION: GoForward
*
* PARAMETERS:
* ~ int Power motor velocity
*
* RETURN:
* ~ void
*
* DESCRIPTIONS:
*        
\*****************************************************************/


void GoForward(int Power){
  analogWrite(MotorForward1,Power);
  analogWrite(MotorReverse1,0);
  analogWrite(MotorForward2,Power);
  analogWrite(MotorReverse2,0);
  analogWrite(MotorForward3,Power);
  analogWrite(MotorReverse3,0);
  analogWrite(MotorForward4,Power);
  analogWrite(MotorReverse4,0);
}

/******************************************************************\
* PRIVATE FUNCTION: GoBackward
*
* PARAMETERS:
* ~ int Power motor velocity
*
* RETURN:
* ~ void
*
* DESCRIPTIONS:
*        
\*****************************************************************/

void GoBackward(int Power){
  analogWrite(MotorForward1,0);
  analogWrite(MotorReverse1,Power);
  analogWrite(MotorForward2,0);
  analogWrite(MotorReverse2,Power);
  analogWrite(MotorForward3,0);
  analogWrite(MotorReverse3,Power);
  analogWrite(MotorForward4,0);
  analogWrite(MotorReverse4,Power);
}

/******************************************************************\
* PRIVATE FUNCTION: TurnRight
*
* PARAMETERS:
* ~ int Power motor velocity
*
* RETURN:
* ~ void
*
* DESCRIPTIONS:
*        
\*****************************************************************/

void TurnRight(int Power){
  analogWrite(MotorForward1,Power);
  analogWrite(MotorReverse1,0);
  analogWrite(MotorForward2,0);
  analogWrite(MotorReverse2,Power);
  analogWrite(MotorForward3,Power);
  analogWrite(MotorReverse3,0);
  analogWrite(MotorForward4,0);
  analogWrite(MotorReverse4,Power);
}

void TurnLeft(int Power){
  analogWrite(MotorForward1,0);
  analogWrite(MotorReverse1,Power);
  analogWrite(MotorForward2,Power);
  analogWrite(MotorReverse2,0);
  analogWrite(MotorForward3,0);
  analogWrite(MotorReverse3,Power);
  analogWrite(MotorForward4,Power);
  analogWrite(MotorReverse4,0);
}

void Stop(){
  analogWrite(MotorForward1,0);
  analogWrite(MotorReverse1,0);
  analogWrite(MotorForward2,0);
  analogWrite(MotorReverse2,0);
  analogWrite(MotorForward3,0);
  analogWrite(MotorReverse3,0);
  analogWrite(MotorForward4,0);
  analogWrite(MotorReverse4,0);
  movementDisable();
}



 

Results

 

If you want more information on this project do not hesitate to leave a comment or to send us a message.

 

Sources

Learn how to program with Arduino

Drive a DC motor using Arduino

Configure and connect HC-06

https://www.aranacorp.com/en/control-a-dc-motor-with-arduino/

Rovy