AT MODE CODE
#include <SoftwareSerial.h>
// SoftwareSerial(RX, TX)
SoftwareSerial BT(10, 11); // RX = 10, TX = 11
void setup() {
Serial.begin(9600); // Serial Monitor
BT.begin(38400); // HC-05 AT Mode default baud rate
Serial.println("HC-05 AT Command Mode Ready");
Serial.println("Type AT commands below:");
}
void loop() {
// Serial Monitor → HC-05
if (Serial.available()) {
char c = Serial.read();
BT.write(c);
}
// HC-05 → Serial Monitor (Response)
if (BT.available()) {
char c = BT.read();
Serial.write(c);
}
}
Slave CODE
/* How to configure and pair two HC-05 Bluetooth Modules
* == SLAVE CODE ==
*/
#include <SoftwareSerial.h>
SoftwareSerial mySerial(8, 9); // RX, TX
#define potentiometer A0 //10k Variable Resistor
#define ledPin 2
int state = 0;
int potValue = 0;
void setup() {
pinMode(potentiometer, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600); // Arduino Serial Monitor baud rate (no change needed)
mySerial.begin(38400); // HC-05 default AT mode baud rate (38400)
delay(100);
}
void loop() {
if(mySerial.available() > 0){
state = mySerial.read();
}
// Controlling the LED
if (state == '1') {
digitalWrite(ledPin, HIGH);
state = 0;
}
else if (state == '0') {
digitalWrite(ledPin, LOW);
state = 0;
}
// Reading the potentiometer
potValue = analogRead(potentiometer);
int potValueMapped = map(potValue, 0, 1023, 0, 180);
mySerial.write(potValueMapped);
delay(300);
}
MASTER CODE
/* How to configure and pair two HC-05 Bluetooth Modules
* == MASTER CODE ==
*/
#include <SoftwareSerial.h>
SoftwareSerial mySerial(8, 9); // RX, TX
#include <Servo.h>
Servo myServo;
#define button A0
int state;
int buttonState;
void setup() { // put your setup code here, to run once
pinMode(button, INPUT_PULLUP);
myServo.attach(2);
Serial.begin(9600); // Begin serial communication with Arduino and Arduino IDE (Serial Monitor)
mySerial.begin(38400); // Default communication rate of the Bluetooth module
delay(10);
}
void loop() {
if(mySerial.available() > 0){ // Checks whether data is comming from the serial port
state = mySerial.read(); // Reads the data from the serial port
Serial.println(state);
}
// Controlling the servo motor
myServo.write(state);
delay(30);
// Reading the button
buttonState = digitalRead(button);
if (buttonState == LOW) {
mySerial.write('1'); // Sends '1' to the master to turn on LED
}else{mySerial.write('0');}
}
