Nov 13, 2011

Sunday night with Bluetooth

What do you do after installing your Christmas lights? Build a robot of course!


This projects started with a modified version of the Boe-Bot on which I added big dirt wheels scavenged from a dead RC car. The simple structure on top is made with my usual LEGO spider-legs system supporting the Arduino and its proto shield. I'm using the Sparfun proto shield since it has a build-in connector for the BlueSmirf Bluetooth breakout board and.. it's red. I also use two power supplies to separate the servos from the rest of the electronics.

Software wise, I used the Sena BTerm app to do some direct communication with the Bluetooth device. This app was very practical since it enabled me to only code on the Arduino side. This made prototyping very fast. The only real work began when I had to make the real Android app using Bluetooth. For that I hacked Google's BluetoothChat demo and after a short time I was using buttons and sliders to communicate with my Arduino. Why was that work? Eclipse...


A great advantage of using an Android device as the communication platform is the real-time outputs. This is very nice when debugging since you don't have to be tether with the laptop.

Edit 1: I later created an Arduino to Arduino link with bluetooth.

Edit 2: Here is similar code a later used for the minirccar



#include  
int leadPin = 3;
//Servo myservo;  // create servo object to control a servo
int servoDir = 90;
int delayTime = 100;
int incomingByte = 0;
int motorState = LOW;
int servoPin = 8;
char gBtMsg[256]; 
void BtReceive(void){
  bool keepReading = true;
  int index = 0;

  gBtMsg[0] = '\0';

  while(keepReading){
    keepReading = false;
 
    if (Serial.available() > 0) {
      // read the incoming byte:
      incomingByte = Serial.read();
      if(incomingByte != 13){
        gBtMsg[index++] = incomingByte;
        keepReading = true;
      }
    }
  }

  gBtMsg[index] = '\0';
void BtSend(char* i_pBtCmd, bool i_ln = true){
  if(i_ln){
    Serial.println(i_pBtCmd);
  } else{
    Serial.print(i_pBtCmd);
  }

  delay(100);
  BtReceive();
void setup()
{
  pinMode(leadPin, OUTPUT);
  pinMode(servoPin,OUTPUT);

  Serial.begin(115200);
  BtSend("$$$", false);
  BtSend("SM,0");
  BtSend("---");
}

void loop()
{
  bool doSomthing = false;
  if (Serial.available() > 0){
    incomingByte = Serial.read();
    doSomthing = true;
  }
  switch(incomingByte){
  case '1':
    servoDir = 1360; //left
    break;
  case '2':
    servoDir = 1580;//90;
    break;
  case '3':
    servoDir = 1800; //right
    break;
  case '4':
    motorState = HIGH;
    break;
  case 'd':
    motorState = LOW;
    break;
   default:
     doSomthing = false;
  }
  for (int temp = 0; temp <= 2 && (doSomthing || (motorState == HIGH)); temp++)
  {
    digitalWrite(servoPin,HIGH);
    delayMicroseconds(servoDir);
    digitalWrite(servoPin,LOW);
    delay(25);
  }

  digitalWrite(leadPin, motorState);

  delay(delayTime);
}



10 comments:

  1. Good project design buddy..... great work appriciated

    ReplyDelete
  2. Can you share Arduino code?
    I want to do something like this but it involves Netdruino.

    ReplyDelete
    Replies
    1. I couldn't find this old project but I've added (in the 'Read more' section of this post) the code from the MiniRCCar project which is almost the same. Hope this can help.

      Delete
  3. Hi Phillipe,
    I'm trying to build a project using an HC-06 module, but am having some really basic problems that I thought with your experience, you might be able to help me with.

    They are:
    1. when I use it to connect a BT USB dongle on my XP laptop with my PC on COM1 using RealTerm terminal emulator on both sides and sending ASCII data at 9600,N,8,1, instead of receiving 7-bit ASCII alpha-numerics, I'm receiving high-order characters. In both directions.
    For example when sending
    1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ
    I receive (in HEX)
    E0 00 00 E0 00 E0 00 00 E0 E0 00 E0 E0 E0 00 E0 00 00 E0 00 00 E0 00 E0
    E0 00 E0 00 E0 E0 E0 E0 E0 E0 E0 00 E0 E0 E0 E0 E0 E0 E0 E0 E0 00 E0 E0
    00 E0 E0 E0 E0 E0 E0 E0 E0 E0 E0 E0 E0 E0 00 E0 E0 E0 00 E0 E0 E0 00 E0
    E0 00 E0 E0 00 E0 E0 E0 E0 E0 E0 E0 E0 E0 E0 E0 E0 E0 00 E0 E0 E0 E0 E0
    E0 E0 E0 E0 E0 E0 E0 00 E0 E0 E0 00 E0 E0 E0 00 E0 E0 E0 00 E0 E0 E0 00
    E0 E0 E0 00 E0 E0 E0 00

    2. I'm unable to talk to the device (over the serial side). Sending AT when the unit is not paired and the LED flashing, I get no response.

    Being a software guy, I blamed the hardware and bought a second HC-06 to check. But no, this one was worse!, I could only get one-way comms (serial ->BT) > the two devices are different models.
    So that didn't solve my problem.

    ReplyDelete
    Replies
    1. Other example of garbled message:
      Send :THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG
      receive (in HEX):
      D5 AB FD 5D 55 6D 79 69 BF 7B 5B 61 51 63 BF 73 61 4F BF 6B 55 65 5F 75
      77 BF 61 53 75 5B BF 57 6F 75 BF 67 7D 4B 4D BF 77 61 71 BF E5 EB 00

      Delete
  4. In case you didn’t already found it, go check this post I did about using the HC-05.
    My initial guess here is that you probably don’t have the right connection speed set on your modules. You can query what the current setting is with “AT+UART?”.

    ReplyDelete
  5. Hi Phillipe,
    After spending a couple of hours with me, Lyman suggested I contact you with the introduction "I sent a good friend of mine some of these modules to "tool" around with as he is a genius with BT".

    Yes, thanks, I read your blogs on HC-05. Unfortunately they are Arduino-oriented, and I'm not at all familiar with Arduino.
    I'm working with a bare breadboard and cables direct to my PC, picture here: http://gpsanimator.com/tmp/BT1.jpg
    I have already built a java app for my Android phone which successfully locates and communicates with a separate device - an i-Blue 747 GPS data logger. This app locates the device, receives position data (NMEA) , monitors the connection and if dropped (by the GPS powering down or going out of range) re-establishing the connection.
    What I'm targeting with the HC-06 project, is to develop the phone app talk to our racing yacht's navigation system (a B&G H2000 Performance processor) over its RS232 port, through which commands and queries can be directed to the processor and present useful tactical info in real time.

    I believe you're correct about a problem with the connection speeds. The problem is that, as I mentioned in my initial comment, I'm unable to establish the AT communication.
    I have 2 HC-06 boards from different suppliers with different markings:
    The original one, I'll call Board 1, is marked JY-MCU V1.06. The second Board 2 has fewer markings: A logo of SJ in a circle and a BT logo. Both have the CSR and MX chips and identify themselves as HC-06 during pairing.
    Only Board 2 provides 2-way comms, the Board 1 only transmits serial-> BT. Both display the message garbling I described earlier.
    I have tried Lyman's suggestion of setting the KEY pin high, but that didn't help. And reading the Chinglish documentation, it would appear that this only applies to the HC-05 to switch between Master and Slave.
    So I am unable to get a response to AT commands.

    I know the solution is simple, and obvious, but after over a week of trial and error, I just can't nail it.

    ReplyDelete
    Replies
    1. Hi friend of Lyman! You are welcome in the land of PhilBlog.
      The fact that your boards only have 4 header pins may mean that mounted modules are not able to be access in command mode. Still, lets assume we can.
      To enter the command mode on the HC module you must 1) Have the module powered down (no power on the VCC) 2) Set the Key pin to HIGH 3) Power up the mmodule (power on VCC) 4) Start using the AT commands.
      Also When opening the COM port to the module use the following settings: "38400,0,0" (Stop bit 0, Parity bit 0)

      Delete
    2. Thanks for your suggestions, however I'm still making no progress.
      I followed your instructions re the KEY pin: remove power to VCC; put + 4.5 on KEY pin; Put + 4.5 V on VCC; attempt AT comms on serial port at 9600,N,8,1 Still no response from HC-06.
      I tried a range of other speeds: from 1200 through 38,400 no change.
      Re your suggestion to open COM port at 38400,0,0: My terminal emulator doesn't support Stop Bit 0. Did you mean 1 stop bit?
      The following observations may not be relevant, but I provide them in hope.
      I have 2 LEDs on the board one on the TXD and one on the RXD lines. On on Board 2 the TXD line sets high on power up, even when communicating "gibberish" over the BT port. On board 1, the TXD line only goes high during data transmission.
      The second observation is if I power up either of the boards with the COM port connected to the PC, its LED remains off, indicating it's not starting? If I disconnect the serial cable, it powers up and flashes as usual at around 2Hz.

      One post I read indicated the need to provide ~5ms between characters in the AT command. I have tried with a range of 0 through 15 ms without success.

      Another post talks about the need for line termination or not after the AT, I have tried both with and without.

      Delete