Convert Figma logo to code with AI

adafruit logocircuitpython

CircuitPython - a Python implementation for teaching coding with microcontrollers

4,026
1,192
4,026
759

Top Related Projects

MicroPython - a lean and efficient Python implementation for microcontrollers and constrained systems

Arduino core for the ESP32

Your Gateway to Embedded Software Development Excellence :alien:

14,111

Arduino IDE 1.x

Quick Overview

CircuitPython is a programming language designed to simplify microcontroller programming for makers and educators. It's a fork of MicroPython, optimized for Adafruit's hardware, and aims to make coding more accessible and beginner-friendly. CircuitPython allows for easy USB-based workflow and supports a wide range of Adafruit boards.

Pros

  • Easy to learn and use, especially for beginners
  • Supports a wide range of Adafruit boards and sensors
  • USB-based workflow allows for quick iterations and easy debugging
  • Large and supportive community with extensive documentation and examples

Cons

  • Performance may be slower compared to compiled languages like C/C++
  • Limited to specific hardware platforms, primarily Adafruit boards
  • May not be suitable for complex, resource-intensive projects
  • Some advanced features of microcontrollers may not be accessible

Code Examples

  1. Blinking an LED:
import board
import digitalio
import time

led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT

while True:
    led.value = True
    time.sleep(0.5)
    led.value = False
    time.sleep(0.5)
  1. Reading a temperature sensor:
import time
import board
import adafruit_dht

dht = adafruit_dht.DHT22(board.D2)

while True:
    try:
        temperature = dht.temperature
        humidity = dht.humidity
        print(f"Temp: {temperature:.1f}°C, Humidity: {humidity:.1f}%")
    except RuntimeError as e:
        print("Reading failed:", e)
    time.sleep(2)
  1. Displaying text on an OLED screen:
import board
import displayio
import adafruit_displayio_ssd1306
import terminalio
from adafruit_display_text import label

displayio.release_displays()
i2c = board.I2C()
display_bus = displayio.I2CDisplay(i2c, device_address=0x3C)
display = adafruit_displayio_ssd1306.SSD1306(display_bus, width=128, height=32)

splash = displayio.Group()
text = "Hello, World!"
text_area = label.Label(terminalio.FONT, text=text, color=0xFFFFFF, x=28, y=15)
splash.append(text_area)
display.show(splash)

Getting Started

  1. Download and install the latest version of CircuitPython for your board from the Adafruit CircuitPython website.
  2. Connect your board to your computer via USB. It should appear as a USB drive named CIRCUITPY.
  3. Create a new file named code.py in the CIRCUITPY drive and add your Python code.
  4. Save the file, and it will automatically run on your board.
  5. To see output or debug, open a serial console (e.g., PuTTY or screen) with a baud rate of 115200.

For more detailed instructions and tutorials, visit the Adafruit Learning System.

Competitor Comparisons

MicroPython - a lean and efficient Python implementation for microcontrollers and constrained systems

Pros of MicroPython

  • More mature project with a larger community and broader device support
  • Smaller memory footprint, allowing it to run on more resource-constrained devices
  • Greater flexibility and customization options for advanced users

Cons of MicroPython

  • Less beginner-friendly documentation and tutorials
  • Fewer built-in libraries and drivers for specific hardware
  • Less consistent API across different boards and microcontrollers

Code Comparison

MicroPython:

import machine
import time

led = machine.Pin(2, machine.Pin.OUT)
while True:
    led.value(not led.value())
    time.sleep(0.5)

CircuitPython:

import board
import digitalio
import time

led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT
while True:
    led.value = not led.value
    time.sleep(0.5)

Both examples demonstrate blinking an LED, but CircuitPython uses more abstracted and beginner-friendly modules like board and digitalio, while MicroPython uses the lower-level machine module.

Arduino core for the ESP32

Pros of arduino-esp32

  • More mature and established ecosystem for ESP32 development
  • Extensive library support and community resources
  • Familiar Arduino-style programming for experienced users

Cons of arduino-esp32

  • Steeper learning curve for beginners compared to CircuitPython
  • Less focus on education and ease of use
  • Requires compilation and flashing, which can be more time-consuming

Code Comparison

CircuitPython:

import board
import digitalio
import time

led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT

while True:
    led.value = True
    time.sleep(0.5)
    led.value = False
    time.sleep(0.5)

arduino-esp32:

#define LED_PIN 2

void setup() {
  pinMode(LED_PIN, OUTPUT);
}

void loop() {
  digitalWrite(LED_PIN, HIGH);
  delay(500);
  digitalWrite(LED_PIN, LOW);
  delay(500);
}

