When LEGO Meets Arduino: Bluetooth Control in Close Range

When LEGO Meets Arduino: Bluetooth Control in Close Range

Once upon a time in Denmark, there was a king named Harald Blatand who unified the constantly warring Danish tribes into a complete nation. The king had a nickname called “blátǫnn,” which translates to Bluetooth in English. In ancient Nordic script, the initials of the king’s name “H” and “B” were written differently:

When LEGO Meets Arduino: Bluetooth Control in Close Range

Indeed, this is the origin of the Bluetooth symbol and name.

The once-giant communication company Sony Ericsson created Bluetooth technology in 1994, borrowing from this story.

Time flies, Sony Ericsson has fallen from grace, but “Bluetooth” has once again achieved “unification,” becoming the global standard for communication protocols.

The former leader in close-range control, infrared remote control, is mature in technology, low in cost, and widely used, but infrared transmission has two insurmountable weaknesses:

1. Extremely strong directionality and extremely short communication distance;

2. Unidirectional communication (unless equipped with both transmission and receiving modules simultaneously);

Infrared communication can either send commands or receive them. Bluetooth transmission is different; within the signal coverage area, no direction is needed, and signals can even pass through walls; communication devices can switch between master and slave states at any time, allowing information to be sent and received.

When LEGO Meets Arduino: Bluetooth Control in Close Range

Note: The comparison ignores point-to-point file transfer scenarios of infrared.

This freedom and convenience have allowed Bluetooth applications to rapidly cover our daily life needs: Bluetooth headsets, Bluetooth mice, smart wristbands, and even remote controls for TVs or smart boxes. Bluetooth has almost become a standard feature in our daily electronic devices.

LEGO is no exception; both the WeDo and EV3 kits support Bluetooth, and the WeDo kit can only be programmed and controlled via Bluetooth. Although Arduino boards do not come with Bluetooth functionality by default (except for Arduino BT), they can easily be expanded for Bluetooth.

Let’s take a look at the protagonist of today’s article:

HC-05 Integrated Bluetooth Module

When LEGO Meets Arduino: Bluetooth Control in Close Range

HC-05 ZS-040 Bluetooth Module

This module is made up of a breakout board and a daughter board. The breakout board has a built-in voltage divider, allowing it to adapt to a voltage input of 3.6V~6V, but the voltage of the Bluetooth daughter board’s communication interface is limited to 3.3V; excessive voltage input may damage the daughter board (don’t ask how I know this).

1.0 Interface and Wiring Diagram

RX is the signal input interface of the Bluetooth daughter board, TX is the signal output interface, and both operate at a voltage of 3.3V. Based on Arduino’s definition of high and low levels, anything above 3V can be recognized as a high level, and the TX interface’s 0V/3.3V state switch can be recognized as a high-low level switch, so the TX interface can be directly connected to Arduino. However, RX, as the input end of the daughter board, must adhere to the 3.3V input limit; during use, it is necessary to reduce the voltage of the signal output from Arduino. The principle of voltage reduction is based on the voltage divider law learned in physics:

When LEGO Meets Arduino: Bluetooth Control in Close Range

Wiring principle diagram and breadboard wiring diagram

Voltage between RX and GND is divided to achieve 2V/(1V+2V)*5V≈3.33V, meeting the voltage requirement of the daughter board.

When LEGO Meets Arduino: Bluetooth Control in Close Range

2.0 Bluetooth Setup

Completing the device wiring does not mean the Bluetooth module can be used directly; at this point, parameters such as communication port, connection password, and master-slave status are still in factory settings, and we need to configure them according to our needs. All configuration parameters can be saved even after power off, so you only need to set them once.

After powering on, the board defaults to working mode. To enter setup mode, you need to hold down the SWITCH button on the Bluetooth board before powering on; when the blue indicator light on the board starts flashing slowly with a 2-second cycle, it indicates that it has entered setup mode.

When LEGO Meets Arduino: Bluetooth Control in Close Range

Setup mode

During the setup process, parameters such as communication ports, device names, connection PIN codes, master-slave states, and checksums must be changed to meet subsequent program requirements. The setup process can be achieved using Arduino’s serial forwarding, where Arduino forwards the commands we input into the serial monitor to MC-05 to complete the settings.

Settings are made using the AT+COMMAND method, with commonly used setup commands as follows:

