All scripts dived into here will display decoded QR data live, one will also capture the information into a text file and the final will use specific QR codes to control the GPIO.

Transcript

Hey gang, Tim here at Core Electronics , and today we're using our Raspberry Pi Single-Board Computer to actively search, identify, and record the data of any QR code. Then we'll go a step further and activate GPIO pins using the specific QR codes.

QR, quick response codes, are absolutely everywhere in our modern world and for great reason. Sharing a lot of similarities to barcodes, but instead of a laser, a camera is used to identify the spaces between the black and white square markings. Encoding data in this way is incredibly useful, and with machine learning, it has never been easier to decode their secrets. There are lots of standards and types of QR codes, but this system will work with all common types.

Creating custom codes is easy. For example, on screen right now, I'm creating one using a QR code generator website. Link to it down below. Any QR made here is going to be readily decoded by our Raspberry Pi system that we're making right now.

For our Raspberry Pi system to work, we're going to incorporate the incredible OpenCV software into our code. OpenCV is a huge resource that helps solve real-time computer vision and imaging processing problems. Its purpose here is to find those QR codes.

Here on the table is everything you need to start decoding these mysterious QR codes. Naturally, you're going to need a camera. I'm going to be using the high-quality camera module with a five-millimeter lens, but you can definitely use the Raspberry Pi official camera module version 2 for these purposes. I'm using here a Raspberry Pi 4 model B. The extra power that these have is very valuable for computer vision systems like the one we're building towards here.You're also going to want a microSD card that's been flashed with Raspberry Pi OS that has been connected to the internet, is fully updated and has the camera enabled inside the settings.

We have also installed a number of packages to the Raspberry Pi OS through five terminal lines that can be found in the article and description below. These, along with the normal power supply, HDMI cord, monitor, and peripheries combo, will be all you need to get started.

So, plug in that microSD card, the mouse, the keyboard, and the monitor. Then, connect the ribbon cable to the CSI camera serial interface connector on the Raspberry Pi board. To connect the ribbon cables, lift up the edges of the CSI connector gently, slip the ribbon cable in until it bottoms out, and then push down the edges of the CSI connector.

With that all done, provide power to the system by plugging in the USB-C port, which will start the booting process. Once the booting sequence is completed, you will be welcomed with the Raspberry Pi OS.

Now, I've created three codes to do the QR decoding, and I have fully commented each. So, for those curious to see what's happening behind the curtains, just simply take a look. Jump onto the article page linked down below to download all of these codes. With them downloaded, unzip the contents onto your Raspberry Pi desktop.

The first one to check out is the simplest one named "QR simple code". All you need to do is right-click it and open it up using Thonny IDE or any Python interpreter that you would like. Then, with the code open, run it by pressing the big green run button. As soon as you run the code, you're going to see it actively search for QR codes. When it finds a QRCode like some of these on here, you're going to see that it captures the data and will write it directly into the shell of Thonny as well as on the live stream.

The next code to check out is the QR simple code with CSV. You're going to open it up in the same way as before and this code does exactly what was happening before except this time it's going to take that recorded information and store it into a text file format in a manner which could easily be transferred into Excel. The format used will be CSV (comma separated value) format and each QR code will have the data, date, and a time stamp recorded into a text file.

To capture this information with the code, you're going to need a text file named "database.csv" saved into the same directory, in this case, here into our desktop, as the code. This file is case dependent and if you unzip the download folder, this text file will already be there for you.

The natural next step, which is the final code shown here on the screen, would be to use the QR codes to activate the GPIO pins on our Raspberry Pi. In this example, if a QR code has embedded into it the data "red", when the Raspberry Pi identifies that QR code, it's going to light up the red LED. Similarly, the same will happen for the green LED when it sees a QR code that says "green". This has all been done with only nine lines of added code to the first example, which you can see here and here.

Now, on the table in front of me, I have wired up two LEDs with matching resistors in series for both onto a breadboard and attached the LEDs to the ground pin and the GPIO pin 8 and GPIO pin 4 respectively of the Raspberry Pi. So, with my QR codes in hand, this side with the data "red"...Encoded onto it and the other side with green. Let's run the code and try and light these LEDs up. Is it too bright for you? Oh, red. And then as soon as that happened, the red lit up and red will stay on. So now I'm going to turn it around and show it the green value. Green.

Now, on the table here, I have controlled some simple LEDs, but understand that these LEDs are just placeholders for absolutely anything. GPIO pins are the doorway to controlling almost an endless amount of sensors, motors, actuators, or signals. It's up to you and your curiosity to decide what you're going to do with it.

Hopefully, this has got you excited to whip up some QR codes, tinker with some code, and try it out for yourself. So until next time, stay cozy.

Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.