Learn Micropython - 10 Code Examples & CST Typing Practice Test
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.
View all 10 Micropython code examples →
Learn MICROPYTHON with Real Code Examples
Updated Nov 21, 2025
Code Sample Descriptions
MicroPython Counter and LED Theme Toggle
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()
Demonstrates a simple counter with theme toggling using MicroPython variables and microcontroller GPIO control (LED indicator).
MicroPython Temperature Sensor Monitor
import random
temp = random.randint(20,35) # Simulated sensor
print(f"Temperature: {temp}")
if temp > 30:
print("Warning: High Temperature!")
Reads temperature from a sensor and prints alerts.
MicroPython Button Press Counter
presses = 0
def buttonPressed():
global presses
presses += 1
print(f"Button pressed: {presses}")
buttonPressed()
buttonPressed()
buttonPressed()
Counts the number of button presses and prints the count.
MicroPython LED Blinker
from machine import Pin
import time
led = Pin(2, Pin.OUT)
for i in range(3):
led.value(1)
print("LED ON")
time.sleep(0.5)
led.value(0)
print("LED OFF")
time.sleep(0.5)
Blinks an LED on and off three times.
MicroPython Light Sensor Alert
from machine import Pin
import random
led = Pin(2, Pin.OUT)
lightLevel = random.randint(0,100)
if lightLevel < 50:
led.value(1)
print("LED ON")
else:
led.value(0)
print("LED OFF")
print(f"Light Level: {lightLevel}")
Monitors light sensor and turns on LED if it is dark.
MicroPython Buzzer Alert
import random
sensorValue = random.randint(50,150)
if sensorValue > 100:
print("Buzzer ON")
else:
print("Buzzer OFF")
print(f"Sensor: {sensorValue}")
Activates buzzer if a threshold value is exceeded.
MicroPython Motor Control
motorActive = True
if motorActive:
print("Motor ON")
else:
print("Motor OFF")
motorActive = False
if motorActive:
print("Motor ON")
else:
print("Motor OFF")
Starts and stops a motor based on a condition.
MicroPython Distance Sensor Monitor
import random
distance = random.randint(5,20)
if distance < 10:
print("Alert: Object too close!")
print(f"Distance: {distance}")
Reads a distance sensor and prints alerts if object is too close.
MicroPython Humidity Sensor Monitor
import random
humidity = random.randint(20,80)
if humidity < 30:
print("Too Dry!")
if humidity > 70:
print("Too Humid!")
print(f"Humidity: {humidity}")
Monitors humidity and prints if it is too high or low.
MicroPython Multi-Sensor Dashboard
from machine import Pin
import random
led = Pin(2, Pin.OUT)
temp = random.randint(25,35)
light = random.randint(0,100)
if temp>30 or light<50:
led.value(1)
print("LED ON")
else:
led.value(0)
print("LED OFF")
print(f"Temp: {temp}, Light: {light}")
Reads multiple sensors and updates LED/buzzer accordingly.
Frequently Asked Questions about Micropython
What is Micropython?
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.
What are the primary use cases for Micropython?
Embedded systems programming. IoT device prototyping. Sensor data acquisition and control. Educational microcontroller projects. Robotics and automation scripting
What are the strengths of Micropython?
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
What are the limitations of Micropython?
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
How can I practice Micropython typing speed?
CodeSpeedTest offers 10+ real Micropython code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.