When LEGO Meets Arduino: Bluetooth Control in Close RangeIf the setup is successful, an OK message will be returned:

When LEGO Meets Arduino: Bluetooth Control in Close Range

After completing the setup, power on again, and the board will begin working according to the new configuration and can be searched and matched by devices such as mobile phones:

When LEGO Meets Arduino: Bluetooth Control in Close Range

3.0 Establishing Communication with Mobile Phones

To achieve Bluetooth control, we first need to communicate with the device. Here we need a mobile app: Bluetooth Terminal. After downloading and installing, open the app to view nearby Bluetooth devices, select “LEGOINO,” and the connection will be completed.

When LEGO Meets Arduino: Bluetooth Control in Close RangeThen we pick up the phone and communicate with the Arduino that is close at hand:

When LEGO Meets Arduino: Bluetooth Control in Close Range

Of course, all these interactions are preset by us:

When LEGO Meets Arduino: Bluetooth Control in Close RangeProgram screenshot

Throughout the process, Arduino is responsible for information forwarding and judgment: forwarding information from the serial monitor to the Bluetooth module or displaying information received from the Bluetooth module in the serial monitor; if a preset command is received, the corresponding operation is executed.

When LEGO Meets Arduino: Bluetooth Control in Close RangeCommand propagation flowchart

4.0 Control

Once the communication link is established, we are just a step away from control; we just need to modify keywords like “hi” and the corresponding program execution code:

When LEGO Meets Arduino: Bluetooth Control in Close Range

Code screenshot

Let’s test it:

When LEGO Meets Arduino: Bluetooth Control in Close Range

Controlling the relay is too simple, so let’s switch to a color recognition module:

When LEGO Meets Arduino: Bluetooth Control in Close RangeColor recognition module, returns the RGB value of the color

Recognition process:

When LEGO Meets Arduino: Bluetooth Control in Close Range

Let’s take a look at the recognized color:

When LEGO Meets Arduino: Bluetooth Control in Close Range

5. Unexpected Gifts

Being able to control switches remotely and return recognized colors allows for more complex functions.

Christmas is coming; if you want to send blessings to someone and make it stand out, you can try LEGO~

So I casually made another LEGO device:

When LEGO Meets Arduino: Bluetooth Control in Close Range

When LEGO Meets Arduino: Bluetooth Control in Close Range

LEGO Cloud Ladder

This device can be easily hidden in a box or behind a monitor:

When LEGO Meets Arduino: Bluetooth Control in Close Range

Go give her/him a little surprise (or scare)~

Wishing everyone a Merry Christmas in advance~

When LEGO Meets Arduino: Bluetooth Control in Close Range

—————————————————————————————————

Postscript

Unknowingly, adhering to the idea of “LEGO structure + Arduino functionality,” the series of articles “When LEGO Meets Arduino” has reached its fifth installment, covering: LEGO Arduino control board, LEGO servo control (gear speed and rotation mapping), IoT control, infrared control, and Bluetooth control, introducing the principles and methods of implementation; it also intersperses the use of PM2.5 dust detectors, W5100 network expansion boards, relays, infrared devices, Bluetooth modules, color recognition modules, striving to present the unique charm of both and the sparks that emerge from their combined applications.

Careful readers will notice that the entire series of articles is based on the idea of “Arduino detector—Arduino—LEGO motor—LEGO structure” and has not used LEGO detectors or non-LEGO power structures. In the future, different topics will be launched sequentially from various angles to strive to bring everyone richer application scenarios and better articles.

Links to previous articles in this series, click to read:

  1. “When LEGO Meets Arduino: How to Control LEGO Components Externally”

  2. “When LEGO Meets Arduino: LEGO PM2.5 Detector”

  3. “When LEGO Meets Arduino: LEGO is Calling You to Dinner~”

  4. “When LEGO Meets Arduino: Close-range Control via Infrared”

Due to the length of the article, many contents such as detector setup, parameter adjustment, libraries used, program design ideas, and codes cannot be presented in detail within the article. To facilitate communication with everyone and hear everyone’s wonderful ideas and needs, we have created a WeChat group for discussion. Everyone is welcome to join the group for discussions.

When LEGO Meets Arduino: Bluetooth Control in Close RangeScan to add “Xuetang Jun”

Join the teacher discussion group of Science and Technology Classroom

Leave a Comment

×