Jotrin Electronics
비고 합계 (USD) 운영
제품과 함께 쇼핑 카트
제품과 함께 쇼핑 카트 : 0
> MEMS / sensing technology > Arduino based cell phone sensor application

Arduino based cell phone sensor application

업데이트 시간: 2021-08-30 12:19:19

Sensors are one of the crucial things when building a project. Smartphones have most of the basic sensors built in like proximity, accelerometer, light, magnetometer, gyroscope etc. and we can actually use these sensors in our Arduino projects.

So in this project, I will show you how these sensors are used and can communicate with Arduino through wired connections.

Parts list.

  • Arduino

  • USB cable (Type B)

  • OTG cable

  • OLED display module

Step 1: Setting up Serial Communication

The serial communication between Arduino and android is the same as the serial communication between the computer and Arduino. The sender sends data bit by bit and the receiver receives it bit by bit.

Serial communication

  • Login to MIT App Inventor and start a new project

  • To establish a serial connection, we need to include Serial from the Connections tab, drag and drop the item

  • Make sure the buad rate is the same as the buad rate used to initialize serial in Arduino

  • Import two buttons and connect and disconnect from Arduino and a checkbox to visually indicate that the device is connected. (You can rename the component and change the text)

  • Add two more buttons to turn the LED on and off

  • Let's block code in the block panel, where we find the various events associated with the various components

  • Placing the click event for each button

  • When the Connect button is pressed, it initializes the serial connection and opens a serial connection, and if it opens successfully, it checks the checkbox

  • When the disconnect button is pressed, it closes the serial communication and unselects the checkbox

  • Now, as soon as the on button is pressed, it sends a data byte representing 1, which is only turned on when the serial communication is on

  • The same thing happens when the off button is pressed, but it will send a 0 instead of a 1, indicating that the LED should be off

  • That's all there is to the design

  • Now save the project

  • Download the apk file of our application via QR code or from the build tab

  • Download the app on your phone and install the app using the apk

  • The phone may say that the source of the app may damage your phone, but it is safe to install it anyway

Arduino code

  • First, we declare a variable to store the LED state, then initialize the serial communication in the setup and define pin 13 as an output since we will be using the built-in LED

  • Then in a loop, we check if the phone is sending any data, and if so, wait about 400 milliseconds to get things to stabilize

  • Then parse the incoming data - what it does is check if there is any int type data available and wait about 1000 milliseconds to check if there is more data coming - this is done internally

  • and stored in a variable

  • and sets the state of the led based on the contents of the variable

Arduino code.png

Hardware connections

  • First connect the Arduino to the phone using the OTG cable

  • Then open our app

  • Then press connect and a message will pop up saying if we are allowed to access the USB device

  • Press connect again to actually connect to the Arduino - the checkbox should be checked

  • Press the button and the UNO's built-in LED should light up and go off when pressed

Step 2: Proximity sensor

This sensor is located near the front camera and is used to detect if there is an object near the sensor. The application of this sensor is when a person talks with the phone close to the ear, it detects and turns off the screen so that the screen does not receive any unwanted input when touched by any body part Usually, the output of this sensor is high or low depending on whether the object is nearby or not, so we can use the proximity sensor to make the LED blink Application

  • Includes buttons and checkboxes to establish serial communication

  • Also includes a sensor tab in the form of a proximity sensor

  • Uncheck the Enable checkbox for the proximity sensor so that the sensor does not produce an output when the application is first run

  • Proximity sensors have an event that outputs high or low depending on whether the object is nearby

  • Whenever the value of the proximity sensor changes, we send a 0 or 1 to indicate the change

  • In addition, a slide switch is included to enable the sensor, otherwise it will keep sending data when we don't want it

  • The slide switch has an event that returns true if the switch is on or off

Arduino code

  • The code used here is the same as when we receive 1s and 0s and turn the LED on and off by blinking

Step 3: Light sensor

Light sensor.png

The sensor detects the amount of light falling on the screen. This is used to change the brightness of the screen, the more ambient light falls on the screen, the brighter the screen should be, the less light is on the screen, the lower the brightness, this helps the user to have no problems in any conditions.

The output of the sensor is measured in lux, which is the unit of brightness of light.

We use events that are triggered every time the light intensity on the sensor changes, so every time the light intensity falling on the sensor changes the event is triggered and we send the measured light intensity to the Arduino to display it on the oled display.

Other parameters to consider are how often the sensor updates its value, which for some may be as long as 1000 ms and for some may be as small as 1 ms. We check this with a specific function.

In addition, some android devices have the flexibility to decrease or increase this sampling rate, and this can be done.


  • Includes connect and disconnect buttons and checkboxes for serial communication

  • Includes a slide switch so that the sensor only sends data when we need it

  • Also includes a label so that we know if the value detected by the sensor is the same as the value selected by Arduino

  • We have completed the design

  • Next include the same blocks we included before for the serial connection, corresponding to the connect and disconnect buttons

  • Next includes an event .LightChanged block, which occurs when the value generated by the sensor changes

  • In this block we first check if the serial port is open, and if so, send the lux value measured at that moment

  • Changed, which returns true when the slide is in the on position

  • and download the application

