How to Use the ESP32-CAM to Scan QR Codes

The ESP32—a powerful dual-core microcontroller with built-in wireless capabilities—has become the go-to choice for hobbyists building cool Internet of Things projects. The ESP32-CAM takes it one step further by combining that speedy microcontroller with a camera module.

One exciting project that takes advantage of this powerful combination is scanning QR codes. QR codes are those square barcodes you see on signs and products. They store information like website links or product details. With the ESP32-CAM, you can easily read those codes and see what they say.

Ready to build your own QR code scanner? Let’s gather the necessary parts and write some code to make it work.

Things You Will Need

For this project, you will need the following items:

  • ESP32-CAM module: This will serve as the brains of your project.
  • A micro USB cable: For powering and programming the ESP32-CAM.
  • FTDI Adapter or ESP32-CAM-MB Adapter (optional): If your ESP32-CAM doesn’t have a built-in USB-to-Serial converter, you’ll need this to upload code.
  • Computer with Arduino IDE: You’ll use this to write and upload the code to your ESP32.
  • A QR Code to scan: You can generate one online for testing.

When choosing your ESP32-CAM board for this project, it’s important to pick the right type. You’ll want one that can be easily programmed from your computer and powered up afterwards.

The ESP32-CAM with the ESP32-CAM-MB daughterboard or the newer ESP32-CAM-CH340 are good choices, as they both come with a USB port for programming and power. It’s best to avoid the bare ESP32-CAM board, as it needs a USB-to-Serial converter that you might not have on hand.

Connecting the ESP32-CAM to the Computer

The bare ESP32-CAM module, though powerful, lacks a built-in USB interface for programming and communication with your computer. This means you’ll need a little help to get it up and running. There are three main options you can choose from:

Option 1: Bare ESP32-CAM + FTDI Adapter

If you have a bare ESP32-CAM module, you can use a USB-to-serial adapter (an FTDI adapter) to connect it to your computer like this:

ESP32 CAM FTDI Adapter Connections

Many FTDI adapters have a jumper that lets you choose between 3.3V and 5V. As we are powering the ESP32-CAM with 5V, make sure the jumper is set to 5V.

Please note that the GPIO 0 pin is connected to Ground. This connection is only necessary while programming the ESP32-CAM. Once you have finished programming the module, you must disconnect this connection.

Remember! You’ll have to make this connection every time you want to upload a new sketch.

Option 2: Bare ESP32-CAM + ESP32-CAM-MB Adapter

Using the FTDI Adapter to program the ESP32-CAM is a bit of a hassle. This is why many vendors now sell the ESP32-CAM board along with a small add-on daughterboard called the ESP32-CAM-MB.

You stack the ESP32-CAM on the daughterboard, attach a micro USB cable, and click the Upload button to program your board. It’s that simple.

ESP32 CAM MB Programmer Hardware Overview

Option 3: ESP32-CAM-CH340

If you have an ESP32-CAM-CH340 module, you’re in luck! This variant comes with a CH340 USB-to-serial chip onboard, eliminating the need for additional hardware. Simply connect it to your computer via a micro USB cable and you’re ready to go.

esp32 cam ch340

Keep in mind that you’ll still need to connect the GPIO0 pin to GND during programming, just like with the FTDI adapter.

Setting Up the Arduino IDE

No matter which option you choose, the setup within the Arduino IDE is the same.

Installing the ESP32 Board

To use the ESP32-CAM, or any ESP32, with the Arduino IDE, you must first install the ESP32 board (also known as the ESP32 Arduino Core) via the Arduino Board Manager.

If you haven’t already, follow this tutorial to install the ESP32 board:

Selecting the Board and Port

After installing the ESP32 Arduino Core, restart your Arduino IDE and click on “Select other board and port…” on the top drop-down menu.

arduino ide 2 select board and port menu

A new window will pop up. Search for the specific type of ESP32 board you are using (in our case, it’s the AI Thinker ESP32-CAM).

Next, select the port that corresponds to your ESP32-CAM board. It’s usually labeled something like “/dev/ttyUSB0” (on Linux or macOS) or “COM6” (on Windows).

arduino ide 2 select esp32 cam board and port

That’s it; the Arduino IDE is now set up for the ESP32-CAM!

Installing the QR Reader Library

Alvaro Viebrantz’s library is one of the best options for making your ESP32-CAM scan QR codes. It’s great because it is self-contained and runs entirely on the ESP32-CAM. But unfortunately, the library is not in the Arduino library manager, so we need to install it manually.

To download the library simply click the following link:

Now, open the Arduino IDE, go to Sketch > Include Library > Add .ZIP library and select the downloaded zip file.

add arduino zip library

If everything worked, you’ll get a message “Successfully installed library from ESP32QRCodeReader-master.zip archive”.

esp32qrcodereader library installed message

Programming the ESP32-CAM

The ESP32QRCodeReader library comes with two examples. To access the example sketches, navigate to File > Examples > ESP32QRCodeReader.

esp32qrcodereader library basic sketch

Let’s try out the basic example.

#include <Arduino.h>
#include <ESP32QRCodeReader.h>

ESP32QRCodeReader reader(CAMERA_MODEL_AI_THINKER);

void onQrCodeTask(void *pvParameters) {
  struct QRCodeData qrCodeData;

  while (true) {
    if (reader.receiveQrCode(&qrCodeData, 100)) {
      Serial.println("Found QRCode");
      if (qrCodeData.valid) {
        Serial.print("Payload: ");
        Serial.println((const char *)qrCodeData.payload);
      } else {
        Serial.print("Invalid: ");
        Serial.println((const char *)qrCodeData.payload);
      }
    }
    vTaskDelay(100 / portTICK_PERIOD_MS);
  }
}

void setup() {
  Serial.begin(115200);
  Serial.println();

  reader.setup();

  Serial.println("Setup QRCode Reader");

  reader.beginOnCore(1);

  Serial.println("Begin on Core 1");

  xTaskCreate(onQrCodeTask, "onQrCode", 4 * 1024, NULL, 4, NULL);
}

void loop() {
  delay(100);
}

Testing the QR Code Scanner

Once you’re ready, click the “Upload” button in the Arduino IDE to transfer the code to your ESP32-CAM. After the upload completes, if you’re using an FTDI adapter or an ESP32-CAM-CH340 module, make sure to disconnect the GPIO 0 pin from GND. This is important for the ESP32-CAM to function correctly in normal operation.

Next, open the Serial Monitor within the Arduino IDE and set the baud rate to 115200. To ensure the ESP32-CAM starts running your freshly uploaded code, give the RST button a quick press. You should see the output that look like this:

esp32 qr code scanner output 1

At this point, you are ready to scan QR codes. Give this QR code (created using the-qrcode-generator.com) a try:

sample qr code

At first, you might see messages like “Found QRCode Invalid: ECC failure” in the Serial Monitor. Don’t worry, this is normal. Slowly move the QR code closer to and further away from the camera until it’s recognized correctly. When the ESP32-CAM successfully decodes the QR code, the message in the Serial Monitor will display the content encoded within it.

esp32 qr code scanner output 2

Congratulations, you’ve built your own QR code scanner!

Amrit Prabhu

Amrit Prabhu

Amrit is an Electronics Engineer who loves making complex programming and hardware concepts accessible. He has more than 15 years of experience, having worked as a Senior Programmer Analyst at Mindtree Ltd. and Symantec on major projects like Windows 8, Wolters Kluwer CCH and NSE. Since 2018, he has authored hundreds of tutorials and guides for Last Minute Engineers, helping readers master everything from basic circuits to IoT. You can find him on LinkedIn