ESP32-CAM QR Code Reader/Scanner (Arduino IDE)

Learn how to build a QR code reader/scanner with the ESP32-CAM board programmed with Arduino IDE. The ESP32-CAM is constantly using its camera to scan for new QR codes using the ESP32QRCodeReader library and a modified version of the quirc library. When it detects a valid QR code, it prints the QR code data in the Serial Monitor.

ESP32-CAM QR Code Reader Scanner Arduino IDE

Project Overview

Here is a quick overview of how the project works.

ESP32-CAM QR Code Scanner Project Overview
  • The ESP32-CAM is constantly scanning for new QR Codes
  • When it detects a valid QR Code, it prints its data in the Arduino IDE Serial Monitor

This project can be used for a ticket attendance system, for product tracking, to act as an access system by checking QR codes to unlock doors or lockers, and many other applications.

Parts Required

We’ll be using the ESP32-CAM board labeled as AI-Thinker module, but other modules should also work by making the correct pin assignment in the code. Another example is the Freenove ESP32-Wrover CAM Board.

The ESP32-CAM board is a $9 device (or less) that combines an ESP32-S chip, an OV2640 camera, a microSD card slot, and several GPIO pins.

ESP32-CAM board is a $9 device with an OV2640 camera, microSD card slot and several GPIO pins

To follow this tutorial you need the following components:

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

For an introduction to the ESP32-CAM, you can follow the next tutorials:

Preparing the Arduino IDE

We’ll program the ESP32 board using Arduino IDE. So you need the Arduino IDE installed as well as the ESP32 add-on. You can follow the next tutorial to install the ESP32 add-on, if you haven’t already:

Installing the ESP32QRCodeReader

For this tutorial, we’ll use the ESP32QRCodeReader library by Alvarowolfx, which makes it easy to read QR codes with the ESP32-CAM board.

Then, in the Arduino IDE, go to Sketch > Include Library > Add .ZIP library and install the library that you’ve just downloaded in the previous link.

Arduino Install Library Sketch include library add zip library

Code – ESP32-CAM QR Code Reader/Scanner

Copy the QR Code Scanner code to your Arduino IDE.

/*********
  Rui Santos & Sara Santos - Random Nerd Tutorials
  Complete project details at https://RandomNerdTutorials.com/esp32-cam-qr-code-reader-scanner-arduino/
*********/

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

