BuddyBot: Controlling a Stepper Motor with an Android Smartphone
By Michael Parks, P.E., Mouser Electronics
Licensed under CC BY-SA
4.0
Software
The BuddyBot project has two software components:
- Code that runs on the STM32 Nucleo and controls the motor, laser, and ultrasonic sensor.
- User Interface (UI) that runs on an Android device. They communicate with each other over a Bluetooth serial
communications link that can be initiated from the Android app.
- Let’s first take a look at the code that runs on the STM32 Nucleo. You can get the code from GitHub. In short, the
code makes the STM32 Nucleo sit in a loop waiting for a character to be received from the Bluetooth module. Based
on the character received, a variety of functions can be triggered. The first 25 lines of the main code looks
something like this, and the filename on GitHub (at the time of this writing) is BuddyBot_v3_STM32
NUCLEO-f401RE.bin :
Figure 6: A section of the Nucleo main code from
BuddyBot_v3_STM32 NUCLEO-f401RE.bin. You can look at an image file of the entire code in the Source Files area
for this project as Nucleo_main_cpp.png, located inside BuddyBotNucleoCode.zip or get the code itself inside
BuddyBotNucleoCode.zip or on github.
- To drive the particular stepper motors we’re using, I wrote a custom library that contains motor.h and motor.cpp
files, for download as images (.png files) in the Source Files area. (As image files, they are named:
Nucleo_motor_h.png andNucleo_motor_cpp.png and are located in BuddyBotPhotos.zip with other images. The actual
source code files motor.h, motor.cpp, and main.cpp are located in BuddyBotNucleoCode.zip)
Depending on how you construct your BuddyBot, there are a few key variables you may be interested in tweaking. In
the main.cpp file:
- int triggerDistanceCM = 100: This variable is the minimum distance, measured in centimeters,
between the ultrasonic sensor and your pet that will trigger a rotation of the motor when the BuddyBot is in
robot mode. This will need to be adjusted depending on the height and angle of your ultrasonic sensor. The
taller the sensor, the larger the variable may need to be.
In the motor.cpp file you may wish to tweak the following:
- int _numMotorPulseCycleStandard = 5: The number of motor rotation cycles that will run per
click of the left or right button in the Android app. A smaller number will result in a smaller angle of
rotation per each button press.
- int _numMotorPulseCycleMax = 10: This variable performs the same function as
_numMotorPulseCycleStandard except it controls the number of rotation cycles that occur when an interrupt is
triggered by the limit switches.
- Once you have the code all written, click the “Compile” button to create the necessary .bin file that you will
drop onto your STM32 Nucleo. You will be asked where you want to save the file to on your computer. Installing
code on the STM32 Nucleo is as simple as dragging the file from wherever you saved it onto the STM32 Nucleo, just
as you would click-and-drag any file onto a USB thumb drive.
Programming the Android App
- Now let’s take look at the Android application (whose source code is located in the Source Files page inside
AndroidApp.zip). The user interface (UI) of the Android application is minimalist and consists of the following
elements:
- “Laser On” checkbox to toggle the laser on and off. If the checkbox is checked, the laser will turn on. If it
is unchecked, the laser will turn off.
- “Robot Mode” checkbox to toggle the automatic mode. If checked, the motor will move at random without user
input. The “Left” and “Right” buttons will be disabled while in auto mode.
- “Left” button will turn the motor to the left when in manual mode.
- “Right” button will turn the motor to the right when in manual mode.
- “Pair Device” button will allow the user to pair their Android device to the BuddyBot.
- “Connect” button will allow the user to connect to the BuddyBot after pairing the two devices.
- “Disconnect” button will only appear once a BuddyBot is connected. Pressing “Disconnect” will stop the
connection between the Android device and the BuddyBot.
Figure
7: A screenshot of Android Designer on the MIT
Application Inventor where the very fancy BuddyBot UI was created.
- 5. The serial communications link is a simple, one-way link that passes single characters from the Android app
to the BuddyBot hardware. The STM32 Nucleo code interprets each character and runs the appropriate function for
each corresponding command character received. The following characters are sent based on the following button
presses:
| Character |
Sent if |
| l (lower case L) |
“Left” arrow button is pressed. |
| r |
“Right” arrow button is pressed. |
| a |
“Laser On” checkbox is checked. |
| z |
“Laser On” checkbox is unchecked. |
| b |
“Robot Mode” checkbox is checked. Puts BuddyBot into auto mode. |
| y |
“Robot Mode” checkbox is unchecked. Puts BuddyBot into manual mode. |
|
- If you are so inclined to write the code yourself or modify the provided BuddyBot.aia file (inside
AndroidApp.zip) , then the “Designer” window of your screen should look similar to this:
The “Blocks” window of your screen should look similar to this:
Figure
8a: Blocks window of the Android designer screen.
Figure
8b: Blocks window of the Android designer screen.
We would love to hear what you think about this project; please tell us in the comments
section below.