Unlocking the Future with the MKR Vidor
By Michael Parks, PE, for Mouser Electronics
In 1965, microelectronics pioneer Gordon Moore predicted that the number of components that could fit onto a quarter-inch piece of silicon would double every year. In 1975, he revised this cadence to every two years. While the exact number of months has varied over time, the general trend has continued for decades. His observation has had such a major impact on the semiconductor industry that this prediction has been famously dubbed Moore’s law.
In general, the principle that you can get more processing capability for the price has been a driving factor in the explosion of innovation over the last half-century. Fast forward to today, and you will observe that the breadth of computationally-powerful embedded electronics available at affordable prices has been a boon for professionals and hobbyists alike. Case in point is Arduino’s MKR Vidor 4000. It marks Arduino’s first foray into field-programmable gate array (FPGA) technology, diverting from its mostly microcontroller-centric product lines.
The MKR Vidor 4000 offers a plethora of unique hardware features while maintaining the popular MKR form factor and pinout. The Vidor is equipped with a micro HDMI (High-Definition Multimedia Interface) display port, a Mobile Industry Processor Interface (MIPI) camera connector, and a 25-pin mini Peripheral Component Interface Express (PCIe) connector that are programmable for a developer. All this new hardware is powered by Intel’s Cyclone 10CL016 FPGA along with an Atmel SAM D21 32-bit Arm Cortex M0+ microcontroller. The FPGA can be beneficial for high-speed digital signal processing (DSP) applications such as onboard video processing.
This digital video processing will be the focus of this project as we explore the new features and constructs that the Vidor introduces to the Arduino ecosystem. This project will leverage the OV5647 camera that interfaces via the MIPI interface to detect a QR, and if valid, will unlock a solenoid-based locking mechanism. The video stream from the camera will also be displayed on a computer monitor via the micro HDMI port.
Materials
Bill of Materials (BOM)
The BOM is listed below. Alternatively, you can visit mouser.com and grab all the parts you need from a prebuilt shopping cart, just click here. At the time of this article, the below BOM is about $155 (United States dollars [USD]). Table 1 lists the items in the BOM.
Table 1: MKR Vidor QR Lock BOM
Quantity
|
Mouser P/N
|
Description
|
Schematic P/N
|
|
1
|
782-ABX00022
|
MKR Vidor FPGA Development Board
|
U1
|
|
1
|
854-ZW-MM-20
|
Jumper Wires
|
|
|
1
|
932-MIKROE-1097
|
Breadboard
|
|
|
1
|
474-DEV-14028
|
Raspberry Pi Camera Module
|
OV5467
|
|
1
|
603-CFR-25JR-52220R
|
220Ω 1/4W 5% Resistor
|
R1
|
|
1
|
941-C503BRANCZ0C0AA1
|
RED 5mm LED
|
LED1
|
|
1
|
474-PRT-10811
|
DC Barrel Jack, Breadboard Compatible
|
CN1
|
|
1
|
821-1N4001G-KR0G
|
1A, 50V Rectifier Diode
|
D1
|
|
1
|
512-TIP122TU
|
Darlington Transistor NPN
|
T1
|
|
1
|
603-CFR-25JR-521K
|
1KΩ, 1/4W, 5% Resistor
|
R2
|
|
1
|
485-1512
|
Lock Style Solenoid
|
L1
|
|
1
|
354-PAA121000
|
12VDC/1000mA Wall Power Adapter
|
|
|
1
|
895-CUB-100-BK
|
Micro USB Cable
|
|
Resources
All source files for this project are located on Mouser's GitHub repository.
Hardware Source Files
An EAGLE computer-aided design (CAD) schematic has been included in this hardware folder for those who wish to remix the project to suit their individual needs. For makers, a free version of EAGLE CAD can be downloaded from the web.
Software
The MKR Vidor can be programmed using the Arduino Integrated Development Environment (IDE). We will write the firmware in Wiring, which is a wrapper for the C programming language tailored to Arduino development boards. The firmware source code (.ino) is stored in the software folder.
Figure 1: 3D Model for Camera Cover
3D Files
We have created a cover that can be 3D printed to hide the wiring associated with the mounting of the camera and light-emitting diode (LED). The source stereolithography (STL) 3D CAD file is included in this 3D Files folder of the GitHub repository (Figure 1).
Images Folder
Any visual image or screen captures of the schematic and 3D printer’s files will be stored in this s folder for those who don’t have access or for those who need to view the original source files.
Tools
Here is a list of recommended tools to have on hand to complete this project:
- Micro HDMI to DVI (Digital Visual Interface) adapter
- Digital multimeter
- Needle nose pliers
- Philips and flathead screwdrivers
Overview
The MKR Vidor (Figure 2) introduces a handful of new concepts to the Arduino ecosystem just as the Arduino Yun did a few years back. For most people, the first Arduino they become familiar with is the Uno board. There are some notable differences between the Uno and the MKR Vidor that are worth highlighting before we delve too deeply into the project.
Figure 2: MKR Vidor 4000 FPGA's New Hardware
The first and most obvious is the physical MKR form factor. Arduino Uno shields are not physically compatible with the MKR family of boards. However, there are many MKR shields available and more coming down the road. The compact MKR form factor makes possible the transition from a prototype to a small volume production.
Electrically, the MKR Vidor is like many newer Arduino boards in that the operating voltage is only 3.3V. While this voltage is more energy efficient, it also means that external devices that require 5V or larger will require more complicated or additional power supplies. Be sure to connect the grounds and avoid ground loops! Also, note that the built-in LED found on the input/output (I/O) pin DX (LED_BUILTIN) is ACTIVE LOW! In other words, to illuminate the LED you must set the pin to low, as in the following example:
DigitalWrite(LED_BUILTIN, LOW); // LED turns ON due to being wired for
// active low operation
The MKR Vidor has two board specific libraries available. The VidorPeripherals and the VidorGraphics libraries allow the firmware to access the various unique hardware components of the MKR Vidor including the MIPI camera, HDMI port, and PCIe connector. A third library, WiFiNINA, gives Wi-Fi connectivity to the MKR Vidor and other new Arduino boards using a NINA-W102 Wi-Fi chipset, such as the MKR WIFI 1010 and Arduino Uno Wi-Fi Rev 2. To discover more about these libraries check out the MKR Vidor project page.
Building the Electronics
In this write-up, we are going to focus on using a breadboard. Good engineering practices dictate that you should first test the circuit on a breadboard before moving on to more permanent solutions such as a perfboard or printed circuit board (PCB). Here is a schematic and picture of the breadboard for reference (Figure 3 and Figure 4):
Figure 3: Schematic Capture of the Circuit
Figure 4: Electronics Mounted onto a Breadboard
MKR Vidor
- Place the MKR Vidor onto the breadboard. Ensure the micro USB (Universal Serial Bus) faces the “1” end of the breadboard.
- Ensure that access to the micro HDMI port and the MIPI camera connector is unobstructed.
- Place a wire from the Vidor ground pin to the ground rail on the breadboard.
Notification LED
- Place the 220Ω resistor onto the breadboard.
- Connect the negative lead of the LED to the ground rail of the breadboard.
- Connect the positive lead of the LED to one lead of the 220Ω resistor.
- Wire the opposite lead of the 220Ω resistor to pin D6 on the MKR Vidor.
Solenoid Lock
- Place the 1kΩ resistor onto the breadboard.
- Wire one lead of the 1kΩ resistor to pin D11 on the MKR Vidor.
- Connect the opposite end of the 1kΩ resistor to the base pin of the NPN Darlington pair transistor.
- Wire the emitter pin of the NPN Darlington pair transistor to the ground.
- Wire the negative lead of the solenoid lock to the collector pin of the NPN Darlington pair transistor.
- Place the diode across the solenoid coil, with the cathode connected to the positive lead of the solenoid and the anode connected to the negative lead of the solenoid.
Figure 5: Part's Datasheet Describing Polarity and Pinout
- Place the direct current (DC) barrel jack onto the breadboard.
- Wire the positive lead of the DC barrel jack to the positive lead of the solenoid.
- Wire the negative lead of the DC barrel jack to the ground rail of the breadboard.
Be sure to check the datasheet of each part to ensure no polarization or to verify the pinout (Figure 5). For example, not all bipolar junction transistors (BJTs) have their base-collector-emitter pins in the same order.
OV5467 Camera
- Release the locking mechanism of the MIPI connector on the MKR Vidor by gently pulling it straight out.
- Insert the ribbon cable into the MIPI connector on the MKR Vidor and gently push in the locking mechanism to secure the ribbon cable to the MKR Vidor board.
- Release the locking mechanism of the MIPI connector on the OV5467 camera board by gently pulling it straight out.
- Insert the ribbon cable into the MIPI connector on the OV5467 and gently push in the locking mechanism to secure the ribbon cable to the OV5467 board.
Micro HDMI to Computer Display
- Insert the micro HDMI cable into the micro HDMI port of the MKR Vidor.
- [IF NEEDED] Insert the other end of the HDMI cable into an HDMI to DVI adapter.
- Insert the DVI or HDMI cable into a computer monitor.
- Power on the computer monitor and set the appropriate input.
Software
In this section, we will detail the software side of the project. We will also look at some options for creating QR codes to use with this project.
Figure 6:Debug Messages Process Initialization and Unlock/Lock Cycles
MKR Vidor Firmware
Key Files
VidorQRLockFirmware.ino: The project-specific code for this effort is stored in this file. It is based upon an example provided by Medium One to demonstrate how embedded devices can interact with the IoT platform backend.
Key Libraries
The preprocessor directive #include lets us add libraries into our projects. This promotes code reuse, and unless you have very specific needs, reinventing the wheel is not necessary. This project uses the following libraries:
<VidorGraphics.h>: This library gives the firmware access to capabilities to display images on a computer monitor via the micro HDMI port.
<VidorCamera.h>: This library permits firmware access to functionality that is provided when an OV5467 camera is attached to the MKR Vidor via the MIPI onboard connector.
To download these libraries from the Arduino IDE, click on Sketch from the toolbar, then Include Library, then Manage Libraries. Search for “VidorPeripherals” and “VidorGraphics” and click on Install for both.
Key Variables and Constants
These are variables you might want to tweak depending on your design choices:
VidorCamera vcam: Gives the firmware access the OV5467 camera functions.
const int MAXDIM = 10: Sets the maximum number of trackable QR codes to 10.
const int lockPin = 11: Allows the firmware to control the transistor that operates the solenoid lock on general-purpose input/output (GPIO) pin D11.
const int ledPin = LED_BUILTIN: Allows the firmware to control the external LED on GPIO pin D6.
static unsigned long startMillis = 0: Records the start time for a timer to control a delay between opening and closing the lock without using the delay() function so the rest of the firmware will not stop.
unsigned long currentMillis = 0: Stores the current value of the system’s timer.
const unsigned long period = 8000: Sets how long the lock should remain unlocked after a valid QR code is detected.
static bool lockEngaged = true: Performs as a Boolean variable to keep track of the status of the lock. “True” indicates the lock is locked; “false” indicates the lock is unlocked.
struct qrPtn: Sets the data structure for storing information about a QR code.
static qrPtn qrBufferPtn[MAXDIM]: Acts as an array within the qrPtn data structure storing information about multiple QR codes, up to the value set by the MAXDIM variable.
A quick note on the const keyword: Placing this keyword in front of the variable type ensures the value will not change during execution, in effect, creating a constant instead of a variable. The advantage of const versus #define is as the the variable is being declared as a type, the compiler can ensure that the variable type is respected throughout the code.
a note on the static keyword: The static keyword ensures that the value stored in the variable remains the same, unless specifically changed by the code, even as a function's call goes out of scope. When the function is called again later, the value that was held previously in the static variable will still be there versus having the variable being declared and initiated as it was the first time the function was called.
Timer versus Delay
Sometimes it is necessary to temporarily halt the execution of the firmware — for example, while waiting for serial communication to be established with a desktop computer. By using the delay() function, we can cause the firmware execution to stop completely. However, an end user might perceive this pause as device unresponsiveness or even a frozen state. For this project, we want to continue to interact with the camera even after a QR code is detected and the lock is unlocked. We also want the lock to reset eight seconds later without feeling the delay. To accomplish this, if we set a timer and check it occasionally, we can enable the system to continue to operate while waiting for the eight seconds to elapse. The unsigned long variables delay, startMillis, currentMillis, and the millis() function are what we will use to employ this construct.
When a QR code is detected, the system's timer is read using millis() and stored with startMillis. After every subsequent iteration through the main loop, the timer is checked again and its reading is stored in currentMillis. When the difference between startMillis and currentMillis exceeds the value set by the delay variable, the lock reset functionality as defined in the lockReset() function is executed.
Key Functions
void setup(): The first function to run will initialize many of the hardware and software components needed, such as the serial communications, camera, and HDMI interfaces and interactions with the GPIO pins.
void loop(): This is the core of the functionality that will run continuously. Care is taken to ensure the main loop has minimal code and that functionality is handed off to specialized functions, each responsible for individual tasks needed to make this project work.
void qrDetectHandler(): This function dictates all the essential operations that handle the LED and solenoid, once a valid QR code is detected.
void resetLock(): This function is called after a certain period of time (set by the period variable), that is, after the lock has been opened to reset the lock to its locked state.
qrrec.readQRCode(): This function empowers interaction with the camera and QR detection functionality.
Generating QR Codes
There are quite a few resources through smartphone apps and websites to generate a QR code (Figure 7). For Android users, check out Barcode Generator from the Google Play store. iOS users can download Qrafter from the app store. Otherwise, visit https://www.qrstuff.com for a free, online QR code generator.
Figure 7: Software Options to Generate QR Codes
Assembly
The lock style solenoid sourced for this project is easier to install on cabinet doors than room doors. Keep this in mind if you are looking to implement the project for yourself.
Figure 8: Testbed Frustrations with Real Locks during the Prototype Phase
These are lessons I learned, which you can use to save yourself some frustration in building this project (Figure 8):
- While the physical connector is a micro HDMI port, the signal that is outputted is a DVI signal. This is due to the cost associated with licensing HDMI technology. In working on this project, I did not have success in displaying anything on a computer monitor via a micro HDMI to HDMI adapter. Success was achieved using a micro HDMI to HDMI adapter, then an HDMI to DVI adapter. Furthermore, the monitor that did work was an older flat-screen monitor that only had a DVI and Video Graphics Array (VGA) port. I suspect that either newer monitors may look for the High-bandwidth Digital Content Protection (HDCP) copyright protection mechanisms, which were not present in this case, or that the newer monitors I have were not capable of a 640 x 480 resolution. According to the folks at Arduino, the monitor must be capable of supporting this resolution.
- If I did not see anything displayed on the computer monitor, I double tapped the reset button on the MKR Vidor to put the board into bootloader mode. Doing so caused the onboard, red LED to start blinking and an Arduino logo displayed itself on the screen.
- I also made sure to connect the ground pin of the MKR Vidor with the ground of the 12V DC barrel jack that powers the solenoid. Failure to do so creates floating grounds at different potentials, which results in an inactive system.
Project in Action
Once you have installed and powered up the project, it is time to do a test run. Be sure to aim the camera at a plain background, preferably one with a single color. Otherwise unintended triggers are possible, as the QR detection code can sometimes have false positives. A plain background greatly reduces this possibility.
When running the system for the first time, I recommend that you connect the MKR Vidor to a computer so you can see the debug messages come in over the serial port. Once the computer is hooked up and the MKR Vidor is connected to an external display monitor, it is time to test the camera. Hold up a QR code in front of the camera. Be sure to hold the QR code back a few feet for the camera to focus. If you use a smartphone app to generate the QR code, be sure to turn the phone’s screen light up to about 80 percent of its full brightness. If you print the QR code on a piece of paper, supply enough ambient lighting for the camera to get a lock on the QR code. As soon as the camera locks in the code, the LED should turn red and the solenoid should retract the lock (Figure 9). After eight seconds, the LED will turn off and the lock will reengage. You can close the door at any time.
Figure 9: QR Code Detection Unlocks Door
Lastly, while the MKR Vidor hardware is available for purchase, much of the functionality has yet to be unleashed. The next generation Arduino IDE is still in development. The new IDE will allow FPGA programming via a block-based visual programming language, like the popular Scratch programming tools. Be sure to check back later as more functionality is released. And don’t forget to let us know how well this project worked for you. Would you expand upon this project? If so, let us know on Facebook, Twitter, LinkedIn, Instagram, or Google+.
Michael Parks, P.E. is a contributing writer for Mouser Electronics and the owner of Green Shoe Garage, a custom electronics design studio and technology consultancy located in Southern Maryland. He produces the S.T.E.A.M. Power Podcast to help raise public awareness of technical and scientific matters. Michael is also a licensed Professional Engineer in the state of Maryland and holds a Master’s degree in systems engineering from Johns Hopkins University.