Learn MICROPYTHON with Real Code Examples
Updated Nov 21, 2025
Code Sample Descriptions
1
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).
2
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.
3
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.
4
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.
5
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.
6
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.
7
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.
8
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.
9
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.
10
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.