Différences entre versions de « ENG-CANSAT-PICO-HowTo »
Ligne 70 : | Ligne 70 : | ||
== Analog Reading == | == Analog Reading == | ||
− | The | + | Lets read some voltage from the ADC1 (attached to GPIO 27). |
+ | |||
+ | The value is read on the ADC via the common {{fname|read_u16()}} method. | ||
+ | |||
+ | That method returns a 16 bits value, so a number between 0 and 65535. | ||
+ | |||
+ | {{ambox|text=Even if the {{fname|read_u16()}} returns a 16 bits values, the effective ADC resolution is still 12 bits. The MicroPython internal will just upscale the ADC value from 0..4095 to returned value 0..65535 by applying a multiplier.}} | ||
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
− | from machine import Pin | + | from machine import Pin, ADC |
− | + | p = Pin( 27 ) | |
+ | analog = ADC( p ) | ||
+ | value = analog.read_u16() | ||
+ | print( 'value: %i' % value ) | ||
x | x | ||
x | x |
Version du 21 février 2022 à 02:35
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.
Everything you learned about python language like variables, loops, conditionals, object programming, ... does applies to MicroPython.
A large part of standard Python libraries have been ported to MicroPython (partially or completely). Keep in mind that MicroControler doesn't have as much power and as much memory than a regular computer. So choice has been made: no gregorian calandar, no numpy (it exists some port anyway), no TKinter.
Print and Waits
This first example how to output text with print() and wait a moment with sleep() .
Everything you will print() will be sent to REPL (so over the USB-Serial line) and being visible in Thonny.
from time import sleep, sleep_ms
for x in range( 10 ): # 0..9
print( "Value %i" % x )
sleep( 0.5 ) # half second
You can also sleep for milliseconds with sleep_ms(x)
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).
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
Lets read some voltage from the ADC1 (attached to GPIO 27).
The value is read on the ADC via the common read_u16() method.
That method returns a 16 bits value, so a number between 0 and 65535.
Even if the read_u16() returns a 16 bits values, the effective ADC resolution is still 12 bits. The MicroPython internal will just upscale the ADC value from 0..4095 to returned value 0..65535 by applying a multiplier. |
from machine import Pin, ADC
p = Pin( 27 )
analog = ADC( p )
value = analog.read_u16()
print( 'value: %i' % value )
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