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.

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