IOT

How to design a QR Code Scanner with ESP32 CAM Module & OpenCV

IoT or the internet of things is characterized as a forthcoming innovation that empowers us to control equipment gadgets through the Internet. Homes of the next generation will become more and more self-controlled and mechanized because of the solace it gives, particularly when utilized in a private home.  

In this blog, we are going to design a QR Code Scanner or Reader made with the ESP32 CAM Module and OpenCV. We will create a program and hardware to scan QR codes using the ESP32 Camera module and Python libraries. Previously, we read QR or barcodes with the Maikrt QR Code Scanning Gadget and Arduino, but the module is somewhat expensive. The project gets a little less expensive while using ESP32 CAM.

QR Codes have become an integral part of our daily lives, as we use them nearly everywhere, whether for payments or to access a website or a link. People also include them in their resumes in order to give the link to their social media pages. Not only do I use them personally, but huge tracking and shipping corporations use them as well.

What is a QR Code?

A QR code is an image-like print that is encoded in a specific format that can be read and decoded by computer software.

QR Code Reader ESP32 Camera

The code is decrypted by recognizing the distinct zones and aligning the dark boxes in a logical order. Each dark box indicates if something is selected or not. The dark spot could contain a value of 0,1,2,4,8,16,32,64 or 128,etc.

Hardware Required:

  • ESP32-CAM Board-AI-Thinker ESP32 Camera Module
  • FTDI Module-USB-to-TTL Converter Module
  • USB Cable-5V Mini-USB Data Cable
  • Jumper Wires-Female to Female Connectors

ESP32 CAM Module:

The ESP32 Based Camera Module was developed by AI-Thinker. The controller contains a Wi-Fi + Bluetooth/BLE chip and is powered by a 32-bit CPU. It has a 520 KB internal SRAM and an external 4M PSRAM. UART, SPI, I2C, PWM, ADC, and DAC are all supported by its GPIO Pins.

The module is compatible with the OV2640 Camera Module, which has a camera resolution of 1600 x 1200 pixels. A 24-pin gold plated connector links the camera to the ESP32 CAM Board. A 4GB SD Card can be used on the board. The photographs captured are saved on the SD Card.

ESP32-CAM Features 

  • The smallest 802.11b/g/n Wi-Fi BT SoC module.
  • Low power 32-bit CPU, can also serve the application processor.
  • Up to 160MHz clock speed, summary computing power up to 600 DMIPS.
  • Built-in 520 KB SRAM, external 4MPSRAM.
  • Supports UART/SPI/I2C/PWM/ADC/DAC.
  • Support OV2640 and OV7670 cameras, built-in flash lamp.
  • Support image WiFI upload.
  • Supports TF card.
  • Supports multiple sleep modes.
  • Embedded Lwip and FreeRTOS.
  • Supports STA/AP/STA+AP operation mode.
  • Support Smart Config/AirKiss technology.
  • Support for serial port local and remote firmware upgrades (FOTA).

ESP32-CAM FTDI Connection

  • There is no programmer chip on the PCB. So, any form of USB-to-TTL Module can be used to program this board. FTDI Modules based on the CP2102 or CP2104 chip, or any other chip, are widely accessible.
  • Connect the FTDI Module to the ESP32 CAM Module as shown below.
ESP32 CAM FTDI Module Connection
ESP32-CAMFTDI Programmer
GNDGND
5VVCC
U0RTX
U0TRX
GPIO0GND

Connect the ESP32’s 5V and GND pins to the FTDI Module’s 5V and GND. Connect the Rx to UOT and the Tx to UOR Pin in the same way. The most crucial thing is that you must connect the IO0 and GND pins. The device will now be in programming mode. You can remove it once the programming is completed.

Project PCB Gerber File & PCB Ordering Online

If you don’t want to put the circuit together on a breadboard and instead prefer a PCB. EasyEDA’s online Circuit Schematics & PCB Design tool was used to create the PCB Board for the ESP32 CAM Board. The PCB appears as seen below.

The Gerber File for the PCB is given below. You can simply download the Gerber File and order the PCB from https://www.nextpcb.com/

Download Gerber File: ESP32-CAM Multipurpose PCB

Now you can visit the NextPCB official website by clicking here: https://www.nextpcb.com/. So you will be directed to the NextPCB website.

  • You can now upload the Gerber File to the Website and place an order. The PCB quality is excellent. That is why the majority of people entrust NextPCB with their PCB and PCBA needs.
  • The components can be assembled on the PCB Board.

Installing ESP32CAM Library

Another streaming process will be used instead of the general ESP webserver example. As a result, another ESPCAM library is required. On the ESP32 microcontroller, the esp32cam library provides an object-oriented API for using the OV2640 camera. It’s an esp32-camera library wrapper.

  • Download the zip library as shown in the image from the following Github Link
  • After downloading, unzip the library and place it in the Arduino Library folder. To do so, follow the instructions below:

Open Arduino -> Sketch -> Include Library -> Add .ZIP Library… -> Navigate to downloaded zip file -> add

Source Code/Program for ESP32 CAM Module

  • Object Counting with ESP32 CAM Module source code is available here. Copy and paste the code into the Arduino IDE.
