Skip to content

14. Networking and communications

This week I worked on defining my final project idea and started to getting used to the documentation process.

Group Assignment

Link

Previous Attempts & testing

In week 16 I made an App to communicate with my circuit using HC-05 bluetooth module. In the documentation of week 16 it shows the problem I faced & the process I went through & how I made a circuit for the bluetooth module. I made another circuit to use it for this week assignment.

Description

For this assignment I am going to use two HC-05 Bluetooth Modules to make my circuits communicate with each other. HC-05 Bluetooth Module is a Bluetooth SPP (Serial Port Protocol) module, designed for transparent wireless serial connection setup. Its communication is via serial communication which makes an easy way to interface with controller or PC. HC-05 Bluetooth module provides switching mode between master and slave mode which means it able to use neither receiving nor transmitting data. My main refrences for this assignment are link 1 & link 2

HC-05 Datasheet

Accourding to datasheet that the TX & RX pins operate on a 3.3V level. By connecting the RX pin directly to the Arduino circuit or my circuit, there will be a risk of damaging the module. That’s why I made a voltage divider circuit for the Bluetooth module in week 16 assugnment.

Configure HC-05

In order to configure both modules we need to switch to AT Command Mode. I tried to do it with a circuit I designed before but it was not possible. So, I did it using Arduino board. First, we need connect the Bluetooth module to the Arduino. What we need to do additionally is to connect the “EN” pin of the Bluetooth module to 5 volts and also switch the TX and RX pins at the Arduino Board.

c5

Before connecting the Arduino board to the USB cable, we need to remove the VCC wire from the HC-05. Then upload this code to Arduino

#include <SoftwareSerial.h>

SoftwareSerial BTSerial(10, 11); // RX | TX

void setup()
{
  pinMode(9, OUTPUT);  // this pin will pull the HC-05 pin 34 (key pin) HIGH to switch module to AT mode
  digitalWrite(9, HIGH);
  Serial.begin(9600);
  Serial.println("Enter AT commands:");
  BTSerial.begin(38400);  // HC-05 default speed in AT command more
}

void loop()
{
  // Keep reading from HC-05 and send to Arduino Serial Monitor
  if (BTSerial.available())
    Serial.write(BTSerial.read());

  // Keep reading from Arduino Serial Monitor and send to HC-05
  if (Serial.available())
    BTSerial.write(Serial.read());
}

After uploading, we need to connect the VCC wire to the HC-05. the LED on the HC-05 bluetooth module will blink for every 2 seconds interval. This indicates that the bluetooth module has entered AT mode.

Open your Serial Monitor to give AT commands . A sentence saying “Enter AT commands:” will pop up. But before that, remember to change “No Line Ending” to “Both NL & CR” and also fix the baud rate at 9600. To check if everything is okay or not, enter “AT” and send. An “OK” should pop up on the Serial Monitor.

Slave Configuration

The required AT commands to set the configuration

  • AT+RMAAD (To clear any paired devices)
  • AT+ROLE=0 (To set it as slave)
  • AT+ADDR (To get the address of this HC-05, remember to jot the address down as it will be used during master configuration)
  • AT+UART=38400,0,0 (To fix the baud rate at 38400)

If the commands entered are replied with “OK” then it indicates that the settings mentioned have been customized. Also, we renamed the slave bluetooth module.

c6

AT Commands pdf file

Master Configuration:

The required AT commands to set the configuration:

AT+RMAAD (To clear any paired devices) AT+ROLE=1 (To set it as master) AT+CMODE=0 (To connect the module to the specified Bluetooth address and this Bluetooth address can be specified by the binding command) AT+BIND=xxxx,xx,xxxxxx (Now, type AT+BIND=98d3,34,906554 obviously with your respective address to the slave. Note the commas instead of colons given by the slave module. AT+UART=38400,0,0 (To fix the baud rate at 38400)

c7

Pairing

the above steps are what we need for a basic configuration of the Bluetooth modules to work as a master and slave devices and now if we reconnect them in normal, data mode, and re-power the modules, in a matter of seconds the master will connect to the slave. Both modules will start flashing every 2 seconds indicating a successful connection.

Programming

For this assignment I used the circuits I made in previous week 7 & week 11 . At first I had problems with programming the circuit with the button. it shows that the programmer is not connected. I tried using different programmers, but all of them showed an error message. After hours of trying different solutions & troubleshooting & continuity test, it turned out that there was an open connection. So, I resoldered it & the programming was successful. I didn’t expect this error since I did program my circuit not a long time ago.

Connection

I used this as a refrence for my circuit connections.

c8

TX & RX are connected to PA4 & PA5 in both circuits

Button is connected to PA3 in button circuit

LED is connected to PA7 in LED circuit

I designed a circuit for the bluetooth module & connected it to my circuit.

b3 b4

Button circuit

e11 e10 e16

LED circuit

e7 e8 e14

Input circuit code

I used this code to program the circuit with a button.

#include<SoftwareSerial.h>
SoftwareSerial mySerial(PA4, PA5);
#define button PA3
int state = 0;
int buttonState = 0;

void setup() {
  pinMode(button, INPUT);
  mySerial.begin(38400); // Default communication rate of the Bluetooth module
}
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
 }

 // 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');

 }  

}

Output circuit code

I used this code to turn on & off the LED in the other circuit.

#include<SoftwareSerial.h>
SoftwareSerial mySerial(PA4, PA5);
#define ledPin PA7
int state = 0;
void setup() {
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  mySerial.begin(38400); // Default communication rate of the Bluetooth module
}
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
 }
 // Controlling the LED
 if (state == '1') {
  digitalWrite(ledPin, HIGH); // LED ON
  state = 0;
 }
 else if (state == '0') {
  digitalWrite(ledPin, LOW); // LED ON
  state = 0;
 } 
}