Circuit Python at Noisebridge: Difference between revisions

From Noisebridge
Jump to navigation Jump to search
No edit summary
 
(17 intermediate revisions by 2 users not shown)
Line 6: Line 6:


* If you are new to Circuit Python, start here! https://learn.adafruit.com/welcome-to-circuitpython/
* If you are new to Circuit Python, start here! https://learn.adafruit.com/welcome-to-circuitpython/
* We are using the `Lolin S2 Mini` board. Here's  some information: https://circuitpython.org/board/lolin_s2_mini/
* Also see [https://learn.adafruit.com/circuitpython-essentials CircuitPython Essentials Learn Guide]
* Todbot has some fun code to play with, like synthesizers! https://gist.github.com/todbot/
* We are using the '''[https://www.wemos.cc/en/latest/s2/s2_mini.html Lolin S2 Mini]''' board. Here's  some information: https://circuitpython.org/board/lolin_s2_mini/
 
==Getting Started with Programming==
 
Your board will automatically run the code found in the file '''code.py.''' Change the contents of that file to run different code.
To use most hardware like NeoPixel LEDs, sensors, or motor drivers, you will need to install the appropriate library. Here's how to do that:
https://learn.adafruit.com/welcome-to-circuitpython/circuitpython-libraries. If you are OK using the CLI you can use [https://learn.adafruit.com/keep-your-circuitpython-libraries-on-devices-up-to-date-with-circup/prepare circup]. Remember to put the libraries you need in the <code>/lib</code> directory. 
 
 
==Documentation Links (thanks to [https://github.com/todbot/circuitpython-tricks todbot's Circuit Python Tricks]!)==
* [https://docs.python.org/3/tutorial/ The Python Tutorial] on Python.org, since "CircuitPython is Python" mostly. (approx. Python 3.4)
* [https://docs.circuitpython.org/en/latest/docs/ CircuitPython API reference], particularly the [https://docs.circuitpython.org/en/latest/shared-bindings/index.html#modules "Core Modules > Modules" section] in the left sidebar for compiled-in libraries like <code>displayio</code>, <code>usb</code>, <code>audioio</code>, <code>ulab.numpy</code>
* [https://github.com/adafruit/Adafruit_CircuitPython_Bundle Pure-Python libraries in Adafruit Library Bundle] for [https://github.com/adafruit/Adafruit_CircuitPython_Bundle/tree/main/libraries/drivers drivers] & [helpers](https://github.com/adafruit/Adafruit_CircuitPython_Bundle/tree/main/libraries/helpers) libraries like <code>board</code>, <code>neopixel</code> & <code>ble</code>
 
==Things to Try!==
 
* Run the "hello world" code found in '''[https://codeberg.org/headrotor/circuitpython-examples/src/branch/main/lolinS2mini/code.py code.py].'''  This will blink the LED and write text to the serial output for debugging.
* Try something simple. Can you make the onboard LED (<code>board.LED</code>) blink from the button (<code>board.BUTTON</code>)? https://learn.adafruit.com/circuitpython-essentials/circuitpython-digital-in-out
* Can you change the brightness of the LED using PWM? https://learn.adafruit.com/circuitpython-essentials/circuitpython-pwm
* Try something more complicated, like controlling Neopixel addressable LEDs: https://learn.adafruit.com/circuitpython-essentials/circuitpython-neopixel
* Your board can look like a USB HID device, so you can make it into a keyboard! https://learn.adafruit.com/make-it-a-keyboard/circuitpython
* Your board is Wi-Fi and Bluetooth capable! Try running a webserver: https://docs.circuitpython.org/projects/httpserver/en/latest/index.html
* Your board can send alerts to your phone using '''[https://docs.ntfy.sh/ ntfy.sh]'''. Here's how: https://www.tomshardware.com/how-to/send-alerts-raspberry-pi-pico-w-to-mobile-device
 
Having problems? Working examples of code for several suggestions can be found here: https://codeberg.org/headrotor/circuitpython-examples
 
==Hooking up Hardware==
 
We have soldering equipment so you can install the headers that came with your board. A good tip is to put the headers into a solderless breadboard before soldering so they are straight and even. Only install the outside headers (odd pins) if you want to use this in a solderless breadboard otherwise they will short! We have lots of hardware to play with including sensors and Neopixel LEDs; you will need the Adafruit library: https://learn.adafruit.com/circuitpython-essentials/circuitpython-neopixel


==Tips and Tricks==
==Tips and Tricks==
Fun code examples from [https://github.com/todbot/circuitpython-tricks todbot's Circuit Python Tricks] and [https://github.com/todbot/qtpy-tricks QT Py Tricks]
Note the button needs a pullup so be sure to set 
<pre>
switch = DigitalInOut(board.button)
switch.direction = Direction.INPUT
switch.pull = Pull.UP
</pre>


To see the names of the pins available, enter the REPL (using Mu or a terminal program
To see the names of the pins available, enter the REPL (using Mu or a terminal program
Line 16: Line 53:
dir(board)
dir(board)
</pre>
</pre>
For the LED library, pin 1 = <tt>IO1</tt> and so forth...


It is possible that the silkscreen labels for pins 12 and 13 have been swapped. Test to make sure!
* For the LED library, pin 1 = <tt>IO1</tt> and so forth... If you have installed only the outside headers, use pin <tt>IO3</tt> (or odd numbered pins) so that there's something to connect to.
* It is possible that the silkscreen labels for pins 12 and 13 have been swapped. Test to make sure!
* Todbot has some fun code to play with, like synthesizers! https://gist.github.com/todbot/


== Lolin S2 Mini pinout ==
== Lolin S2 Mini pinout ==
Here is a mapping of Python pin names to the board pins (labeled in white on the bottom of the board). a "YES" in the "Touch" column means that <code>touchio</code> capacitive sensing can be used for that pin.
{| class="wikitable" style="margin:auto"
|+ Lolin D2 Mini Pinout
|-
! Python Name !! Alternate Name !! Pin Number !! Touch
|-
| board.IO0 || board.BUTTON  || (button) ||
|-
| board.IO1 || || 1 || YES
|-
| board.IO2 || || 2 || YES
|-
| board.IO3 || || 3 || YES
|-
| board.IO4 || || 4 || YES
|-
| board.IO5 || || 5 || YES
|-
| board.IO6 || || 6 || YES
|-
| board.IO7 || board.SCK || 7  || YES
|-
| board.IO8 || || 8  || YES
|-
| board.IO9 || board.MISO || 9  || YES
|-
| board.IO10 || || 10  || YES
|-
| board.IO11 || board.MOSI || 11  || YES
|-
| board.IO12 || || 12  || YES
|-
| board.IO13 || || 13  || YES
|-
| board.IO14 || || 14  || YES
|-
| board.IO15 || board.LED || (LED)  ||
|-
| board.IO16 || || 16  ||
|-
| board.IO17 || || 17  ||
|-
| board.IO18 || || 18  ||
|-
| board.IO21 || || 21  ||
|-
| board.IO33 || board.SDA || 33  ||
|-
| board.IO34 || || 34  ||
|-
| board.IO35 || board.SCL || 35  ||
|-
| board.IO36 || || 36  ||
|-
| board.IO37 || || 37  ||
|-
| board.IO38 || || 38  ||
|-
| board.IO39 || || 39  ||
|-
| board.IO40 || || 40  ||
|-
|}
Some alternate pin names are available, these don't correspond to anything on the board so no need to use them.
<pre>
<pre>
board.A0 board.IO3
board.A0 board.IO3
board.BUTTON board.IO0
board.D0 board.IO5
board.D0 board.IO5
board.D1 board.IO35 board.SCL
board.D1 board.IO35  
board.D2 board.IO33 board.SDA
board.D2 board.IO33  
board.D3 board.IO18
board.D3 board.IO18
board.D4 board.IO16
board.D4 board.IO16
board.D5 board.IO7 board.SCK
board.D5 board.IO7  
board.D6 board.IO9 board.MISO
board.D6 board.IO9  
board.D7 board.IO11 board.MOSI
board.D7 board.IO11  
board.D8 board.IO13
board.D8 board.IO13
board.IO1
board.IO10
board.IO12
board.IO14
board.IO15 board.LED
board.IO17
board.IO2
board.IO21
board.IO34
board.IO36
board.IO37
board.IO38
board.IO39
board.IO4
board.IO40
board.IO6
board.IO8
</pre>
</pre>


Line 55: Line 143:




We have already done this for you! But you might need to do it again, or if you get your own boards, this is how we do it.  
''We have already done this for you! But you might need to do it again, or if you get your own boards, this is how we do it.''
 


# Install <tt>esptool.py</tt> from
# Download the latest Circuit Python binary from here: https://circuitpython.org/board/lolin_s2_mini/
# Download the latest Circuit Python binary from here:  
# Put the Lolin S2 board into tty mode: while holding down the "<tt>0</tt>" button, press and release the "<tt>RESET</tt>" button. Make sure there's a tty (com port) available.
# Put the Lolin S2 board into tty mode: while holding down RESET button, press and release the 0 button
# Install <tt>esptool.py</tt> frop PIP: <tt> python -m pip install esptool </tt>
# At the command line, enter <pre> esptool.py -p /dev/ttyACM2 --chip esp32s2 --baud 1000000 write_flash -z 0x0 adafruit-circuitpython-lolin_s2_mini-en_US-8.0.0-beta.6.bin</pre>  (use the filename of the binary you just downloaded, it will be more recent than 8.0.0)
# At the command line, enter <pre> esptool.py -p /dev/ttyACM0 --chip esp32s2 --baud 1000000 write_flash -z 0x0 adafruit-circuitpython-lolin_s2_mini-en_US-8.0.0-beta.6.bin</pre>  (use the right serial port if not ttyACM0, also use filename of the binary you just downloaded: it will be more recent than 8.0.0)
# You can also do the previous step using [https://adafruit.github.io/Adafruit_WebSerial_ESPTool/], but only on Chrome browsers
# (if this doesn't work at first, try erasing flash with <tt>esptool.py --port PORT_NAME erase_flash</tt>, and/or repeat until it enumerates as <tt>/dev/ttyACM1</tt>)
# Reconnect your S2 Mini board, you should see a thumbdrive appear labeled <syntaxhighlight inline lang="css">CIRCUITPYTHON</syntaxhighlight>
# You can also do the previous 2 steps using https://adafruit.github.io/Adafruit_WebSerial_ESPTool/ or  https://espressif.github.io/esptool-js/ but only on Chrome browsers. This can fix problems if esptool is not working.
# Reconnect your S2 Mini board, you should see a removable drive appear labeled <tt>CIRCUITPY</tt>
# You are now able to edit <tt>code.py</tt>, add libraries to <tt>/lib</tt>, and connect to the serial port (usually <tt>/dev/ttyACM0</tt> on Mac/Linux or <tt>COM3</tt> on Windows
# You are now able to edit <tt>code.py</tt>, add libraries to <tt>/lib</tt>, and connect to the serial port (usually <tt>/dev/ttyACM0</tt> on Mac/Linux or <tt>COM3</tt> on Windows

Latest revision as of 18:52, 12 September 2023

Circuit Python at Noisebridge[edit]

Welcome to this page, which will get you started with Circuit Python. You can do this any time, or with a group at Circuit Hacking Monday

Some links to get started:

Getting Started with Programming[edit]

Your board will automatically run the code found in the file code.py. Change the contents of that file to run different code. To use most hardware like NeoPixel LEDs, sensors, or motor drivers, you will need to install the appropriate library. Here's how to do that: https://learn.adafruit.com/welcome-to-circuitpython/circuitpython-libraries. If you are OK using the CLI you can use circup. Remember to put the libraries you need in the /lib directory.


Documentation Links (thanks to todbot's Circuit Python Tricks!)[edit]

Things to Try![edit]

Having problems? Working examples of code for several suggestions can be found here: https://codeberg.org/headrotor/circuitpython-examples

Hooking up Hardware[edit]

We have soldering equipment so you can install the headers that came with your board. A good tip is to put the headers into a solderless breadboard before soldering so they are straight and even. Only install the outside headers (odd pins) if you want to use this in a solderless breadboard otherwise they will short! We have lots of hardware to play with including sensors and Neopixel LEDs; you will need the Adafruit library: https://learn.adafruit.com/circuitpython-essentials/circuitpython-neopixel

Tips and Tricks[edit]

Fun code examples from todbot's Circuit Python Tricks and QT Py Tricks

Note the button needs a pullup so be sure to set

switch = DigitalInOut(board.button)
switch.direction = Direction.INPUT
switch.pull = Pull.UP

To see the names of the pins available, enter the REPL (using Mu or a terminal program

import board
dir(board)
  • For the LED library, pin 1 = IO1 and so forth... If you have installed only the outside headers, use pin IO3 (or odd numbered pins) so that there's something to connect to.
  • It is possible that the silkscreen labels for pins 12 and 13 have been swapped. Test to make sure!
  • Todbot has some fun code to play with, like synthesizers! https://gist.github.com/todbot/

Lolin S2 Mini pinout[edit]

Here is a mapping of Python pin names to the board pins (labeled in white on the bottom of the board). a "YES" in the "Touch" column means that touchio capacitive sensing can be used for that pin.

Lolin D2 Mini Pinout
Python Name Alternate Name Pin Number Touch
board.IO0 board.BUTTON (button)
board.IO1 1 YES
board.IO2 2 YES
board.IO3 3 YES
board.IO4 4 YES
board.IO5 5 YES
board.IO6 6 YES
board.IO7 board.SCK 7 YES
board.IO8 8 YES
board.IO9 board.MISO 9 YES
board.IO10 10 YES
board.IO11 board.MOSI 11 YES
board.IO12 12 YES
board.IO13 13 YES
board.IO14 14 YES
board.IO15 board.LED (LED)
board.IO16 16
board.IO17 17
board.IO18 18
board.IO21 21
board.IO33 board.SDA 33
board.IO34 34
board.IO35 board.SCL 35
board.IO36 36
board.IO37 37
board.IO38 38
board.IO39 39
board.IO40 40

Some alternate pin names are available, these don't correspond to anything on the board so no need to use them.

board.A0 board.IO3
board.D0 board.IO5
board.D1 board.IO35 
board.D2 board.IO33 
board.D3 board.IO18
board.D4 board.IO16
board.D5 board.IO7 
board.D6 board.IO9 
board.D7 board.IO11 
board.D8 board.IO13

Installing Circuit Python on Lolin S2 Mini boards[edit]

We have already done this for you! But you might need to do it again, or if you get your own boards, this is how we do it.


  1. Download the latest Circuit Python binary from here: https://circuitpython.org/board/lolin_s2_mini/
  2. Put the Lolin S2 board into tty mode: while holding down the "0" button, press and release the "RESET" button. Make sure there's a tty (com port) available.
  3. Install esptool.py frop PIP: python -m pip install esptool
  4. At the command line, enter
     esptool.py -p /dev/ttyACM0 --chip esp32s2 --baud 1000000 write_flash -z 0x0 adafruit-circuitpython-lolin_s2_mini-en_US-8.0.0-beta.6.bin
    (use the right serial port if not ttyACM0, also use filename of the binary you just downloaded: it will be more recent than 8.0.0)
  5. (if this doesn't work at first, try erasing flash with esptool.py --port PORT_NAME erase_flash, and/or repeat until it enumerates as /dev/ttyACM1)
  6. You can also do the previous 2 steps using https://adafruit.github.io/Adafruit_WebSerial_ESPTool/ or https://espressif.github.io/esptool-js/ but only on Chrome browsers. This can fix problems if esptool is not working.
  7. Reconnect your S2 Mini board, you should see a removable drive appear labeled CIRCUITPY
  8. You are now able to edit code.py, add libraries to /lib, and connect to the serial port (usually /dev/ttyACM0 on Mac/Linux or COM3 on Windows