#include <WebServer.h>
#include <WiFi.h>
#include <esp32cam.h>
const char* WIFI_SSID = “ssid”;
const char* WIFI_PASS = “password”;
WebServer server(80);
static auto loRes = esp32cam::Resolution::find(320, 240);
static auto midRes = esp32cam::Resolution::find(350, 530);
static auto hiRes = esp32cam::Resolution::find(800, 600);
void serveJpg()
{
auto frame = esp32cam::capture();
if (frame == nullptr) {
Serial.println(“CAPTURE FAIL”);
server.send(503, “”, “”);
return;
}
Serial.printf(“CAPTURE OK %dx%d %db\n”, frame->getWidth(), frame->getHeight(),
static_cast<int>(frame->size()));
server.setContentLength(frame->size());
server.send(200, “image/jpeg”);
WiFiClient client = server.client();
frame->writeTo(client);
}
Void handleJpgLo()
{
if (!esp32cam::Camera.changeResolution(loRes)) {
Serial.println(“SET-LO-RES FAIL”);
}
serveJpg();
}
Void handleJpgHi()
{
if (!esp32cam::Camera.changeResolution(hiRes)) {
Serial.println(“SET-HI-RES FAIL”);
}
serveJpg();
}
Void handleJpgMid()
{
if (!esp32cam::Camera.changeResolution(midRes)) {
Serial.println(“SET-MID-RES FAIL”);
}
serveJpg();
}
Void setup(){
Serial.begin(115200);
Serial.println();
{
using namespace esp32cam;
Config cfg;
cfg.setPins(pins::AiThinker);
cfg.setResolution(hiRes);
cfg.setBufferCount(2);
cfg.setJpeg(80);
bool ok = Camera.begin(cfg);
Serial.println(ok ? “CAMERA OK” : “CAMERA FAIL”);
}
WiFi.persistent(false);
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
Serial.print(“http://”);
Serial.println(WiFi.localIP());
Serial.println(” /cam-lo.jpg”);
Serial.println(” /cam-hi.jpg”);
Serial.println(” /cam-mid.jpg”);
server.on(“/cam-lo.jpg”, handleJpgLo);
server.on(“/cam-hi.jpg”, handleJpgHi);
server.on(“/cam-mid.jpg”, handleJpgMid);
server.begin();
}
Void loop()
{
server.handleClient();
}

You must make a little adjustment to the code before uploading it. Change the SSID and password variables to match the WiFi network you’re using.

Compile the code and upload it to the ESP32 CAM Board. However, you must follow a few steps each time you post.

  • When you push the upload button, make sure the IO0 pin is shorted to the ground.
  • If you notice dots and dashes during uploading, immediately press the reset button.
  • Remove the I01 pin shorting with Ground and push the reset button one more after the code has been uploaded.
  • If the output is still not the Serial monitor, push the reset button once again.

Now you can see a similar output as in the image below.

So that’s it for the ESP32-CAM section. Because the ESP32-CAM is broadcasting live video, make a note of the IP address displayed.

Python Library Installation

  • In order for the live video stream to appear on our computer, we must develop a Python script that allows us to retrieve the video frames. The first step is to get Python installed. Go to python.org and download Python.
  • Once downloaded, install Python.

Install NumPy, OpenCV, and pyzbar libraries from the command prompt.

  • type: pip install numpy and press enter. After the installation is done.
  • type: pip install opencv-python and press enter.
  • type: pip install pyzbar and press enter, close the command prompt.

Python Code + QR Code Scanner ESP32 CAM

  • Now open Idle or another Python code editor.
  • Now Make a fresh folder. Create a new python file within the folder and put the code below into it.
import cv2
import numpy as np
import pyzbar.pyzbar as pyzbar
import urllib.request

#cap = cv2.VideoCapture(0)
font = cv2.FONT_HERSHEY_PLAIN

url=’http://192.168.1.61/’
cv2.namedWindow(“live transmission”, cv2.WINDOW_AUTOSIZE)

prev=””
pres=””
while True:
img_resp=urllib.request.urlopen(url+’cam-hi.jpg’)
imgnp=np.array(bytearray(img_resp.read()),dtype=np.uint8)
frame=cv2.imdecode(imgnp,-1)
#_, frame = cap.read()

decodedObjects = pyzbar.decode(frame)
for obj in decodedObjects:
pres=obj.data
if prev == pres:
pass
else:
print(“Type:”,obj.type)
print(“Data: “,obj.data)
prev=pres
cv2.putText(frame, str(obj.data), (50, 50), font, 2,
(255, 0, 0), 3)

cv2.imshow(“live transmission”, frame)

key = cv2.waitKey(1)
if key == 27:
break

cv2.destroyAllWindows()
  • Update the URL variable in the preceding code with the IP address copied from the Arduino Serial Monitor. Then save and run the code.

Note: If you’re having problems with the pyzbar library, get Microsoft Visual C++ 2013 Redistributable (x64) – 12.0.30501 from Microsoft. click here

Show some QR Codes in front of the ESP32 CAM module to test the project’s function. The QR Code detail will appear on the computer screen, as seen in the figure below.

QR Code Scanner Reader ESP32 CAM Camera Module

Conclusion:

With the ESP32 CAM Module and the OpenCV Python libraries, this is how you may make your own QR Code Scanner or Reader. We will be back soon with more informative blogs soon.

Leave a Reply

Your email address will not be published.