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

Counter and LED Theme Toggle - Micropython Typing CST Test

Loading…

Counter and LED Theme Toggle — Micropython Code

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

from machine import Pin

count = 0
isDark = False

led = Pin(2, Pin.OUT)

def updateUI():
	print(f"Counter: {count}")
	if isDark:
		led.value(1)
		print("Theme: Dark")
	else:
		led.value(0)
		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()

updateUI()
increment()
increment()
toggleTheme()
decrement()
reset()

Micropython Language Guide

MicroPython is a lean and efficient implementation of Python 3 designed to run on microcontrollers and small embedded systems. It enables developers to write Python code for hardware with minimal overhead while maintaining Python syntax and semantics.

Primary Use Cases

  • ▸Embedded systems programming
  • ▸IoT device prototyping
  • ▸Sensor data acquisition and control
  • ▸Educational microcontroller projects
  • ▸Robotics and automation scripting

Notable Features

  • ▸Lightweight Python 3 interpreter for microcontrollers
  • ▸Built-in hardware access modules
  • ▸REPL for interactive programming
  • ▸Support for various communication protocols
  • ▸Extensible with user modules and libraries

Origin & Creator

Created by Damien George in 2013 as an open-source project to bring Python programming to microcontrollers.

Industrial Note

MicroPython is often used in embedded systems prototyping, IoT development, robotics, and research applications, though some production-level devices also use it for lightweight scripting.

Quick Explain

  • ▸MicroPython allows Python code execution on resource-constrained devices like ESP32, ESP8266, STM32, and RP2040.
  • ▸It provides direct access to hardware peripherals, including GPIO pins, PWM, ADC/DAC, I2C, SPI, and UART.
  • ▸Ideal for embedded prototyping, IoT projects, and educational applications.

Core Features

  • ▸Runs on resource-constrained microcontrollers
  • ▸Direct access to hardware and peripherals
  • ▸Filesystem access via onboard storage or USB
  • ▸Event-driven and procedural programming support
  • ▸Interoperable with MicroPython libraries and extensions

Learning Path

  • ▸Learn Python fundamentals
  • ▸Understand microcontroller architecture and GPIO
  • ▸Install and configure MicroPython on boards
  • ▸Experiment with peripherals and sensors
  • ▸Build small IoT or automation projects

Practical Examples

  • ▸Blinking LEDs and buttons
  • ▸Temperature, humidity, and environmental monitoring
  • ▸Controlling motors and servos
  • ▸Basic IoT data logging and Wi-Fi reporting
  • ▸Robotics or small automation systems

Comparisons

  • ▸More general-purpose than CircuitPython, supports more boards
  • ▸Requires slightly more setup and programming knowledge
  • ▸Smaller abstraction for beginners compared to CircuitPython
  • ▸Higher flexibility for advanced embedded applications
  • ▸Optimized for performance and memory on constrained devices

Strengths

  • ▸Python syntax makes embedded programming accessible
  • ▸Efficient for low-power, low-memory devices
  • ▸Wide support for many microcontroller boards
  • ▸Interactive REPL allows rapid testing and debugging
  • ▸Strong open-source community support

Limitations

  • ▸Limited performance for compute-intensive tasks
  • ▸Smaller standard library than full Python
  • ▸Memory constraints require careful code management
  • ▸Fewer high-level abstractions compared to CircuitPython for beginners
  • ▸Complexity increases with advanced peripheral integration

When NOT to Use

  • ▸Applications requiring high-level GUI or desktop libraries
  • ▸Large-scale production embedded systems without optimization
  • ▸Performance-critical real-time control
  • ▸Tasks requiring large memory or multi-threading
  • ▸Complex industrial automation beyond prototyping

Cheat Sheet

  • ▸import machine, time - core modules
  • ▸led = machine.Pin(2, machine.Pin.OUT); led.value(1) - turn on LED
  • ▸adc = machine.ADC(machine.Pin(36)); value = adc.read()
  • ▸PWM: pwm = machine.PWM(machine.Pin(5)); pwm.duty(512)
  • ▸Network: import network; station = network.WLAN(network.STA_IF)

FAQ

  • ▸Is MicroPython beginner-friendly?
  • ▸Yes, but it assumes some understanding of Python and microcontrollers.
  • ▸Can MicroPython run on all boards?
  • ▸No, only supported boards with sufficient memory and compatible firmware.
  • ▸Do I need Python experience?
  • ▸Basic Python knowledge helps but not strictly required.
  • ▸Can MicroPython handle networking?
  • ▸Yes, with Wi-Fi/Ethernet capable boards and networking modules.
  • ▸Is it suitable for commercial embedded production?
  • ▸Primarily used for prototyping and small-scale devices, not full industrial production.

30-Day Skill Plan

  • ▸Week 1: Python basics and REPL experimentation
  • ▸Week 2: GPIO, PWM, ADC, DAC basics
  • ▸Week 3: Sensor integration and data logging
  • ▸Week 4: Networking and communication modules
  • ▸Week 5: Deploy complete MicroPython scripts with automation

Final Summary

  • ▸MicroPython brings Python 3 to microcontrollers and embedded systems.
  • ▸Supports GPIO, PWM, ADC/DAC, networking, and peripherals.
  • ▸Ideal for prototyping, IoT, robotics, and education.
  • ▸Lightweight and efficient for constrained hardware.
  • ▸Offers REPL and scripting for rapid development and testing.

Project Structure

  • ▸main.py - primary script executed at boot
  • ▸boot.py - optional initialization script
  • ▸lib/ - user or third-party MicroPython libraries
  • ▸data/ - storage for logs or configuration
  • ▸assets/ - optional resources such as images or fonts

Monetization

  • ▸Educational kits and courses
  • ▸IoT and embedded device prototyping
  • ▸Maker projects and hobbyist devices
  • ▸Robotics and automation services
  • ▸STEM education and workshops

Productivity Tips

  • ▸Keep scripts modular
  • ▸Test hardware modules individually
  • ▸Use REPL for iterative development
  • ▸Document hardware and code
  • ▸Update libraries and firmware regularly

Basic Concepts

  • ▸Modules for hardware access (machine, network, uos, etc.)
  • ▸REPL for interactive experimentation
  • ▸boot.py and main.py for startup scripts
  • ▸Event-driven and polling mechanisms for sensors
  • ▸Support for networking protocols (HTTP, MQTT, WebSockets)

Official Docs

  • ▸MicroPython Official Documentation
  • ▸MicroPython Boards and Ports
  • ▸MicroPython Libraries Reference
  • ▸MicroPython Forum and GitHub
  • ▸Networking and Hardware Guides

More Micropython Typing Exercises

MicroPython Temperature Sensor MonitorMicroPython Button Press CounterMicroPython LED BlinkerMicroPython Light Sensor AlertMicroPython Buzzer AlertMicroPython Motor ControlMicroPython Distance Sensor MonitorMicroPython Humidity Sensor MonitorMicroPython Multi-Sensor Dashboard

Practice Other Languages

CReactPythonC++RustTypeScriptKotlinPHPJavaC#RubyMqlCqlN1qlCypher