MicroPython Commands for Raspberry Pi Pico W (Basic to Advanced)
1. Importing Modules
MicroPython requires essential modules for GPIO, timing, Wi-Fi, etc.
Example:
import machine # For hardware access
import utime # For delays and timestamps
import network # For Wi-Fi (Pico W only)
2. GPIO Control
Configure GPIO as input/output.
led = Pin(25, Pin.OUT)
led.value(1) # ON
led.value(0) # OFF
button = Pin(15, Pin.IN, Pin.PULL_UP)
if button.value() == 0: print('Pressed')
3. Sleep/Delay
utime.sleep(1) # 1 second
utime.sleep_ms(500) # 500 milliseconds
4. PWM for Servo or LED
pwm = PWM(Pin(15))
pwm.freq(50) # Servo freq
pwm.duty_u16(5000)
5. I2C Communication
i2c = I2C(0, scl=Pin(17), sda=Pin(16), freq=400000)
devices = i2c.scan()
6. Wi-Fi Connection (STA)
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect('SSID', 'PASS')
7. ADC Read
adc = ADC(Pin(26))
value = adc.read_u16()
8. File Read/Write
MicroPython Commands for Raspberry Pi Pico W (Basic to Advanced)
with open('log.txt', 'w') as f:
f.write('Hello')
with open('log.txt', 'r') as f:
print(f.read())
9. uasyncio Multitasking
import uasyncio as asyncio
async def task(): ...
asyncio.run(main())
10. Interrupt Handling
button.irq(trigger=Pin.IRQ_FALLING, handler=callback)
11. Web Server
socket + HTTP server for controlling Pico via browser
12. Timer Events
tim = Timer()
tim.init(freq=1, mode=Timer.PERIODIC, callback=cb)
13. UART Communication
uart = UART(0, baudrate=9600, tx=Pin(0), rx=Pin(1))
14. SoftAP Mode
ap = network.WLAN(network.AP_IF)
ap.active(True)
ap.config(essid='MyPico', password='12345678')
15. MQTT Client
client = MQTTClient('id', 'broker')
client.connect()
client.publish('topic', 'msg')
16. OTA Updates
response = urequests.get('url')
with open('main.py', 'w') as f:
f.write(response.text)
MicroPython Commands for Raspberry Pi Pico W (Basic to Advanced)
17. Custom Modules
# mymodule.py
def greet(): print('Hi')
# main.py
import mymodule; mymodule.greet()
18. Memory Optimization
import gc
gc.collect()
print(gc.mem_free())
19. Web Dashboard with JS
Use HTML + JavaScript (AJAX) to control pins via browser
20. ESP32-CAM Integration
uart.write('snap') or HTTP to ESP32-CAM