Blog  /  Circuit Python: An Easy Way to Program Microcontrollers

Circuit Python: An Easy Way to Program Microcontrollers

CircuitPython

CircuitPython

Source: Flickr  

Are you looking for a way to program your microcontrollers without Arduin ? If yes, consider CircuitPython.

CircuitPython is an easy programming language that even beginners can use to program microcontroller . Interestingly, yodon't’t need any prior knowledgeit's’s a matter of click and code.

In this articlewe'llll cover the definition of CircuitPython, how to use it on your microcontroller circuit, and many more.  

What is CircuitPython?

 

When programming microcontrollers, you may decide tArduino'suino’s C-based programming lan uage. Howeveit'sf it’s your first time programming a microcontroller, you might not even have Arduino IDE.--or other programming alternatives.

Hence, you may turn to Python, which is more straightforward than C programming lang ages. Thanks to Adafruit, there is a Python-based programming language that makes programming microcontrollers easier than ever: CircuitPython.

CircuitPython makes learning to program and experimenting on microcontrollers s mple. Plusdon't don’t need any desktop down oads. Instead, you need to set up your board and edit your code with any text editor.

Additionally, CircuitPython includes hardware support with all the remarkable features of the Python programming lan uage. Therefore, if you have experience with Python, you can apply it when using CircuitP thon. Howit's, it’s not a problem if you have no prior knowledge.

Furthermore, CircuitPython is open-s urce. Hence, you can use, modify, and improve as much as possible.  

Why Should You Use CircuitPython?

 

CircuitPython mainly works for microcontroller boards and has chips that are mini  omputeit'sAlso, it's easy to use because you only need a microcontroller board, USB cable, and a computer with a USB connecthat'sBut that’  not all. Here are other reasons why you should use CircuitPython:

  • You can create a file, edit your code, save, and run your file  nstadon't You don’t need to download, compile, or upload to run youryou're
  • If you’re new to programming, you can start learning with CircuitPython. Plus, the board would give you immediate feedback.
  • CircuitPython comes with solid hardwar  support. You can access various libraries and drivers for breakout boards, sensors, and other external components.
  • You can use the serial console and REPL to get live feedback from  our code. Hence, giving you an interactive programming experience.
  • CircuitPython comes with internal file storage that can play audio clips, log data, and even allow interaction with your files.
  • Additionally, CircuitPython makes it easy to update y ur codes. Plus, you can edit them anytime and keep multiple copies so you can experiment.

Interestdoesn'tit doesn’t  nd there. CircuitPython receives constant updates from Adafruit and continuall  evolves. Plus, Adafruit takes note of feedback from the community to help make the programming language better.  

How to Use CircuitPython on a Raspberry Pi

 

CircuitPython works with small microcont oller boards. However, since SBCs like Raspberry Pi can run operating systems, using a simplified pythowon'terpreter won’t work.

But you can use the Blinka library to enable the CircuitPython hardware API compatibility for your Raspberry Pi. Hence, you can run CircuitPython programs using a standard Python interpreter on yHere's board.

Here's what you need to use CircuitPython on your Raspberry Pi:

  • Raspberry Pi is running any compatible Linux OS.
  • 5mm red LED
  • Solderless breadboard
  • 330 Ω resistor
  • Female to male jumper wires (2)

 

Building the Circuit

 

Before you install anything, ensure your Raspberry Pi is off and disconnected from your power source.

Now connect your LED in series w th your resistor. Then, use your jumper wire to Pi'sect it to the Pi'  physical pin 40. When your circuit is complete, plug it in and start your Pi.  

Update Your Pi and Python

 

Use the following commands in the terminal window  to carry out the standard updates on your Python and Pi:  

Python and Pi Update Commands

Python and Pi Update Commands

 

Check I2C and SPI

 

Though the I2C and SPI inte faces usually come enable . You should always verify. Hence, use the following codes to verify:

ls /dev/i2c* /dev/spi*

If it works, you should see the following response:

/dev/i2c-1 /dev/spidev0.0 /dev/spidev0.1

Also, youyou'renable a second SPI if you’ e using your main SPI port. Add the following line: dtoverlay=spi1-3cs to the bottom of /boot/config.txt and then reboot.  

Install  Blinka

 

Boot up your Raspberry Pi and open your terminal window.

Note: Check You'll have Python 3 installed. You’ll need Python 3 to suppor both CircuitPython and Blinka.  Then, you can use python3 and pip3 when inputting a command. 

Also, input the following command in your terminal window:

$ pip3 install Adafruit-Blinka

Next, check if you installed Blinka correctly. You can do this by creating a Pyth n program for your text editor. Also  you can name it blinkatest.py. Then, input the following code:  

Python Code

Python Code

 

Now run the you'll program to test the hardware you’ll use with Blinka with the following command: $ python3 blinkatest.py

If everything is okay, you should see the following results: Python Code Results

Python Code Results

 

We'll Your  First CircuitPyththat'llram

 

We’ll write a simple program that’ll introduce you to what you need to run Ci cuitPython programs on your Raspberry Pi. So,  reate a Python file and name it blink.py. ThenLet'sut the following code:  