Arduino code

  • First we need to initialize the various parameters to set up the oled display

  • Now, if you don't have an oled display, you can always use a different one

  • In the setup function, we set some oled parameters like text size, color and cursor position

  • In the loop section we check if there is any data coming in

  • If so, we will first parse the data as floating point numbers

  • After parsing, we will pass it to the oled for display

Arduino code.png


Connect the oled display to the Arduino using jumpers and a breadboard

Pinout of the oled display => Arduino pins (these connections are used for the Arduino uno)


  • Now connect the phone to android using the otg cable

  • Launch the app and initialize the connection by pressing the connect button, then toggle the slide switch

  • Try waving your hand around the front camera, this is where the sensor is located

  • The changed value should appear on the oled display and under the slide switch in the app

Step 4: Accelerometer sensor

Accelerometer sensor.png

This sensor is used to determine the orientation of the phone. It is widely used in games and other interactive applications.

It measures the acceleration along the x, y and z axes. The units considered are SI units, i.e. ms^-2. The range of values is -9.8 to +9.8.

xAccel: 0 when the phone is stationary on a plane, positive when the phone is tilted to the right (i.e. its left side is raised) and negative when the phone is tilted to the left (i.e. its right side is raised in size).

yAccel: 0 when the phone is stationary on a flat surface, positive when the bottom is lifted, and negative when the top is lifted.

zAccel: equal to -9.8 (Earth's gravity in meters/second when the device is at rest parallel to the ground and the display is facing up, 0 when it is perpendicular to the ground and +9.8 when it is facing down. This value can also be affected by gravity or anti-gravity acceleration.

The event we are interested in is acceleration.Changed, which is triggered when the sensor measures a value different from the previous measurement.

To pass the value to the Arduino, we will not use it, but will use the timer event, which is an element that is triggered after each specified time has passed.

The accelerometer has a very high sampling rate, so if we send data when the measured value is different from the previous one, what will happen is that the application will try to send new data regardless of whether the Arduino received the previous data, so we are sending the timer data.

So we will include a timer that ticks in milliseconds at each specified time and when it ticks, we will send the data via serial.


  • Includes connect and disconnect buttons and checkboxes to initialize serial communication.

  • Also includes a slider switch to enable and disable the accelerometer as well as the timer.

  • Then 6 labels are placed using the horizontal arrangement available in the Layout tab, 2 of them next to each other.

  • A slider is also included, which we will use to set the timer tick period.

  • Keep the minimum and maximum values for sliders 1 and 1000 respectively, so that we don't need to map them, as the timer will be from 1 to 1000 milliseconds.

  • Add events for connecting and disconnecting to the block code.

  • For the slide switch, which is the same as the light sensor, the function returns true when the slide switch is in the open position and the accelerometer and timer are enabled.

  • The accelerometer updates itself automatically, so special events are required.

  • The slider change event sets the timer interval to the position of the thumb.

  • When the timer ticks, it sends acceleration along x, y, and z to the Arduino via serial, and updates the label depending on whether serial is on or not.

Arduino code

  • We parse 3 times to read the 3 axis values and print them using some format.

  • Since we have parsed these values, they are now treated as floating point numbers, so we can now use them directly in other applications.

Arduino code.png

Step 5: Gyroscope

This sensor measures the speed of rotation along the x, y, and z axes. The unit of measurement is degrees per second.


  • Includes connect and disconnect buttons and checkboxes to operate serial communication.

  • Also includes a slide switch to enable and disable the gyroscope and timer.

  • Includes 6 labels, 2 for each label horizontally.

  • Includes a slider to change the tick time of the timer.

  • Add events to the block code for connecting and disconnecting.

  • For the slide switch, a function that returns true when the switch is in the on position, then enables the gyroscope.

  • When the timer ticks, it sends the value from the gyroscope to the Arduino and updates the label.

  • The slider will change the tick time based on its thumb position.

Arduino Code

  • The code parses 3 times and displays the value using the OLED.

Arduino Code.png

Step 6: Magnetometer

This sensor measures the value of the magnetic field along three axes in Tesla.

It is mainly used as a digital compass to show the direction. The sensor installed in the phone is sensitive enough to measure the geomagnetic strength, but we can also use a household magnet (fridge magnet).


  • Includes connect and disconnect buttons and checkboxes for serial communications.

  • Includes slide switches to enable and disable the magnetometer and timer.

  • Slider for changing the timer tick time.

  • Adds connect and disconnect events to the block code.

  • Add switch.Changed event to enable the sensor or disable the sensor if the switch is on.

  • Includes timer events that send measurements from the sensor to the Arduino and update the label.

Arduino Code

  • The code parses floating point data and displays it in some format.

Arduino Code.png

Jotrin hope you enjoy this project!


이전: Based on TI CC3200 BleFi - BLE Wifi IoT Gateway Solution

다음: Google's own ARM chip may be released in 2023 to match Apple's M1?





라이브 채팅