Extra 5% OFF Use Code: OL05
Free Shipping over β‚Ή999

Introduction

What is MicroPython?

MicroPython is a lightweight version of Python 3 that runs on microcontrollers like:

  • ESP32
  • ESP8266
  • Raspberry Pi Pico
  • STM32, and more.

It lets you control hardware using Python, making embedded programming more beginner-friendly and fun compared to C or C++.

Why use MicroPython?

FeatureBenefit
βœ… Python-basedEasy to learn and write
βœ… LightweightDesigned for low-memory microcontrollers
βœ… InteractiveYou can test commands live using the REPL
βœ… Cross-platformRuns on many microcontroller boards
βœ… Fast prototypingGreat for beginners, makers, and IoT projects

What Can MicroPython Do?

With MicroPython, you can:

  • Blink LEDs
  • Read sensors (temperature, motion, light, etc.)
  • Control motors, buzzers, and relays
  • Build web servers (Wi-Fi enabled boards like ESP32)
  • Store data in files or memory
  • Communicate with other devices (I2C, SPI, UART)

Simple Example:

Here’s a basic LED blink program in MicroPython:

from machine import Pin
from time import sleep

led = Pin(2, Pin.OUT)  # Use GPIO2 (common onboard LED pin)

while True:
    led.on()
    sleep(1)
    led.off()
    sleep(1)

πŸ’‘ This code turns an LED on and off every second.

πŸ“¦ Tools You Use With MicroPython

ToolUse
Thonny IDESimple Python IDE with MicroPython support
uPyCraftLightweight IDE for MicroPython
esptool.pyFor flashing firmware
WebREPLRemote access over Wi-Fi

πŸ“Œ Key Concepts

  • REPL: Read-Eval-Print Loop β€” test code live.
  • Flash: Install firmware onto your board.
  • GPIO: General Purpose Input Output β€” control pins.
  • Libraries: MicroPython has built-in and custom modules.

🎯 Summary

MicroPython = Python + Hardware Control

It’s perfect for:

  • Students learning coding and electronics
  • Quick prototyping of IoT projects
  • Makers and hobbyists
  • Teaching real-time systems with ease

Comment (1)

  1. esp32-c3 with ws2812 led with webserver and ssd1306 display micropython firmware : MicroPython v1.24.1 on 2024-11-29; ESP32C3 module with ESP32C3 hard ware connection : gpio 1,3 sda,scl SSD1306 gpio 2 = ws2812 Din code : import network import socket import neopixel from machine import Pin, I2C, reset from time import sleep import ujson import ssd1306 # === LED + OLED Setup === NUM_LEDS = 1 np = neopixel.NeoPixel(Pin(2), NUM_LEDS) # I2C OLED i2c = I2C(0, scl=Pin(1), sda=Pin(3)) oled = ssd1306.SSD1306_I2C(128, 64, i2c) # === Variables === current_color = (255, 0, 0) brightness = 100 rainbow_mode = False # === Functions === def set_led_color(color, bright): try: if len(color) != 3: print("Invalid color:", color) return r, g, b = [int(c * (bright / 100)) for c in color] np[0] = (r, g, b) np.write() except Exception as e: print("LED color error:", e) def save_state(): try: with open("state.json", "w") as f: ujson.dump({"color": current_color, "brightness": brightness, "rainbow": rainbow_mode}, f) except Exception as e: print("Save error:", e) def load_state(): global current_color, brightness, rainbow_mode try: with open("state.json") as f: state = ujson.load(f) color = state.get("color", [255, 0, 0]) if isinstance(color, list): current_color = tuple(color) brightness = state.get("brightness", 100) rainbow_mode = state.get("rainbow", False) except Exception as e: print("Load error:", e) def update_display(status="Ready"): oled.fill(0) oled.text("Color: {}".format(current_color), 0, 0) oled.text("Brightness: {}".format(brightness), 0, 10) oled.text("Rainbow: {}".format("ON" if rainbow_mode else "OFF"), 0, 20) oled.text(status, 0, 50) oled.show() # === Access Point Setup === ssid = "ESP32_LED" password = "12345678" ap = network.WLAN(network.AP_IF) ap.active(True) ap.config(essid=ssid, password=password) while not ap.active(): pass print("AP active:", ap.ifconfig()) # === HTML Page === def web_page(): r, g, b = current_color hex_color = "%02x%02x%02x" % (r, g, b) return f""" ESP32 LED ESP32 LED Controller Brightness: {brightness}% Toggle Rainbow const cp = document.getElementById('colorPicker'); const b = document.getElementById('brightness'); const bv = document.getElementById('bval'); cp.oninput = () => fetch('/color?hex=' + cp.value.substring(1)); b.oninput = () => {{ bv.innerText = b.value; fetch('/brightness?val=' + b.value); }}; """ # === Web Server === s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind(('', 80)) s.listen(5) # === Initial State === load_state() set_led_color(current_color, brightness) update_display("Booted") # === Main Loop === while True: conn, addr = s.accept() print("Client connected:", addr) try: request = conn.recv(1024).decode() request = request.replace("\r", "").replace("\n", "") if "/rainbow" in request: rainbow_mode = not rainbow_mode save_state() if not rainbow_mode: set_led_color(current_color, brightness) update_display("Rainbow {}".format("ON" if rainbow_mode else "OFF")) elif "/brightness?val=" in request: try: val = int(request.split("/brightness?val=")[1].split(" ")[0]) brightness = max(0, min(100, val)) if not rainbow_mode: set_led_color(current_color, brightness) save_state() update_display("Brightness {}".format(brightness)) except Exception as e: print("Brightness error:", e) elif "/color?hex=" in request: try: hex_str = request.split("/color?hex=")[1].split(" ")[0].strip() if len(hex_str) == 6: color = int(hex_str, 16) r = (color >> 16) & 0xFF g = (color >> 8) & 0xFF b = color & 0xFF current_color = (r, g, b) rainbow_mode = False set_led_color(current_color, brightness) save_state() update_display("Color Set") else: print("Invalid hex string:", hex_str) except Exception as e: print("Color error:", e) response = web_page() conn.send("HTTP/1.1 200 OK\r\nContent-type: text/html\r\n\r\n") conn.sendall(response.encode()) except Exception as e: print("Request error:", e) finally: conn.close()

Leave a Reply

Your email address will not be published.

Need Help?