Intro: LED Bluetooth Dimmer
First of all, I am no Electrical or a Software Engineer, I am a Physicist, so if there are better ways of doing this, please let me know. I ♥ Arduino and love to play with it. This is an Instructable which is for beginners wanting to learn to control electronics using Bluetooth. I had a project idea in my mind to control LED brightness over Arduino using Bluetooth. The project seemed simple but I encountered many hurdles. The biggest of all of them being the conversion of the ASCII code that the Arduino receives over Bluetooth to an integer such that the sketch written by me could be implemented, to control the brightness of the LED.
I tried searching the internet, but could not find a solution. I tried and tested many things and this worked for me. Hence, I would like to share it.
Step 1: The Components
1 x Arduino (UNO) with the USB cable
1 x LED
1 x HC-06 Bluetooth module
1 x Prototyping Breadboard
1 x male to male jumper wires
1 x A power supply to supply power to Arduino. I use a Power Bank which has 5 V and 1 amp O/P with a 6000 mAh capacity
1 x smartphone or a tablet or any mobile device with Bluetooth capacity
Install an app like Roboremo.
Step 2: The Connections
ARDUINO UNO BOARD USED WITH HC-06 BLUETOOTH MODULE.
PIN NO. ON BOARD---------CONNECTED TO ON PROTOTYPING SHIELD
9 PWM---------The anode of the LED
GND---------The cathode of the LED
3.3 V---------The VCC of the Bluetooth module
GND---------The GND of Bluetooth module
TX---------The RX of Bluetooth module (note)
RX---------The TX of Bluetooth module
The Arduino is charged using the Circle Power Bank.
Upload the sketch given, before using the Arduino, using the USB cable and the Arduino IDE.
NOTE: Remove the TX and RX connections while uploading the sketch to the Arduino.
Step 3: The Sketch
In the simplest form, the sketch should receive information in numbers between 0 to 255. This number is then used to control the brightness of the LED using the analogWrite(pin,value) function. The slider on the Roboremo app should send the number between 0 to 255 over Bluetooth to the Arduino's HC-06 Bluetooth module.
However, the module receives data coded in ASCII form (check ASCII). There are possible ways to convert this encoded ASCII to integer number as needed by the sketch.
I have tried using the type casting method. Everyone is aware that a variable's type has to be declared before implementing the sketch. If a variable type needs to be changed while implementing the program it can be done by just typing (int), or (byte) or any other type needed to be converted to. For example,
int i; //The variable i is declared as an integer
float f; //The variable f is declared as a float
f = 3.6;
i = (int) f; // now i is 3, i.e. i is the integer value of the float f
so, declare the fadeness variable in the sketch as byte and then during the implementation in void loop() cast it to integer using the (int) .
This method has issues. The smartphone receives data serially and a number 103 is receives as separate 1 and 0 and another 3. This will make the LED blink with brightness 1 and when the next bit is received it will be 0 and the LED will glow with brightness 0 and then with brightness 3 when 3 is received. The method of type casting, hence fails.
There is another method to implement this, and it works "EUREKA!". For this to be implemented declare variable which controls the brightness of the LED (i.e. fadeAmount as given in sketch) as String. Upon, implementation while reading the data about the fadeAmount over the Bluetooth read it using the Serial.readString() function. It is a modification of Serial.read() function, which reads the data received over Bluetooth as String. Now, convert this string to integer using the .toInt() function. It converts the String received to a number like 100 or 200 without any spaces.
However, my observation shows that it is slow and there may be other better ways to make it work faster.
Step 4: The App Used
I used the Roboremo app found on the Google Playstore. Any app can be used with a slider capability and which can connect using Bluetooth. Edit the UI of the App using the 'edit ui' option in the the 'Menu' tab. Select a slider and let the minimum value on it to be 0 and the maximum value to be 255. Connect the App to the Arduino over Bluetooth by selecting the 'Connect' button in the 'Menu' tab.
You can use any other App with similar functionalities.