Sensors to Arduino Micro to HC-05 Bluetooth to PC using keboard.press commands

Hello, hopefully someone can guide me in the right direction, I'm fairly new to arduino and am stuck trying to make my project wireless through Bluetooth.

My project uses Capacitive sensors and a rotary encoder to send keyboard.press commands to my PC, I have this working with an Arduino Micro through a USB cable.

I want to make it wireless and I have got a HC-05 module which I have been able to pair to my PC. I have been through the very helpful guide at http://homepages.ihug.com.au/~npyner/Arduino/GUIDE_2BT.pdf which I was able to get the bluetooth module working with my phone.

Essentially what I want to do is replace the USB cable with the HC-05 using Serial1.begin(9600), is this possible? I know the guide says a common problem is uploading and testing with the same PC, is there a way around this?

Do I need to change the HC-05 to be a master rather than a slave? When I do this it won't connect to my PC even after previously being paired.

The other thing I think may be the problem is that my PC "pairs" to the HC-05 but doesn't "connect", this must be causing problems. Help!

There are some notes on using Windows in the childs' guide notes you have. Indeed I used windows before I got a smartphone.

There are two entirely separate areas of operation.

  1. Data transfer between PC and Arduino
  2. Uploading and debugging programmes via bluetooth

The following addresses the first.

Bluetooth remains a slave

No changes at Arduino end - you have done all that

You need a Kosher terminal programme for the PC. I use RealTerm. There are others - all free. Using RealTerm is pretty intuitive and, once things are debugged, is a better way to go than serial monitor, even without bluetooth

PCs rarely have bluetooth out of the box. Any problems are likely to do with the installation of the bluetooth dongle. Device Manager will show you which COM port it is on. Things are easier with laptops with built-in bluetooth.

Uploading and debugging via bluetooth has been covered here recently. I know nothing about it.

Nick_Pyner:
There are some notes on using Windows in the childs' guide notes you have. Indeed I used windows before I got a smartphone.

There are two entirely separate areas of operation.

  1. Data transfer between PC and Arduino
  2. Uploading and debugging programmes via bluetooth

The following addresses the first.

Bluetooth remains a slave

No changes at Arduino end - you have done all that

You need a Kosher terminal programme for the PC. I use RealTerm. There are others - all free. Using RealTerm is pretty intuitive and, once things are debugged, is a better way to go than serial monitor, even without bluetooth

PCs rarely have bluetooth out of the box. Any problems are likely to do with the installation of the bluetooth dongle. Device Manager will show you which COM port it is on. Things are easier with laptops with built-in bluetooth.

Uploading and debugging via bluetooth has been covered here recently. I know nothing about it.

My PC is a laptop. I will keep the HC-05 set to be slave.

OK, using RealTerm I am able to connect to the HC-05, on COM8 (Outgoing) I get a constant stream of info, pressing one of the capacitive sensors makes it slow right down, the HC-05 LED blinks twice.
Connecting to COM7 (Incoming) works but the LED still blinks on/off fast like it isn't connected.
I really don't know what I am doing with RealTerm.

Do I need to upload the sketch over bluetooth (COM7) in order for the sensors to communicate the script to the PC over bluetooth?
I have been reading on uploading over Bluetooth, the problem seems to be the Arduino doesn't know the right moment to reset, I'm not sure how this can be done easily.

The COM port(s) in use are machine specific. They can vary. My laptop usually uses COM40 but is currently using COM7 but it has n ever done that before. The Toshiba handler on my Dell 430 only uses one port. I found it just by blundering through the list.

I don't know anything about uploading Arduino programmes via bluetooth but there is never any obligation to do so. If you can connect the cable, use it.

Nick_Pyner:
The COM port(s) in use are machine specific. They can vary. My laptop usually uses COM40 but is currently using COM7 but it has n ever done that before. The Toshiba handler on my Dell 430 only uses one port. I found it just by blundering through the list.

I don’t know anything about uploading Arduino programmes via bluetooth but there is never any obligation to do so. If you can connect the cable, use it.

So you should be able to upload over the USB cable, remove it then send data over bluetooth? Even though they are on different COM ports?

I can upload the sketch over cable no problems and the keyboard.press commands work fine with the cable connected but as soon as I swap the cable from the PC to a battery (5V Powerbank) it can no longer talk to the PC. I can connect to the bluetooth using RealTerm and get a stream of data on the Outgoing port (…F880F880808080808080F880F8F880F880F880… when display is on Hex) and CPS# goes from ~1200 down to ~100 when the capacitive sensor is touched so that tells me there is some sort of communication between the sensors, arduino and HC-05.

