Difference between revisions of "Circuitpython"
Line 83: | Line 83: | ||
for more details see: [https://learn.adafruit.com/circuitpython-on-the-arduino-nano-rp2040-connect/accelerometer-gyroscope on how to use the onboard IMU of the Arduino Nano RP2040 Connect (LSM6DSOXTR)] | for more details see: [https://learn.adafruit.com/circuitpython-on-the-arduino-nano-rp2040-connect/accelerometer-gyroscope on how to use the onboard IMU of the Arduino Nano RP2040 Connect (LSM6DSOXTR)] | ||
+ | |||
+ | = Finding your PIN names - Pin mapping script = | ||
+ | |||
+ | CircuitPython uses it own names which makes it easier to use the same code on different boards. But this requires you to first find out which PINs on your physical board map to the logical pins in the program code. | ||
+ | |||
+ | There is a [https://learn.adafruit.com/circuitpython-essentials/circuitpython-pins-and-modules pin mapping scriptt avaiable for this at Adafruit]. You can download the [https://learn.adafruit.com/circuitpython-essentials/circuitpython-pins-and-modules project bundel there]. Here is the script, too: | ||
+ | |||
+ | <syntaxhighlight lang="python" line='line'> | ||
+ | # SPDX-FileCopyrightText: 2020 anecdata for Adafruit Industries | ||
+ | # SPDX-FileCopyrightText: 2021 Neradoc for Adafruit Industries | ||
+ | # SPDX-FileCopyrightText: 2021-2023 Kattni Rembor for Adafruit Industries | ||
+ | # SPDX-FileCopyrightText: 2023 Dan Halbert for Adafruit Industries | ||
+ | # | ||
+ | # SPDX-License-Identifier: MIT | ||
+ | |||
+ | """CircuitPython Essentials Pin Map Script""" | ||
+ | import microcontroller | ||
+ | import board | ||
+ | try: | ||
+ | import cyw43 # raspberrypi | ||
+ | except ImportError: | ||
+ | cyw43 = None | ||
+ | |||
+ | board_pins = [] | ||
+ | for pin in dir(microcontroller.pin): | ||
+ | if (isinstance(getattr(microcontroller.pin, pin), microcontroller.Pin) or | ||
+ | (cyw43 and isinstance(getattr(microcontroller.pin, pin), cyw43.CywPin))): | ||
+ | pins = [] | ||
+ | for alias in dir(board): | ||
+ | if getattr(board, alias) is getattr(microcontroller.pin, pin): | ||
+ | pins.append(f"board.{alias}") | ||
+ | # Add the original GPIO name, in parentheses. | ||
+ | if pins: | ||
+ | # Only include pins that are in board. | ||
+ | pins.append(f"({str(pin)})") | ||
+ | board_pins.append(" ".join(pins)) | ||
+ | |||
+ | for pins in sorted(board_pins): | ||
+ | print(pins) | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | When you run the script on a specific controller you get a text ouput that is the mapping. | ||
+ | |||
+ | == CircuitPython Pin Mapping for Arduino Nano Connect RP2024 == | ||
+ | |||
+ | <syntaxhighlight lang="python" line='line'> | ||
+ | board.A0 (GPIO26) | ||
+ | board.A1 (GPIO27) | ||
+ | board.A2 (GPIO28) | ||
+ | board.A3 (GPIO29) | ||
+ | board.A4 board.SDA (GPIO12) | ||
+ | board.A5 board.SCL (GPIO13) | ||
+ | board.CS1 board.ESP_CS (GPIO9) | ||
+ | board.D0 board.TX (GPIO0) | ||
+ | board.D1 board.RX (GPIO1) | ||
+ | board.D10 (GPIO5) | ||
+ | board.D11 board.MOSI (GPIO7) | ||
+ | board.D12 board.MISO (GPIO4) | ||
+ | board.D13 board.LED board.SCK (GPIO6) | ||
+ | board.D2 (GPIO25) | ||
+ | board.D3 (GPIO15) | ||
+ | board.D4 (GPIO16) | ||
+ | board.D5 (GPIO17) | ||
+ | board.D6 (GPIO18) | ||
+ | board.D7 (GPIO19) | ||
+ | board.D8 (GPIO20) | ||
+ | board.D9 (GPIO21) | ||
+ | board.ESP_BUSY (GPIO10) | ||
+ | board.ESP_GPIO0 (GPIO2) | ||
+ | board.ESP_RESET (GPIO3) | ||
+ | board.INT1 (GPIO24) | ||
+ | board.MICROPHONE_CLOCK (GPIO23) | ||
+ | board.MICROPHONE_DATA (GPIO22) | ||
+ | board.MISO1 (GPIO8) | ||
+ | board.MOSI1 (GPIO11) | ||
+ | board.SCK1 (GPIO14) | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | == CircuitPython Pin Mapping for Raspberry PI Pico == | ||
+ | |||
+ | |||
= Install the Mu Editor = | = Install the Mu Editor = |
Revision as of 10:34, 22 June 2024
Introduction and step by step tutorial from Adafruit
Contents
Installing CircuitPython
Go to circuitpython.org/downloads and download the version for your board.
For the Raspberry PI Pico connect the board to the computer and the folder RPI-RP2 shows ups in your file system. For the Arduino Nano Connect RP2040 force the bootloder mode to upload the .uf2 file. Copy the .uf2 into the folder of the board.
Now you should be able to connect to the board and use the editor.
If you need more help, here is a more detailed description on the installation process at adafruit.com
CircuitPy Drive
After installing CircuitPython on the board and reconnecting the board, it should show up as a USB drive on your computer with the name CircuitPy.
Here you can directly access the code you write and data you safe from your programs.
As the board is a USB drive do not just unplug it - use "safe remove" feature of your OS.
Running your first CircuitPython Program
In the file system of the board (that shows up as USB Drive CIRCUITPY) there is a file code.py.
To write your program, open code.py and edit it with your code. Once you safe it, it will be automatically excuted. The file name must be code.py or main.py.
Try it out with the following program. Just use any editor and open code.py. Replace it with the following code and you should see the LED flash. Changes the sleep times. Each time you safe it the new program will be active:
1 # write on serial line and blink onbaord LED
2 import board
3 import digitalio
4 import time
5
6 led = digitalio.DigitalInOut(board.LED)
7 led.direction = digitalio.Direction.OUTPUT
8
9 while True:
10 led.value = False
11 time.sleep(0.8)
12 led.value = True
13 time.sleep(0.2)
Using Libraries
Here is an example of how to use libraries, in this case to read the accelerometer of the Arduino Nano Connect RP2024.
Download the library (in this case it is in the zip from Adafruit, see Download Project Bundle).
Copy the library folders into the library folder on the USB drive (CIRCUITPY).
Now you can use the libraries, e.g. for reading the accelerometer using the adafruit_lsm6ds library.
1 # SPDX-FileCopyrightText: Copyright (c) 2020 Bryan Siepert for Adafruit Industries
2 #
3 # SPDX-License-Identifier: MIT
4 import time
5 import board
6 from adafruit_lsm6ds.lsm6dsox import LSM6DSOX
7
8 i2c = board.I2C() # uses board.SCL and board.SDA
9 # i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
10 sensor = LSM6DSOX(i2c)
11
12 while True:
13 print("Acceleration: X:%.2f, Y: %.2f, Z: %.2f m/s^2" % (sensor.acceleration))
14 print("Gyro X:%.2f, Y: %.2f, Z: %.2f radians/s" % (sensor.gyro))
15 print("")
16 time.sleep(0.5)
for more details see: on how to use the onboard IMU of the Arduino Nano RP2040 Connect (LSM6DSOXTR)
Finding your PIN names - Pin mapping script
CircuitPython uses it own names which makes it easier to use the same code on different boards. But this requires you to first find out which PINs on your physical board map to the logical pins in the program code.
There is a pin mapping scriptt avaiable for this at Adafruit. You can download the project bundel there. Here is the script, too:
1 # SPDX-FileCopyrightText: 2020 anecdata for Adafruit Industries
2 # SPDX-FileCopyrightText: 2021 Neradoc for Adafruit Industries
3 # SPDX-FileCopyrightText: 2021-2023 Kattni Rembor for Adafruit Industries
4 # SPDX-FileCopyrightText: 2023 Dan Halbert for Adafruit Industries
5 #
6 # SPDX-License-Identifier: MIT
7
8 """CircuitPython Essentials Pin Map Script"""
9 import microcontroller
10 import board
11 try:
12 import cyw43 # raspberrypi
13 except ImportError:
14 cyw43 = None
15
16 board_pins = []
17 for pin in dir(microcontroller.pin):
18 if (isinstance(getattr(microcontroller.pin, pin), microcontroller.Pin) or
19 (cyw43 and isinstance(getattr(microcontroller.pin, pin), cyw43.CywPin))):
20 pins = []
21 for alias in dir(board):
22 if getattr(board, alias) is getattr(microcontroller.pin, pin):
23 pins.append(f"board.{alias}")
24 # Add the original GPIO name, in parentheses.
25 if pins:
26 # Only include pins that are in board.
27 pins.append(f"({str(pin)})")
28 board_pins.append(" ".join(pins))
29
30 for pins in sorted(board_pins):
31 print(pins)
When you run the script on a specific controller you get a text ouput that is the mapping.
CircuitPython Pin Mapping for Arduino Nano Connect RP2024
1 board.A0 (GPIO26)
2 board.A1 (GPIO27)
3 board.A2 (GPIO28)
4 board.A3 (GPIO29)
5 board.A4 board.SDA (GPIO12)
6 board.A5 board.SCL (GPIO13)
7 board.CS1 board.ESP_CS (GPIO9)
8 board.D0 board.TX (GPIO0)
9 board.D1 board.RX (GPIO1)
10 board.D10 (GPIO5)
11 board.D11 board.MOSI (GPIO7)
12 board.D12 board.MISO (GPIO4)
13 board.D13 board.LED board.SCK (GPIO6)
14 board.D2 (GPIO25)
15 board.D3 (GPIO15)
16 board.D4 (GPIO16)
17 board.D5 (GPIO17)
18 board.D6 (GPIO18)
19 board.D7 (GPIO19)
20 board.D8 (GPIO20)
21 board.D9 (GPIO21)
22 board.ESP_BUSY (GPIO10)
23 board.ESP_GPIO0 (GPIO2)
24 board.ESP_RESET (GPIO3)
25 board.INT1 (GPIO24)
26 board.MICROPHONE_CLOCK (GPIO23)
27 board.MICROPHONE_DATA (GPIO22)
28 board.MISO1 (GPIO8)
29 board.MOSI1 (GPIO11)
30 board.SCK1 (GPIO14)
CircuitPython Pin Mapping for Raspberry PI Pico
Install the Mu Editor
Follow the instructions for installting the Mu Editor.
- download software from codewith.mu
- Install it on your system
- On first start up select CircuitPython as Mode
- Test if it works in the CircuitPython REPL console