|
| 1 | +--- |
| 2 | +title: 'Getting Started with Nicla Vision' |
| 3 | +description: 'This tutorial teaches you how to set up the board, how to use the OpenMV IDE and how to run a MicroPython sketch.' |
| 4 | +difficulty: easy |
| 5 | +tags: |
| 6 | + - Getting Started |
| 7 | + - OpenMV |
| 8 | + - Setup |
| 9 | + - MicroPython |
| 10 | +author: 'Benjamin Dannegård' |
| 11 | +libraries: |
| 12 | + - name: MicroPython |
| 13 | + url: http://docs.MicroPython.org/en/latest/ |
| 14 | +software: |
| 15 | + - openMV |
| 16 | +--- |
| 17 | + |
| 18 | +## Overview |
| 19 | +The OpenMV IDE is meant to provide an Arduino like experience for simple machine vision tasks using a camera sensor. In this tutorial, you will learn about some of the basic features of the OpenMV IDE and how to create a simple MicroPython script. The Nicla Vision board has OpenMV firmware on the board by default, making it easy to connect to the OpenMV IDE. |
| 20 | + |
| 21 | +## Goals |
| 22 | +- The basic features of the OpenMV IDE |
| 23 | +- How to create a simple MicroPython script |
| 24 | +- How to use the OpenMV IDE to run MicroPython on Nicla Vision |
| 25 | + |
| 26 | + |
| 27 | +### Required Hardware and Software |
| 28 | +- Nicla Vision board (<https://store.arduino.cc/products/nicla-vision>) |
| 29 | +- Micro USB cable (either USB A to Micro USB or USB C to Micro USB) |
| 30 | +- OpenMV IDE 2.6.4+ |
| 31 | + |
| 32 | +## Instructions |
| 33 | + |
| 34 | +Using the OpenMV IDE you can run [MicroPython](http://docs.MicroPython.org/en/latest/) scripts on the Nicla Vision board. MicroPython provides a lot of classes and modules that make it easy to quickly explore the features of the Nicla Vision. In this tutorial you will first download the OpenMV IDE and set up the development environment. [Here](https://openmv.io/) you can read more about the OpenMV IDE. OpenMV comes with its own firmware that is built on MicroPython. You will then learn to write a simple script that will blink the on-board RGB LED using some basic MicroPython commands. |
| 35 | + |
| 36 | +### 1. Downloading the OpenMV IDE |
| 37 | + |
| 38 | +Before you can start programming OpenMV scripts for the Nicla Vision you need to download and install the OpenMV IDE. |
| 39 | + |
| 40 | +Open the [OpenMV download](https://openmv.io/pages/download) page in your browser, download the version that you need for your operating system and follow the instructions of the installer. |
| 41 | + |
| 42 | +### 2. Connecting to the OpenMV IDE |
| 43 | + |
| 44 | +Connect the Nicla Vision to your computer via the USB cable if you haven't done so yet. |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | +Click on the "connect" symbol at the bottom of the left toolbar. |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | +A pop-up will ask you how you would like to proceed. Select "Reset Firmware to Release Version". This will install the latest OpenMV firmware on the Nicla Vision. You can leave the option of erasing the internal file system unselected and click "OK". |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | +Nicla Vision's green LED will start flashing while the OpenMV firmware is being uploaded to the board. A terminal window will open which shows you the flashing progress. Wait until the green LED stops flashing and fading. You will see a message saying "DFU firmware update complete!" when the process is done. |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | +The board will start flashing its blue LED when it's ready to be connected. After confirming the completion dialog the Nicla Vision should already be connected to the OpenMV IDE, otherwise click the "connect" button (plug symbol) once again. |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | +### 3. Preparing the Script |
| 65 | + |
| 66 | +Create a new script by clicking the "New File" button in the toolbar on the left side. Import the required module `pyb`: |
| 67 | + |
| 68 | +```python |
| 69 | +import pyb # Import module for board related functions |
| 70 | +``` |
| 71 | + |
| 72 | +A module in Python is a confined bundle of functionality. By importing it into the script it gets made available. For this example we only need `pyb`, which is a module that contains board related functionality such as PIN handling. You can read more about its functions [here](https://docs.micropython.org/en/latest/library/pyb.html). |
| 73 | + |
| 74 | +Now we can create the variables that will control our built-in RGB LED. With `pyb` we can easily control each color. |
| 75 | + |
| 76 | +```python |
| 77 | +redLED = pyb.LED(1) # built-in red LED |
| 78 | +greenLED = pyb.LED(2) # built-in green LED |
| 79 | +blueLED = pyb.LED(3) # built-in blue LED |
| 80 | +``` |
| 81 | + |
| 82 | +Now we can easily distinguish between which color we control in the script. |
| 83 | + |
| 84 | +### 4. Creating the Main Loop in the Script |
| 85 | + |
| 86 | +Putting our code inside a while loop will make the code run continuously. In the loop we turn on an LED with `on`, then we use the `delay` function to create a delay. This function will wait with execution of the next instruction in the script. The duration of the delay can be controlled by changing the value inside the parentheses. The number defines how many milliseconds the board will wait. After the specified time has passed, we turn off the LED with the `off` function. We repeat that for each color. |
| 87 | + |
| 88 | +```python |
| 89 | +while True: |
| 90 | + # Turns on the red LED |
| 91 | + redLED.on() |
| 92 | + # Makes the script wait for 1 second (1000 milliseconds) |
| 93 | + pyb.delay(1000) |
| 94 | + # Turns off the red LED |
| 95 | + redLED.off() |
| 96 | + pyb.delay(1000) |
| 97 | + greenLED.on() |
| 98 | + pyb.delay(1000) |
| 99 | + greenLED.off() |
| 100 | + pyb.delay(1000) |
| 101 | + blueLED.on() |
| 102 | + pyb.delay(1000) |
| 103 | + blueLED.off() |
| 104 | + pyb.delay(1000) |
| 105 | +``` |
| 106 | + |
| 107 | +### 5. Uploading the Script |
| 108 | + |
| 109 | +Here you can see the complete blink script: |
| 110 | + |
| 111 | +```python |
| 112 | +import pyb # Import module for board related functions |
| 113 | + |
| 114 | +redLED = pyb.LED(1) # built-in red LED |
| 115 | +greenLED = pyb.LED(2) # built-in green LED |
| 116 | +blueLED = pyb.LED(3) # built-in blue LED |
| 117 | + |
| 118 | +while True: |
| 119 | + |
| 120 | + # Turns on the red LED |
| 121 | + redLED.on() |
| 122 | + # Makes the script wait for 1 second (1000 milliseconds) |
| 123 | + pyb.delay(1000) |
| 124 | + # Turns off the red LED |
| 125 | + redLED.off() |
| 126 | + pyb.delay(1000) |
| 127 | + greenLED.on() |
| 128 | + pyb.delay(1000) |
| 129 | + greenLED.off() |
| 130 | + pyb.delay(1000) |
| 131 | + blueLED.on() |
| 132 | + pyb.delay(1000) |
| 133 | + blueLED.off() |
| 134 | + pyb.delay(1000) |
| 135 | +``` |
| 136 | + |
| 137 | +Connect your board to the OpenMV IDE and upload the above script by pressing the play button in the lower left corner. |
| 138 | + |
| 139 | + |
| 140 | + |
| 141 | +Now the built-in LED on your Nicla Vision board should be blinking red, green and then blue repeatedly. |
| 142 | + |
| 143 | +## Using the Nicla Visions Camera |
| 144 | + |
| 145 | +You can easily access the camera on the Nicla Vision through OpenMV IDE. Below is a short script that will set up the camera and take an image. The board will blink it's LED to indicate when it will take the picture. The image can be seen in the frame buffer while the script is running. |
| 146 | + |
| 147 | +```python |
| 148 | +import pyb # Import module for board related functions |
| 149 | +import sensor # Import the module for sensor related functions |
| 150 | +import image # Import module containing machine vision algorithms |
| 151 | + |
| 152 | +redLED = pyb.LED(1) # built-in red LED |
| 153 | +blueLED = pyb.LED(3) # built-in blue LED |
| 154 | + |
| 155 | +sensor.reset() # Initialize the camera sensor. |
| 156 | +sensor.set_pixformat(sensor.RGB565) # Sets the sensor to RGB |
| 157 | +sensor.set_framesize(sensor.QVGA) # Sets the resolution to 320x240 px |
| 158 | +sensor.set_vflip(True) # Flips the image vertically |
| 159 | +sensor.set_hmirror(True) # Mirrors the image horizontally |
| 160 | + |
| 161 | +redLED.on() |
| 162 | +sensor.skip_frames(time = 2000) # Skip some frames to let the image stabilize |
| 163 | + |
| 164 | +redLED.off() |
| 165 | +blueLED.on() |
| 166 | + |
| 167 | +print("You're on camera!") |
| 168 | +sensor.snapshot().save("example.jpg") |
| 169 | + |
| 170 | +blueLED.off() |
| 171 | +print("Done! Reset the camera to see the saved image.") |
| 172 | +``` |
| 173 | + |
| 174 | +The camera that comes with the Nicla Vision supports RGB 565 images. That's why we use `sensor.set_pixformat(sensor.RGB565)`, enabling the camera to take an image with color. Then we need to set the resolution of the camera. Here we will use `sensor.set_framesize(sensor.QVGA)`. |
| 175 | + |
| 176 | +Using `sensor.set_vflip` and `sensor.set_hmirror` will help us set the correct orientation of the image. If you hold the board with the USB cable facing down you want to call `sensor.set_vflip(True)`. The image will be mirrored, if you want the image to be displayed as you see it from your perspective, you want to call `sensor.set_hmirror(True)`. |
| 177 | + |
| 178 | +Running this script in OpenMV will show the image that the camera is currently capturing in the top right corner, inside the frame buffer. The on board red LED will be on for a couple of seconds, then the blue LED will turn on, this indicates when the picture is about to be taken. A message will be printed in the serial terminal when the image is taken. |
| 179 | + |
| 180 | + |
| 181 | + |
| 182 | +The image will be saved as "example.jpg" in the boards directory. It is also possible to save the image in a ".bmp" format. If you reset the camera by pressing the reset button the image file will appear in the boards directory. |
| 183 | + |
| 184 | +## Using the Nicla Vision with Arduino IDE |
| 185 | + |
| 186 | +As mentioned before, the Nicla Vision comes with OpenMV firmware pre installed. This makes it easier to use the board with OpenMV out of the box. It is possible to use the Nicla Vision with the Arduino IDE. First make sure that you have the latest core installed. To install the core navigate into **Tools > Board > Boards Manager...**, in the Boards Manager window search for **Nicla Vision MBED** and install it. When this core is installed and you have your board connected to your computer, select the port that the board is connected to and the boards core. You should now be able to upload an Arduino sketch to the board. |
| 187 | + |
| 188 | +If you wish to use the board with OpenMV after it has been used with the Arduino IDE. You have to put the board into bootloader mode and install OpenMV firmware. You do this by double pressing the reset button, located next to the LED. When the board is in bootloader mode and connected to your computer, follow the steps above in the **2. Connecting to the OpenMV IDE** section to connect the board to the OpenMV IDE again. |
| 189 | + |
| 190 | +## Conclusion |
| 191 | +In this tutorial you learned how to use the OpenMV IDE with your Nicla Vision board. You also learned how to control the Nicla Vision's RGB LED with MicroPython functions and to upload the script to your board using the OpenMV IDE. |
| 192 | + |
| 193 | +### Next Steps |
| 194 | +- Experiment with MicroPythons capabilities. If you want some examples of what to do, take a look at the examples included in the OpenMV IDE. Go to: **File > Examples > Arduino > ** in the OpenMV IDE. |
| 195 | +- It is possible to use the board for more advanced image processing tasks. Be sure to take a look at our other tutorials if you want to learn more. |
| 196 | +- Take a look at our other Nicla Vision tutorials which showcase its many uses. You can find them [here](https://docs.arduino.cc/hardware/nicla-vision#tutorials) |
| 197 | + |
| 198 | +## Troubleshooting |
| 199 | + |
| 200 | +### OpenMV Firmware Flashing Issues |
| 201 | +- If the upload of the OpenMV firmware fails during the download, put the board back in bootloader mode and try again. Repeat until the firmware gets successfully uploaded. |
| 202 | +- If the OpenMV IDE still can't connect after flashing the firmware, try uploading the latest firmware using the "Load Specific Firmware File" option. You can find the latest firmware in the [OpenMV Github repository](https://github.com/openmv/openmv/releases). Look for a file named **firmware.bin**. |
| 203 | +- If you see a "OSError: Reset Failed" message, reset the board by pressing the reset button. Wait until you see the blue LED flashing, connect the board to the OpenMV IDE and try running the script again. |
0 commit comments