ENG-CANSAT-PICO-HowTo

De MCHobby - Wiki
Sauter à la navigation Sauter à la recherche

MicroPython How To

This section of the tutoriel contains small portion of code explaining how to use digital pins, analog pins, buses and all the fundamentals to control the board with Python code.

Print and Waits

This first example how to output text with print() and wait a moment with sleep() .

Control an output

The onboard LED is tied to the GPIO 25. Here how to control it.

from machine import Pin
led = Pin(25, Pin.OUT)
led.value(1) # Switch on the LED
led.value(0) # Switch off the LED

This will work the same with any GPIO pins.

Input Reading

The following code will read the state of the GP15 configured as input pin.

>>> from machine import Pin
>>> p = Pin( 15, Pin.IN )
>>> p.value()
0                <--- Pin tied to GND
>>> p.value()
1                <--- Pin tied to 3.3V

The logical state of a floating pin is unpredictable, it may be High, it may be Low... we don't know.

To avoids that issue, the best is to use a pull-up resistor. Many microcontroler have internal pull-up resistor (it just needs to be activated).

Input Reading with Pull-Up

The pico can activates a Pull-up resistor on any of its input pins.

When the Pin is tied to ground then the input is Low (0) otherwise, the pull-up resistor will drive back the pin voltage to 3.3V.

The following code will read the state of the GP15 configured as input pin + pull-up.

>>> from machine import Pin
>>> p = Pin( 15, Pin.IN, PIN.PULL_UP )
>>> p.value()
0                <--- Pin tied to GND
>>> p.value()
1                <--- Pin tied to 3.3V or floating.

Analog Reading

The onboard LED is tied to the GPIO 25. Here how to control it.

from machine import Pin
x
x
x
x

PWM Output

The onboard LED is tied to the GPIO 25. Here how to control it.

from machine import Pin
x
x
x
x

Hardware buses

I2C Bus

The onboard LED is tied to the GPIO 25. Here how to control it.

from machine import Pin
x
x
x
x

UART Bus

The onboard LED is tied to the GPIO 25. Here how to control it.

from machine import Pin
x
x
x
x


SPI Bus

The onboard LED is tied to the GPIO 25. Here how to control it.

from machine import Pin
x
x
x
x

Additional tutorials

Anyway, the followings are also goods and complete references.

Getting Started

Tlogo-pico-get-started.png

Get Started with MicroPython on Raspberry Pi Pico.

Pico Python SDK

Tlogo-pico-python-sdk2.png

Advanced documentation for MicroPython environment on RP2040.

Pico debugging

Tlogo-pico-python-sdk.jpg

Debugging using another Pico (PicoProbe)

Resetting Flash

Tlogo-pico-python-sdk.jpg

How to reflash MicroPython on the Pico

 



Written by Meurisse D. for MCHobby


MCHobby investit du temps et de l'argent dans la réalisation de traduction et/ou documentation. C'est un travail long et fastidieux réalisé dans l'esprit Open-Source... donc gratuit et librement accessible.
SI vous aimez nos traductions et documentations ALORS aidez nous à en produire plus en achetant vos produits chez MCHobby.