From e965756afcfa0df84892b75432df3952344b79c8 Mon Sep 17 00:00:00 2001 From: Hannes Siebeneicher Date: Fri, 10 Nov 2023 10:58:32 +0100 Subject: [PATCH 1/5] Update digital-analog-pins.md --- .../digital-analog-pins.md | 31 ++++++++----------- 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/content/micropython/01.basics/05.digital-analog-pins/digital-analog-pins.md b/content/micropython/01.basics/05.digital-analog-pins/digital-analog-pins.md index 5b66e6d653..718528610f 100644 --- a/content/micropython/01.basics/05.digital-analog-pins/digital-analog-pins.md +++ b/content/micropython/01.basics/05.digital-analog-pins/digital-analog-pins.md @@ -7,15 +7,15 @@ micropython_type: basics In this chapter we will learn about managing digital and analog pins. -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. +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. -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. +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. ***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.*** ## Digital Pins -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. +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. ### Digital Write @@ -23,7 +23,7 @@ In this section we will introduce the `machine` module to control the state of a 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. -Finally, to turn set the pin to a high or low state, we set the `value` to either `1` or `0`. +Finally, to turn the pin to a high or low state, we set the `value` to either `1` or `0`. ```python from machine import Pin #import pin function @@ -36,7 +36,7 @@ myLED.value(0) #set pin to a low state (0) / OFF To create the classic "blink" example, we can also import the `time` module, and create a `while` loop. -The following examples blinks the onboard LED every second. +The following example blinks the onboard LED every second. ```python from machine import Pin @@ -90,34 +90,29 @@ while True: ## Analog Pins -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. +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. There are four methods to use inside the ADC class: `ADC.init`, `ADC.block()`, `ADC.read_16()` and `ADC.read_uv()`. ### Analog Read -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. +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. -```python -import machine -import time +***Note: This is currently only available on the nightly build*** -# Make sure to follow the GPIO map for the board you are using. -# Pin 29 in this case is the "A3" pin on the Nano 33 BLE / BLE Sense -adc_pin = machine.Pin(29) -adc = machine.ADC(adc_pin) +```python +from machine import ADC while True: - reading = adc.read_u16() - print("ADC: ",reading) - time.sleep_ms(500) + adc = ADC("A4") + adc.read_u16() ``` ## PWM (Pulse Width Modulation) [PWM](/learn/microcontrollers/analog-output) is used to produce analog results with digital means, by switching ON/OFF a signal rapidly. -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. +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. For this, we need to import `PWM` and `Pin` from the `machine` module. From 3222e42c801823a82a2982dc04bf5b7d0a38b6f7 Mon Sep 17 00:00:00 2001 From: Hannes Siebeneicher Date: Mon, 13 Nov 2023 11:36:16 +0100 Subject: [PATCH 2/5] move adc read information --- .../digital-analog-pins.md | 19 +++++++++++++------ .../06.board-examples/board-examples.md | 18 ++++++++++++++++++ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/content/micropython/01.basics/05.digital-analog-pins/digital-analog-pins.md b/content/micropython/01.basics/05.digital-analog-pins/digital-analog-pins.md index 718528610f..fce7fec3ac 100644 --- a/content/micropython/01.basics/05.digital-analog-pins/digital-analog-pins.md +++ b/content/micropython/01.basics/05.digital-analog-pins/digital-analog-pins.md @@ -96,18 +96,25 @@ There are four methods to use inside the ADC class: `ADC.init`, `ADC.block()`, ` ### Analog Read -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. - -***Note: This is currently only available on the nightly build*** +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. ```python -from machine import ADC +import machine +import time + +# Make sure to follow the GPIO map for the board you are using. +# Pin 29 in this case is the "A3" pin on the Nano 33 BLE / BLE Sense +adc_pin = machine.Pin(29) +adc = machine.ADC(adc_pin) while True: - adc = ADC("A4") - adc.read_u16() + reading = adc.read_u16() + print("ADC: ",reading) + time.sleep_ms(500) ``` +***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")`. Check out the example [here](http://localhost:8000/micropython/basics/board-examples#analog-read)*** + ## PWM (Pulse Width Modulation) [PWM](/learn/microcontrollers/analog-output) is used to produce analog results with digital means, by switching ON/OFF a signal rapidly. diff --git a/content/micropython/01.basics/06.board-examples/board-examples.md b/content/micropython/01.basics/06.board-examples/board-examples.md index e76570b0a6..58ceaf4e0a 100644 --- a/content/micropython/01.basics/06.board-examples/board-examples.md +++ b/content/micropython/01.basics/06.board-examples/board-examples.md @@ -49,6 +49,20 @@ Before you start using the board's pins, it might be a good idea to check out th | D20/A6 | GPIO36 | ADC/NINA-W102 | | D21/A7 | GPIO35 | ADC/NINA-W102 | +### Analog Read + +***Note: This is currently only available on the nightly build*** + +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. + +```python +from machine import ADC + +while True: + adc = ADC("A4") + adc.read_u16() +``` + ### Sensors #### IMU (LSM6DSOX) @@ -474,6 +488,10 @@ In the MicroPython port of the Nano 33 BLE board, the pinout is the same as the | D20/A6 | 28 | | D21/A7 | 3 | +### Analog Read + +***The following example is currently only possible with the nightly build*** + ### LED Control There are 3 different LEDs that can be accessed on the Nano 33 BLE: **RGB, the built-in LED** and the **power LED**. From b908b32e00b0443fbd75ac075f5384b9b12d6700 Mon Sep 17 00:00:00 2001 From: Hannes Siebeneicher Date: Mon, 13 Nov 2023 11:57:21 +0100 Subject: [PATCH 3/5] Update digital-analog-pins.md --- .../01.basics/05.digital-analog-pins/digital-analog-pins.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/micropython/01.basics/05.digital-analog-pins/digital-analog-pins.md b/content/micropython/01.basics/05.digital-analog-pins/digital-analog-pins.md index fce7fec3ac..29f10c177e 100644 --- a/content/micropython/01.basics/05.digital-analog-pins/digital-analog-pins.md +++ b/content/micropython/01.basics/05.digital-analog-pins/digital-analog-pins.md @@ -113,7 +113,7 @@ while True: time.sleep_ms(500) ``` -***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")`. Check out the example [here](http://localhost:8000/micropython/basics/board-examples#analog-read)*** +***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://localhost:8000/micropython/basics/board-examples#analog-read).*** ## PWM (Pulse Width Modulation) From 33dde6ce703696487deeca126e1346973956bdc4 Mon Sep 17 00:00:00 2001 From: Hannes Siebeneicher Date: Mon, 4 Dec 2023 14:02:01 +0100 Subject: [PATCH 4/5] Update digital-analog-pins.md --- .../01.basics/05.digital-analog-pins/digital-analog-pins.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/micropython/01.basics/05.digital-analog-pins/digital-analog-pins.md b/content/micropython/01.basics/05.digital-analog-pins/digital-analog-pins.md index 29f10c177e..0b69f2ae93 100644 --- a/content/micropython/01.basics/05.digital-analog-pins/digital-analog-pins.md +++ b/content/micropython/01.basics/05.digital-analog-pins/digital-analog-pins.md @@ -113,7 +113,7 @@ while True: time.sleep_ms(500) ``` -***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://localhost:8000/micropython/basics/board-examples#analog-read).*** +***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).*** ## PWM (Pulse Width Modulation) From 1e03ec038b79feccb5a2ebfb9df5220f6019e75b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20S=C3=B6derby?= <35461661+karlsoderby@users.noreply.github.com> Date: Mon, 11 Dec 2023 11:54:27 +0100 Subject: [PATCH 5/5] Update content/micropython/01.basics/06.board-examples/board-examples.md --- .../micropython/01.basics/06.board-examples/board-examples.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/micropython/01.basics/06.board-examples/board-examples.md b/content/micropython/01.basics/06.board-examples/board-examples.md index 58ceaf4e0a..8314f1dde1 100644 --- a/content/micropython/01.basics/06.board-examples/board-examples.md +++ b/content/micropython/01.basics/06.board-examples/board-examples.md @@ -51,7 +51,7 @@ Before you start using the board's pins, it might be a good idea to check out th ### Analog Read -***Note: This is currently only available on the nightly build*** +***Note: This is currently only available on the nightly build. Follow [this link](https://docs.arduino.cc/micropython/#firmware) to download it.*** 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.