The CircuitPython code is more readable and beginner-friendly, while the arduino-esp32 code follows the traditional Arduino structure. CircuitPython uses Python syntax, making it easier for those familiar with the language, while arduino-esp32 uses C++, which may be more challenging for beginners but offers more low-level control and performance optimization possibilities.

Your Gateway to Embedded Software Development Excellence :alien:

Pros of PlatformIO Core

  • Supports a wide range of platforms and frameworks, not limited to CircuitPython
  • Offers a more comprehensive development ecosystem with library management and build tools
  • Integrates well with various IDEs and text editors

Cons of PlatformIO Core

  • Steeper learning curve for beginners compared to CircuitPython's simplicity
  • Requires more setup and configuration for projects
  • May be overkill for simple microcontroller projects

Code Comparison

CircuitPython example:

import board
import digitalio
import time

led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT

while True:
    led.value = True
    time.sleep(0.5)
    led.value = False
    time.sleep(0.5)

PlatformIO Core example (Arduino framework):

#include <Arduino.h>

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  digitalWrite(LED_BUILTIN, HIGH);
  delay(500);
  digitalWrite(LED_BUILTIN, LOW);
  delay(500);
}

Both examples demonstrate a simple LED blinking program, showcasing the syntax differences between CircuitPython and C++ (Arduino) in PlatformIO.

14,111

Arduino IDE 1.x

Pros of Arduino

  • Larger ecosystem with extensive libraries and community support
  • More mature platform with longer history and broader hardware compatibility
  • Generally faster execution speed for low-level operations

Cons of Arduino

  • Steeper learning curve, especially for beginners
  • Less intuitive syntax compared to Python-based alternatives
  • Limited built-in support for modern features like USB and Bluetooth

Code Comparison

Arduino:

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  digitalWrite(LED_BUILTIN, HIGH);
  delay(1000);
  digitalWrite(LED_BUILTIN, LOW);
  delay(1000);
}

CircuitPython:

import board
import digitalio
import time

led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT

while True:
    led.value = True
    time.sleep(1)
    led.value = False
    time.sleep(1)

The Arduino code uses C++ syntax and requires explicit setup and loop functions. CircuitPython uses Python syntax, which is generally more readable and beginner-friendly. CircuitPython also benefits from Python's extensive standard library and easier-to-use APIs for hardware interactions.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

CircuitPython

.. image:: https://s3.amazonaws.com/adafruit-circuit-python/CircuitPython_Repo_header_logo.png

|Build Status| |Doc Status| |License| |Discord| |Weblate|

circuitpython.org <https://circuitpython.org>__ | Get CircuitPython <#get-circuitpython>__ | Documentation <#documentation>__ | Contributing <#contributing>__ | Branding <#branding>__ | Differences from Micropython <#differences-from-micropython>__ | Project Structure <#project-structure>__

