Arduino Compiles and Uploads but Does Not Run Code on Board

hero

Python with Arduino Boards

Learn well-nigh compatibility between the popular Python language and Arduino boards, how to ready your surroundings, and discover in-depth tutorials and useful links.

Overview

MicroPython is an implementation of the Python programming language that comes with a subset of the Python standard library, and is designed to run on microcontrollers. A great advantage of using MicroPython is that information technology is easy to larn and has great documentation for a number of boards. At the moment, there are four boards that can be used together with MicroPython, you can read more than about them in the compatible boards section.

Arduino officially supports OpenMV's co-operative of MicroPython, and through the OpenMV IDE yous tin can install MicroPython, connect/disconnect your lath and upload your scripts

Arduino boards officially supporting MicroPython.
Arduino boards officially supporting MicroPython.

There's quite the difference betwixt how we program an Arduino board with the Arduino IDE, using the Arduino programming language (based on C++), and how we programme it using MicroPython. When uploading what nosotros phone call a sketch to a lath, we first compile the sketch we write, and then upload it to the board, replacing the old sketch with a new.

To apply MicroPython, nosotros first need to install it on the board. Then, we can load a

                                    script              .              py                              

, similar the following blink example:

                          

1 import fourth dimension

2 from machine import Pivot

3

iv led = Pin ( 6 , Pin . OUT )

5

6 while ( Truthful ) :

7 led . on ( )

8 fourth dimension . sleep_ms ( 250 )

9 led . off ( )

x time . sleep_ms ( 250 )

As MicroPython is already running on the board, we don't need to compile and upload the lawmaking, nosotros only need to provide the instructions (which is done via serial communication).

When installing MicroPython on a board, it can merely run MicroPython scripts, until we "uninstall" information technology. To put the board back in "normal manner" we demand to reset the bootloader, which is a unique process for each board. These instructions are available in the compatible boards section in this commodity.

OpenMV Editor

The Arduino IDE does non yet support Python. Instead, nosotros tin use OpenMV, a platform that supports programming Arduino boards with MicroPython. Through the OpenMV editor, we can install MicroPython, and upload scripts straight to the board. There's besides a number of examples available direct in the editor.

The OpenMV editor.
The OpenMV editor.

OpenMV Examples

Further down this article, y'all can observe a lot of useful code examples that will help you to get started.

You tin can also bank check out the full listing of examples in the OpenMV's GitHub repository.

Compatible Boards

There are currently four Arduino boards that officially supports MicroPython. They are listed below:

  • Nano 33 BLE
  • Nano 33 BLE Sense
  • Nano RP2040 Connect
  • Portenta H7

All of above are also compatible with the OpenMV IDE.

Nano 33 BLE

The Nano 33 BLE
The Nano 33 BLE

If you need help getting started with MicroPython on the Nano 33 BLE board, you tin can check out the tutorial beneath:

  • Getting started with MicroPython with Nano 33 BLE (setting upwards your environment).
  • Nano 33 BLE Python API guide (a collection of useful scripts).

To reset the bootloader on the Nano 33 BLE board, double tap the reset push speedily. This will reset your board to manufacturing plant setting.

Nano 33 BLE Sense

The Nano 33 BLE + BLE Sense
The Nano 33 BLE + BLE Sense

If you need help getting started with MicroPython on the Nano 33 BLE Sense board, you can bank check out the tutorials below:

  • Getting started with MicroPython with Nano 33 BLE Sense (setting upwardly your surround).
  • Nano 33 BLE Sense Python guide (a drove of useful scripts).

To reset the bootloader on the Nano 33 BLE Sense board, double tap the reset button quickly. This will reset your board to manufactory setting.

Nano RP2040 Connect

The Nano RP2040 Connect
The Nano RP2040 Connect

If you demand help getting started with MicroPython on the Nano RP2040 Connect board, you tin can check out the tutorials below:

  • Getting Started with OpenMV and MicroPython on Nano RP2040 Connect
  • Nano RP2040 Connect Python guide

To reset the bootloader, you will need to short to connect a jumper wire between the REC and GND pin, and press the reset button. More than detailed instructions are available in the Nano RP2040 Connect technical reference.

Portenta H7

The Portenta H7
The Portenta H7

If you lot need aid getting started with MicroPython on the Portenta H7 board, you can cheque out the tutorial below:

  • Getting Started with OpenMV and MicroPython on Portenta H7

Acquire Python

As MicroPython is an implementation of the Python linguistic communication, you can besides run a lot of Python scripts straight on the board. For example, running this python script on your figurer also works when running it on your board.

                          

one value1 = 2

2 value2 = v

iii

4 print ( value1 + value2 )

This ways it'due south time to learn the Python language, which there is a lot of resources for. We recommend taking a await at the following resource to better understand the Python linguistic communication:

  • python.org
  • learnpython.org
  • w3schools guide to Python

MicroPython Docs

Visit the MicroPython documentation for an understanding on how Python runs on microcontrollers.

Note that many examples will non work directly with Arduino boards, but will provide an understanding of how Python can run on a microcontroller.

API

