Raspberry Pi Pico W - Tips and Trick

Annotation about Raspberry Pi Pico and Raspberry Pi Pico W

Check if file exist with MicroPython

import os

def sub_file_found():
    try:
        os.stat('file.txt')
        return True
    except OSError:
        return False 

Write lines in a file with MicroPython

def sub_save_lines(n,p):
    f = open('file.txt', 'w')
    f.write(n)
    f.write('\n')
    f.write(p)
    f.write('\n')
    f.close();

Read lines in a file with MicroPython

def sub_read_lines():
    f = open('file.txt', 'r')
    lines = f.readlines()
    f.close();
    return lines

Delete a file with MicroPython

import os

def sub_clear_file():
    os.remove("file.txt")

Manage on board LED with MicroPython

Simple blink example

led = machine.Pin("LED",machine.Pin.OUT)

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

Convert same object to string with MicroPython

requestString = request.decode('utf-8')

OR

request = str(request)

Try and catch Exception with MicroPython

    try:
        ...
        return True
    except OSError:
        return False  

raise RuntimeError('test')

Restart Raspberry Pi Pico via code with MicroPython

import machine

machine.reset()

Raspberry Pi Pico Pinout

Image big

Raspberry Pi Pico W Pinout

Image Big