In this post, I will explain how to control the brightness of several LEDs from the cell phone via Bluetooth, using the PWM pins of Arduino.
MATERIALS:
- 1 Arduino UNO
- 3 resistors of 220 ohms
- 3 LEDs of different colors
- 1 Bluetooth module HC-05
SOFTWARE:
- Arduino IDE
- APP Inventor
WIRING DIAGRAM:
GRAPHICAL INTERFACE OF THE APP
The application is composed of three parts:
1.- Bluetooth connection button cell phone - HC-05 module:Pressing this button opens a list of the devices linked to our cell phone.
To connect to the HC-05 Bluetooth module from the application, we press the Connect BT button.
A list of the Bluetooth devices linked to our cell phone will appear, we select the device. In my case, I am using the HC-05 module.
2.- Sliders with values from 0 to 255 (PWM value):By moving the slider from one place to another, we increase or decrease the brightness of the LEDs. Move to the right to increase and to the left to decrease.
3.- Labels:Indicate the values of the sliders (PWM value).
EXPLANATION OF THE APPLICATION CODE.
Cellular Connection - HC-05 module.
We have two blocks:
1.- When ListPiker1.BeforePicking:
When pressing Connect BT, a list of items will open with the names and addresses of all the bluetooth devices linked to our cell phone.
2.- When ListPiker1.AfterPicking:
In this part, we establish the connection of the Bluetooth module with our phone.
Sliders
We initialize 3 variables and enclose them. In these variables, we will store the values of the sliders that we will later send via Bluetooth.
In the next block, we obtain the value of the slider's position, store it in a variable, and print that value on a label. We do this for each of the sliders.
Sending data via Bluetooth
To send data via Bluetooth, we add a timer. This timer will help us send the data every time we modify the slider value after a certain time.
Within the timer code block:
We check if the Bluetooth module is connected to our phone. If it is connected, we send a list with the values of the sliders via Bluetooth. Otherwise, we disable the sliders.
The values of the sliders will be sent in the form of an array as follows[value1, value2, value3, ... , valueN].
Since in this project we are using the serial monitor to visualize the values we receive, we need to add another timer so that the phone can receive that data; otherwise, the application will stop working. For this, we add the following block.
This block of code helps us receive the data sent from the Arduino in our app.
EXPLANATION OF THE ARDUINO CODE
We define an array with the pins where the LEDs will be connected; these pins must have PWM; otherwise, we will not be able to vary the brightness of the LEDs.
int leds[3] = {5,6,9};
We create a string type variable; this variable will store the value we receive via the serial port as a string.
String str = "";
We define a constant with the value of the following character: ' , 'This character will serve us later to separate the values of the array that we receive via Bluetooth from the phone.
const char separator = ',';
We define a variable and give it the value of the number of data that will arrive through the serial port, in this case it is 3 because from the application we send 3 values from the 3 sliders.
const int numberOfData = 3;
We define an array which we will use to store the data sent from the application via bluetooth (previously separated), it will also serve to define the pwm values for the leds.
int data[numberOfData];
In the setup function, we initialize the serial port and declare the pins where the leds are connected as outputs.
void setup()
{
Serial.begin(9600);
for (int i = 0; i < 4; i++){
pinMode(leds[i], OUTPUT);
}
}
In the void setup we have the following:
Every time we receive data, we will store it in the previously declared string: "str", the data does not arrive in the following way [data1, data2, data3,..., dataN], to be able to use these values we have to extract the parameters separated by commas, for this we first have to read the string up to " ] ". We do this with the following line of code.
str = Serial.readStringUntil(']');
Doing this, the string looks like this:
[123, 23, 200
To be able to use this data without any problem the next step is to remove the first element: " [ ", we do this with the following line of code.
str.remove(0,1);
Doing this, the string looks like this:
123, 23, 200
The next step is to extract the parameters from the string so we can use them separately, for this we use the following code
for (int i = 0; i < numberOfData ; i++)
{
int index = str.indexOf(separator);
data[i] = str.substring(0, index).toInt();
str = str.substring(index + 1);
}
Doing that, we have the data as follows:
data[0] = 123 data[1] = 23 data[2] = 200
Finally, we are going to assign a value to each led, we can do this in two ways, a normal way and a simplified way.
Normal:
analogWrite(leds[0], data[0]); analogWrite(leds[1], data[1]); analogWrite(leds[2], data[2]);
Simplified:
for (int i = 0; i < numberOfData; i++) {
analogWrite(leds[i], data[i]);
FULL CODE:
int leds[3] = {5, 6, 9};
String str = "";
const char separator = ',';
const int numberOfData = 3;
int data[numberOfData];
void setup()
{
Serial.begin(9600);
for (int i = 0; i < 4; i++) {
pinMode(leds[i], OUTPUT);
}
}
void loop()
{
if (Serial.available())
{
str = Serial.readStringUntil(']');
str.remove(0, 1);
for (int i = 0; i < numberOfData ; i++)
{
int index = str.indexOf(separator);
data[i] = str.substring(0, index).toInt();
str = str.substring(index + 1);
}
}
for (int i = 0; i < numberOfData; i++) {
analogWrite(leds[i], data[i]);
}
}





Post a Comment
Hello! We're so glad you've made it this far and are reading this article on Edeptec.
This form is an open space for you: you can leave a comment with your questions, suggestions, experiences, or simply your opinion on the topic discussed.
» Did you find the information helpful?
» Do you have any personal experiences you'd like to share?
» Do you have any topics you'd like to see covered in future articles?
Remember that this space is for learning and sharing, so we encourage you to participate respectfully and constructively. Your comments can help other readers who are on the same path, whether in electronics, programming, sports, or technology.
Thank you for being part of this learning community! Your participation is what makes this project grow.