Bluetooth Module Set-up and Servo Motor Control Using Bluetooth Communication
· 2019-11-20 · 2
Based on the provided JSON content, here is the English translation:
This practice can be connected to a project that manufactures a Bluetooth controller and wirelessly controls a robotic arm.
Preparation: 2 Arduino boards, 2 Bluetooth modules, 1 joystick, 1 servo motor
Content:
1. Before making Bluetooth connections, learn about Bluetooth sensors and perform basic setup for Bluetooth connectivity.
2. After connecting Bluetooth to each Arduino, use the joystick to send signals to the other Arduino, which receives them through the Bluetooth module and operates the servo motor.
1. Bluetooth Setup
When connecting Bluetooth modules to each other, separate configuration of the Bluetooth module is required. Bluetooth modules communicate through pairing in a master-slave relationship. Therefore, the Bluetooth module that primarily sends signals is set as the master, and the Bluetooth module that receives signals is set as the slave. Additionally, the PIN number must be set identically for mutual pairing.
The Bluetooth module connection is as follows:
The HC-06 is used as the Bluetooth module and consists of 4 pins.
The Bluetooth module has RXD and TXD pins. When connecting to Arduino:
RX(Arduino) - TX(Bluetooth sensor)
RX(Bluetooth sensor) - TX(Arduino)
Note that they must be connected **in reverse to each other**.
In this practice, pin 2 is used for RX and pin 3 for TX, so:
D2(RX) - TXD(Bluetooth module)
D3(TX) - RXD(Bluetooth module)
The coding is as follows:
```
#include //Call serial communication library
int blueRx=2; //Rx (set receiving pin)
int blueTx=3; //Tx (set transmitting pin)
SoftwareSerial mySerial(blueRx, blueTx); //Object declaration for serial communication
void setup()
{
Serial.begin(9600); //Serial monitor
mySerial.begin(9600); //Bluetooth serial
}
void loop()
{
if (mySerial.available()) { //When Bluetooth communication is available
Serial.write(mySerial.read()); //Print Bluetooth content to serial monitor
}
if (Serial.available()) {
mySerial.write(Serial.read()); //Write serial monitor content to Bluetooth side
}
```
The function to understand in coding is **SoftwareSerial name(RX, TX)**. In Arduino, the serial communication port is pins 0(RX) and 1(TX), allowing one-to-one communication. However, **besides pins 0 and 1, other pins can be used for serial communication through the SoftwareSerial name(RX, TX) function.**
After configuring the code this way, open the serial monitor screen and configure it as follows:
Set line ending to none and baud rate to 9600.
Next, type AT in the serial monitor window, and if OK appears, the Bluetooth setup preparation is successful.
Now, the commands for setting the Bluetooth master, slave, PIN number, and additionally the Bluetooth name are as follows:
-AT+ROLE=M (Set Bluetooth module as master)
-AT+ROLE=S (Set Bluetooth module as slave)
-AT+PIN number (Set Bluetooth module PIN number)
-AT+NAME name (Set Bluetooth module name)
The operation video is as follows.
After opening the serial monitor, set line ending and 9600 baud rate as shown above, then proceed with Bluetooth setup. After setup is complete, when the master Bluetooth is turned on, it searches for and pairs with the Bluetooth sensor acting as a slave in the vicinity.
\--\--\--\--\--\--\--\--\--\--\--\--\--\--\--\--\--\--\--\--\--\--\--\--\--\--\--\--\--\--\--\--\--\--\--\--\--\--\--\--\--\--\--\--\--\--\--\--\--\--\--\--\--\--\--\--\--\--\--\--\--\--\--\--\--\--\--\--\--\--\--\---
Next, let's proceed with the practice of connecting a joystick to the master side and a servo motor to the slave side, and controlling it through Bluetooth communication.
Preparation: Arduino Uno, Arduino Nano (Uno can also be used), 2 Bluetooth modules, 1 joystick, 1 servo motor, 1 1uF electrolytic capacitor, 1 47uF electrolytic capacitor, 1 L4940V5 voltage regulator
Content: When the master slave transmits analog values according to joystick input to the slave, the slave receives the transmitted values and applies them to the servo motor. Through this practice, you can understand how to control other IoT products through Bluetooth communication.
The circuit connection is as follows:
As shown in the circuit diagram, a capacitor and linear voltage regulator (regulator) were used.
**The linear voltage regulator requires the input voltage to always be higher than the output voltage, and the output voltage is always fixed according to the voltage regulator used. Pay attention to the capacitors at both ends of the circuit diagram. These capacitors are called electrolytic (decoupling) capacitors. Electrolytic capacitors remove noise (voltage ripple) and power interference in the current. They use capacitor charging and discharging to convert input voltage into clean voltage.**
Most linear voltage regulator datasheets contain recommended circuits with ideal capacitor values and types according to the application. Refer to the datasheet as needed to construct the circuit.
The power from the voltage regulator and the power from the Arduino must be isolated from each other, but the ground is connected in common.
**Why use external power for the servo motor?**
**1. Although the servo motor can be driven by Arduino, there is a risk of circuit damage due to inductive voltage spikes.**
**2. Servo motors tend to have rapidly increasing current consumption when the motor moves. While the servo motor maintains a specific position, it typically consumes very small current, but during the few milliseconds when the servo motor moves, it consumes hundreds of mA. This abrupt change in consumption current causes the 5V power system to fluctuate, affecting other components enough to be visible to them.**
**3. Driving the servo motor with insufficient current may cause malfunction of the servo motor.**
The joystick connection used on the master side is as follows:
X-axis -> A0 (analog input/output pin)
Y-axis -> A1 (analog input/output pin)
Both master and slave use digital input/output pin 2 for RX and pin 3 for TX.
When Bluetooth connection is established, as shown in the photo, the red light of both Bluetooth modules continuously lights up without blinking.
The coding is as follows:
**Master**
```
#include
int VRX = A0; //Use only joystick X-axis
int previousVRX=0; //Variable to compare current analog value with previous analog value
int BLE_RX = 2; //Rx (set receiving pin)
int BLE_TX = 3; //Tx (set transmitting pin)
SoftwareSerial bleSerial(BLE_RX, BLE_TX); //Object declaration for serial communication
void setup()
{
bleSerial.begin(9600); //bleSerial Bluetooth communication
pinMode(VRX, INPUT);
}
void loop ()
{
int vrx = analogRead(VRX); //Read joystick analog value to vrx
delay(50);
vrx = map(vrx, 0, 1023, 0, 179); //Map analog range 0~1023 to servo motor angle range 0~179
vrx = constrain(vrx, 2, 178); //Set operation range to 0~179 degrees and limit actual operation range to 2~178 degrees to prevent servo motor trembling when operating at 0 or 179 degrees
if ((vrx <= 50) && (vrx >= 0)) //When x-axis input value is less than 50 degrees and greater than 0 degrees
{
if ((vrx > previousVRX+4) or (vrx < previousVRX-4)) {
//Since 1~2 difference in analog input value can cause servo motor to tremble, compare current value with previous value and transmit analog value through Bluetooth only when there is +4, -4 difference
bleSerial.write(vrx); //Transmit x-axis analog value via Bluetooth
delay(10);
previousVRX = vrx; //Replace previous value with current value
}
}
else if((vrx >= 70) && (vrx <= 177)) //When x-axis input value is less than 70 degrees and greater than or equal to 177 degrees
{
if ((vrx > previousVRX+4) or (vrx < previousVRX-4))
{
bleSerial.write(vrx);
delay(10);
previousVRX = vrx;
}
}
}
```
**Slave**
```
#include
#include
Servo servo1;
int angle1 =90;
int vrx = 0;
#define BLE_RX (2) //Rx (set receiving pin)
#define BLE_TX (3) //Tx (set transmitting pin)
SoftwareSerial bleSerial(BLE_RX, BLE_TX);
void setup() {
bleSerial.begin(9600);
Serial.begin(9600);
servo1.attach(11); //Use servo motor pin 11
servo1.write(angle1); //Set servo motor initial angle to 90 degrees
}
void loop() {
if (bleSerial.available()) { //When Bluetooth communication is available
vrx = bleSerial.read(); //Transmit Bluetooth transmitted value to vrx
servo1.write(vrx); //Rotate servo motor according to vrx value
delay(10);
}
}
```
The operation video is as follows.
In the operation video, you can confirm that the motor angle value changes according to joystick movement. Since the master transmits the joystick analog value directly, you can confirm that the servo motor movement follows the joystick.
Thank you.
2
댓글 0개
등록된 댓글이 없습니다.