## Programujeme dosku Arduino Nano RP2040 Connect v jazyku Python
[Mirek Biňas](https://bletvaska.github.io)
/ [**Namakaný webinár**](http://www.namakanyden.sk/)
[![Arduino with Python](images/arduino.with.python.webp)](https://realpython.com/arduino-python/)
notes:
* problem ako dostat python do arduina trapi uz cele generacie
* problemom je mala SRAM aj FLASH
* firmata
![Snek](images/snek.svg)
## [sneklang](https://sneklang.org/)
A Python-inspired Language for Embedded Devices
notes:
* projekt https://github.com/keith-packard/snek
* `cd ~/bin/Snek`
* `./snek-uno-install -port /dev/ttyACM1 -hex snek-uno-1.7.hex`
![Arduino Nano RP2040 Connect](images/arduino.nano.rp2040.connect.jpg)
notes:
* doska vydaná v máji 2021
* prísľub v júli 2021
* v júli došlo k posunu na september 2021
* dnes v novembri 2021 sa pozrieme na Python-y pre túto dosku
![Features of Arduino Nano RP2040 Connect](images/arduino.features.jpg)
notes:
* https://www.conrad.sk/p/arduino-doska-nano-rp2040-connect-nano-2371847
* je vo veľkosti Arduino Nano
* preto sa aj doska volá nano
* pracovné napätie je 3.3V
* srdcom je mikrokontrolér RP2040
* frekvencia 133MHz
* dve jadrá
* SRAM 264k
* FLASH 16M
![Interfaces of Arduino Nano RP2040 Connect](images/arduino.interfaces.jpg)
notes:
* https://www.conrad.sk/p/arduino-doska-nano-rp2040-connect-nano-2371847
* u-blox NINA-W102 radio modul
* WiFi 802.11b/g/n
* Bluetooth®
* BLE v4.2
![Extended Features of Arduino Nano RP2040 Connect](images/arduino.extended.features.jpg)
notes:
* https://www.conrad.sk/p/arduino-doska-nano-rp2040-connect-nano-2371847
* ma 6 osi IMU
* Inertial measurement unit - inerciální měřící jednotka
* LSM6DSOX
* akcelerometer
* gyroskop
* nema magnetometer
* PDM mikrofon
* PDM - Pulse Density Modulation - pulzne sirkova modulacia
* PDM mikrofón MP34DT05
![Overview of Pins of Arduino Nano RP2040 Connect](images/arduino.pins.jpg)
notes:
* https://www.conrad.sk/p/arduino-doska-nano-rp2040-connect-nano-2371847
* tieto info nájdete aj na krabičke
![Pins of Arduino Nano RP2040 Connect](images/arduino.nano.rp2040.connect-pins.png)
notes:
* všetky piny môžu byť aj PWM
* GPIO0 a 1 (D0 a D1) sú vyhradené pre UART
* analógové piny A6 a A7 nie sú pripojené priamo k RP2040 a nedajú sa použiť
* sú pripojené k WiFi modulu
* možno sa dajú použiť v Arduino SDK
* A4 a A5 sú pre I2C komunikáciu
* akcelerometer a gyroskop
![Arduino Nano RP2040 Connect Pinout](images/arduino.nano.rp2040.connect.pinout.png)
notes:
* https://circuitstate.com/news/arduino-nano-rp2040-connect-is-in-the-stores/
## Piny
* 8 analógových pinov
* `A4` a `A5` sú určené pre I2C
* piny `A6` a `A7` nie sú prepojené cez RP2040 a nedajú sa použiť
* 22 digitálnych GPIO pinov
* `D2` - `D12`
* `A4` - `A5`
* 20 PWM pinov
notes:
* https://docs.arduino.cc/tutorials/nano-rp2040-connect/rp2040-01-technical-reference
![Arduino Nano RP2040 Connect Block Diagram](images/arduino.nano.rp2040.connect-block.diagram.png)
notes:
* https://circuitstate.com/news/arduino-nano-rp2040-connect-is-in-the-stores/
## RGB LED
* pripojená cez modul U-blox® Nina W102
notes:
* je pripojena cez wifi modul, takze k nej nemame priamy pristup :-(
## Arduino a Python
* [MicroPython](https://micropython.org/download/ARDUINO_NANO_RP2040_CONNECT/) - od nočných zostavení v1.17 (17.nov.2021)
* [Circuit Python](https://circuitpython.org/board/arduino_nano_rp2040_connect/) - od v6.3
* [OpenMV MicroPython](https://docs.arduino.cc/tutorials/nano-rp2040-connect/rp2040-openmv-setup)
## REPL
(Read, Evaluate, Print, Loop)
notes:
* toto je vlastnost, ktoru budete prave pri programovani mikrokontrolerov milovat
* zive hranie sa s mikrokontrolerom bez nutnosti neustaleho vypekania noveho kodu
* ved uvidite ;)
* [CircuitPython](https://circuitpython.org/) je fork [MicroPython](http://micropython.org/)-u
* sú určené na programovanie mikrokontrolérov
* odľahčená verzia Python 3
* optimalizovaný na beh na mikrokontroléroch (min 16k RAM a 256k FLASH)
* za CircuitPython-om stojí [Adafruit](https://www.adafruit.com/)
* MicroPython vďaka RP2040 získal podporu od [Raspberry Pi Foundation](https://www.raspberrypi.org/)
[![Thonny](images/thonny.jpg)](https://realpython.com/python-thonny/)
notes:
* nebudem vas trapit nastrojmi z prikazoveho riadku ;)
* ziadny rshell, mpremote ani vim
* budem pouzivat thonny, pretoze:
* ma lepsie vyriesenu podporu prace s mikrokontrolermi
* ma zvyraznovanie syntaxe v rezime repl
## Nahratie *Python-u
1. odpojiť zariadenie
2. aktivovať Bootloader režim prepojením pinov `GND` a `REC`
3. znovu pripojiť zariadenie k počítaču
4. nahrať firmvér na zariadenie `RPI-RP2`
5. po nahratí sa zariadenie samo reštartuje
notes:
* https://docs.arduino.cc/tutorials/nano-rp2040-connect/rp2040-openmv-setup
[![MicroPython](images/micropython.png)](http://micropython.org/)
## Blink
* zabudovaná LED je na digitálnom pin-e `13`
* v MicroPython-e je dostupná cez `GPIO6`
```python
from machine import Pin
from time import sleep
led = Pin(6, Pin.OUT)
while True:
led.on()
sleep(1)
led.off()
sleep(0.5)
```
## Networking
```python
>>> import network
>>> wlan = network.WLAN(network.STA_IF)
>>> wlan.active(True)
>>> wlan.scan()
[(b'IoTLab', b'\xe8e\xd4\xca\xcea', 10, -64, 4, 1)]
>>> wlan.connect('IoTLab', 'password')
>>> wlan.isconnected()
True
>>> wlan.ifconfig()
('192.168.0.150', '255.255.255.0', '192.168.0.1', '192.168.0.1')
```
## Pohybový senzor LSM6DSOX
* typ senzoru označovaný aj ako IMU (Inertial Measurement Unit)
* [LSM6DSOX obsahuje](https://docs.arduino.cc/tutorials/nano-rp2040-connect/rp2040-imu-basics)
* akcelerometer
* gyroskop
![IMU Accelerometer](images/imu.accelerometer.png)
notes:
* https://docs.arduino.cc/tutorials/nano-rp2040-connect/rp2040-imu-basics
### Čítanie dát z akcelerometra
```python
from machine import I2C, Pin
from lsm6dsox import LSM6DSOX
import time
i2c = I2C(0, scl=Pin(13), sda=Pin(12))
lsm = LSM6DSOX(i2c)
while True:
ax, ay, az = lsm.read_accel()
print(f"x:{ax:>8.3f} y:{ay:>8.3f} z:{az:>8.3f}")
time.sleep_ms(100)
```
![IMU Accelerometer](images/imu.gyroscope.png)
notes:
* https://docs.arduino.cc/tutorials/nano-rp2040-connect/rp2040-imu-basics
### Čítanie dát z gyroskopu
```python
from machine import I2C, Pin
from lsm6dsox import LSM6DSOX
import time
i2c = I2C(0, scl=Pin(13), sda=Pin(12))
lsm = LSM6DSOX(i2c)
while True:
gx, gy, gz = lsm.read_gyro()
print(f"x:{gx:>8.3f} y:{gy:>8.3f} z:{gz:>8.3f}")
time.sleep_ms(100)
```
## PDM Mikrofón
* aktuálne nie je podpora (?!)
notes:
* nie som si isty, lebo nieco som kedysi zachytil vo fore na temu audio a pdm
## Bluetooth
* vo firmvéri sú dve knižnice:
* [`ubluetooth`](http://docs.micropython.org/en/latest/library/bluetooth.html)
* [`aioble`](https://github.com/detectlabs/aioble)
[![CircuitPython](images/circuitpython.png)](https://circuitpython.org/)
## Zapnutie bootloader režimu v MP
`machine.bootloader()`
## Blink
* zabudovaná LED je dostupná cez `board.LED` alebo cez `board.D13`
```python
import time
import board
from digitalio import DigitalInOut, Direction
# LED setup for onboard LED
led = DigitalInOut(board.LED)
led.direction = Direction.OUTPUT
while True:
led.value = True
time.sleep(1)
led.value = False
time.sleep(0.5)
```
## Networking
([treba stiahnuť moduly](https://learn.adafruit.com/circuitpython-on-the-arduino-nano-rp2040-connect/wifi))
```python
import board
import busio
from digitalio import DigitalInOut
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
from adafruit_esp32spi import adafruit_esp32spi
# ESP32 pins
esp32_cs = DigitalInOut(board.CS1)
esp32_ready = DigitalInOut(board.ESP_BUSY)
esp32_reset = DigitalInOut(board.ESP_RESET)
# uses the secondary SPI connected through the ESP32
spi = busio.SPI(board.SCK1, board.MOSI1, board.MISO1)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
print(esp.scan_networks())
esp.connect_AP('IoTLab', 'password')
print(esp.pretty_ip(esp.ip_address))
```
### Čítanie dát z akcelerometra
([treba stiahnuť moduly](https://learn.adafruit.com/circuitpython-on-the-arduino-nano-rp2040-connect/accelerometer-gyroscope))
```python
import board
from adafruit_lsm6ds.lsm6dsox import LSM6DSOX
import time
i2c = board.I2C()
lsm = LSM6DSOX(i2c)
while True:
ax, ay, az = lsm.acceleration
print(f"x:{ax:>8.3f} y:{ay:>8.3f} z:{az:>8.3f}")
time.sleep(0.1)
```
### Čítanie dát z gyroskopu
([treba stiahnuť moduly](https://learn.adafruit.com/circuitpython-on-the-arduino-nano-rp2040-connect/accelerometer-gyroscope))
```python
import board
from adafruit_lsm6ds.lsm6dsox import LSM6DSOX
import time
i2c = board.I2C()
lsm = LSM6DSOX(i2c)
while True:
gx, gy, gz = lsm.gyro
print(f"x:{gx:>8.3f} y:{gy:>8.3f} z:{gz:>8.3f}")
time.sleep(0.1)
```
## PDM Mikrofón
```python
import time
import array
import math
import board
import audiobusio
def mean(values):
return sum(values) / len(values)
def normalized_rms(values):
minbuf = int(mean(values))
samples_sum = sum(
float(sample - minbuf) * (sample - minbuf)
for sample in values
)
return math.sqrt(samples_sum / len(values))
mic = audiobusio.PDMIn(board.MICROPHONE_CLOCK,
board.MICROPHONE_DATA, sample_rate=16000, bit_depth=16)
samples = array.array('H', [0] * 160)
while True:
mic.record(samples, len(samples))
magnitude = normalized_rms(samples)
print(magnitude)
time.sleep(0.1)
```
## Bluetooth
* vo firmvéri je modul [`_bleio`](https://circuitpython.readthedocs.io/en/latest/shared-bindings/_bleio/index.html)
* prefix `_` znamená, že modul je pre interné použitie a nie pre koncového používateľa
* odporúčanie je použiť [`adafruit_ble`](https://circuitpython.readthedocs.io/projects/ble/en/latest/), ktorý je postavený na `_bleio`
## Nedostatky
* klient pre MQTT
* v MicroPythone ešte nie je (ale očakávam, že bude)
* v CircuitPythone je externá knižnica [Adafruit MiniMQTT](https://github.com/adafruit/Adafruit_CircuitPython_MiniMQTT)
* Bluetooth?
* RGB LED
## Ďalšie zdroje
* [Arduino Nano RP2040 Connect rozcestník](https://docs.arduino.cc/hardware/nano-rp2040-connect)
* [Nano RP2040 Connect Cheat Sheet](https://docs.arduino.cc/tutorials/nano-rp2040-connect/rp2040-01-technical-reference)
* [Datasheet](https://content.arduino.cc/assets/ABX00053-datasheet.pdf)
* Arduino Nano RP2040 Connect na
* [rpishop.cz](https://rpishop.cz/arduino/3807-arduino-nano-rp2040-connect.html?ssa_query=arduino%20nano%20rp2040)
* [rlx.sk](https://rlx.sk/sk/original-arduino-boards/8202-abx00052-arduino-nano-rp2040-connect.html)
![qr code](https://api.qrserver.com/v1/create-qr-code/?data=https://bit.ly/3DOHvq2&size=300x300)
(**https://bit.ly/3DOHvq2**)