Building easy apps using MIT App Inventor and sensors on your phone

Shreya Sinha
4 min readJan 30, 2021

--

When elementary and high school students are introduced to the concept of “Sensors” in their science classes, it sometimes becomes difficult for them to visualize and anticipate the possible uses of the sensors. This article is an attempt to enable students to try out various sensors hands-on with a smartphone and MIT App Inventor.

MIT App Inventor is an intuitive, visual programming environment that allows everyone — even children — to build fully functional apps for smartphones and tablets. The blocks-based tool facilitates the creation of complex, high-impact apps in significantly less time than traditional programming environments. The project seeks to democratize software development by empowering all people, especially young people, to move from technology consumption to technology creation.

The MIT App Inventor platform provides support for the following sensors:

Screenshot of the list of sensors available on MIT App inventor
Screenshot of the list of sensors available on MIT App inventor. Users can click on the “?” icon next to the names of the sensors to get a detailed description of what each of the sensors does and the configuration.

Pre-requisites

  1. A smartphone
  2. Account on MIT App Inventor
  3. Beginner knowledge of various components of MIT App Inventor (trust me — it is super easy! Refer to beginner level tutorials or YouTube videos if you get stuck anywhere :) )

What are we building?

We will build a “Quote Shaker” app. On opening the app, whenever we shake our phone, the app will display a new helpful quote. This app uses the accelerometer sensor present in the smartphone to detect the “shaking” motion of the phone so that it can display a new random quote every time the phone is shaken.

About the sensor

We are using the AccelerometerSensor available commonly in smartphones.

AccelerometerSensor is a component that can detect shaking and measure acceleration approximately in three dimensions using SI units (m/s²).

You can find more details about how the sensor behaves (when the phone is tilted right, left, forward, backward or when it is shaken with different velocities) on the MIT App inventor portal.

Setting up the layout

The following image shows a simple layout that we want for our app.

Layout of the app
  1. Screen1 is the screen that we are looking at
  2. VerticalArrangement1 and VerticalArrangement2 are the components which hold the texts for Heading (default value “Quotes Shaker”) and Subtext (default value “Shake your phone to get a new quote”)
  3. The text field Quote will be set to a new quote whenever we shake the phone hence, no default value for it originally.
  4. AccelerometerSensor1 is a non-visible component of our app i.e. it will not be visible to the user on the app but we will still be used in our app(to detect shaking)

PS: This is just a simple layout. You can get as creative as you want. Explore the other User Interface and Layout components and make your app prettier.

Using the sensor in our code

Set up the code block as seen in the screenshot below. You can add any and as many quotes that are your favourites!

Code block

The above code in easier words says — whenever the “AccelerometerSensor” (on your phone) is “Shaking”, set the “text” of the “Quote” field/label (that we created in the layout) to any random item from the list of quotes that we have provided.

Testing the app

Woohoo! This is all it takes to build a simple app using the sensors in our phones. Our app is now ready to test.

  1. Do a live test of your app to see if it is working fine
  2. Share your app with your friends so that they can try it out on their phones
This is how are app changes screens when the phone is shaken

What next?

You can try building more apps with other sensors that are supported. Some ideas on more sensors which can be explored and other apps that can be built are:

  1. Obstacle detector using Proximity Sensor
  2. Telling the ambient light level of the room using Light Sensor
  3. Steps tracker using Pedometer
  4. SOS app to send current location using Location Sensor

… the list is endless and so are the bounds of your creativity and imagination.

--

--