Below you will find some useful examples that can exist used by whatsoever Arduino board. For more specific features, such every bit on-board sensors, connectivity and advice, please refer to the individual guides:

  • Nano RP2040 Connect Python API guide
  • Nano BLE API guide
  • Nano BLE Sense API guide

Print

A simple script that volition impress

                                    "Hello world!"                              

every second.

                          

1 import time

two

3 content = "How-do-you-do world!"

four

5 while True :

6 print ( content )

seven time . sleep ( 1 )

Functions

This script prints

                                    "Howdy world!"                              

every second. In addition,

                                    counter_function              (              )                              

likewise

                          

1 import fourth dimension

2

3 content = "Howdy world!"

4 count = 0

5

half dozen def counter_function ( ) :

7 global count

8 count = count + 1

9

10 while True :

11 counter_function ( )

12 print ( content , count )

xiii time . sleep ( 1 )

For loop

Simple use of a for loop and functions. This script counts to 10, and so back to 0.

                          

1 import time

two

3 content = "How-do-you-do world!"

4 count = 0

5

6 def function_increase ( ) :

seven global count

viii count = count + ane

9 print ( count )

x

11 def function_decrease ( ) :

12 global count

13 count = count - ane

fourteen print ( count )

15

16 while True :

17 for x in range ( 10 ) :

18 function_increase ( )

nineteen time . slumber ( 1 )

20

21 for x in range ( 10 ) :

22 function_decrease ( )

23 time . sleep ( 1 )

Digital Write

Writes a loftier and low value to a digital pin every one second. Also prints state in the terminal.

                          

1 from machine import Pin

two import utime

3

iv p2 = Pin ( 25 , Pin . OUT )

five

vi while ( True ) :

vii p2 . value ( 0 )

8 print ( "off" )

ix utime . sleep ( 1 )

10 p2 . value ( 1 )

xi print ( "on" )

12 utime . sleep ( i )

Digital Read (pull up)

Reading digital pins with a

                                    PULL_UP                              

configuration.

                          

1 from machine import Pin

ii import utime

3

four p2 = Pin ( 25 , Pin . IN , Pin . PULL_UP )

5

six while ( True ) :

7 print ( p2 . value ( ) )

8 utime . sleep ( ane )

Digital Read (pull down)

Reading digital pins with a

                                    PULL_DOWN                              

configuration.

                          

1 from car import Pivot

2 import utime

three

4 p2 = Pin ( 25 , Pin . IN , Pivot . PULL_DOWN )

v

vi while ( Truthful ) :

7 impress ( p2 . value ( ) )

eight utime . sleep ( 1 )

Analog Read

Read an analog pivot and impress it to the terminal with a filibuster of 0.5 seconds.

                          

1 import machine

2 import time

3

4 # Make sure to follow the GPIO map for the board you are using.

5 # Pin 29 in this case is the "A3" pivot on the Nano BLE / BLE Sense

6 adc_pin = machine . Pin ( 29 )

seven adc = automobile . ADC ( adc_pin )

8

9 while True :

10 reading = adc . read_u16 ( )

11 print ( "ADC: " , reading )

12 fourth dimension . sleep_ms ( 500 )

PWM

Write a specific duty to a specific pin.

                          

1 from machine import Pin , PWM , ADC

2

3 pwm = PWM ( Pin ( 15 ) )

4 duty = 30000 #betwixt 0-65000

v

6 pwm . freq ( 1000 )

7

viii while Truthful :

9 pwm . duty_u16 ( duty )

Delay

To use a uncomplicated delay, we can use the

                                    time                              

module. If nosotros want to write in seconds, nosotros can use

                                    fourth dimension              .              sleep              (              seconds              )                              

, and for milliseconds

                                    fourth dimension              .              sleep_ms              (              milliseconds              )                              

.

                          

one import time

2

3 while True :

4 time . sleep ( 0.5 ) #or fourth dimension.sleep_ms(500)

five print ( "Hello earth!" )

Interrupt

Beneath is an example of a simple interrupt that uses a pull upwards button and an LED.

The program blinks an LED, until the button is pressed. The button is attached to an interrupt, which turns off an LED for iii seconds.

                          

ane import car

2 from machine import Pin

3 import time

iv

five interrupt = Faux

6

7 def callback ( pin ) :

eight global interrupt

9 interrupt = True

10

11 led = Pin ( half dozen , Pin . OUT )

12

thirteen push button = machine . Pin ( 25 , machine . Pin . IN , car . Pin . PULL_UP )

14

15 button . irq ( trigger = motorcar . Pin . IRQ_FALLING , handler = callback )

xvi

17 while True :

eighteen

19 led . on ( )

20 time . slumber ( 0.5 )

21 led . off ( )

22 fourth dimension . sleep ( 0.5 )

23

24 if interrupt :

25 state = machine . disable_irq ( )

26 machine . enable_irq ( country )

27 led . off ( )

28 impress ( "Interrupt: LED off for three seconds!" )

29 time . slumber ( 3 )

30 interrupt = Fake

demareehimsed.blogspot.com

Source: https://docs.arduino.cc/learn/programming/arduino-and-python/

0 Response to "Arduino Compiles and Uploads but Does Not Run Code on Board"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel