Leveraging the Power of CircuitPython for IoT Devices (2024)

CircuitPython and Raspberry Pi Pico can together transform everyday devices into smart wonders. This short tutorial will help you write a simple program for using CircuitPython and upload it to Raspberry Pi Pico.

The Internet of Things (IoT) refers to a network of devices capable of communicating with each other and the internet using unique identifiers. These devices have processing capabilities, high computing power, sensing units, and powerful embedded systems, allowing for applications as varied as ‘smart devices’, ‘smart cities’, and ‘smart schools’. IoT connects everyday objects like lights, monitors, cars, and doors to the internet, enabling the exchange of data and information among people, processes, and things. This data and information find applications in fields like healthcare, education, and agriculture. For instance, IoT can help monitor farming environments, prevent theft by tracking home activities, control machines in factories, and report power outages.

Some of the most popular IoT devices are Alexa and smartwatches, which are used for home automation and healthcare. These devices rely on hardware components called microcontroller units (MCUs) and microprocessor units (MPUs) to perform complex tasks. For example, Alexa uses the NXP i.MX RT106A MCU, enabling it to run the Alexa voice service directly on the device. It also incorporates the MediaTek MT8516 MPU, which supports voice assistants and machine learning applications.

Raspberry Pi Pico, the high-performance MCU

In this article, we will explore an MCU called Raspberry Pi Pico based on the RP2040 microcontroller chip by Raspberry Pi, and learn how to write a sample code for blinking an LED using CircuitPython. Raspberry Pi Pico is a low-cost, high-performance MCU board that uses the RP2040 chip. This chip boasts a dual-core ARM Cortex-M0+ processor, 264KB of SRAM, and 2MB of flash memory. Additionally, it offers 26 GPIO pins, three analogue inputs, and support for I2C, SPI, and UART protocols. Raspberry Pi Pico can run Python or C/C++ codes, making it remarkably easy to program and customise. Let me guide you through setting up your development environment, writing a simple firmware program, and uploading it to your Raspberry Pi Pico board.

Exploring Python for IoT

Python is a widely-used and versatile programming language with applications spanning various domains, including the Internet of Things (IoT). One of the devices that can be used for IoT projects is the Raspberry Pi Pico, as this low-cost microcontroller board can run Python code. To program the Pico with Python, we will use CircuitPython, a Python variant optimised for microcontrollers and embedded systems.

CircuitPython has many built-in libraries and modules that simplify the coding and interfacing with hardware components. To power our Pico device, we will need a power source that can provide 5V and 2A current source. Options include a battery pack or a power adapter with a micro-USB connector. For this article, I have used an Adafruit adapter, which is a USB wall charger compatible with the Pico board. You can also use any other power adapter that meets the specifications and has a micro-USB port.

Circuit connections

Here is a sample of a digital circuit that I created. It connects GPIO pin 22 to a resistor with a minimum resistance of 500 ohms. For a green LED circuit, you can use a 220-ohm resistor. This is because the frequency of red light is the highest in the visible light spectrum, and according to the following equation, energy is directly proportional to frequency:

Energy = h * frequency

This means that red LED bulbs heat up much faster than green LED bulbs. To prevent this, high-resistance resistors are needed. The other end of the resistor connects to the positive terminal of the LED bulb, marked by the LED’s smaller leg. The negative end is connected to the GND (ground) pin of the Raspberry Pi Pico.

Software configuration

To program the Raspberry Pi Pico with CircuitPython, follow these steps:

  • Download the latest version of CircuitPython for Pico from the official website.
  • Connect the Pico to the PC by holding down the BOOTSEL button while inserting the USB cable. This puts the Pico in bootloader mode, and a drive named RPI-RP2 will appear on the screen.
  • Drag and drop the adafruit_circuitpython_etc.uf2 file downloaded in step 1 to the RPI-RP2 drive. This installs CircuitPython on the Pico, and the drive disappears.
  • A new drive called CIRCUITPY appears, which contains the files and folders that are required for CircuitPython to run on the board.

The main file we will use is code.py, which is the default CircuitPython code file executed when the board is powered on or reset. The lib folder contains the libraries or modules for our code.

Writing and running the code

To write and run our code, we can use any text editor or IDE supporting CircuitPython. In this case, we will use Thonny IDE, a simple and beginner-friendly IDE with a built-in CircuitPython interpreter. To use Thonny IDE, we need to select the CircuitPython interpreter from the Tools menu and choose Raspberry Pi Pico as the device.

In Thonny IDE, we can write our code in the editor window and save it as code.py on the CIRCUITPY drive. For example, we can write code to blink an LED light connected to pin 22 of the Pico board with a 0.5 second interval, as shown here:

Blink.py1 import time2 import board3 import digitalio45 led = digitalio.DigitalInOut(board.GP22)6 led.direction = digitalio.Direction.OUTPUT78 while True:9 led.value = not led.value10 time.sleep(0.5)11 led.value = not led.value12 time.sleep(0.5)13

In this article, we have learned how to program the Raspberry Pi Pico with CircuitPython. We covered downloading and installing CircuitPython, using Thonny IDE to write and run code, and demonstrated how to blink an LED light connected to the Pico board.

CircuitPython is a powerful and user-friendly programming language that empowers us to create a wide range of IoT projects using the Pico board and other hardware components.

I am an expert and enthusiast-based assistant. I have access to a wide range of information and can provide assistance on various topics. I can help you with questions and provide insights based on my knowledge and access to search results.

Now, let's dive into the concepts mentioned in the article you provided.

CircuitPython and Raspberry Pi Pico

CircuitPython is a variant of the Python programming language that is optimized for microcontrollers and embedded systems. It provides a simplified and beginner-friendly way to program microcontrollers. Raspberry Pi Pico is a low-cost, high-performance microcontroller board that uses the RP2040 chip developed by Raspberry Pi. It is compatible with CircuitPython and can run Python or C/C++ codes.

Internet of Things (IoT)

The Internet of Things (IoT) refers to a network of devices that can communicate with each other and the internet using unique identifiers. These devices have processing capabilities, high computing power, sensing units, and powerful embedded systems. IoT enables the exchange of data and information among people, processes, and things. It finds applications in various fields such as healthcare, education, agriculture, and home automation. IoT devices can include everyday objects like lights, monitors, cars, and doors.

Microcontroller Units (MCUs) and Microprocessor Units (MPUs)

Microcontroller units (MCUs) and microprocessor units (MPUs) are hardware components used in IoT devices to perform complex tasks. MCUs are integrated circuits that contain a microprocessor, memory, and input/output peripherals on a single chip. They are designed for low-power and real-time applications. MPUs, on the other hand, are more powerful processors that can handle more complex tasks. They are commonly used in devices like smartwatches and voice assistants.

Circuit Connections and LED Blinking

The article mentions creating a digital circuit that connects GPIO pin 22 of the Raspberry Pi Pico to an LED bulb. The circuit includes a resistor to control the flow of current. The code provided in the article demonstrates how to write a program in CircuitPython to blink the LED light connected to pin 22 of the Pico board with a 0.5-second interval. The program uses the time and digitalio libraries to control the LED.

Software Configuration and Programming Raspberry Pi Pico with CircuitPython

To program the Raspberry Pi Pico with CircuitPython, the article provides the following steps:

  1. Download the latest version of CircuitPython for Pico from the official website.
  2. Connect the Pico to the PC by holding down the BOOTSEL button while inserting the USB cable. This puts the Pico in bootloader mode, and a drive named RPI-RP2 will appear on the screen.
  3. Drag and drop the adafruit_circuitpython_etc.uf2 file downloaded in step 1 to the RPI-RP2 drive. This installs CircuitPython on the Pico, and the drive disappears.
  4. A new drive called CIRCUITPY appears, which contains the files and folders required for CircuitPython to run on the board.
  5. The main file used is code.py, which is the default CircuitPython code file executed when the board is powered on or reset. The lib folder contains the libraries or modules for the code .

Thonny IDE and Writing Code

The article suggests using Thonny IDE, a simple and beginner-friendly IDE with a built-in CircuitPython interpreter, to write and run the code for Raspberry Pi Pico. Thonny IDE allows you to select the CircuitPython interpreter from the Tools menu and choose Raspberry Pi Pico as the device. You can write your code in the editor window and save it as code.py on the CIRCUITPY drive. The provided example code in the article demonstrates how to blink an LED light connected to pin 22 of the Pico board with a 0.5-second interval.

CircuitPython is a powerful and user-friendly programming language that empowers users to create a wide range of IoT projects using the Raspberry Pi Pico board and other hardware components.

I hope this information helps you understand the concepts mentioned in the article. If you have any further questions, feel free to ask!

Leveraging the Power of CircuitPython for IoT Devices (2024)
Top Articles
Latest Posts
Article information

Author: Geoffrey Lueilwitz

Last Updated:

Views: 5839

Rating: 5 / 5 (60 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Geoffrey Lueilwitz

Birthday: 1997-03-23

Address: 74183 Thomas Course, Port Micheal, OK 55446-1529

Phone: +13408645881558

Job: Global Representative

Hobby: Sailing, Vehicle restoration, Rowing, Ghost hunting, Scrapbooking, Rugby, Board sports

Introduction: My name is Geoffrey Lueilwitz, I am a zealous, encouraging, sparkling, enchanting, graceful, faithful, nice person who loves writing and wants to share my knowledge and understanding with you.