You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/micropython/01.basics/05.digital-analog-pins/digital-analog-pins.md
+10-8Lines changed: 10 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -7,23 +7,23 @@ micropython_type: basics
7
7
8
8
In this chapter we will learn about managing digital and analog pins.
9
9
10
-
All the compatibles boards have a series of pins, most of these pins work as a general-purpose input/output (GPIO) pin. There are Digital Pins and Analog Pins depending by the signal. We will learn how to use the inputs and outputs.
10
+
All the compatible boards have a series of pins, most of these pins work as a general-purpose input/output (GPIO) pin. There are Digital Pins and Analog Pins depending on the signal. We will learn how to use the inputs and outputs.
11
11
12
-
There are essentially two types of pins, analog and digital pins. Digital pins can be set to either HIGH (usually 5V or 3.3V) or LOW (0V). You can use that to e.g. read a button state or to toggle an LED.
12
+
There are essentially two types of pins, analog and digital pins. Digital pins can be set to either HIGH (usually 5V or 3.3V) or LOW (0V). You can use that to e.g. read a button state or toggle an LED.
13
13
14
14
***Important: unfortunately, the MicroPython implementation does not match the regular pinout of your board. This means, that if you want to use for example, digital pin (5), it might be digital pin (27) on one board, or digital pin (14) on another. Please visit the [Board API article](/micropython/basics/board-api) to see what the pin map for your board is.***
15
15
16
16
## Digital Pins
17
17
18
-
Digital signals have two distinct values: HIGH (1) or LOW (0). You use digital signals in situations where the input or output will have one of those two values. For example you can use a digital signal to turn an LED on or off.
18
+
Digital signals have two distinct values: HIGH (1) or LOW (0). You use digital signals in situations where the input or output will have one of those two values. For example, you can use a digital signal to turn an LED on or off.
19
19
20
20
### Digital Write
21
21
22
22
In this section we will introduce the `machine` module to control the state of a pin. In this example, we will name the pin `myLED`.
23
23
24
24
In MicroPython we can declare a `Pin` with two arguments: Pin number, such as `25`, which defines the number of the pin that you would like to control, and `Pin.OUT`, to declare a pin as output.
25
25
26
-
Finally, to turn set the pin to a high or low state, we set the `value` to either `1` or `0`.
26
+
Finally, to turn the pin to a high or low state, we set the `value` to either `1` or `0`.
27
27
28
28
```python
29
29
from machine import Pin #import pin function
@@ -36,7 +36,7 @@ myLED.value(0) #set pin to a low state (0) / OFF
36
36
37
37
To create the classic "blink" example, we can also import the `time` module, and create a `while` loop.
38
38
39
-
The following examples blinks the onboard LED every second.
39
+
The following example blinks the onboard LED every second.
40
40
41
41
```python
42
42
from machine import Pin
@@ -90,13 +90,13 @@ while True:
90
90
91
91
## Analog Pins
92
92
93
-
An example of analog pin is the ADC class, that supplies an interface to analog-to-digital converters, and figures a single endpoint that can sample a continuous voltage and convert it to a discretised value.
93
+
An example of the analog pin is the ADC class, which supplies an interface to analog-to-digital converters, and figures a single endpoint that can sample a continuous voltage and convert it to a discretized value.
94
94
95
95
There are four methods to use inside the ADC class: `ADC.init`, `ADC.block()`, `ADC.read_16()` and `ADC.read_uv()`.
96
96
97
97
### Analog Read
98
98
99
-
To read an analog pin, we can use the `ADC.read_u16` command. This reads the specified analog pin, and return an integer in the range 0 - 65535. For this, we need to import `ADC` and `Pin` from the `machine` module.
99
+
To read an analog pin, we can use the `ADC.read_u16` command. This reads the specified analog pin and returns an integer in the range 0 - 65535. For this, we need to import `ADC` and `Pin` from the `machine` module.
100
100
101
101
```python
102
102
import machine
@@ -113,11 +113,13 @@ while True:
113
113
time.sleep_ms(500)
114
114
```
115
115
116
+
***If you are using an [Arduino Nano RP2040 Connect](https://store.arduino.cc/products/arduino-nano-rp2040-connect), you can also do the following: `adc = ADC("A4")`. For more information check out the example [here](http://docs.arduino.cc/micropython/basics/board-examples#analog-read).***
117
+
116
118
## PWM (Pulse Width Modulation)
117
119
118
120
[PWM](/learn/microcontrollers/analog-output) is used to produce analog results with digital means, by switching ON/OFF a signal rapidly.
119
121
120
-
As a result, you can simulate a specific voltage written to a pin. In the example below, we write `30000` in a range between 0 - 65535 (16 bits), which if you connect an LED to the pin, it will be on at about "half" capacity.
122
+
As a result, you can simulate a specific voltage written to a pin. In the example below, we write `30000` in a range between 0 - 65535 (16 bits), which if you connect an LED to the pin, will be on at about "half" capacity.
121
123
122
124
For this, we need to import `PWM` and `Pin` from the `machine` module.
Copy file name to clipboardExpand all lines: content/micropython/01.basics/06.board-examples/board-examples.md
+18Lines changed: 18 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -49,6 +49,20 @@ Before you start using the board's pins, it might be a good idea to check out th
49
49
| D20/A6 | GPIO36 | ADC/NINA-W102 |
50
50
| D21/A7 | GPIO35 | ADC/NINA-W102 |
51
51
52
+
### Analog Read
53
+
54
+
***Note: This is currently only available on the nightly build. Follow [this link](https://docs.arduino.cc/micropython/#firmware) to download it.***
55
+
56
+
To read an analog pin, we can use the `ADC.read_u16` command. This reads the specified analog pin and returns an integer in the range 0 - 65535. For this, we need to import `ADC` from the `machine` module.
57
+
58
+
```python
59
+
from machine importADC
60
+
61
+
whileTrue:
62
+
adc = ADC("A4")
63
+
adc.read_u16()
64
+
```
65
+
52
66
### Sensors
53
67
54
68
#### IMU (LSM6DSOX)
@@ -474,6 +488,10 @@ In the MicroPython port of the Nano 33 BLE board, the pinout is the same as the
474
488
| D20/A6 | 28 |
475
489
| D21/A7 | 3 |
476
490
491
+
### Analog Read
492
+
493
+
***The following example is currently only possible with the nightly build***
494
+
477
495
### LED Control
478
496
479
497
There are 3 different LEDs that can be accessed on the Nano 33 BLE: **RGB, the built-in LED** and the **power LED**.
0 commit comments