Turning LED OFF and ON on the ESP32 camera module, using bluetooth.

The popular ESP32 camera module that I discussed in my previous post has a large build in LED.
In the standard build in camera program, that LED is always on. In the ESP32 camera example that comes with the ESP32 core in the Arduino IDE, that LED is OFF, both programs have no possibility to toggle the state of the LED, which is a pity.
I had a glance at adding a button, that would be visible in the UI, but that seemed quite an undertaking(see footnote), and I thought it would be easier to use bluetooth for that.
In order to do that, we need to do the following:
Open the Camera example in the IDE and add the below statements to your declaration section (say right above the “// WARNING!!! Make sure that you have either selected ESP32 Wrover Module”)

#include "BluetoothSerial.h"
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
BluetoothSerial SerialBT;
int incoming;
#define LED_BUILTIN 4

Once you have done that, go to the setup() section and add:

  SerialBT.begin("ESP32Camera"); //Bluetooth device name
  Serial.println("The device started, now you can pair it");
  pinMode (LED_BUILTIN, OUTPUT);//Specify that LED pin is output

Subsequently go to the “loop” section. That should only have a delay(10000); statement. Replace the entire ‘void loop() by:

void loop() {
  // put your main code here, to run repeatedly:
   if (SerialBT.available()) //Check if we receive anything from Bluetooth
    incoming = SerialBT.read(); //Read what we recevive 

    if (incoming == 49)
        digitalWrite(LED_BUILTIN, HIGH);
        SerialBT.println("LED turned ON");
    if (incoming == 48)
        digitalWrite(LED_BUILTIN, LOW);
        SerialBT.println("LED turned OFF");

Upload that program and check if the camera functions as normal. Then you need some kind of Bluetooth application on your phone. There are various options, but I like “Serial Bluetooth Terminal“.
To pair the Camera, open the Serial Bluetooth Terminal and tap the 3 horizontal bars in the top left.
Then choose ‘Devices’, make sure you are in the Bluetooth Classic tab, and tap the cogwheel in the top right. There you can scan for available devices and once found pair with ESP32Camera.

Then go back to the ‘Devices’ menu you were before and choose ESP32Camera (it is likely the only one there). Tap the back arrow, so you are in the main terminal menu and tap the ‘connect’  icon in the top right (it is just left of the ‘delete bin’.

The terminal window will show you the connection status.
Then tap in the command window (the one at the bottom), your keyboard comes up and press 1, to turn on the LED and 0 to turn off the LED, press ‘send’ on yr keyboard, or the right arrow in the terminal to send yr command.

Full software found here.

Altering the CameraWebServer file.

Switching the LED ON and OFF, can be done through the webserver as well. One thing that cannot be avoided if you want to do that, is to alter the html file of the webserver. Where is that? Well it is a compressed file that is available in HEX form as the index_ov2640_html_gz, array in the camera_index.h file.
So, in order to change that, you need to convert it to an HTML file and after changing it code it again to HEX. Here I show how to do that.

Fortunately that is not something one needs to do oneself, as it already has been done. Also the Robotzero website  discusses that topic.


8 thoughts on “Turning LED OFF and ON on the ESP32 camera module, using bluetooth.”

  1. Hello, I’m going to combine ESP32CAM with HC-SR501 PIR motion sensor.
    You can see my code here: https://shrib.com/#PocketGopher6ogvKyZ
    I connected the Motion sensor output to pin number 12 or 13 directly(Is it possible to connect motion sensor output to esp32-cam directly?).
    but I got blinking led without any motion detected?
    where is the problem?

    1. At first glance your code seems fine.
      Two things though: with how much volt are you feeding the module. The module as is needs 5 volt but can be made to work with 3.3volt as well. Secondly, the HC-SR501 module dies not do well with the ESP’s because of the radiomodule causing interference.
      Try moving the module further away from your ESP and in case you have an arduino; test your code and module on an arduino to see if it works

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: