Différences entre versions de « ENG-CANSAT-PICO-HowTo »
Ligne 23 : | Ligne 23 : | ||
>>> p = Pin( 15, Pin.IN ) | >>> p = Pin( 15, Pin.IN ) | ||
>>> p.value() | >>> p.value() | ||
− | 0 | + | 0 <--- Pin tied to GND |
>>> p.value() | >>> p.value() | ||
− | 1 | + | 1 <--- Pin tied to 3.3V |
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
+ | When the Pin is neither tied to GND, neither to 3.3V, then the pin voltage is said "floating". | ||
+ | |||
+ | 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). | ||
== Analog Reading == | == Analog Reading == |
Version du 21 février 2022 à 00:48
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.
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
When the Pin is neither tied to GND, neither to 3.3V, then the pin voltage is said "floating".
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).
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.
Written by Meurisse D. for MCHobby