Bluetooth 4.0 HM10 cc2541 Duplex

By Dan Chen, November 24, 2015

In this example, I will show you how to communicate one micro controller to another micro controller BOTH WAYS via serial with cc2541 (BLE-HM-10).

For board design and code please download BLE_Duplex

 

Introduction

HM-10 is a BLE module for embedded system to get BLE wireless communication with BLE capable devices (e.g. iPhone and iPad). It is fully configurable by a rich and well documented AT command-set and allows transparent data communication via serial UART (default baudrate 9600bps).

The Bluetooth 4.0 HM-10 is basically a breakout board for cc2541, it broke out the LED pins, RX/TX and also adding the voltage regulator that regular 5v to 3.3 v.

 

sjineesprul bfdgd_zpsewhckphz-1

 

Bluetooth 4.0 HM-10 Master Slave Module

Bluetooth V4.0 BLE 2540 Hm-10

Setup with FTDI + Arduino Serial Monitor + AT Command

There are so many things that you can do with Bluetooth 4.0 HM-10, but first you need to setup with FTDI cable to understand what and how it’s doing.

You will need a FTDI cable and 4 female to male wires to hook up to Bluetooth 4.0 HM-10 / BLE

 

bluetooth

 

 

On your computer, open Arduino.
Make sure you select the correct usb serial port.
Open Serial Monitor and make sure the baud rate is 9600, this is the default baud rate.
Screen Shot 2015-11-17 at 11.37.35 AM

Type in
AT+NAME?     (NO SPACES IN AT COMMAND)
you should get OK+NAME:HMSoft

Screen Shot 2015-11-17 at 11.40.58 AM

If it says nothing, check your connections.

Now we can talk to BLE with AT command and the rest is simple.

See more AT Commands at the very end of the post.

 

Sending & Receiving (Both Ways )
Setting the Master-Slave mode

With this method, you can turn the device on and have them talk to each other right away, without pairing, without initializing … it is wonderful. The data goes both ways.

First you will need to Query the native MAC address using AT Command AT+ADDR?

You will get something like this 20C38FF61DA1, each BLE has a unique MAC address.

Use AT+CON[param1] and AT+ROLE[param1]  to pair to another device.

 

Example

BLE A has Mac Address 11C11FF11DA1, I used AT+ADDR? to figure it out
BLE B has Mac Address 22C22FF22DA2, I used AT+ADDR? to figure it out

Send  AT+CON22C22FF22DA2 to BLE A
Send  AT+CON11C11FF11DA1 to BLE B
(Send the B address to A, A address to B)

Send AT+ROLE0 to BLE A
Send AT+ROLE1 to BLE B
(Doesn’t matter which one)

Now it’s ready to use on you ATMEGA 328P, Arduino or Attiny.
The red light will stay solid after the connection has been made on both BLE. This should take less than a second.

Find out AT Command here

https://www.seeedstudio.com/wiki/images/c/cd/Bluetooth4_en.pdf

 

Sending & Receiving Both Ways
on ATMEGA 328P / Arduino

bluetooth2

It’s very easy to talk to ATMEGA 328P / Arduino with the BLE, you can just use the hardware serial. Software serial should work for Attiny 44 to talk to BLE.

The data can talk both ways! Just Serial.println(Whatever you want);

In the code below, the  ATMEGA 328Ps  “analog reads” the potentiometer data and sends serial data via BLE to each other turn each other’s servo.

Hardware Serial1 for M0 and 32u4

For boards like A Star 32u4 and Feather M0 make sure you use Serial1 instead of Serial.

 

Example Board

I took out the FTDI cable pads and upload my program with ISP pins to save some space on my board. I want this board to be part of my wearable project, that is why it’s ULTA compact.

Screen Shot 2015-11-24 at 6.46.18 PM

I did not use any SMD male pins to save some vertical space, because this needs to be wearable.

DSC09456

Each micro controller below runs the same code! So as soon as the BLE is on, it starts to look for the assigned address, it takes less than a second. The solid RED LED indicator on the BLE means the connection has been establish.

DSC09425

To make this wearable and flat, I have flexible cable coming out of it.

DSC09433

DSC09439 DSC09449

 

Trace

b1

Cut Outlineb2

Through Holes

b3

//receiving
int led = 3;           // the pin that the LED is attached to
#include 
Servo myservo;  // create servo object to control a servo

//sending
int sensorValue = 0;
int presensorValue = 0;

void setup() {
  pinMode(led, OUTPUT);
  myservo.attach(A2);  // attaches the servo on pin 9 to the servo object
  myservo.write(90);              // tell servo to go to position in variable 'pos'
  delay(500);
  myservo.write(80);              // tell servo to go to position in variable 'pos'
  Serial.begin(9600);

}

void loop() {

  //receiving
  while (Serial.available() > 0) {
    int pos = Serial.parseInt();
    if (pos > 1 && pos < 180) {
      myservo.write(pos);              // tell servo to go to position in variable 'pos'
      //Serial.println(pos);
      analogWrite(led, 255);
    }
    sender();
  }
  sender();

  analogWrite(led, 10);



}




void sender() {

  // Sending
  // read the input on analog pin 0:
  //sensorValue = analogRead(A1);
  sensorValue = map(analogRead(A3), 0, 1024, 5, 175);


  //with noise reduction
  if (sensorValue == presensorValue  || sensorValue - 1 == presensorValue || sensorValue + 1 == presensorValue) {
  }
  else {
    Serial.println(sensorValue);
  }
  presensorValue = sensorValue;

  delay(50);

}