INTERACTION DESIGN STUDIO

PROCESSING AND ARDUINO VIA BLUETOOTH

TIME TO GET RID OF THE CABLES




We are going to start this sketch with soldering our previous circuit into a perfboard so that we can get rid of the jumpers, cables and make a more robust development. The finished circuit is shown below which is almost the same with the previous sketch that we build using the breadboard.

Arduino H Bridge with PWM Sketch 07

Arduino H Bridge with PWM Sketch 07

Arduino Bluetooth Module

The final step for the first part of this project is adding a Bluetooth Module to the circuit. By doing this, we will be able to transfer EEG values to our Arduino board wirelessly and drive our DC motors by using the H-Bridge soldered on to the board. In our first tangible project, we are going to see how this will make our lives easier. For this sketch, we are going to use HC Serial Bluetooth Product and more specifically HC-06. To prevent a confusion from the beginning, there are two modules that can be used to build the Bluetooth circuit, HC-05 and HC-06. To quote from Erich Styger’s blog

The HC-05 has the ‘full’ firmware on it: many AT commands, and can be both master and slave module. The HC-06 firmware on the other hand only can be a slave device, with very limited AT commands. Or in other words:

The HC-05 module can build a connection to other modules. E.g. a Robot being a master and connecting to slave bluetooth module. Or in slave mode to make a wireless bridge to a notebook.

The HC-06 module only can be a slave. This makes it only useful for say connecting a notebook as a master to a robot with a slave module e.g. for a wireless serial bridge.

For most use cases the HC-06 is enough, as typically I want to have a wireless UART connection to my devices from my notebook.

To build this simple circuit that will host our Bluetooth Module,  you may follow the instructions found in Instructables. I advise you not to use the default RX and TX ports on the board but rather use other digital pins by using the Software Serial Library. This has many advantages, ability to upload a sketch without the need to unplug RX-TX modules and screen the connection between Arduino and the computer as well as the connection between the Bluetooth Module and the computer, just to name a few. After connecting the power and the ground of the module to the Arduino, we are going to connect RX of the module to Digital Pin 4 and TX of the module to Digital Pin 5.

Bluetooth Module and Circuit Working

Bluetooth Module and Circuit Working

Arduino H-Bridge Circuit with Two Motor Control and Bluetooth Module

Arduino H-Bridge Circuit with Two Motor Control and Bluetooth Module

Now the first thing that we will do is pairing the Bluetooth Module with our device and by using a simple Arduino sketch, we are going to be sure that it works. The default code for pairing the Bluetooth Module is 1234. This may be different for other Bluetooth modules, so it is best to refer to the manual of the module used if any trouble is observed about the pairing process.

After pairing the Bluetooth module with our computer, we are going to test the connection between Processing and Arduino through HC-06 Bluetooth Module with a simple sketch that can be found below.

Bluetooth Module and Circuit Working

Arduino code can be downloaded here

/*
 Developed by Alis Design, March 2015
 and Revisited Nov 2017
 www.alis.design 
 
 based on the sketch developed by 
 Written By Mohannad Rawashdeh
 
*/


// This program shown how to control arduino from PC Via Bluetooth
// Connect ...
// arduino>>bluetooth
// D04   >>>  Rx
// D05   >>>  Tx

#include <SoftwareSerial.h>   // import the serial library

SoftwareSerial B(4, 5);       // RX, TX
int ledpin=13;                // led on D13 will show blink on / off
int BluetoothData;            // the data given from Computer

void setup() {
  B.begin(9600);
  Serial.begin(57600);
  pinMode(ledpin,OUTPUT);
}

void loop() {
  if (B.available()){
    BluetoothData=B.read();
    if(BluetoothData=='H'){   // if number 1 pressed ....
      digitalWrite(ledpin,1);
    }
    if (BluetoothData=='L'){// if number 0 pressed ....
      digitalWrite(ledpin,0);
    }
  }
  Serial.println(BluetoothData);
}

 

and the Processing code

/*
 
 Developed by Alis Design, March 2015
 and revisited November 2017 
 www.aliseckinkarayol.com
 
 based on the Arduino Playground tutorial:
 http://www.arduino.cc/playground/Learning/Tutorial01

*/


import processing.serial.*;
Serial port;

//button setup
color currentcolor;
RectButton rect1, rect2;
boolean locked = false;

void setup() {
  //set up window
  size(200, 200);
  color baseColor = color(102, 102, 102);
  currentcolor = baseColor;
  
  // List all the available serial ports in the output pane. 
  // You will need to choose the port that the Arduino board is 
  // connected to from this list. The first port in the list is 
  // port #0 and the third port in the list is port #2. 
  println(Serial.list()); 
  port = new Serial(this, "/dev/tty.HC-06-DevB", 9600); 
  
  // Define and create rectangle button #1
  int x = 30;
  int y = 100;
  int size = 50;
  color buttoncolor = color(153, 102, 102);
  color highlight = color(102, 51, 51); 
  rect1 = new RectButton(x, y, size, buttoncolor, highlight);
  
  // Define and create rectangle button #2
  x = 90;
  y = 100; 
  size = 50;
  buttoncolor = color(153, 153, 153);
  highlight = color(102, 102, 102); 
  rect2 = new RectButton(x, y, size, buttoncolor, highlight);
}

void draw() {
  background(currentcolor);
  stroke(255);
  update(mouseX, mouseY);
  rect1.display();
  rect2.display();
}

void update(int x, int y) {
  if(locked == false) {
    rect1.update();
    rect2.update();
  } 
  else {
    locked = false;
  }
  // Turn LED on and off if buttons pressed where
  // H = on (high) and L = off (low)
  if(mousePressed) {
    if(rect1.pressed()) {            //ON button
      currentcolor = rect1.basecolor;
      port.write('H');
    } 
    else if(rect2.pressed()) {       //OFF button
      currentcolor = rect2.basecolor;
      port.write('L');
    }
  }
}

class Button {
  int x, y;
  int size;
  color basecolor, highlightcolor;
  color currentcolor;
  boolean over = false;
  boolean pressed = false;   
  void update() 
  {
    if(over()) {
      currentcolor = highlightcolor;
    } 
    else {
      currentcolor = basecolor;
    }
  }
  boolean pressed() 
  {
    if(over) {
      locked = true;
      return true;
    } 
    else {
      locked = false;
      return false;
    }
  }
  boolean over() 
  { 
    return true;
  }
  void display() 
  {
  }
}
class RectButton extends Button {
  RectButton(int ix, int iy, int isize, color icolor, color ihighlight) 
  {
    x = ix;
    y = iy;
    size = isize;
    basecolor = icolor;
    highlightcolor = ihighlight;
    currentcolor = basecolor;
  }
  boolean over() 
  {
    if( overRect(x, y, size, size) ) {
      over = true;
      return true;
    } 
    else {
      over = false;
      return false;
    }
  }
  void display() 
  {
    stroke(255);
    fill(currentcolor);
    rect(x, y, size, size);
  }
}
boolean overRect(int x, int y, int width, int height) {
  if (mouseX >= x && mouseX <= x+width && 
    mouseY >= y && mouseY <= y+height) {
    return true;
  } 
  else {
    return false;
  }
}

 

If everything goes as described above, we may celebrate our first Bluetooth Connection with a coffee…

If not, check the wirings and restart the Arduino.