Hi, welcome to an ESP2 tutorial using Arduino IDE by Robojax. My name is
Ahmad Shamshiri. i'm from Canada. in this video we are going to learn how we
can use the Bluetooth feature of ESP32. we are going to learn how we can turn ON
and OFF an LED using an app from we your mobile device via bluetooth serial
monitor. I'm pressing this and LEDs on half okay you can get the code for this
by clicking at the link below the video in the description which will take you to
www.robojax.com/learn/arduino let's get started with this.
one of the great feature of ESP 32 beside having the Wi-Fi is Bluetooth can
easily configure this so you can communicate with with your app or any
other Bluetooth device to send and receive information simply we are going
to send using serial Bluetooth we can send information or command where serial
Bluetooth so the LED is turned ON and also we can turn it OFF. you can do many
other things so this was a simple example so you can get started with this.
I am assuming that your Arduino IDE is ready to work with ESP32. If your Arduino IDE is not ready
Please watch my video from the link below so your IDE is ready before continuing with this video.
Wiring Explained
let me explain how the wiring I've put here an LED and series with a resistor
the resistor can be anything around 200Ω to 1kΩ but you cannot put
larger than that if you put larger value resistor the intensity of light will be
very low you may not be able to see it. if you put the smaller resistor might
kill that in LED. and this side of the resistor is connected LED, LED
has two pins the larger pin on this side is anode the other side is cathode
anode is connected to the positive so this will be connected to the pin that a
voltage comes three volts 3.3 volts and this is a positive the other side is
connected to this point using this wire. the positive is connected to pin
23 and then the negative on the other side is connected to the ground. and if
you're using a module that the ground is not here connect a wire to the other
ground somewhere else doesn't matter the pin is important here 23
Arduino Code explained
let me now explain the code. I've written this code specifically for this purpose
from the available basic Bluetooth serial
I've modified it. this BluetoothSerial.h is part of the driver for ESP 32
and arduino ide so you don't worry it's already included.
do not worry about about this creating a sense of this class and we call it
serialBT or bluetooth. i've created an integer received which i've not used and
then I've created a character called receivedChar or receive character and
here I've defined 3 variables turnON, turnOFF, and LEDpin. turnON is a
character and I set it as constant so it cannot change when you put a character
put single quote around it. so I'm turning on the LED with 'a' you can change it to
whatever character that is available on the keyboard ASCII character. and then
for off 'b' you can put also a numeric value as well but because I did a
character put it inside a single quote. so we have turnON and turnOFF and
then we've connected our LED to the pin 23 this is an LEDpin. and inside
the setup we initialize the serial monitor so it can display the value for
us on the serial monitor here. the
value here the lower corner it must match (must be 115200) otherwise you will not be able to
see it. the serialBT.begin is initializing it and this is a Bluetooth
name when you search on your device you will see it whatever name you time here
and then these three texts are printed on the serial monitor on this computer.
and then we define the the LEDpin as an output using pinMode so this is as
if I typed 23 and output so distant now acts as an output pin. setup is done.
inside the loop we use serialBT.read this reads the serial Bluetooth
wherever it is and converts it to character and then store the value in
here received car which we've defined at the top of the code this line checks if
the serial monitor has some data from the computer then read the value and
then send it to the Bluetooth or app on the mobile device and then here we check
if the value is for a mobile device then we print the text called "Received" this
will go, remember this serialsBT this goes on the app and then on the same app with
render character that have been received and then also we print this text on the
serial monitor of this computer. and then we print also the character on the
serial monitor of computer. So this two line is for the app and these two lines are
for the serial monitor. and this line and this line we say if received CHAR is
equal equal this two equal sign means compare this value with this. turn on
which we have defined at the top of the code here 'a' and 'b' we check it every is
equal to that then we print this on the screen of the app. and we say LED ON and also
we printed on the serial monitor LED on and when and then using digitalWrite
LED pin, pin 23 is set to HIGH so this will turn ON the LED. and if the value is
equal OFF then we print this tax on the app from this text on the serial monitor
of computer and then using digitalWrite LEDpin we set it to LOW. so this
will turn off the LED F we print multiple time the same off nothing will
happen because the LED is already the pin is LOW we just set it to LOW again
which nothing happens. and then we wait for 20 millisecond and the loop
continuously monitor incoming data.
Selecting COM port and uploading the code to ESP32 board.
after that we have to select the port from here now we don't know which one
for the first time you will find it out by right-clicking on the start menu go
to Device Manager and this (esp32) is already connected scroll down under the ports
collapse it and you will see Silicon lab CP 21 that's 2102 so just driver is for
all 21 0x let me disconnect it so you see that it disappeared and under this
now if I connect it again you see it is connected. so this is a COM4 now
click on tools' port and select COM4. now the board and the port have been
selected.
Now click there to download(or send ) the code on ESP32
you
Installing the App
I'm going to show you how to get the Bluetooth serial monitor app I'm using
android going to play store and first for Bluetooth terminal and I'm
installing the first one. so you should install it too. and then we can continue to
pair a bluetooth device first go to your settings I'm going to the connected
devices Bluetooth here make sure the Bluetooth is on as you can see it's
active now and now this is deactivated it is active and then click touch on
pair it will search for the device and here's for example this is my device
ESP32_Robojax when I touch now this is connected. the next step is to open the
Bluetooth serial and then go to this menu devices and then you will see
Bluetooth device this is green connected and also we are reading a text here
waiting for the connection you see this is failed now let's go ahead and this
time touch this one and it shows connector so that was a previous device.
now we are connected. to disconnect quickly just touch here. and it is
disconnected. and if you touch this again it will be connected.
Turning ON and OFF LED demonstration
Now let me me demonstrate it.
I will connect the app as you can see it is connected to ESP32_Robojax
it says connected. and here I will send 'a' when I press a they LED is ON and here
it shows "Received a" LED ON and after that it shows uppercase with M that the
return character do not worry about. it here (on serial monitor) also it says character is received
LED on and this is for the carriage return because when you send 'a' you have
to send an enter as well that's why two character is sent so you can eliminate
it if you want but it doesn't matter so I did not bother for that. then for this
one to turn it off we send 'b' and LED s OFF now . and the other it shows off just from
here you see now it's disconnected and I'm connecting it again make sure that
it is connected and then we're sending a when I type a here and send it LED is on
now to turn it off remove that and typ 'b' we will turn it off this app has few
buttons that you can program. here if I hold this button you will see that you
can put the text and you can put the character whatever way you wanted hex a
multi line or whatever I just put a text and then I type LED on and then 'a'. now
after that just touch this and that is programmed so I have two buttons program
led on LED off this will send a and therefore send B if I touch it the LED is
ON, touch again with this other one that will off turn on turn off I will have
separate video showing you how to turn on and off with one button just sending
a once it will turn it on and then the other time it will turn it off. for
simplicity here we just use this two buttons a
and be. thank you for watching this was how to control an LED via ESP32
Bluetooth and your mobile device if you learn something and found this useful
please thumb up 👍 asd this will help my video in the search algorithm of YouTube.
you
thank you for watching the fourth how to control an LED via ESp32 Bluetooth
and your mobile device if you learn something and found this useful please
thumb up 👍 and this will help my video in the search algorithm of YouTube if you
have comment or question posted a comment section below I try to answer
and reply and if you want to get updates of my upcoming videos make sure to
subscribe
Subtitle typed and corrected by Ahmad Shamshiri in Canada on August 05, 2020
I did my part to help you learn. Now it's your turn.
You can subscribe to my channel, thumb up 👍 the video and say "thank you" in the comment section.
The link to my PayPal account is below this video if you want to send me chase donation. Thank you.