// FOR THIS PROJECT, YOUR ESP32-CAM NEEDS TO HAVE PSRAM.
// Some of the compatible boards: CAMERA_MODEL_AI_THINKER | CAMERA_MODEL_WROVER_KIT | CAMERA_MODEL_ESP_EYE
// CAMERA_MODEL_M5STACK_PSRAM | CAMERA_MODEL_M5STACK_V2_PSRAM | CAMERA_MODEL_M5STACK_WIDE  
ESP32QRCodeReader reader(CAMERA_MODEL_AI_THINKER);

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

  while (true) {
    if (reader.receiveQrCode(&qrCodeData, 100)) {
      Serial.println("Scanned new QRCode");
      if (qrCodeData.valid) {
        Serial.print("Valid payload: ");
        Serial.println((const char *)qrCodeData.payload);
      }
      else {
        Serial.print("Invalid payload: ");
        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);
}

View raw code

How the Code Works

The code starts by including the necessary libraries to perform the QR code scanning with the ESP32-CAM board:

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

Then, create an object called reader for the QR code scanner, for this guide we are using the the AI-Thinker ESP32-CAM model.

// FOR THIS PROJECT, YOUR ESP32-CAM NEEDS TO HAVE PSRAM.
// Some of the compatible boards: CAMERA_MODEL_AI_THINKER | CAMERA_MODEL_WROVER_KIT | CAMERA_MODEL_ESP_EYE
// CAMERA_MODEL_M5STACK_PSRAM | CAMERA_MODEL_M5STACK_V2_PSRAM | CAMERA_MODEL_M5STACK_WIDE

ESP32QRCodeReader reader(CAMERA_MODEL_AI_THINKER);

Note: your board needs to have PSRAM. This code was tested and it should be compatible with these ESP32-CAM board versions:

  • CAMERA_MODEL_AI_THINKER
  • CAMERA_MODEL_WROVER_KIT
  • CAMERA_MODEL_ESP_EYE
  • CAMERA_MODEL_M5STACK_PSRAM
  • CAMERA_MODEL_M5STACK_V2_PSRAM
  • CAMERA_MODEL_M5STACK_WIDE

setup()

In the setup() function start by initializing a Serial communication at 115200 baud rate for debugging purposes.

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

The reader.setup() starts the camera hardware using the library’s default settings for the AI-Thinker board or other board that you chose to define.

reader.setup();
Serial.println("Setup QRCode Reader");

The camera reader will run on CPU core 1 (the ESP32 chip has two cores).

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

Finally, create a FreeRTOS task to run the onQrCodeTask function. This allows us to continue running the QR code scanner in the background.

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

onQrCodeTask()

In the onQrCodeTask function, it keeps constantly scanning for new QR codes with the while (true) and when it detects a valid QR code it prints the data in the Serial Monitor.

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

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

Upload Code to ESP32-CAM AI-Thinker using ESP32-CAM-MB USB Programmer

To upload code to the ESP32-CAM board, attach the ESP32-CAM-MB micro USB programmer to your board (you can learn how it works by reading this guide).

ESP32-CAM-MB Micro USB Programmer CH340G Serial Chip OV2640 Camera

Then, connect the board to your computer using a USB cable.

After that, in your Arduino IDE, go to Tools Board and select the AI-Thinker ESP32-CAM. Or search for that board on the top bar. You must have the ESP32 add-on installed. Otherwise, this board won’t show up on the Boards menu.

Select the AI-Thinker ESP32-CAM in Arduino IDE

Go to Tools Port and select the COM port the ESP32-CAM is connected to.

Note: if the board doesn’t show up, it means that you probably don’t have the CH340C drivers installed on your computer. Go to Google and search “CH340C drivers” followed by your operating system and install the drivers.

Finally, click the Upload button in your Arduino IDE.

Program ESP32-CAM with Arduino IDE

And that’s it! Your QR code scanner code should be running in your ESP32-CAM.

Demonstration

After uploading the code, open the Serial Monitor at a baud rate of 115200. Press the ESP32-CAM reset button. It should initialize the QR code scanner. Check the Arduino IDE Serial Monitor window to see if everything is working as expected.

ESP32-CAM QR Code Scanner Reader Example Arduino IDE Start ok

Point the ESP32-CAM to a QR code, here’s a sample QR code with the data https://randomnerdtutorials.com/.

QR Code Example RandomNerdTutorials.com
ESP32-CAM Camera QR Code Scanner Reader Testing Arduino IDE Serial Monitor Demonstration

The payload data should be printed in the Arduino IDE Serial Monitor:

ESP32-CAM QR Code Scanner Reader Example Arduino IDE Serial Monitor Demonstration

If in the Arduino IDE Serial Monitor, you keep getting this message:

Invalid payload: ECC failure

You might need to use a smaller QR code, make the camera more stable, and point it directly to the QR code with better lighting conditions.

The ESP32-CAM has some limitations in resolution and struggles with motion, so you need to aim it steadily at the QR code. Also, detecting QR codes can be difficult in poor lighting conditions, whether it’s too bright or too dark.

Troubleshooting and Tips

As we’ve mentioned earlier, this QR Code scanner uses a lot of memory, so your ESP32-CAM must have PSRAM. Here are some boards that were tested and should work with this example:

  • CAMERA_MODEL_AI_THINKER
  • CAMERA_MODEL_WROVER_KIT
  • CAMERA_MODEL_ESP_EYE
  • CAMERA_MODEL_M5STACK_PSRAM
  • CAMERA_MODEL_M5STACK_V2_PSRAM
  • CAMERA_MODEL_M5STACK_WIDE

If you’re getting any of the following errors, read our ESP32-CAM Troubleshooting Guide: Most Common Problems Fixed

  • Failed to connect to ESP32: Timed out waiting for packet header
  • Camera init failed with error 0x20001 or similar
  • Brownout detector or Guru meditation error
  • Sketch too big error – Wrong partition scheme selected
  • Board at COMX is not available – COM Port Not Selected
  • Psram error: GPIO isr service is not installed

Wrapping Up

We hope you’ve found this tutorial useful and you can use it in your projects. If you don’t have an ESP32-CAM board, you can click here to get one.

As mentioned previously, we have other tutorials with the ESP32-CAM that you may like:

Thank you for reading.



Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Enjoyed this project? Stay updated by subscribing our newsletter!

Leave a Comment

Download Our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.