lum1988:
So you should be able to upload over the USB cable, remove it then send data over bluetooth? Even though they are on different COM ports?

Yes.

as soon as I swap the cable from the PC to a battery (5V Powerbank) it can no longer talk to the PC.

Probably has nothing to do with communications, and quite a lot to do with power supply. Where is the 5v connected to on Arduino?

Nick_Pyner:
Yes. Probably has nothing to do with communications, and quite a lot to do with power supply. Where is the 5v connected to on Arduino?

5V Powerbank/battery USB cable into the micro USB plug on the arduino micro.
5V on Arduino connected to VCC on HC-05.

The same problem occurs using a 9V battery through a power supply module or a phone charger from the mains into arduino micro usb plug.

Here is how I have wired it up for clarification.

Nothing to see, but don't even bother posting anything with a 9v PP3 battery

Nick_Pyner:
Nothing to see, but don't even bother posting anything with a 9v PP3 battery

I have fixed the image.

Yeah I had noticed reading on other tutorials and projects that the 9V battery is a poor choice.

I figured a powerbank would be a good reliable source of 5V power and so easy to just plug straight into the micro usb plug on the arduino micro, FYI it has an output of 1000mA or 2100mA depending on what USB plug you use, I think 1000mA would be more than enough for the arduino though.

Your diagramme looks fine.

After a lot of trial and error, including trying a whole new HC-05 module I have come up with the conclusion that you can't use the Keyboard.h library with the HC-05 bluetooth chip.

I think it's something to do with the built-in USB communication chip on the arduino micro which the Keyboard.h library uses to identify to the PC that it is a keyboard/HID.

I tried changing the HC-05 class to a keyboard (AT+CLASS=000540) which managed to change the symbol of the bluetooth device on the PC to a wee keyboard but it still didn't come up in the "Mouse, keyboard & pen" field of the "bluetooth and other devices" panel. Compared to when you plug the arduino micro directly to the PC using a USB cable it would appear in the "Mouse, keyboard & pen" field as a keyboard (with the Keyboard.h library loaded to the arduino)

I'm sure there are ways to make a bluetooth keyboard using an arduino but it is not by using the Keyboard.h library.

What other ways are there of sending keyboard presses to the PC?

Can the HC-05 definitely be used to send keyboard/HID commands to the PC? Or do I need to look into an RN-42 module?

I know nothing about the keyboard, and I note the conspicuous absence of code. At a guess, you might be sharing the COM port between keyboard and bluetooth. This may be fixed by taking bluetooth off the hardware serial and using software serial instead. Software serial is never a good idea as it clogs up Arduino but the human interface will probably disguise that.

The Micro uses native USB. The keyboard functions only work with that.

Serial simulates a serial port via USB and has no relation with the RX and TX pins on the Micro; those are Serial1. They don't interfere with the upload (contrary to e.g. an Uno or Mega).

If you managed to make the HC-05 show as a keyboard, have you tried something simple like

Serial1.print('1');

Which in an editor should show a one (if it works).

Nick_Pyner:
I know nothing about the keyboard, and I note the conspicuous absence of code. At a guess, you might be sharing the COM port between keyboard and bluetooth. This may be fixed by taking bluetooth off the hardware serial and using software serial instead. Software serial is never a good idea as it clogs up Arduino but the human interface will probably disguise that.

OK, here is the code:

#include <CapacitiveSensor.h>
#include <Encoder.h>
#include <Keyboard.h>

int threshold = 150; //

//ENCODER
long oldPosition  = -999;

//ENCODER---------
Encoder myEnc(2, 3);
///

//setting the capacitive sensors 
CapacitiveSensor   cs_4_5 = CapacitiveSensor(4,5);   
CapacitiveSensor   cs_4_6 = CapacitiveSensor(4,6);    
CapacitiveSensor   cs_4_7 = CapacitiveSensor(4,7);    
CapacitiveSensor   cs_4_8 = CapacitiveSensor(4,8);    
CapacitiveSensor   cs_4_9 = CapacitiveSensor(4,9);    

void setup()                    
{
   //begin serial communication
   Serial1.begin(9600);
   //begin keyboard
   Keyboard.begin();
}

void loop()                    
{
    //update the encoder for the position.
    updateEncoder();
    //update capacitive sensors to find out if they have been touched. 
    updateCapacitiveSensors();
}

