loading

Hi everyone, In this project i will show you how to control LED light with your Android device along with Arduino. I am using Hc-06 bluetooth module for arduino to communicate with the android device.

Step 1: Components

  • Arduino Uno
  • HC-06 Bluetooth Module
  • Jumpers
  • Breadboard
  • LED
  • Push Buttons
  • 10k Resistors
  • Arduino Bluetooth App

CODE:

#include
int bluetoothTx = 2; // TX-O pin of bluetooth mate, Arduino D2 int bluetoothRx = 3; // RX-I pin of bluetooth mate, Arduino D3 int led = 13; int buttonPin1 = 7; int buttonPin2 = 8; int button1State = 0; int button2State = 0; int dataFromBt; boolean lightBlink = false; SoftwareSerial bluetooth(bluetoothTx, bluetoothRx); void setup() { Serial.begin(9600); // Begin the serial monitor at 9600bps bluetooth.begin(115200); // The Bluetooth Mate defaults to 115200bps bluetooth.print("$"); // Print three times individually bluetooth.print("$"); bluetooth.print("$"); // Enter command mode delay(100); // Short delay, wait for the Mate to send back CMD bluetooth.println("U,9600,N"); // Temporarily Change the baudrate to 9600, no parity // 115200 can be too fast at times for NewSoftSerial to relay the data reliably bluetooth.begin(9600); // Start bluetooth serial at 9600 pinMode(led, OUTPUT); pinMode(buttonPin1, INPUT); pinMode(buttonPin2, INPUT); } void loop() { if (bluetooth.available()) // If the bluetooth sent any characters { // Send any characters the bluetooth prints to the serial monitor Serial.println((char)bluetooth.read()); dataFromBt = bluetooth.read(); //Serial.println(dataFromBt); if (dataFromBt == '1') { Serial.println("led on"); digitalWrite(led, HIGH); bluetooth.print("1"); } if (dataFromBt == '0') { Serial.println("led off"); digitalWrite(led, LOW); bluetooth.print("0"); } if (dataFromBt == 'b') { Serial.println("a"); lightBlink = true; } else { lightBlink = false; } } if (Serial.available()) // If stuff was typed in the serial monitor { // Send any characters the Serial monitor prints to the bluetooth //String myStr = (String)Serial.read(); //char myStr1[] = "hello this is testing!"; // uint8_t payload[myStr.length() + 1]; // myStr.getBytes(payload, myStr.length()+1); int bytes=Serial.available(); //Serial.readBytes(buffer, startPosition, bytes); bluetooth.print((char)Serial.read()); } // and loop forever and ever! if (lightBlink) { digitalWrite(led, HIGH); bluetooth.print("1"); Serial.println("HIGH"); delay(500); digitalWrite(led, LOW); bluetooth.print("0"); Serial.println("LOW"); delay(500); } //------arduino push button code---------------- button1State = digitalRead(buttonPin1); button2State = digitalRead(buttonPin2); if (button1State == HIGH) { digitalWrite(led, HIGH); bluetooth.print("1"); Serial.println("on"); } if (button2State == HIGH) { digitalWrite(led, LOW); Serial.println("off"); bluetooth.print("0"); } }

<p>that cood not work</p><p>#include </p><p> int bluetoothTx = 2; // TX-O pin of bluetooth mate, Arduino D2 </p><p> int bluetoothRx = 3; // RX-I pin of bluetooth mate, Arduino D3</p><p> int led = 13;</p><p> int buttonPin1 = 7;</p><p> int buttonPin2 = 8;</p><p> int button1State = 0;</p><p> int button2State = 0;</p><p> int dataFromBt; boolean lightBlink = false; </p><p>SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);</p><p> void setup() {</p><p> Serial.begin(9600); // Begin the serial monitor at 9600bps </p><p> bluetooth.begin(115200); // The Bluetooth Mate defaults to 115200bps</p><p>bluetooth.print(&quot;$&quot;); // Print three times individually </p><p>bluetooth.print(&quot;$&quot;);</p><p> bluetooth.print(&quot;$&quot;); // Enter command mode</p><p>delay(100); // Short delay, wait for the Mate to send back CMD </p><p>bluetooth.println(&quot;U,9600,N&quot;); // Temporarily Change the baudrate to 9600, no parity </p><p> // 115200 can be too fast at times for NewSoftSerial to relay the data reliably</p><p> bluetooth.begin(9600); // Start bluetooth serial at 9600</p><p> pinMode(led, OUTPUT);</p><p> pinMode(buttonPin1, INPUT);</p><p> pinMode(buttonPin2, INPUT);</p><p> }</p><p> void loop() {</p><p> if (bluetooth.available())// If the bluetooth sent any characters</p><p> { </p><p>// Send any characters the bluetooth prints to the serial monitor Serial.println((char)bluetooth.read()); </p><p>dataFromBt = bluetooth.read();</p><p> // Serial.println(dataFromBt);</p><p>if (dataFromBt == '1'); { </p><p> Serial.println(&quot;led on&quot;);</p><p> digitalWrite(led, HIGH);</p><p> bluetooth.print(&quot;1&quot;); }</p><p> if (dataFromBt == '0');; {</p><p> Serial.println(&quot;led off&quot;);</p><p> digitalWrite(led, LOW); </p><p> bluetooth.print(&quot;0&quot;); } </p><p>if (dataFromBt == 'b'); </p><p>{ Serial.println(&quot;a&quot;); </p><p>lightBlink = true; </p><p>}</p><p> else </p><p>{</p><p> lightBlink = false; } }</p><p> if (Serial.available()) </p><p>// If stuff was typed in the serial monitor { // Send any characters the Serial monitor prints to the bluetooth //</p><p>String myStr = (String)Serial.read(); </p><p>//char myStr1[] = &quot;hello this is testing!&quot;;</p><p> // uint8_t payload[myStr.length() + 1];</p><p> // myStr.getBytes(payload, myStr.length()+1); int bytes=Serial.available(); //Serial.readBytes(buffer, startPosition, bytes); bluetooth.print((char)Serial.read()); }</p><p> // and loop forever and ever! if (lightBlink) { digitalWrite(led, HIGH); bluetooth.print(&quot;1&quot;); Serial.println(&quot;HIGH&quot;);</p><p> delay(500);</p><p> digitalWrite(led, LOW);</p><p> bluetooth.print(&quot;0&quot;);</p><p> Serial.println(&quot;LOW&quot;);</p><p> delay(500); }</p><p> //------arduino push button code---------------- button1State = digitalRead(buttonPin1); button2State = digitalRead(buttonPin2);</p><p> if (button1State == HIGH) {</p><p> digitalWrite(led, HIGH);</p><p> bluetooth.print(&quot;1&quot;);</p><p> Serial.println(&quot;on&quot;); }</p><p> if (button2State == HIGH) {</p><p> digitalWrite(led, LOW);</p><p> Serial.println(&quot;off&quot;);</p><p> bluetooth.print(&quot;0&quot;); } }</p>
May i ask what app u use?
<p>Thanks for sharing :)</p>
You are welcome :)

About This Instructable

1,096views

20favorites

License:

More by KashifB:Arduino Bluetooth Android - LED Controller Project 
Add instructable to: