Change Location EUR
 
Mouser Europe - Flag Mouser Europe

Incoterms: DDP is available to customers in EU Member States.
All prices include duty and customs fees on select shipping methods.

Incoterms: DDU applies to most non-EU customers.
Duty, customs fees and taxes are collected at time of delivery.


Please confirm your currency selection:

Euros
Euros are accepted for payment only in EU member states these countries.

US Dollars
USD is accepted in all countries.

Other currency options may also be available - see
Mouser Worldwide.

Bench Talk for Design Engineers

Bench Talk

rss

Bench Talk for Design Engineers | The Official Blog of Mouser Electronics


ArduPy: When Arduino Met Python Mike Parks

(Source: Adafruit)

Much of Python’s recent success has hinged on simplicity and the ability to evolve to meet new challenges. Python continues to venture into new territories, from desktop and web applications to machine learning and data science applications. Recently a curious amalgamation of programming languages was released for embedded systems development. Known as ArduPy—a combination of Arduino and Python—this new tool suite seeks to leverage the Arduino embedded ecosystem’s popularity by offering a way to convert popular Arduino libraries into MicroPython libraries.

ArduPy consists of two major components. First is the ArduPy Core, which acts as the interface to support basic hardware operations on compatible development boards. Because ArduPy leverages MicroPython, which utilizes the universal Arduino API, support for new boards can be accomplished with minimal effort. The second component is the Arduino Library Python wrapper. This takes an Arduino library for an external sensor or actuator and transforms it into MicroPython-compatible code. This definitely can help jumpstart your prototyping with a new sensor by eliminating the need to start from scratch. Given the plethora of Arduino libraries available, this feature is an incredible opportunity for the MicroPython community to further “slither” it’s way into new embedded system projects.

Getting Started

To get started with ArduPy, the first thing to do is download and install the latest version of Python. Python is available for Windows, Mac, and Linux. The latest version can be downloaded here. Once Python is installed in your environment, it is time to download the ArduPy Integrated Platform (AIP). The AIP is a command-line utility that allows developers to quickly and easily interact with any ArduPy-compatible board. To download and install the AIP, open your command-line interface, and issue the following command:

#pip3 install ardupy-aip

Lastly, it is time to download and install Visual Studio Code and the Seeed ArduPy IDE extension. Visual Studio Code can be downloaded here for Windows, Mac, and Linux. Once installed, search the Extensions Market for Seeed ArduPy IDE. Once selected in the search results, click Install, and the plugin will be added to Visual Studio. Now you are set up to take advantage of all the features of the ArduPy toolset.

Why ArduPy?

So what exactly are the advantages of the ArduPy toolset over Arduino or straight MicroPython? According to Seeed, developers of ArduPy, there are several advantages that should appeal to makers and professionals alike, including:

  • Experience a feature rich Python3 programming environment on embedded devices with limited resources.
  • Getting started is quick and easy, thanks to the use of the ubiquitous Visual Studio Code IDE. ArduPy installation and configuration is quick and easy.
  • Support for simple file systems, great for IoT devices that serve up simple web service with HTML, CSS, and JavaScript files.
  • Support for automatic running of boot.py when a board is reset.
  • Support serial terminal interaction (REPL) to make prototyping quick and easy. Test ideas without having to upload new firmware.
  • Access to a rich third-party library that can be made MicroPython compatible thanks to the Arduino Library Python wrapper.
  • Customize your own ArduPy firmware through the AIP without knowing any details of the underlying ArduPy toolset.

First ArduPy Project

To get started, plug a compatible development board into your computer with a USB cable. One recommendation is the Seeed Wio Terminal development board, available for purchase from Mouser; click here. The Wio Terminal is a convenient yet inexpensive microcontroller development platform full of useful functionality, including an LCD display, 2.4GHz/5GHz Wi-Fi (802.11 a/b/g/n), Bluetooth LE 5.0, IMU, microphone, buzzer, microSD card, configurable buttons, light sensor, five-way switch, infrared emitter (IR 940 nm), and is CryptoAuthentication ready. It is powered by Microchip ATSAMD51P19 Arm® Cortex®-M4F core running at 120MHz (boost up to 200MHz) along with 4MB of external Flash and 19 KB of RAM.

Once the Wio (or any ArduPy compatible-board) is plugged into your computer, open a command-line interface, and issue the following command:

#aip board

This will list any ArduPy compatible boards currently hooked up to your computer. If you ever forget this command or to learn how to use the AIP, issue the help command:

#aip help

The help command is the go-to command to learn everything that is possible via the ArduPy command-line tool. Some of the additional commands available through the AIP include:

  • build:         Build ArduPy firmware contains using the installed libraries
  • list:            List installed ArduPy libraries
  • install:       Install Arduino library binding with ArduPy
  • uninstall:   Uninstall ArduPy library
  • flash:         Flash firmware to ArduPy board
  • shell:         Integrated mpfshell for real-time interaction with an ArduPy board

Now that the tools are set up on the development computer, it’s time to get ArduPy firmware onto the development board. Every board will have a unique UF2 ArduPy firmware file that must be downloaded and installed. For the Wio Terminal, that file download can be found here. Next, the Wio Terminal must be plugged into the development computer and put into bootloader mode. This can be accomplished by quickly sliding the power switch twice. If successful, a blue LED will begin to flash, and a USB flash drive titled ARDUPY will appear on the desktop.

Double-clicking the ARDUPY folder will reveal a file titled main.py that contains the default Python script file that automatically runs every time the file is updated. However, if you want the script to run every time the board boots up, create a file name boot.py on your desktop. Let’s add the example code to blink the onboard LED. Using Visual Studio Code to edit the boot.py file, add the following code:

 

import time

import board

from digitalio import DigitalInOut, Direction

 

led = DigitalInOut(board.D13)

led.direction = Direction.OUTPUT

 

while True:

    led.value = True

    print("LED ON")

    time.sleep(1)

    led.value = False

    print("LED OFF")

    time.sleep(1)

 

Save the boot.py file. Next, from the command-line interface, issue the following command:

# aip shell -n -c "put /<your-file-path-here>/boot.py"

Unplug the Wio Terminal and plug it back in, and the code should automatically start running. Your first ArduPy project is done!

Give ArduPy a Spin

ArduPy is still relatively new and is rapidly expanding to include new development boards and Python libraries. To keep the ArduPy firmware lightweight, it only includes a subset of the MicroPython libraries including time, Pin, DAC, ADC, PWM, and LCD. But it is easily expandable to meet the specific needs of any particular application. Furthermore, if you aren’t satisfied with the pre-built libraries, ArduPy provides the means to write your own ArduPy libraries and implement them into ArduoPy projects.

ArduPy is a slick way for those looking to leverage their Python programming experience while learning about embedded development. For those who have been cutting their teeth in the Arduino ecosystem, ArduPy is a great way to learn the Python programming language, which can be useful beyond the embedded environment.

Have you given ArduPy a spin? Let us know in the comments about what projects you’ve built using the ArduPy tools.



« Back


Michael Parks, P.E. is the co-founder of Green Shoe Garage, a custom electronics design studio and embedded security research firm located in Western Maryland. He produces the Gears of Resistance 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.


All Authors

Show More Show More
View Blogs by Date

Archives