CircuitPython is a beginner friendly, open source version of Python for tiny, inexpensive computers called microcontrollers. Microcontrollers are the brains of many electronics including a wide variety of development boards used to build hobby projects and prototypes. CircuitPython in electronics is one of the best ways to learn to code because it connects code to reality. Simply install CircuitPython on a supported USB board usually via drag and drop and then edit a code.py file on the CIRCUITPY drive. The code will automatically reload. No software installs are needed besides a text editor (we recommend Mu <https://codewith.mu/>_ for beginners.)

Starting with CircuitPython 7.0.0, some boards may only be connectable over Bluetooth Low Energy (BLE). Those boards provide serial and file access over BLE instead of USB using open protocols. (Some boards may use both USB and BLE.) BLE access can be done from a variety of apps including code.circuitpython.org <https://code.circuitpython.org>_.

CircuitPython features unified Python core APIs and a growing list of 300+ device libraries and drivers that work with it. These libraries also work on single board computers with regular Python via the Adafruit Blinka Library <https://github.com/adafruit/Adafruit_Blinka>_.

CircuitPython is based on MicroPython <https://micropython.org>. See below <#differences-from-micropython> for differences. Most, but not all, CircuitPython development is sponsored by Adafruit <https://adafruit.com>_ and is available on their educational development boards. Please support both MicroPython and Adafruit.

Get CircuitPython

Official binaries for all supported boards are available through circuitpython.org/downloads <https://circuitpython.org/downloads>. The site includes stable, unstable and continuous builds. Full release notes are available through GitHub releases <https://github.com/adafruit/circuitpython/releases> as well.

Documentation

Guides and videos are available through the Adafruit Learning System <https://learn.adafruit.com/>__ under the CircuitPython category <https://learn.adafruit.com/category/circuitpython>. An API reference is also available on Read the Docs <http://circuitpython.readthedocs.io/en/latest/?>. A collection of awesome resources can be found at Awesome CircuitPython <https://github.com/adafruit/awesome-circuitpython>__.

Specifically useful documentation when starting out:

  • Welcome to CircuitPython <https://learn.adafruit.com/welcome-to-circuitpython>__
  • CircuitPython Essentials <https://learn.adafruit.com/circuitpython-essentials>__
  • Example Code <https://github.com/adafruit/Adafruit_Learning_System_Guides/tree/master/CircuitPython_Essentials>__

Contributing

See CONTRIBUTING.md <https://github.com/adafruit/circuitpython/blob/main/CONTRIBUTING.md>__ for full guidelines but please be aware that by contributing to this project you are agreeing to the Code of Conduct <https://github.com/adafruit/circuitpython/blob/main/CODE_OF_CONDUCT.md>. Contributors who follow the Code of Conduct <https://github.com/adafruit/circuitpython/blob/main/CODE_OF_CONDUCT.md> are welcome to submit pull requests and they will be promptly reviewed by project admins. Please join the Discord <https://adafru.it/discord>__ too.

Branding

While we are happy to see CircuitPython forked and modified, we'd appreciate it if forked releases not use the name "CircuitPython" or the Blinka logo. "CircuitPython" means something special to us and those who learn about it. As a result, we'd like to make sure products referring to it meet a common set of requirements.

If you'd like to use the term "CircuitPython" and Blinka for your product here is what we ask:

  • Your product is supported by the primary "adafruit/circuitpython" <https://github.com/adafruit/circuitpython>_ repo. This way we can update any custom code as we update the CircuitPython internals.

  • Your product is listed on circuitpython.org <https://circuitpython.org>__ (source here <https://github.com/adafruit/circuitpython-org/>_). This is to ensure that a user of your product can always download the latest version of CircuitPython from the standard place.

  • Your product supports at least one standard "Workflow <https://docs.circuitpython.org/en/latest/docs/workflows.html>__" for serial and file access:

    • With a user accessible USB plug which appears as a CIRCUITPY drive when plugged in.
    • With file and serial access over Bluetooth Low Energy using the BLE Workflow.
    • With file access over WiFi using the WiFi Workflow with serial access over USB and/or WebSocket.
  • Boards that do not support the USB Workflow should be clearly marked.

If you choose not to meet these requirements, then we ask you call your version of CircuitPython something else (for example, SuperDuperPython) and not use the Blinka logo. You can say it is "CircuitPython-compatible" if most CircuitPython drivers will work with it.


Differences from MicroPython <https://github.com/micropython/micropython>__

CircuitPython:

  • Supports native USB on most boards and BLE otherwise, allowing file editing without special tools.
  • Floats (aka decimals) are enabled for all builds.
  • Error messages are translated into 10+ languages.
  • Concurrency within Python is not well supported. Interrupts and threading are disabled. async/await keywords are available on some boards for cooperative multitasking. Some concurrency is achieved with native modules for tasks that require it such as audio file playback.

Behavior


-  The order that files are run and the state that is shared between
   them. CircuitPython's goal is to clarify the role of each file and
   make each file independent from each other.

   -  ``boot.py`` runs only once on start up before
      workflows are initialized. This lays the ground work for configuring USB at
      startup rather than it being fixed. Since serial is not available,
      output is written to ``boot_out.txt``.
   -  ``code.py`` (or ``main.py``) is run after every reload until it
      finishes or is interrupted. After it is done running, the vm and
      hardware is reinitialized. **This means you cannot read state from**
      ``code.py`` **in the REPL anymore, as the REPL is a fresh vm.** CircuitPython's goal for this
      change includes reducing confusion about pins and memory being used.
   -  After the main code is finished the REPL can be entered by pressing any key.
      - If the file ``repl.py`` exists, it is executed before the REPL Prompt is shown
      - In safe mode this functionality is disabled, to ensure the REPL Prompt can always be reached
   -  Autoreload state will be maintained across reload.

-  Adds a safe mode that does not run user code after a hard crash or brown out. This makes it
   possible to fix code that causes nasty crashes by making it available through mass storage after
   the crash. A reset (the button) is needed after it's fixed to get back into normal mode.
-  Safe mode may be handled programmatically by providing a ``safemode.py``.
   ``safemode.py`` is run if the board has reset due to entering safe mode, unless the safe mode
   initiated by the user by pressing button(s).
   USB is not available so nothing can be printed.
   ``safemode.py`` can determine why the safe mode occurred
   using ``supervisor.runtime.safe_mode_reason``, and take appropriate action. For instance,
   if a hard crash occurred, ``safemode.py`` may do a ``microcontroller.reset()``
   to automatically restart despite the crash.
   If the battery is low, but is being charged, ``safemode.py`` may put the board in deep sleep
   for a while. Or it may simply reset, and have ``code.py`` check the voltage and do the sleep.
-  RGB status LED indicating CircuitPython state.
   - One green flash - code completed without error.
   - Two red flashes - code ended due to an exception.
   - Three yellow flashes - safe mode. May be due to CircuitPython internal error.
-  Re-runs ``code.py`` or other main file after file system writes by a workflow. (Disable with
   ``supervisor.disable_autoreload()``)
-  Autoreload is disabled while the REPL is active.
-  ``code.py`` may also be named ``code.txt``, ``main.py``, or ``main.txt``.
-  ``boot.py`` may also be named ``boot.txt``.
-  ``safemode.py`` may also be named ``safemode.txt``.

API
~~~

-  Unified hardware APIs. Documented on
   `ReadTheDocs <https://circuitpython.readthedocs.io/en/latest/shared-bindings/index.html>`_.
-  API docs are Python stubs within the C files in ``shared-bindings``.
-  No ``machine`` API.

Modules
~~~~~~~

-  No module aliasing. (``uos`` and ``utime`` are not available as
   ``os`` and ``time`` respectively.) Instead ``os``, ``time``, and
   ``random`` are CPython compatible.
-  New ``storage`` module which manages file system mounts.
   (Functionality from ``uos`` in MicroPython.)
-  Modules with a CPython counterpart, such as ``time``, ``os`` and
   ``random``, are strict
   `subsets <https://circuitpython.readthedocs.io/en/latest/shared-bindings/time/__init__.html>`__
   of their `CPython
   version <https://docs.python.org/3.4/library/time.html?highlight=time#module-time>`__.
   Therefore, code from CircuitPython is runnable on CPython but not
   necessarily the reverse.
-  tick count is available as
   `time.monotonic() <https://circuitpython.readthedocs.io/en/latest/shared-bindings/time/__init__.html#time.monotonic>`__

--------------

Project Structure
-----------------

Here is an overview of the top-level source code directories.

Core
~~~~

The core code of
`MicroPython <https://github.com/micropython/micropython>`__ is shared
amongst ports including CircuitPython:

-  ``docs`` High level user documentation in Sphinx reStructuredText
   format.
-  ``drivers`` External device drivers written in Python.
-  ``examples`` A few example Python scripts.
-  ``extmod`` Shared C code used in multiple ports' modules.
-  ``lib`` Shared core C code including externally developed libraries
   such as FATFS.
-  ``logo`` The CircuitPython logo.
-  ``mpy-cross`` A cross compiler that converts Python files to byte
   code prior to being run in MicroPython. Useful for reducing library
   size.
-  ``py`` Core Python implementation, including compiler, runtime, and
   core library.
-  ``shared-bindings`` Shared definition of Python modules, their docs
   and backing C APIs. Ports must implement the C API to support the
   corresponding module.
-  ``shared-module`` Shared implementation of Python modules that may be
   based on ``common-hal``.
-  ``tests`` Test framework and test scripts.
-  ``tools`` Various tools, including the pyboard.py module.

Ports
~~~~~

Ports include the code unique to a microcontroller line.

The following ports are available: ``atmel-samd``, ``cxd56``, ``espressif``, ``litex``, ``mimxrt10xx``, ``nordic``, ``raspberrypi``, ``renode``, ``silabs`` (``efr32``), ``stm``, ``unix``.

However, not all ports are fully functional. Some have limited limited functionality and known serious bugs.
For details, refer to the **Port status** section in the `latest release <https://github.com/adafruit/circuitpython/releases/latest>`__ notes.

Boards
~~~~~~

-  Each ``port`` has a ``boards`` directory containing boards
   which belong to a specific microcontroller line.
-  A list of native modules supported by a particular board can be found
   `here <https://circuitpython.readthedocs.io/en/latest/shared-bindings/support_matrix.html>`__.

`Back to Top <#circuitpython>`__

.. |Build Status| image:: https://github.com/adafruit/circuitpython/workflows/Build%20CI/badge.svg
   :target: https://github.com/adafruit/circuitpython/actions?query=branch%3Amain
.. |Doc Status| image:: https://readthedocs.org/projects/circuitpython/badge/?version=latest
   :target: http://circuitpython.readthedocs.io/
.. |Discord| image:: https://img.shields.io/discord/327254708534116352.svg
   :target: https://adafru.it/discord
.. |License| image:: https://img.shields.io/badge/License-MIT-brightgreen.svg
   :target: https://choosealicense.com/licenses/mit/
.. |Weblate| image:: https://hosted.weblate.org/widgets/circuitpython/-/svg-badge.svg
   :target: https://hosted.weblate.org/engage/circuitpython/?utm_source=widget