void updateEncoder(){
  long newPosition = myEnc.read();
  if (newPosition != oldPosition) {
    
    //I am only reading every 12 steps or it's too quick
    if(newPosition % 12 ==0){
       Serial1.println(newPosition/4);
       if(newPosition > oldPosition){
        //GOING COUNTERCLOCKWISE
        Keyboard.write(216); //left arrow
       }
       else{
        //GOING CLOCKWISE
        Keyboard.write(215); //right arrow
       }
    }
    oldPosition = newPosition;
  } 
 }

void updateCapacitiveSensors(){
    long total1 =  cs_4_5.capacitiveSensor(30);  
    long total2 =  cs_4_6.capacitiveSensor(30);  
    long total3 =  cs_4_7.capacitiveSensor(30);  
    long total4 =  cs_4_8.capacitiveSensor(30);  
    long total5 =  cs_4_9.capacitiveSensor(30);  
    
    Serial1.print("\t");                    // tab character for debug windown spacing
    Serial1.print(total1);                  // print sensor output 1
    Serial1.print("\t");
    Serial1.print(total2);                  // print sensor output 2
    Serial1.print("\t");
    Serial1.print(total3);                  // print sensor output 3
    Serial1.print("\t");
    Serial1.print(total4);                  // print sensor output 4
    Serial1.print("\t");
    Serial1.print(total5);                  // print sensor output 5
    Serial1.print("\t");
           
    if(total1>threshold){
      Keyboard.press(128); 
      Keyboard.press(130); 
      Keyboard.press('e'); 
      delay(500);
      Keyboard.releaseAll();
    }
    else if(total2>threshold){
      Keyboard.press(128); 
      Keyboard.press(130); 
      Keyboard.press('n'); 
      delay(500);
      Keyboard.releaseAll();
    }
    else if(total3>threshold){
      Keyboard.press(128); 
      Keyboard.press(130); 
      Keyboard.press('w'); 
      delay(500);
      Keyboard.releaseAll();
    }
    else if(total4>threshold){
      Keyboard.press(128); 
      Keyboard.press(130); 
      Keyboard.press('b'); 
      delay(500);
      Keyboard.releaseAll();
    }
        else if(total5>threshold){
      Keyboard.press(128); 
      Keyboard.press(130); 
      Keyboard.press('a'); 
      delay(500);
      Keyboard.releaseAll();
    }
  }

The USB (COM6) comes up as a different COM port to the HC-05 (COM7 & COM8). I have also tried to use Software Serial with no luck.

sterretje:
The Micro uses native USB. The keyboard functions only work with that.

Serial simulates a serial port via USB and has no relation with the RX and TX pins on the Micro; those are Serial1. They don’t interfere with the upload (contrary to e.g. an Uno or Mega).

If you managed to make the HC-05 show as a keyboard, have you tried something simple like

Serial1.print(‘1’);

Which in an editor should show a one (if it works).

OK, does that mean I was correct that the keyboard.press functions only work through USB?
As you can see in my code, I am using Serial1.
I am able to connect to the HC-05 using RealTerm and also on my phone, I get a stream of data on the Outgoing port and CPS# goes from ~1200 down to ~100 when the capacitive sensor is touched so that tells me there is some sort of communication between the sensors, arduino and HC-05.

Yes, your keyboard library functions only work through USB.

For the rest I can't really help you. Only the advise that you first need to use a simple test sketch that can send a character that will be displayed in e.g. notepad or the IDE, not in a terminal program.

OK, do you know any other ways of sending keyboard presses?

I see there is a PS2keyboard.h library and I have seen projects where people have made them wireless with an arduino UNO and a HC-05 bluetooth module but I have no idea if you could use the PS2keyboard library without using an actual playstation keyboard or how to trick it into outputting keyboard presses to the PC from sensor input.

No idea; did you try what I suggested in reply #13?

Please post some links to that wireless PS2 stuff.

And PS2 usually does not stand for playstation 2, it was the connector used for keyboard and mouse before USB took over.

sterretje:
No idea; did you try what I suggested in reply #13?

Please post some links to that wireless PS2 stuff.

And PS2 usually does not stand for playstation 2, it was the connector used for keyboard and mouse before USB took over.

So I can get it to send 1's to the serial monitor but not to a notepad/IDE. Interesting note, when I remove the USB cable from the PC and power it using my battery it sends L's instead of 1's.

http://www.instructables.com/id/Wireless-Keyboard-With-Arduino/ is the wireless PS2 project.

After reading more I found the PS2 library can't do the .press command which is what I need because I want to hold down 3 keys at once.