Arduino Bluetooth Shield

From ElectroDragon
Revision as of 12:13, 28 November 2016 by Chao (talk | contribs) (Created page with "== Pin Definition == {| class="wikitable sortable" |- ! Arduino Pins !! Header text |- | 0 || Arduino Default UART RX |- | 1 || Arduino Default UART TX |- | 2 || Bluetooth Sta...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Pin Definition

Arduino Pins Header text
0 Arduino Default UART RX
1 Arduino Default UART TX
2 Bluetooth Status Pin, default for LED
3 Extra UART1 RX
4 Extra UART1 TX

Quick setup

  1. Attached the shield on, LED start flashing quickly
  2. get anrdoid bluetooth Serial communication tool on your phone (if you use bluetooth on phone, and other devices can do in similar way), we get bluetooth SPP from google play here, any other bluetooth serial communication tool should be fine.
  3. search device, send default password 1234 and pair it. LED should stay high when device is connected (not after paired)
  4. Upload the demo code below or use serial communication tool (depends on your shield setting) to have communication now.

Board Switches

  • UART / UART1: UART connects to arduino RX and TX, UART connects to arduino pin 3 (rx) and pin 4 (tx).
  • 3V3/5V: choose either 5V or 3V3 UART power
  • Serial/Arduino(shield) Switch: Choose either send UART communication to the pins of output or to the arduino shield and arduino.

Use UART1, 3V3 and arduino(shield) switch position for the following demo code.

Demo Code 1.

Send one message out, and waiting for messages.

#include <SoftwareSerial.h>

 
#define rxPin 3
#define txPin 4
 
SoftwareSerial mySerial(rxPin, txPin);
 
void setup()
{
   // define pin modes for tx, rx pins:
   pinMode(rxPin, INPUT);
   pinMode(txPin, OUTPUT);
   mySerial.begin(9600);
   Serial.begin(9600);
   mySerial.println("Hello, Electrodragon");
}
 
void loop()
{
  while(mySerial.available()) 
      Serial.print((char)mySerial.read());    
}

Demo Code 2.

The following code will send back what it receives.

#include <SoftwareSerial.h>
#define rxPin 2
#define txPin 3

SoftwareSerial mySerial(rxPin, txPin);
 
void setup()
{
   // define pin modes for tx, rx pins:
   pinMode(rxPin, INPUT);
   pinMode(txPin, OUTPUT);
   mySerial.begin(9600);
   Serial.begin(9600);
}
 
void loop()
{
  int i = 0;
  char someChar[32] = {0};
  // when characters arrive over the serial port...
  if(Serial.available()) {
    do{
      someChar[i++] = Serial.read();
      //As data trickles in from your serial port you are grabbing as much as you can, 
      //but then when it runs out (as it will after a few bytes because the processor 
      //is much faster than a 9600 baud device) you exit loop, which then restarts, 
      //and resets i to zero, and someChar to an empty array.So please be sure to keep this delay 
      delay(3);                  
 
    }while (Serial.available() > 0);
 
    mySerial.println(someChar);
    Serial.println(someChar);
  }
 
  while(mySerial.available()) 
      Serial.print((char)mySerial.read());    
}