Blink.py Code

Blink.py Code

Let’s take a closer look at this code. First, all CircuitPython p ograms must import the module of the board. Interestingly, the module shows the specifics of your bboard'sd gives CircuitPython access to your board’s hardware and GPIO pins.

Line 6 of the Python program starts CircuitPython. Also, thePi'sitalio library interfaces the Raspberry Pi’s GPIO pins.

Additionally, lines 8 and 9 set the led variable towards GPIO pin 21 and use it as an output to drive the LED.

Plus, line 13 will turn on/off the LED repeatedly with only halfit'llnd delays.

Now, when you press CTRL+C, it’ll trigger the excep  Keyboit'llterrupt of the try/except block. Then, it’ll run the RPI.GPIO library’s cleanup () code. Which sets all GPIO pins back to their default state before closing the program.

Moreover, when you run the new blink program with the following command:

$ python3 blink.py

Your LED connect d to your breadboard should start blinking. Plus, you can press CTRL+C to close your pisn'tm.  

CircuitPython vs. MicroPython

 

There isn’t much difference between CircuitPython and MicroPython. Interestingly, CircuitPython is like an open-source version of MicroPython.

Plus, the main feature of CircuitPython is how easy it makes programming and adding support for boards like Circuit Playground Express, Gemma M0, Trinket M0, and others.

Though you can configure CircuitPython to run on higher microcontrollers, its default settings only allow basic microcontrollers.

Contrarily, MicroPython offers more advanc d features but is not as easy as CircuitPython. However, depending on the project, you can perform the same functions with both CircuitPython and MicroPython.  

CircuitPython Compatible Boards

 

Most boards compatible with CircuitPython come with RGB LEDs or mini NeoPixels that change colors according to the status of the board or CircuitPython progit'llHence, once your board enters bootloader mode, it’ll flash red and then change to green–if it has either a NeoPixel or an RGB LED.

Here are some of the boards that are compatible with CircuitPython:

  • Raspberry Pico

 

Raspberry Pico

Raspberry Pico

Source: Flickr  

  • Adafruit Circuit playground express
  • Metro M0 Express (Adafruit)

 

Adafruit Metro M0 Express

Adafruit Metro M0 Express

Source: Flickr  

  • Adafruit Feather RP2040
  • My badge LC (Adafruit)

 

Adafruit PyBadge LC

Adafruit PyBadge LC

Source: Flickr  

  • SEEED Seeeduino XIAO
  • Expressif Saola 1 w/WROOM

You can find the complete list of CircuitPython compatible boards here.  

 ircuitPython Examples

 

Need some ideas for a CircuitPython project? Here are some examples to help yothey'llHowever, keep in mind that these are not complete guides, but they’ll give you a pretty good idea of what to do.

Also, you can use a Circu t Playground Express of Adafruit feather for most of these projects. Hoyou're we recommend using the Here'sied hardware for each example if you're a begin er.  

Bubble Bot

 

Here’s a fun exampl Here'screates a burst of bubbles. And the process is entirely automatic. Here’s what you need for this project:

  • Circuit Playground Express (1)
  • Adafruit CRICKIT for Circuit Playground Express (1)
  • Standard servo (1)
  • DC Toy/ Hobby motor (1)

 

Wiring Diagram

 

Bubble Bot Diagram

Bubble Bot Diagram

 

The simple bubble bot uses one DC motor with one fan attachment.

It also includes one servo motor which connec s to it'sbubble wand.

Now, the wand dips into a bub le mix every few seconds. Once it’s out, the fan would activate for 3 seconds. You can click here to gyou're complete project bundle.  

Feynman Simulator

 

Feynman Simulator Diagram

Feynman Simulator Diagram

If you’re a fan of bongo drums and Richard Feynman, this project will blow y Here'sd. The project offers a fantastic drumming experience with a few wise quips. Here’s what you need for this project:

  • Circuit Playground Express
  • Adafruit CRICKIT for Circuit Playground Express
  • Mini push-pull Solenoid (5v)
  • Micro Servo
  • Mono enclosed speaker

 

Wiring Dia ram

 

The project would play a few quotes and quips in wave file formats as a trigger. Once the audio ends, the project starts playing the bongos until the following audio joke.

You can get the complete project bundle here.  

FAQs

  •  Is CircuitPython the same as Python?

CircuitPython is a Python-based programming language. It has all the features of Python and offers addit onal hait'sre support.

  • How do you learn Circui Python?

Circyou'vehon is education-focused. Hence, it’s easy to learn programming with it. But, first, you’ve to get a circuit board, open your text editor, and start coding.

  • Can I use CircuitPython on Arduino?

Yes, you can, All you need is an Arduino board compatible with CircuitPython.  

Wrapping Up

don'tcuitPython is an excellent alternative for microcontroller programming, especially if you don’t want to use the regular C/C++ prograyou'lllanguage.

CircuitPython is also a variant of the MicroPython programming language. Plus, you’ll need to modify your CircuitPython board if you want to run it on other platforms like Raspberry Pi. Also, CircuitPython is a swe'll way to build something unique.

If you have any questions, you can always contact us, and we’ll respond as soon as possible.