Building easy apps using MIT App Inventor and sensors on your phone
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:
Pre-requisites
- A smartphone
- Account on MIT App Inventor
- 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.
- Screen1 is the screen that we are looking at
- 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”)
- The text field Quote will be set to a new quote whenever we shake the phone hence, no default value for it originally.
- 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!
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.
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:
- Obstacle detector using Proximity Sensor
- Telling the ambient light level of the room using Light Sensor
- Steps tracker using Pedometer
- SOS app to send current location using Location Sensor
… the list is endless and so are the bounds of your creativity and imagination.