|
| 1 | +--- |
| 2 | +title: Getting Started with OpenMV and MicroPython |
| 3 | +coverImage: assets/por_openmv_gs_cover.svg |
| 4 | +tags: [Getting Started, OpenMV, Setup, Blink, MicroPython] |
| 5 | +description: This tutorial teaches you how to set up the board, how to use the OpenMV IDE and how to run a MicroPython blink example with OpenMV. |
| 6 | +--- |
| 7 | + |
| 8 | +# Getting Started with OpenMV and MicroPython |
| 9 | +## Overview |
| 10 | +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. |
| 11 | + |
| 12 | +### You Will Learn |
| 13 | +- The basic features of the OpenMV IDE |
| 14 | +- How to create a simple MicroPython script |
| 15 | +- How to use the OpenMV IDE to run MicroPython on Portenta H7 |
| 16 | + |
| 17 | + |
| 18 | +### Required Hardware and Software |
| 19 | +- Portenta H7 board (<https://store.arduino.cc/portenta-h7>) |
| 20 | +- USB-C cable (either USB-A to USB-C or USB-C to USB-C) |
| 21 | +- Portenta Bootloader Version 20+ |
| 22 | +- OpenMV IDE 2.6.4+ |
| 23 | + |
| 24 | +## Instructions |
| 25 | + |
| 26 | +Using the OpenMV IDE you can run [MicroPython](http://docs.MicroPython.org/en/latest/) scripts on the Portenta H7 board. MicroPython provides a lot of classes and modules that make it easy to quickly explore the features of the Portenta H7. 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. |
| 27 | + |
| 28 | +### 1. Downloading the OpenMV IDE |
| 29 | + |
| 30 | +Before you can start programming OpenMV scripts for the Portenta you need to download and install the OpenMV IDE. |
| 31 | + |
| 32 | +***IMPORTANT: Before you connect the Portenta to the OpenMV IDE make sure you update the bootloader as explained in the "Flashing the OpenMV Firmware" section!*** |
| 33 | + |
| 34 | +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. |
| 35 | + |
| 36 | +### 2. Flashing the OpenMV Firmware |
| 37 | + |
| 38 | +Connect the Portenta to your computer via the USB-C cable if you haven't done so yet. Make sure you first update the bootloader to the latest version using the **PortentaH7_updateBootloader** sketch in the examples menu in the Arduino IDE. |
| 39 | + |
| 40 | +Instructions on how to update the bootloader can be found in the ["Updating the Portenta Bootloader" tutorial](https://www.arduino.cc/pro/tutorials/portenta-h7/por-ard-bl). |
| 41 | + |
| 42 | +After updating the bootloader put the Portenta in bootloader mode by double-pressing the reset button on the board. The built-in green LED will start fading in and out. Now open the OpenMV IDE. |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | +Click on the "connect" symbol at the bottom of the left toolbar. |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | +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 Portenta H7. You can leave the option of erasing the internal file system unselected and click "OK". |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | +Portenta H7'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. |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | +***Installing the OpenMV firmware will overwrite any existing sketches in the internal flash of Portenta H7. Also the M7 port won't be exposed in the Arduino IDE anymore. To re-flash the M7 with an Arduino firmware you need to put the board into bootloader mode. To do so double press the reset button on the Portenta H7 board. The built-in green LED will start fading in and out. In bootloader mode you will see the Portenta M7 port again in the Arduino IDE.*** |
| 59 | + |
| 60 | +The board will start flashing its blue LED when it's ready to be connected. After confirming the completion dialog the Portenta H7 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 | +```py |
| 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 colour. |
| 75 | + |
| 76 | +```py |
| 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 colour. |
| 87 | + |
| 88 | +```py |
| 89 | +while True: |
| 90 | + # Turns on the red LED |
| 91 | + redLED.on() |
| 92 | + # Makes the script wait for 1 second (1000 miliseconds) |
| 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 | +```py |
| 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 miliseconds) |
| 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 Portenta board should be blinking red, green and then blue repeatedly. |
| 142 | + |
| 143 | +## Conclusion |
| 144 | +In this tutorial you learned how to use the OpenMV IDE with your Portenta board. You also learned how to control the Portenta H7's RGB LED with MicroPython functions and to upload the script to your board using the OpenMV IDE. |
| 145 | + |
| 146 | +### Next Steps |
| 147 | +- 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>Portenta H7** in the OpenMV IDE. |
| 148 | +- Take a look at our other Portenta H7 tutorials which showcase its many uses. You can find them [here](https://www.arduino.cc/pro/tutorials/portenta-h7) |
| 149 | + |
| 150 | +## Troubleshooting |
| 151 | +### OpenMV Firmware Flashing Issues |
| 152 | +- 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. |
| 153 | +- 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** in the PORTENTA folder. |
| 154 | +- If you experience issues putting the board in bootloader mode, make sure you first update the bootloader to the latest version using the **PortentaH7_updateBootloader** sketch from the examples menu in the Arduino IDE. |
| 155 | +- 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. |
| 156 | +- In bootloader versions 17 and older there was a bug that could put the Portenta into a boot loop when the transmission aborted while flashing a large firmware file. This was fixed in the bootloader version 18. |
| 157 | + |
| 158 | +**Authors:** Sebastian Romero, Benjamin Dannegård |
| 159 | +**Reviewed by:** Lenard George [2021-04-12] |
| 160 | +**Last revision:** Sebastian Romero [2021-05-03] |
0 commit comments