Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
š Ready to explore and create with us? Click [here] and join today!
Bluetooth Lock Controllerļ
This project uses an Android app created with MIT App Inventor to remotely control a servo motor via Bluetooth, simulating a locking mechanism. Users can command the servo to move to either the ālockedā or āunlockedā position by sending specific messages through the app.
The system uses a JDY-31 Bluetooth module to receive these messages and instructs an Arduino Uno board to adjust the servo motorās angle accordingly. The servo transitions to a ālockedā position upon receiving the ā1ā message and to an āunlockedā position upon receipt of the ā0ā message.
This Android application will be constructed utilizing a complimentary web-based platform known as MIT App Inventor. The project presents an excellent opportunity to gain familiarity with the interfacing of an Arduino with a smartphone.
1. Build the Circuitļ

2. Create the Android Appļ
The Android application will be developed using a free web application known as MIT App Inventor. MIT App Inventor serves as an excellent starting point for Android development, owing to its intuitive drag-and-drop features allowing for the creation of simplistic applications.
Now, letās begin.
Go to Get Started with MIT App Inventor, and click āonline toolā to login. You will require a Google account to register with MIT App Inventor.
After logging in, navigate to Projects -> Import project (.aia) from my computer. Subsequently, upload the
Bluetooth_controlled_lock.aia
file located in the pathultimate-sensor-kit\iot_project\bluetooth\03-Bluetooth_lock_controller
.You can also directly download here:
Bluetooth_controlled_lock.aia
Upon uploading the
.aia
file, you will see the application on the MIT App Inventor software. This is a pre-configured template. You can modify this template after you have familiarized yourself with MIT App Inventor through the following steps.In MIT App Inventor, you have 2 primary sections: the Designer and the Blocks. You can switch between these two sections in the upper right corner of the page.
The Designer allows you to add buttons, text, screens, and modify the overall aesthetic of your application.
Next, thereās the Blocks section. This section lets you craft custom functionalities for your app, allowing you to program each component on the appās GUI to achieve desired features.
To install the application on a smartphone, navigate to the Build tab.
You can generate a
.apk
file. After selecting this option, a page will appear allowing you to choose between downloading a.apk
file or scanning a QR code for installation. Follow the installation guide to complete the application installation.You also download our pre-compiled APK here:
Bluetooth_controlled_lock.apk
If you wish to upload this app to Google Play or another app marketplace, you can generate a
.aab
file.
3. Upload the Codeļ
Open the
03-Bluetooth_lock_controller.ino
file under the path ofultimate-sensor-kit\iot_project\bluetooth\03-Bluetooth_lock_controller
, or copy this code into Arduino IDE.After selecting the correct board and port, click the Upload button.
Open the Serial monitor(set baudrate to 9600) to view debug messages.
4. App and Bluetooth module Connectionļ
Ensure that the application created earlier is installed on your smartphone.
Initially, turn on Bluetooth on your smartphone.
Navigate to the Bluetooth settings on your smartphone and look for names like JDY-31-SPP.
After clicking it, agree to the Pair request in the pop-up window. If prompted for a pairing code, please enter ā1234ā.
Now open the newly installed Control_RGB_LED APP.
In the APP, click on the lock icon to establish a connection between the APP and Bluetooth module.
This page displays a list of all paired Bluetooth devices. Choose the
xx.xx.xx.xx.xx.xx JDY-31-SPP
option from the list. The name of each device is listed next to its MAC address.If you donāt see any devices on the page shown above, it could be because this app is not authorized to scan for nearby devices. In such a case, you will need to adjust the settings manually.
To access the APP Info page, long-press the app icon and select it. Alternatively, if you have another method to reach this page, use that instead.
Navigate to the Permissions page.
To enable the APP to scan for nearby devices, go to Nearby devices and select Always.
Now, restart the APP and repeat steps 5 and 6 to successfully connect to Bluetooth.
After a successful connection, you will be redirected to the main page where it will display āconnectedā. Then, you can click on either āUnlockā or āLockā to control the locking mechanism.
5. Code explanationļ
Define the communication pins and initialize the SoftwareSerial library
const int bluetoothTx = 3; const int bluetoothRx = 4; SoftwareSerial bleSerial(bluetoothTx, bluetoothRx);
The above code defines the transmit (Tx) and receive (Rx) pins used by the JDY-31 Bluetooth module for communication. It then initializes the SoftwareSerial library, which allows the Bluetooth module to communicate with the Arduino board.
Define servo-related constants and create a servo object
const int servoPin = 9; const int lockAngle = 180; const int unlockAngle = 90; Servo myservo;
Here, the pin attached to the servo is defined, along with the angles for ālockā and āunlockā positions. A Servo object
myservo
is also created for controlling the servo motor.Initialize the servo and serial communications
void setup() { myservo.attach(servoPin); Serial.begin(9600); bleSerial.begin(9600); }
Control servo based on Bluetooth moduleās input
void loop() { if (bleSerial.available() > 0) { char message = bleSerial.read(); if (message == '1') { myservo.write(lockAngle); Serial.println("Locked"); } else if (message == '0') { myservo.write(unlockAngle); Serial.println("Unlocked"); } } }
The
loop()
function runs repeatedly. It reads incoming messages from the Bluetooth module. If the message is ā1ā, the servo is moved to the ālockedā position, and if the message is ā0ā, the servo is moved to the āunlockedā position. The current status (āLockedā or āUnlockedā) is printed to the Serial Monitor.