Devices & Components
Arduino Uno Rev3
HC-05 Bluetooth Module
LED (generic)
Jumper wires (generic)
Through Hole Resistor, 1 kohm
Through Hole Resistor, 2 kohm
Software & Tools
Arduino IDE
Bluetooth terminal (Android)
Project description
Code
Code
arduino
This code will allow to set an output (pin 13) to either high or low using the commands "on" and "off". In addition to that, we can check the status of the pin with the command "status".
1// We will receive from pin 10 and transmit to pin 11 2const int BTRX = 10; 3const int BTTX = 11; 4 5// Output LED at pin 13 6const int LED = 13; 7 8// We will use this variable to keep track of the status 9int val = 0; 10 11// We use software serial to avoid conflicts 12// with the default RX/TX pins of the Arduino board 13#include <SoftwareSerial.h> 14SoftwareSerial SerialBT(BTRX, BTTX); 15 16// Define the message 17String msg; 18 19// Setup the connection 20void setup() { 21 SerialBT.begin(9600); 22 SerialBT.println("Bluetooth connection is established"); 23 pinMode(LED, OUTPUT); 24} 25 26// Loop waiting for a message in every iteration 27// Analyze message, 28// Trigger action (LED on / off / check status) 29 30void loop() { 31 if (SerialBT.available()){ // Data are pending 32 msg = SerialBT.readString(); // Read the message 33 if (msg == "on") { 34 digitalWrite(LED, HIGH); 35 SerialBT.print("LED at Pin "); 36 SerialBT.print(LED); 37 SerialBT.println(" is ON"); 38 } 39 else 40 if (msg == "off") { 41 digitalWrite(LED, LOW); 42 SerialBT.print("LED at Pin "); 43 SerialBT.print(LED); 44 SerialBT.println(" is OFF"); 45 } 46 else 47 if (msg == "status") { 48 val = digitalRead(LED); 49 SerialBT.print("LED at Pin "); 50 SerialBT.print(LED); 51 SerialBT.println(" is "); 52 if (val == 0) { 53 SerialBT.println("OFF"); 54 } else { 55 SerialBT.println("ON"); 56 } 57 } 58 else { 59 SerialBT.print("Command <"); 60 SerialBT.print(msg); 61 SerialBT.println("> is unknown"); 62 } 63 } 64}
Code
arduino
This code will allow to set an output (pin 13) to either high or low using the commands "on" and "off". In addition to that, we can check the status of the pin with the command "status".
1// We will receive from pin 10 and transmit to pin 11 2const int BTRX 3 = 10; 4const int BTTX = 11; 5 6// Output LED at pin 13 7const int LED = 8 13; 9 10// We will use this variable to keep track of the status 11int val = 12 0; 13 14// We use software serial to avoid conflicts 15// with the default RX/TX 16 pins of the Arduino board 17#include <SoftwareSerial.h> 18SoftwareSerial SerialBT(BTRX, 19 BTTX); 20 21// Define the message 22String msg; 23 24// Setup the connection 25void 26 setup() { 27 SerialBT.begin(9600); 28 SerialBT.println("Bluetooth connection 29 is established"); 30 pinMode(LED, OUTPUT); 31} 32 33// Loop waiting for 34 a message in every iteration 35// Analyze message, 36// Trigger action (LED 37 on / off / check status) 38 39void loop() { 40 if (SerialBT.available()){ // 41 Data are pending 42 msg = SerialBT.readString(); // Read the message 43 if 44 (msg == "on") { 45 digitalWrite(LED, HIGH); 46 SerialBT.print("LED 47 at Pin "); 48 SerialBT.print(LED); 49 SerialBT.println(" is 50 ON"); 51 } 52 else 53 if (msg == "off") { 54 digitalWrite(LED, 55 LOW); 56 SerialBT.print("LED at Pin "); 57 SerialBT.print(LED); 58 59 SerialBT.println(" is OFF"); 60 } 61 else 62 if (msg 63 == "status") { 64 val = digitalRead(LED); 65 SerialBT.print("LED 66 at Pin "); 67 SerialBT.print(LED); 68 SerialBT.println(" is "); 69 70 if (val == 0) { 71 SerialBT.println("OFF"); 72 } else 73 { 74 SerialBT.println("ON"); 75 } 76 } 77 else { 78 79 SerialBT.print("Command <"); 80 SerialBT.print(msg); 81 SerialBT.println("> 82 is unknown"); 83 } 84 } 85}
Downloadable files
Terminal
Terminal

Wiring
Wiring

Terminal
Terminal

Wiring
Wiring

Prototype
Prototype

Comments
Only logged in users can leave comments