DIY

Using smartphone sensors in Arduino projects

Using smartphone sensors in Arduino projects Sensors are key parts in some projects using Arduino.
At the same time, most of the main sensors are built into a modern Android smartphone, such as a proximity sensor, an accelerometer, a light sensor, a magnetometer, a gyroscope, etc. These sensors can be used in Arduino projects until the sensors for the project are delivered from an online store.
In this tutorial, the wizard will show us how these sensors can be used and how to “get” them to communicate with the Arduino via a wired connection.
This will require:
-Arduino uno; -USB- cable (type B); – OTG cable;
– OLED display module (for displaying incoming data);
In the project, the wizard uses the MIT App Inventor software to create an application that will do the job of transferring the collected data from the sensor to the phone. The MIT App Inventor is ideal for those who don't want to get into the details but want the job done.
App Inventor is a visual development environment for android apps that requires minimal programming knowledge from the user.
Serial communication between Arduino and Android is similar to serial communication between computer and Arduino. The sender sends data bit by bit, and the receiver receives it bit by bit.
Using smartphone sensors in Arduino projects Using smartphone sensors in Arduino projects Serial communication – Application
Log in to MIT app inventor and start a new project.
To establish serial communication, you need to enable the serial from connectivity tab by dragging and dropping the item.
Make sure the download speed is the same like upload speed in Arduino.
Import two buttons to connect and disconnect to the Arduino and one checkbox to visually represent that the device is connected.
Add two more buttons to turn the LED on and off.
Move on to block coding. On the blocks panel, we find various events associated with different components.
Clicking the place of the event in front of each button
When the connect button is pressed, it initiates a serial connection and opens a serial connection.
When the disable button is pressed, it closes the serial communication and unchecks the checkbox
Now, as soon as the on button is pressed, it will send a data byte, that is, the LED should only turn on if the serial communication is open.
Ditto happens when the off button is pressed, an indication will be sent that the LED should turn off.
Next, save the project.
Export the APK file of the application to the phone and install the application.
Install the code for the Arduino.

 int ledState = 0; void setup () & # 123; Serial.begin (9600); pinMode (13, OUTPUT); } void loop () & # 123; if (Serial.available () & gt; 0) & # 123; delay (400); ledState = Serial.parseInt (); digitalWrite (13, ledState); }}  

Next we connect the Arduino to the phone using the OTG cable.
Open the application.
Press the connect button. A message should appear to allow access to the USB device. Confirm access.
Press Connect to actually connect to the Arduino.
When the button is pressed, the built-in LED should light up, and after turning it off, it should go out.
 Using smartphone sensors in Arduino projects Using smartphone sensors in Arduino projects Using smartphone sensors in Arduino projects Using smartphone sensors in Arduino projects Using smartphone sensors in Arduino projects Then you can start working with sensors.
Proximity sensor.
This sensor is located next to the front camera and is used to determine if an object is near the sensor or not. The work of this sensor is that when a person brings the phone close to their ear to talk, the sensor detects this and turns off the screen.
Typically, the output of this sensor is high or low depending on whether the object is nearby. < This way, you can blink the LED using the proximity sensor.
Turn on the button and checkbox to establish serial communication.
Enable the proximity sensor in the sensors tab.
Uncheck the enable proximity sensor to prevent the sensor from generating an output signal when you first launch the app.
The proximity sensor has an event that outputs a high or low level depending on whether an object is nearby or no.
Every time the value of the proximity sensor changes, it sends 0 or 1 to indicate the change.
The code is the same as in the first step.
Using smartphone sensors in Arduino projects  Using smartphone sensors in Arduino projects Using smartphone sensors in Arduino projects  Using smartphone sensors in Arduino projects Light sensor.
Using smartphone sensors in Arduino projects This sensor detects the amount of light falling on the screen. It is used to change the brightness of the screen: the more ambient light falls on the screen, the higher the brightness of the screen should be and vice versa, the less light falls on the screen, the lower the brightness of the screen.
The unit of light brightness is measured in lux.
The event we are using is an event that fires every time the light level on the sensor changes. Every time the intensity of the light striking the sensor changes, the event is triggered.
Another parameter to consider is how often the sensor updates the value, for some devices it can reach 1000ms, and for some it is only 1 ms.
Turn on the connect/disconnect buttons and a checkbox for serial communication.
Turn on the slide switch so that the sensor sends data only when needed.
Set the checkbox to know if the values ​​perceived by the sensor and the values ​​selected by the Arduino are the same.
Then you need to enable the same blocks for serial connections that were enabled earlier, corresponding to the connect and disconnect buttons.
Enable the .LightChanged event block.
We enable the event corresponding to the Slide.Changed slider switch ..
Arduino Code
First of all, you need to initialize various parameters to configure the OLED display.
In the configuration function, you need to set several OLED parameters, such as text size, color and cursor position.

 void loop () & # 123; if (Serial.available () & gt; 0) & # 123; delay (400); data = Serial.parseFloat (); display.clearDisplay (); display.setCursor (0, 0); display.print (& # 34; Intensity = & # 34;); display.print (data); display.print (& # 34; Lx & # 34;); display.display (); }}  

