Skip to content

Commit 9d3805a

Browse files
committed
Content update (ADC, RTC)
1 parent c6d44f2 commit 9d3805a

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed

content/micropython/01.basics/06.board-examples/board-examples.md

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1663,6 +1663,65 @@ while True:
16631663

16641664
This practical example demonstrates controlling an LED based on a button's state. The LED, connected to pin `P107` (configured as an output), is turned on or off depending on the button's input read from pin `P105` (set as an input with a pull-up resistor). The main loop continually checks the button's state; pressing the button fixes the LED on while releasing it turns the LED off. A brief delay is included for debouncing, ensuring stable operation without false triggers from the button.
16651665

1666+
### Analog to Digital Converter
1667+
1668+
The `ADC` class in MicroPython provides an interface for the Analog-to-Digital (ADC) converter of the Portenta C33 board, enabling the measurement of continuous voltages and their conversion into discrete digital values. This functionality is crucial for applications that, for example, require reading from analog sensors. The `ADC` class represents a single endpoint for sampling voltage from an analog pin and converting it to a digital value.
1669+
1670+
The available ADC pins of the Portenta C33 board in MicroPython are the following:
1671+
1672+
| **Available ADC Pins** |
1673+
|:----------------------:|
1674+
| `P006` |
1675+
| `P005` |
1676+
| `P004` |
1677+
| `P002` |
1678+
| `P001` |
1679+
| `P015` |
1680+
| `P014` |
1681+
| `P000` |
1682+
1683+
#### Initializing the ADC
1684+
1685+
First, to use an ADC of the Portenta C33 board, create an ADC object associated with a specific pin. This pin will be used to read analog values.
1686+
1687+
```python
1688+
from machine import ADC
1689+
1690+
# Create an ADC object on a specific pin
1691+
adc = ADC(pin)
1692+
```
1693+
1694+
#### Reading Analog Values
1695+
1696+
You can read analog values as raw values using the `read_u16()` method. This method returns a raw integer from 0-65535, representing the analog reading.
1697+
1698+
```python
1699+
# Reading a raw analog value
1700+
val = adc.read_u16()
1701+
```
1702+
1703+
#### Practical Example
1704+
1705+
This example demonstrates the use of the `ADC` class to read values from a potentiometer on the Portenta C33 board. First, connect your potentiometer to the Portenta C33 board. One outer pin goes to `GND`, the other to `3V3`, and the middle pin to an analog-capable I/O pin, such as `P006`. This setup creates a variable voltage divider, with the voltage at the center pin changing as you adjust the potentiometer.
1706+
1707+
```python
1708+
from machine import ADC, Pin
1709+
import time
1710+
1711+
# Initialize the ADC on the potentiometer-connected pin
1712+
pot_pin = Pin('P006')
1713+
pot_adc = ADC(pot_pin)
1714+
1715+
while True:
1716+
# Read the raw analog value
1717+
raw_value = pot_adc.read_u16()
1718+
print("- Potentiometer raw value:", raw_value)
1719+
1720+
# Delay for readability
1721+
time.sleep(0.1)
1722+
```
1723+
The example starts by importing the necessary modules and setting up the ADC on a pin connected to a potentiometer (`P006`). The ADC object (`pot_adc`) is used to interface with the potentiometer. Inside the loop, the analog value from the potentiometer is continuously read using the `read_u16()` method that provides a raw integer value scaled between `0` and `65535`, reflecting the potentiometer's position. The analog value value is printed to the console, and a short delay is included in the loop to ensure the output is readable.
1724+
16661725
### Pulse Width Modulation
16671726

16681727
Pulse Width Modulation (PWM) is a method to emulate an analog output using a digital pin. It does this by rapidly toggling the pin between low and high states. Two primary aspects define PWM behavior:
@@ -1755,3 +1814,57 @@ while True:
17551814
time.sleep(0.001)
17561815
```
17571816

1817+
### Real-Time Clock
1818+
1819+
The `RTC` class in MicroPython provides a way to manage and utilize the Real-Time Clock (RTC) of the Portenta C33 board. This feature is essential for applications that require accurate timekeeping, even when the main processor is not active. The RTC maintains accurate time and date, functioning independently from the main system. It continues to keep track of the time even when the board is powered off, as long as it's connected to a power source like a battery.
1820+
1821+
#### Initializing the RTC
1822+
1823+
To use the RTC, create first an RTC object. This object is then used to set or read the current date and time.
1824+
1825+
```python
1826+
import machine
1827+
1828+
# Create an RTC object
1829+
rtc = machine.RTC()
1830+
```
1831+
1832+
#### Setting and Getting Date and Time
1833+
1834+
The RTC allows you to set and retrieve the current date and time. The date and time are represented as an 8-tuple format.
1835+
1836+
```python
1837+
# Setting the RTC date and time
1838+
rtc.datetime((2024, 1, 4, 4, 20, 0, 0, 0))
1839+
1840+
# Getting the current date and time
1841+
current_datetime = rtc.datetime()
1842+
print("- Current date and time:", current_datetime)
1843+
```
1844+
1845+
The 8-tuple for the date and time follows the format `(year, month, day, weekday, hours, minutes, seconds, subseconds)`.
1846+
1847+
#### Practical Example
1848+
1849+
A practical use case for the RTC is to add timestamps to sensor data readings. By setting the current time on the RTC, you can then append an accurate timestamp each time a sensor value is logged.
1850+
1851+
```python
1852+
import machine
1853+
1854+
# Initialize the RTC and set the current datetime
1855+
rtc.datetime((2024, 1, 4, 4, 20, 0, 0, 0))
1856+
1857+
# Function to read a sensor value (placeholder)
1858+
def read_sensor():
1859+
# Replace with actual sensor reading logic
1860+
return 42
1861+
1862+
# Read sensor value and get the current time
1863+
sensor_value = read_sensor()
1864+
timestamp = rtc.datetime()
1865+
1866+
# Output the sensor value with its timestamp
1867+
print("- Sensor value at ", timestamp, ":", sensor_value)
1868+
```
1869+
1870+
In this example, every sensor reading is accompanied by A timestamp, which can be crucial for data analysis or logging purposes. The RTC's ability to maintain time independently of the main system's power status makes it reliable for time-sensitive applications.

0 commit comments

Comments
 (0)