1. Home
  2. /
  3. Circuitpython
  4. /
  5. Counter and LED Theme Toggle

Counter and LED Theme Toggle - Circuitpython Typing CST Test

Loading…

Counter and LED Theme Toggle — Circuitpython Code

Demonstrates a simple counter with theme toggling using CircuitPython variables and microcontroller GPIO control (LED indicator).

import board
import digitalio

count = 0
isDark = False

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

def updateUI():
	print(f"Counter: {count}")
	if isDark:
		led.value = True
		print("Theme: Dark")
	else:
		led.value = False
		print("Theme: Light")

def increment():
	global count
	count += 1
	updateUI()

def decrement():
	global count
	count -= 1
	updateUI()

def reset():
	global count
	count = 0
	updateUI()

def toggleTheme():
	global isDark
	isDark = not isDark
	updateUI()

# Simulate actions
updateUI()
increment()
increment()
toggleTheme()
decrement()
reset()

Circuitpython Language Guide

CircuitPython is an open-source derivative of MicroPython, designed by Adafruit to simplify programming microcontrollers for beginners and makers. It emphasizes easy setup, rapid prototyping, and accessible hardware interaction with Python.

Primary Use Cases

  • ▸Educational programming for beginners
  • ▸Prototyping embedded electronics projects
  • ▸Interactive art and maker projects
  • ▸Home automation with microcontrollers
  • ▸Quick testing of sensors and hardware modules

Notable Features

  • ▸Easy-to-use Python APIs for hardware
  • ▸Cross-platform USB support for code editing
  • ▸Automatic code execution on device boot
  • ▸Wide library support for sensors and peripherals
  • ▸Interactive REPL for real-time experimentation

Origin & Creator

Created by Adafruit Industries in 2017, based on MicroPython, to make Python accessible for beginners working with microcontrollers.

Industrial Note

CircuitPython is primarily educational and hobbyist-focused, though it can also be used for light industrial prototyping and interactive installations.

Quick Explain

  • ▸CircuitPython allows developers to write Python code directly for microcontrollers without complex toolchains.
  • ▸It includes simplified APIs for sensors, LEDs, displays, and other hardware devices.
  • ▸Ideal for education, hobbyist electronics, and rapid prototyping of embedded systems.

Core Features

  • ▸Runs on microcontrollers (Adafruit boards, ESP32, etc.)
  • ▸Supports Python 3 syntax with simplified modules
  • ▸Built-in filesystem for editing code directly
  • ▸Plug-and-play USB connectivity
  • ▸Battery-powered and portable projects

Learning Path

  • ▸Learn basic Python programming
  • ▸Understand microcontroller pins and I/O
  • ▸Install and configure CircuitPython on a board
  • ▸Experiment with LEDs, sensors, and motors
  • ▸Build complete interactive projects

Practical Examples

  • ▸Blinking LEDs or NeoPixels
  • ▸Temperature and humidity monitoring
  • ▸Controlling servos or motors
  • ▸Interactive art installations
  • ▸Data logging from multiple sensors

Comparisons

  • ▸Simpler than standard Python for embedded systems
  • ▸More beginner-friendly than MicroPython for beginners
  • ▸Focuses on Adafruit ecosystem compatibility
  • ▸Limited performance compared to C/C++ microcontroller programming
  • ▸Ideal for education and prototyping rather than production embedded systems

Strengths

  • ▸Extremely beginner-friendly
  • ▸Rapid prototyping and iteration
  • ▸Cross-platform editing from Windows, macOS, Linux
  • ▸Extensive Adafruit and community libraries
  • ▸Real-time experimentation with hardware

Limitations

  • ▸Not suitable for performance-critical embedded tasks
  • ▸Limited memory and storage on microcontrollers
  • ▸Not ideal for multi-threaded or heavy computation
  • ▸Smaller ecosystem compared to full Python
  • ▸Primarily focused on Adafruit hardware

When NOT to Use

  • ▸High-performance real-time embedded systems
  • ▸Memory-intensive applications
  • ▸Commercial products requiring long-term support
  • ▸Multi-threaded or multiprocessing tasks
  • ▸Large-scale industrial automation without prototyping focus

Cheat Sheet

  • ▸import board, digitalio - access pins
  • ▸led = digitalio.DigitalInOut(board.D13); led.direction = digitalio.Direction.OUTPUT
  • ▸led.value = True/False - turn on/off LED
  • ▸import time; time.sleep(1) - delay
  • ▸Using REPL: print(variable), test sensor readings

FAQ

  • ▸Is CircuitPython suitable for beginners?
  • ▸Yes, it's designed for learners and makers.
  • ▸Can CircuitPython run on any microcontroller?
  • ▸No, only supported boards with sufficient memory and compatible firmware.
  • ▸Do I need Python knowledge before CircuitPython?
  • ▸Basic Python is helpful but not strictly required.
  • ▸Can CircuitPython connect to the internet?
  • ▸Yes, with Wi-Fi/Ethernet capable boards and libraries.
  • ▸Is it suitable for industrial production?
  • ▸Primarily for prototyping and educational projects, not high-performance industrial systems.

30-Day Skill Plan

  • ▸Week 1: Python basics and REPL usage
  • ▸Week 2: Digital and analog I/O with pins
  • ▸Week 3: Sensor integration and library usage
  • ▸Week 4: Actuators and displays control
  • ▸Week 5: Combine multiple peripherals for interactive projects

Final Summary

  • ▸CircuitPython is an educational and prototyping-focused Python variant for microcontrollers.
  • ▸It simplifies hardware programming with easy-to-use APIs and interactive REPL.
  • ▸Ideal for beginners, makers, and rapid prototyping projects.
  • ▸Supports sensors, displays, actuators, and IoT connectivity.
  • ▸Focuses on accessibility, learning, and creative electronics projects.

Project Structure

  • ▸code.py - main script
  • ▸lib/ - CircuitPython libraries for peripherals
  • ▸boot.py - optional boot configuration
  • ▸data/ - data storage (files, logs)
  • ▸assets/ - optional images, fonts, or media

Monetization

  • ▸Educational kits and courses
  • ▸Maker projects and hobby electronics
  • ▸Interactive installations and prototyping services
  • ▸IoT prototype development
  • ▸STEM education tools and workshops

Productivity Tips

  • ▸Keep code.py short and modular
  • ▸Test peripherals individually before combining
  • ▸Use REPL to quickly verify logic
  • ▸Document hardware connections
  • ▸Update libraries to latest CircuitPython versions

Basic Concepts

  • ▸Modules and libraries for hardware control
  • ▸REPL for interactive Python experimentation
  • ▸code.py for automatic script execution
  • ▸Digital and analog I/O with pins
  • ▸Support for sensors, displays, LEDs, and actuators

Official Docs

  • ▸CircuitPython Official Documentation
  • ▸Adafruit Learning System CircuitPython Guides
  • ▸CircuitPython Library Bundle Documentation
  • ▸MicroPython Reference (for compatibility)
  • ▸Adafruit Hardware Guides

More Circuitpython Typing Exercises

CircuitPython Temperature Sensor MonitorCircuitPython Button Press CounterCircuitPython LED BlinkerCircuitPython Light Sensor AlertCircuitPython Buzzer AlertCircuitPython Motor ControlCircuitPython Distance Sensor MonitorCircuitPython Humidity Sensor MonitorCircuitPython Multi-Sensor Dashboard

Practice Other Languages

CReactPythonC++RustTypeScriptKotlinPHPJavaC#RubyMqlCqlN1qlCypher