Devices & Components
Arduino Uno Rev3
HC-05 Bluetooth Module
Male/Male Jumper Wires
5 mm LED: Red
Resistor 1k ohm
Software & Tools
Arduino IDE
MIT App Inventor
Project description
Code
The code for HC-05 and Arduino Interfacing
c_cpp
This is the code you have to type into the Arduino IDE. The comments are not necessary, but I have commented heavily for better understanding, especially for people just starting out Arduino.
1/*Code to control an LED via bluetooth through a phone app. 2Project by MukeshArvindh. Code by MukeshArvindh.*/ 3 4/*If you are going to copy and paste the code, then do not forget 5to delete the void setup() and void loop() function from the 6sketch you are using before doing so,as the functions already 7exist in this sketch. Copy-pasting the comments will not 8cause any changes in the outcome or code.*/ 9 10/*Note:This code has been compiled and checked multiple times, and has 11proven to be accurate. The product also works as intended.*/ 12 13//Bluetooth uses serial communication. So, we use many serial functions 14//in this sketch. 15const int LED = 5; 16/*Declaring that there is an LED on pin 5 of the arduino board. We use 17const as we will not change this. You don't have to name it LED. You 18can even put your name instead.*/ 19char switchstate; 20/*declaring that there is a variable called switchstate, which will 21hold a character value. This is due to programming of the app, which 22will send a text value to arduino. If we use 'int' instead of 23'char' the code will not work properly.*/ 24void setup() {//Here the code only runs once. 25Serial.begin(9600); 26/*To start serial communication at a rate of 9600 bits per second. This 27is the default rate anyways.*/ 28pinMode(LED, OUTPUT); 29//Declaring that the LED is an output. 30} 31void loop() {//This code repeats. This is our main code. 32while(Serial.available()>0){ 33 //code to be executed only when Serial.available()>0 34 /*Serial.available>0 is to check if there is any reading from the 35 HC-05 Bluetooth module.*/ 36switchstate = Serial.read(); 37/*The character we had declared earlier is now being assigned a value- 38the value of whatever Serial.read() is.*/ 39//Serial.read() is to read the value coming from app. 40Serial.print(switchstate); 41//This will print the value onto the Serial monitor. 42Serial.print("\ 43"); 44//This moves to the next line after every new line printed. 45delay(15); 46/*Gives a break of 15 milliseconds. Delay is for human eye, and for 47speed of some computers, as some will crash at high speeds.*/ 48if(switchstate == '1'){//Checking if the value from app is '1' 49 digitalWrite(5, HIGH); 50 //If it is, write the component on pin 5(LED) high. 51} 52else if(switchstate == '0'){//Else, if the vaue from app is '0', 53 digitalWrite(5, LOW);//Write the component on pin 5(LED) low. 54} 55} 56}
Downloadable files
This is the voltage divider circuit between Arduino and HC-05.
This is the voltage divider circuit between Arduino and HC-05.

HC-05 to Arduino Connections
This is the way a circuit is meant to be formed between an HC-05 and an arduino.
HC-05 to Arduino Connections

This is the voltage divider circuit between Arduino and HC-05.
This is the voltage divider circuit between Arduino and HC-05.

HC-05 to Arduino Connections
This is the way a circuit is meant to be formed between an HC-05 and an arduino.
HC-05 to Arduino Connections

Comments
Only logged in users can leave comments