Hardware
OLED display connects to Arduino using jumpers and breadboard.
OLED display pins = & gt; Arduino pins
VCC = & gt; 5V
GND = & gt; GND
SCL = & gt; A5
SDA = & gt; A4
Then we connect the phone to android using the otg cable.
Launch the application. Then you can wave your hand near the front camera, there is a sensor.
The changing value should be displayed on the OLED display and also under the slider in the app.
 Using smartphone sensors in Arduino projects Using smartphone sensors in Arduino projects Using smartphone sensors in Arduino projects Accelerometer.
Using smartphone sensors in Arduino projects This sensor used to determine the orientation of the phone. Widely used in gaming and other interactive applications.
It measures acceleration in 3 axes x, y and z.
The setup is almost the same as in the previous step with minor changes.
The code is below.

 void loop () & # 123; if (Serial.available () & gt; 0) & # 123; delay (400); data1 = Serial.parseFloat (); data2 = Serial.parseFloat (); data3 = Serial.parseFloat (); display.clearDisplay (); display.setCursor (0, 0); display.print (& # 34; x-axis & # 58; & # 34;); display.print (data1); display.println (& # 34; m/s ^ 2 & # 34;); display.print (& # 34; y-axis & # 58; & # 34;); display.print (data2); display.println (& # 34; m/s ^ 2 & # 34;); display.print (& # 34; z-axis & # 58; & # 34;); display.print (data3); display.println (& # 34; m/s ^ 2 & # 34;); display.display (); }}  

The wiring diagram is the same as for the light sensor.
Using smartphone sensors in Arduino projects  Using smartphone sensors in Arduino projects Using smartphone sensors in Arduino projects Gyroscope.
Using smartphone sensors in Arduino projects This sensor measures the speed of rotation in three axes x, y and z. The unit of measurement is degrees per second.
The setting is similar to the previous ones with minor changes.
The code can be downloaded below.

 void loop () & # 123; if (Serial.available () & gt; 0) & # 123; delay (400); data1 = Serial.parseFloat (); data2 = Serial.parseFloat (); data3 = Serial.parseFloat (); display.clearDisplay (); display.setCursor (0, 0); display.print (& # 34; x-axis & # 58; & # 34;); display.print (data1); display.println (& # 34; deg/s & # 34;); display.print (& # 34; y-axis & # 58; & # 34;); display.print (data2); display.println (& # 34; deg/s & # 34;); display.print (& # 34; z-axis & # 58; & # 34;); display.print (data3); display.println (& # 34; deg/s & # 34;); display.display (); }}  

Using smartphone sensors in Arduino projects Using smartphone sensors in Arduino projects Using smartphone sensors in Arduino projects Magnetometer.
Using smartphone sensors in Arduino projects This sensor measures magnetic field values ​​along three axes with units in teslas. < br> Mainly used in digital compass to indicate direction.
Enable the connect and disconnect button and the checkbox for serial communication.
Enable the slide switch to enable and disable the magnetometer and timer.
Set the slider to change the timer countdown time.
Add connect and disconnect events in block coding.
Add a switch.Changed event, and if the switch is on, it enables or disables the sensor.
Enable timer events.
Load the code.

 void loop () & # 123; if (Serial.available () & gt; 0) & # 123; delay (400); data1 = Serial.parseFloat (); data2 = Serial.parseFloat (); data3 = Serial.parseFloat (); display.clearDisplay (); display.setCursor (0, 0); display.print (& # 34; x-axis & # 58; & # 34;); display.print (data1); display.println (& # 34; T & # 34;); display.print (& # 34; y-axis & # 58; & # 34;); display.print (data2); display.println (& # 34; T & # 34;); display.print (& # 34; z-axis & # 58; & # 34;); display.print (data3); display.println (& # 34; T & # 34;); display.display (); }}  

Using smartphone sensors in Arduino projects  Using smartphone sensors in Arduino projects  Using smartphone sensors in Arduino projects In principle, that's it. In this way, you can replace the sensors and check the work of your Arduino projects. More details on this material can be found on the page: https://www.instructables.com/Sensors-in-Phone-and-Arduino-Serial-Communication/

Source:

usamodelkina.ru

Show More

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button