# Scan QR Codes with CircuitPython

## Overview

Info: 

Info: 

![](https://cdn-learn.adafruit.com/assets/assets/000/103/802/medium800thumb/camera_ezgif.com-optimize%288%29.jpg?1628783013)

CircuitPython 7 adds the `qrio` module, which can decode QR codes from images grabbed with compatible cameras like the OV2640. ([What is a QR code?](https://learn.adafruit.com/qr-codes))

These demos are designed to work with the Espressif Kaluga development kit, which comes with a compatible camera, can connect via WiFi to Adafruit IO, and has plenty of RAM to store the images for analysis.

This guide includes three demos:

- Scan To REPL: Each scanned and decoded barcode is printed in the REPL, so you can see it in mu or other compatible software
- Scan To USB HID: Each scanned and decoded barcode is typed into the connected host computer using a virtual keyboard
- Scan To Adafruit IO: Each scanned and decoded barcode is sent to Adafruit IO, where you can view it on a dashboard or connect it to triggers.

Additionally, each demo uses the Kaluga's LCD screen to show the live image in greyscale as well as the last scanned code.

For the Scan To Web demo, we've made sure the code works with the free version of Adafruit IO, so give it a try even if you haven't subscribed to IO+ yet.

## Creating QR Codes

There are lots of sites & services to generate QR codes. [This one](https://www.the-qrcode-generator.com/) seems pretty no-nonsense.

If you have Python installed on your host computer, you can use the [Adafruit miniQR Library](https://circuitpython.readthedocs.io/projects/miniqr/en/latest/#)to show QR codes in a terminal window.

If you use the Duck Duck Go search engine, you can search for "qr" + your terms, [like so](https://duckduckgo.com/?t=ffsb&q=qr+https%3A%2F%2Fadafrui.it&ia=answer).

![](https://cdn-learn.adafruit.com/assets/assets/000/103/803/medium800/camera_Screenshot_2021-08-12_10-54-15.png?1628783668)

## Overview of QR Scanning

### Create a QR Decoder object

You'll want to create the QR Decoder object just once in your program, if possible. When creating it, pass in the width and height of the image that you will later decode.

`qrdecoder = qrio.QRDecoder(cam.width, cam.height)`

### Capture a greyscale or YUV image

qrio uses only the luminance, greyscale, or "Y" value of pixels (determined by the PixelPolicy value passed to its decode function). For the OV2640 camera, placing it in YUV mode and then using the `EVEN_BYTES` policy gets the correct data.

`cam.colorspace = adafruit_ov2640.OV2640_COLOR_YUV``bitmap = displayio.Bitmap(cam.width, cam.height, 65536)`

### Retrieve decoded strings

A QR code is usually ASCII, but the result of decoding with `qrio` is a byte string "payload" and an encoding name. If it is ASCII or UTF-8, you can decode it with the decode method; if that fails, you can turn it into the ASCII representation of the byte string, which will print like `b"..."`:

`    for row in qrdecoder.decode(bitmap, qrio.PixelPolicy.EVEN_BYTES):`  
`        payload = row.payload`  
`        try:`  
`            payload = payload.decode("utf-8")`  
`        except UnicodeError:`  
`            payload = str(payload)`  
`        print(payload)`

## Limitations of QR Scanning

To keep the program more responsive, the image is captured at a limited 160x120 resolution. This puts a limit on how complex the QR code can be, because each dot within the QR code needs to be at least a couple of pixels big in the recorded image.

Many OV2640 cameras don't focus clearly on close objects, and have trouble taking properly exposed pictures of LCD and OLED screens. QR codes 75 to 100mm (3" to 4" inches) across printed on card stock seem to work best at a scanning distance from 150mm to 300mm (6" to 12"), while scanning from a phone screen is frustrating and rarely works.

A "fish-eye" or wide-angle lens will distort the QR code in a way that qrio doesn't correct for. You need a normal or telephoto lens to scan QR codes. If straight lines don't look straight in your camera, it's not going to work.

## Parts
Featured
### USB Type A Plug Breakout Cable with Premium Female Jumpers

[USB Type A Plug Breakout Cable with Premium Female Jumpers](https://www.adafruit.com/product/4448)
If you'd like to connect a USB-capable chip to your USB host, this cable will make the task very simple. **There is no converter chip in this cable!** Its basically a plain USB cable that's cut in half and with jumper sockets on the power and data lines. Simple to use,...

In Stock
[Buy Now](https://www.adafruit.com/product/4448)
[Related Guides to the Product](https://learn.adafruit.com/products/4448/guides)
![Angled shot of Black, USB-A plug cable with four socket jumpers.](https://cdn-shop.adafruit.com/640x480/4448-01.jpg)

Featured
### USB Extension Cable - 3 meters / 10 ft long

[USB Extension Cable - 3 meters / 10 ft long](https://www.adafruit.com/product/993)
This handy USB extension cable will make it easy for you to extend your USB cable when it won't reach. The connectors are gold plated for years of reliability. We use these handy cables all over our factory and homes. Cable is standard "Adafruit black".  
  
Cable length:...

In Stock
[Buy Now](https://www.adafruit.com/product/993)
[Related Guides to the Product](https://learn.adafruit.com/products/993/guides)
![USB Type A Extension Cable](https://cdn-shop.adafruit.com/640x480/993-03.jpg)

Featured
### Adafruit IO+ Subscription Pass – One Year

[Adafruit IO+ Subscription Pass – One Year](https://www.adafruit.com/product/3792)
The all-in-one Internet of Things service from Adafruit you know and love is now _even better_ with IO+. The 'plus' stands for MORE STUFF! More feeds, dashboards, storage, speed. Power up your [Adafruit IO](https://io.adafruit.com/) with the $99 pass for 1 year of the...

In Stock
[Buy Now](https://www.adafruit.com/product/3792)
[Related Guides to the Product](https://learn.adafruit.com/products/3792/guides)
![Text image that reads "IO+"](https://cdn-shop.adafruit.com/640x480/3792-01.jpg)

# Scan QR Codes with CircuitPython

## Scan To REPL

Info: 

Are you new to using CircuitPython? No worries, [there is a full getting started guide here](https://learn.adafruit.com/welcome-to-circuitpython "Welcome to CircuitPython").

Adafruit suggests using the Mu editor to edit your code and have an interactive REPL in CircuitPython.&nbsp;[You can learn about Mu and installation in this tutorial](https://learn.adafruit.com/welcome-to-circuitpython/installing-mu-editor "Mu tutorial").

## Download the Project Bundle

Your project will use a specific set of CircuitPython libraries and the&nbsp; **code.py** &nbsp;file. In order to get the libraries you need, click on the&nbsp; **Download Project Bundle** &nbsp;link below, and uncompress the .zip file.

Drag the contents of the uncompressed bundle directory onto your board's **CIRCUITPY** &nbsp;drive, replacing any existing files or directories with the same names, and adding any new ones that are necessary.

![Folder](https://adafruit.github.io/Adafruit_Learning_System_Guides/CircuitPython_qrio_repl.png )

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/CircuitPython_qrio/repl/code.py

Once the code is uploaded, the program will automatically start and display a greyscale capture from the camera.

Hold a printed QR code in front of the camera at a distance of about 6". The scanned QR code will be shown on the LCD and also in the REPL.

If something goes wrong, you can use the REPL to diagnose the problem.

# Scan QR Codes with CircuitPython

## Scan To USB HID

Info: 

Are you new to using CircuitPython? No worries, [there is a full getting started guide here](https://learn.adafruit.com/welcome-to-circuitpython "Welcome to CircuitPython").

Adafruit suggests using the Mu editor to edit your code and have an interactive REPL in CircuitPython.&nbsp;[You can learn about Mu and installation in this tutorial](https://learn.adafruit.com/welcome-to-circuitpython/installing-mu-editor "Mu tutorial").

## Download the Project Bundle

Your project will use a specific set of CircuitPython libraries and the&nbsp; **code.py** &nbsp;file. In order to get the libraries you need, click on the&nbsp; **Download Project Bundle** &nbsp;link below, and uncompress the .zip file.

Drag the contents of the uncompressed bundle directory onto your board's **CIRCUITPY** &nbsp;drive, replacing any existing files or directories with the same names, and adding any new ones that are necessary.

![Folder](https://adafruit.github.io/Adafruit_Learning_System_Guides/CircuitPython_qrio_usb_hid.png )

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/CircuitPython_qrio/usb_hid/code.py

Once the code is uploaded, the program will automatically start and display a greyscale capture from the camera.

Hold a printed QR code in front of the camera at a distance of about 6". The scanned QR code will be shown on the LCD and also typed into an attached host computer via USB.

If something goes wrong, you can use the REPL to diagnose the problem.

# Scan QR Codes with CircuitPython

## Scan To Adafruit IO

Info: 

Are you new to using CircuitPython? No worries, [there is a full getting started guide here](https://learn.adafruit.com/welcome-to-circuitpython "Welcome to CircuitPython").

Adafruit suggests using the Mu editor to edit your code and have an interactive REPL in CircuitPython.&nbsp;[You can learn about Mu and installation in this tutorial](https://learn.adafruit.com/welcome-to-circuitpython/installing-mu-editor "Mu tutorial").

## Set up the IO Feed

Create a feed called "barcode". (You can choose another feed name but you'll need to **make sure** that Adafruit IO's "key" for the feed matches what you use in your CircuitPython program!)

You can also add this Feed to a Dashboard, if you want to show it together with information from other Feeds.

![](https://cdn-learn.adafruit.com/assets/assets/000/103/795/medium800/camera_Screenshot_2021-08-10_11-44-24.png?1628613886)

## settings.toml File Setup for Adafruit IO

If you don't have a **settings.toml** file in your **CIRCUITPY** drive yet, create one and add the information about your wifi connection.

Then, add the following code to your **settings.toml** file, **replacing** &nbsp;`your-aio-username` **with your Adafruit IO username.**

Then, **replace** &nbsp;`your-super-long-aio-key` **with your Adafruit IO Active Key**.

```auto
CIRCUITPY_WIFI_SSID="your-wifi-ssid"
CIRCUITPY_WIFI_PASSWORD="your-wifi-password"
ADAFRUIT_AIO_USERNAME="your-aio-username"
ADAFRUIT_AIO_KEY="your-super-long-aio-key"
```

Make sure you **save this file** before proceeding as **settings.toml** in the root directory of your board **CIRCUITPY** drive **.**

## Download the Project Bundle

Your project will use a specific set of CircuitPython libraries and the&nbsp; **code.py** &nbsp;file. In order to get the libraries you need, click on the&nbsp; **Download Project Bundle** &nbsp;link below, and uncompress the .zip file.

Drag the contents of the uncompressed bundle directory onto your board's **CIRCUITPY** &nbsp;drive, replacing any existing files or directories with the same names, and adding any new ones that are necessary.

![Folder](https://adafruit.github.io/Adafruit_Learning_System_Guides/CircuitPython_qrio_adafruit_io.png )

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/CircuitPython_qrio/adafruit_io/code.py

Once the code is uploaded, the program will automatically start and display a greyscale capture from the camera.

Hold a printed QR code in front of the camera at a distance of about 6". The scanned QR code will be shown on the LCD and also uploaded to Adafruit IO via WiFi.

If something goes wrong, you can use the REPL to diagnose the problem.

# Scan QR Codes with CircuitPython

## Documentation: qrio module

# Scan QR Codes with CircuitPython

## Documentation: adafruit_ov2640


## Guide Products

### ESP32-S2 Kaluga Dev Kit featuring ESP32-S2 WROVER

[ESP32-S2 Kaluga Dev Kit featuring ESP32-S2 WROVER](https://www.adafruit.com/product/4729)
The **ESP32-S2-Kaluga-1** kit is a full featured development kit by Espressif for the ESP32-S2 that comes with everything but the kitchen sink! From TFTs to touch panels, this dev board has it all. Espressif designed this kit to demonstrate the ESP32-S2’s human-computer...

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/4729)
[Related Guides to the Product](https://learn.adafruit.com/products/4729/guides)
### USB Type A Plug Breakout Cable with Premium Female Jumpers

[USB Type A Plug Breakout Cable with Premium Female Jumpers](https://www.adafruit.com/product/4448)
If you'd like to connect a USB-capable chip to your USB host, this cable will make the task very simple. **There is no converter chip in this cable!** Its basically a plain USB cable that's cut in half and with jumper sockets on the power and data lines. Simple to use,...

In Stock
[Buy Now](https://www.adafruit.com/product/4448)
[Related Guides to the Product](https://learn.adafruit.com/products/4448/guides)
### USB Extension Cable - 3 meters / 10 ft long

[USB Extension Cable - 3 meters / 10 ft long](https://www.adafruit.com/product/993)
This handy USB extension cable will make it easy for you to extend your USB cable when it won't reach. The connectors are gold plated for years of reliability. We use these handy cables all over our factory and homes. Cable is standard "Adafruit black".  
  
Cable length:...

In Stock
[Buy Now](https://www.adafruit.com/product/993)
[Related Guides to the Product](https://learn.adafruit.com/products/993/guides)
### Adafruit IO+ Subscription Pass – One Year

[Adafruit IO+ Subscription Pass – One Year](https://www.adafruit.com/product/3792)
The all-in-one Internet of Things service from Adafruit you know and love is now _even better_ with IO+. The 'plus' stands for MORE STUFF! More feeds, dashboards, storage, speed. Power up your [Adafruit IO](https://io.adafruit.com/) with the $99 pass for 1 year of the...

In Stock
[Buy Now](https://www.adafruit.com/product/3792)
[Related Guides to the Product](https://learn.adafruit.com/products/3792/guides)

## Related Guides

- [Using Piezo Buzzers with WipperSnapper](https://learn.adafruit.com/using-piezo-buzzers-with-wippersnapper.md)
- [Using ItsaSNAP for HomeKit PIR Motion Detection](https://learn.adafruit.com/itsasnap-homekit-pir-motion-detection.md)
- [No-Code Rain Sensing Smart Desktop Umbrella Stand](https://learn.adafruit.com/no-code-rain-sensing-smart-desktop-umbrella-stand.md)
- [No-Code Counters and Email Reports with Adafruit IO Actions](https://learn.adafruit.com/no-code-counters-and-email-reports-with-adafruit-io-actions.md)
- [CircuitPython OctoPrint Controller and Monitor](https://learn.adafruit.com/circuitpython-octoprint-controller-and-monitor.md)
- [DIY IoT Doorbell Camera with MEMENTO](https://learn.adafruit.com/diy-iot-doorbell-camera-with-memento.md)
- [Use Apple HomeKit Devices with itsaSNAP and Adafruit IO](https://learn.adafruit.com/use-apple-homekit-devices-with-itsasnap.md)
- [Where's My Friend? A Location-Aware Display with PyPortal and ItsASnap](https://learn.adafruit.com/where-s-my-friend-a-location-display-frame-with-pyportal.md)
- [Capturing Camera Images with CircuitPython](https://learn.adafruit.com/capturing-camera-images-with-circuitpython.md)
- [Quick Start: Pico W / 2W with WipperSnapper](https://learn.adafruit.com/quick-start-the-pico-w-with-wippersnapper.md)
- [Monitor Your Greenhouse with a No-Code Environmental Sensor](https://learn.adafruit.com/monitor-your-greenhouse-with-a-no-code-environmental-sensor.md)
- [Halloween Neon LED Signs](https://learn.adafruit.com/halloween-neon-led-signs.md)
- [No-Code IKEA Vindriktning Air Quality Sensor Hack with Adafruit IO](https://learn.adafruit.com/no-code-ikea-vindriktning-hack-with-qt-py-esp32-s3-and-adafruit-io.md)
- [MicroLipo v2 Case](https://learn.adafruit.com/microlipo-case.md)
- [CircuitPython Webcam with OV2640](https://learn.adafruit.com/circuitpython-webcam-with-ov2640.md)
