Difference between revisions of "About Circuit Playground Express"
(48 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
− | = | + | =First things first: what is a microcontroller?= |
Computer and processor are generic terms for anything that can run a program, basically. <br> | Computer and processor are generic terms for anything that can run a program, basically. <br> | ||
A controller or microcontroller usually refers to a simple processor that does only one task, like listening to sensors. <br> | A controller or microcontroller usually refers to a simple processor that does only one task, like listening to sensors. <br> | ||
In explaining microcontrollers, we’ll distinguish them from computers, which contain more powerful processors that can run an operating system. <br> | In explaining microcontrollers, we’ll distinguish them from computers, which contain more powerful processors that can run an operating system. <br> | ||
− | + | One of the most used microcontroller is [[About Arduino | Arduino]]. In this case though we will be talking about Adafruit Circuit Playground, a board with a hand design for several project. | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | In | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
+ | =='''Adafruit Circuit Playground Express & Blue Fruit'''== | ||
+ | How's that different from an [[About Arduino | Arduino]]? | ||
The Circuit Playground Express & Blue Fruit, produced by Adafruit, are microcontrollers, just like the Arduino Uno, but designed for wearable projects. | The Circuit Playground Express & Blue Fruit, produced by Adafruit, are microcontrollers, just like the Arduino Uno, but designed for wearable projects. | ||
Line 36: | Line 16: | ||
[[File:Da1d-ADA-3333-1-0-2-1000x667.jpg|450px]][[file:4333-10 1024x1024.jpg|300px]] | [[File:Da1d-ADA-3333-1-0-2-1000x667.jpg|450px]][[file:4333-10 1024x1024.jpg|300px]] | ||
− | ==CircuitPython & MU Editor== | + | =='''CircuitPython & MU Editor'''== |
[[file:Circuitpython_circuit_playground_adafruit_blinka_computer.png|450px]] [[file:17850097.png|200px]] | [[file:Circuitpython_circuit_playground_adafruit_blinka_computer.png|450px]] [[file:17850097.png|200px]] | ||
Line 50: | Line 30: | ||
Before we start we all have to do this to set up: | Before we start we all have to do this to set up: | ||
− | ===1. Install or update CircuitPython!=== | + | ==='''1. Install or update CircuitPython!'''=== |
https://learn.adafruit.com/adafruit-circuit-playground-express/circuitpython-quickstart | https://learn.adafruit.com/adafruit-circuit-playground-express/circuitpython-quickstart | ||
Line 69: | Line 49: | ||
'''That's it! You're done :)''' | '''That's it! You're done :)''' | ||
− | ===2. Installing Mu | + | ==='''2. Installing Mu Edito'''r=== |
Download Mu from(if you are using computers from school, the Mu Editor is already installed) | Download Mu from(if you are using computers from school, the Mu Editor is already installed) | ||
Line 90: | Line 70: | ||
[[File:Circuitpython Screen Shot 2017-12-24 at 3.16.23 PM.png]] | [[File:Circuitpython Screen Shot 2017-12-24 at 3.16.23 PM.png]] | ||
− | |||
− | In the code area, where it says, # Write your code here :-), write the | + | ===3'''. Hello, World!'''=== |
− | following code: | + | In the code area, where it says, # Write your code here :-), write the following code: |
+ | |||
'''print( 8 )''' | '''print( 8 )''' | ||
+ | |||
+ | Then click the Save button to save the code and run it on the CircuitPlayground. | ||
+ | |||
+ | You should see this message appear in the Serial Dialogue Panel: | ||
+ | |||
+ | '''code.py output: | ||
+ | 8''' | ||
+ | |||
+ | Change the message to something else. If you want to print characters instead of numbers, you must use quotation marks. | ||
+ | |||
+ | '''print( "hello" )''' | ||
+ | |||
+ | Let's program the device to print two things, with a time delay in between the two print statements: | ||
+ | |||
+ | '''import time''' | ||
+ | |||
+ | '''print( "hello" )''' | ||
+ | |||
+ | '''time.sleep( 0.5 )''' | ||
+ | |||
+ | '''print( "World" )''' | ||
+ | |||
+ | Now let's make a loop, so the message will be running forever: | ||
+ | |||
+ | '''import time | ||
+ | |||
+ | '''while True:''' | ||
+ | |||
+ | '''print( "hello" )''' | ||
+ | '''time.sleep( 0.5 )''' | ||
+ | '''print( "World" )''' | ||
+ | |||
+ | |||
+ | |||
+ | '''Indentation and commenting options''' | ||
+ | |||
+ | Python programs are Space holder Character Sensitive. | ||
+ | |||
+ | To indent the three lines, I selected them all and then pressed the '''Tab key'''. | ||
+ | |||
+ | To unindent, select some lines and press '''Shift-Tab.''' | ||
+ | For commenting: select lines and press '''command(control)-k''' | ||
+ | |||
+ | [[File:Mu CircuitPython Mode Cheat Sheet 1.jpg |600px]] | ||
+ | |||
+ | ==='''4. Interacting to the Serial Console''' === | ||
+ | |||
+ | |||
+ | ==== '''The REPL'''==== | ||
+ | |||
+ | The other feature of the serial connection is the '''Read-Evaluate-Print-Loop''', or REPL. | ||
+ | |||
+ | The REPL allows you to enter individual lines of code and have them run immediately. | ||
+ | |||
+ | It's really handy if you're running into trouble with a particular program and can't figure out why. | ||
+ | |||
+ | It's interactive so it's great for testing new ideas. | ||
+ | |||
+ | To use the REPL, you first need to be connected to the serial console. | ||
+ | |||
+ | Once that connection has been established, you'll want to press '''Ctrl + C'''. | ||
+ | |||
+ | If there is code running, it will stop and you'll see Press any key to enter the REPL. Use '''CTRL-D to reload'''. | ||
+ | |||
+ | Follow those instructions, and press any key on your keyboard. | ||
+ | |||
+ | https://learn.adafruit.com/adafruit-circuit-playground-express/the-repl | ||
+ | |||
+ | |||
+ | ==='''5. Simple digital and analog inputs and output'''=== | ||
+ | |||
+ | |||
+ | |||
+ | '''1. Digital:''' | ||
+ | |||
+ | https://learn.adafruit.com/circuitpython-essentials/circuitpython-digital-in-out | ||
+ | |||
+ | |||
+ | |||
+ | '''2. Digital in and out With external electronic components:''' | ||
+ | |||
+ | '''After we followed the example, now let's try to connect an external Led.''' | ||
+ | |||
+ | let's connect the led to A1(D6). There is a list of pinout from Adafruit's website: | ||
+ | |||
+ | https://learn.adafruit.com/adafruit-circuit-playground-express/pinouts | ||
+ | |||
+ | [[File:CircuitPlayground-express leds.jpg|500px]] | ||
+ | |||
+ | '''instead of the onboard button, we can also use an external button''' | ||
+ | |||
+ | [[File:CircuitPlayground-express leds-button.jpg|500px]] | ||
+ | |||
+ | |||
+ | [[File:Digital-external.PNG|500px]] | ||
+ | |||
+ | |||
+ | '''3. Analog input:''' | ||
+ | |||
+ | https://learn.adafruit.com/circuitpython-essentials/circuitpython-analog-in | ||
+ | |||
+ | |||
+ | '''4. Analog input and output with external electronic components:''' | ||
+ | |||
+ | [[File:CircuitPlayground-express cpx-analog-read.jpg|500px]] | ||
+ | |||
+ | [[File:Analog-read-led-code.PNG|500px]] | ||
+ | |||
+ | import time | ||
+ | import board | ||
+ | import analogio | ||
+ | |||
+ | from adafruit_circuitplayground import cp | ||
+ | |||
+ | analogin = analogio.AnalogIn(board.A2) | ||
+ | |||
+ | def getVoltage(pin): | ||
+ | return (pin.value * 3.3) / 300 | ||
+ | |||
+ | while True: | ||
+ | sensor = getVoltage(analogin) | ||
+ | cp.play_tone(sensor,0.05) | ||
+ | print("analog voltage:", sensor) | ||
+ | time.sleep(0.01) | ||
+ | |||
+ | ==='''6. CircuitPython Made Easy'''=== | ||
+ | |||
+ | ====Libraries:==== | ||
+ | |||
+ | Libraries are codes other people have written for you. The | ||
+ | libraries we will be using are included as part of the CircuitPython code, but you still | ||
+ | must import them into your program. Other libraries, for many uses, must be | ||
+ | downloaded and placed in '''a folder named lib''' on the CIRCUITPY drive. | ||
+ | |||
+ | |||
+ | '''CircuitPython Libraries already included in CPX, But if you are using CPB,''' (CPX = Circuit Playground Express, CPB = Circuit Playground Bluefruit) | ||
+ | |||
+ | '''you need to install CircuitPython Libraries by download the file and put them in the folder named lib on the CIRCUITPY drive.''' | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | we will use circuit playground libraries, a simple way to include the Circuit Playground functionality in our code. | ||
+ | |||
+ | more reference and examples: | ||
+ | |||
+ | https://learn.adafruit.com/circuitpython-made-easy-on-circuit-playground-express | ||
+ | |||
+ | |||
+ | ====Examples with CPX On-Board sensors:==== | ||
+ | |||
+ | SAVE AS: | ||
+ | |||
+ | if you want to save the code.py file in different names as well as in different locations: | ||
+ | |||
+ | double click on the file name in Mu editor. | ||
+ | |||
+ | [[File:Save-as-Capture.PNG|500px]] | ||
+ | |||
+ | ====='''1. Red LED:'''===== | ||
+ | |||
+ | https://learn.adafruit.com/circuitpython-made-easy-on-circuit-playground-express/red-led | ||
+ | |||
+ | ====='''2. Slide Switch:'''===== | ||
+ | |||
+ | https://learn.adafruit.com/circuitpython-made-easy-on-circuit-playground-express/slide-switch | ||
+ | |||
+ | ====='''3. Smiley face'''===== | ||
+ | |||
+ | [[File:Smiley face.PNG|600px]] | ||
+ | |||
+ | ====='''4. light sensor:'''===== | ||
+ | |||
+ | https://learn.adafruit.com/circuitpython-made-easy-on-circuit-playground-express/light | ||
+ | |||
+ | ====='''5. play tone:'''===== | ||
+ | |||
+ | https://learn.adafruit.com/circuitpython-made-easy-on-circuit-playground-express/play-tone | ||
+ | |||
+ | ====='''Exercise:'''===== | ||
+ | |||
+ | combine the code from examples "Play tone" and "light sensor". and use light sensor input to generate tones. | ||
+ | |||
+ | |||
+ | ====='''Conditionals:'''===== | ||
+ | ----------------------------------------------------------------------------------------------------------------- | ||
+ | |||
+ | Evaluate a condition and react. If True, do this. If False, do this other thing. | ||
+ | |||
+ | '''"while"''' creates a repeated action. while this condition is true, continue to do this thing over and over. | ||
+ | |||
+ | when the condition is false, go on to the rest of the program. | ||
+ | |||
+ | '''"if"''' creates a single action. if this condition is true, do this thing. if false, don't do it. | ||
+ | |||
+ | '''"else"''' creates a single action, when the if the condition is false. | ||
+ | |||
+ | There is a CircuitPython reference PDF: | ||
+ | |||
+ | [[File:CircuitPython Reference Sheet.pdf]] | ||
+ | |||
+ | please read it after this lesson. | ||
+ | |||
+ | |||
+ | ====='''6. Capacitive Touch & NeoPixels:'''===== | ||
+ | [[file:1-touch-light-Capture.PNG|600px]] | ||
+ | |||
+ | |||
+ | ====='''7. Accelerometer: Tap'''===== | ||
+ | |||
+ | https://learn.adafruit.com/circuitpython-made-easy-on-circuit-playground-express/tap | ||
+ | |||
+ | ====='''8. Play audio file'''===== | ||
+ | |||
+ | https://learn.adafruit.com/circuitpython-made-easy-on-circuit-playground-express/play-file | ||
+ | |||
+ | ====='''9. Drum machine'''===== | ||
+ | |||
+ | download the drum sample file here: | ||
+ | |||
+ | https://learn.adafruit.com/adafruit-circuit-playground-express/playground-drum-machine | ||
+ | |||
+ | since we are using the build-in Library, instead of using the code from the link, our code should look like this: | ||
+ | |||
+ | [[File:Drum-machine-Capture.PNG|600px]] | ||
+ | |||
+ | ===7. CPX external inputs and outputs Wiring=== | ||
+ | |||
+ | |||
+ | |||
+ | [[File: Circuit playground Adafruit Circuit Playground Express Pinout.png|600px]] | ||
+ | |||
+ | '''Connect to external speakers:''' | ||
+ | |||
+ | |||
+ | [[file:Circuit playground hp.png|600px]] | ||
+ | |||
+ | |||
+ | |||
+ | '''Connect to servo motors:''' | ||
+ | |||
+ | single motor: | ||
+ | |||
+ | [[file:Circuitpython safe.png|600px]] | ||
+ | |||
+ | multiple motors: | ||
+ | |||
+ | [[File:Circuit playground CProbot.png|600px]] | ||
+ | |||
+ | |||
+ | you can find the library and code for the servo motors here: | ||
+ | |||
+ | https://learn.adafruit.com/adafruit-circuit-playground-express/circuitpython-servo | ||
+ | |||
+ | |||
+ | ultra-sonic sensor: | ||
+ | |||
+ | [[File:Sensors cpx-hcsr06-breadboard-crocclipse bb.png |600px]] | ||
+ | |||
+ | https://learn.adafruit.com/jack-o-theremin/circuit-python-code | ||
+ | [[Category:Circuit Playground Express]][[Category:Microcontrollers]] [[Category:Mu Editor]] |
Latest revision as of 10:56, 17 January 2023
First things first: what is a microcontroller?
Computer and processor are generic terms for anything that can run a program, basically.
A controller or microcontroller usually refers to a simple processor that does only one task, like listening to sensors.
In explaining microcontrollers, we’ll distinguish them from computers, which contain more powerful processors that can run an operating system.
One of the most used microcontroller is Arduino. In this case though we will be talking about Adafruit Circuit Playground, a board with a hand design for several project.
Adafruit Circuit Playground Express & Blue Fruit
How's that different from an Arduino?
The Circuit Playground Express & Blue Fruit, produced by Adafruit, are microcontrollers, just like the Arduino Uno, but designed for wearable projects.
Thus, is a little different from the normal boards from the hardware side, since the pins go out to those big wide sewable pads, instead of normal pin headers (like on the Uno)
because Circuit Playground Express running on a faster processor compared to Arduino Uno. We can also use it for audio-based projects.
CircuitPython & MU Editor
Python is the fastest-growing programming language. And CircuitPython is based on Python language but designed to run on microcontroller boards.
CircuitPython is designed with education in mind.
It's easy to start learning how to program and you get immediate feedback from the board.
Before we start we all have to do this to set up:
1. Install or update CircuitPython!
https://learn.adafruit.com/adafruit-circuit-playground-express/circuitpython-quickstart
*1. Click the link below and download the latest UF2 file: https://circuitpython.org/board/circuitplayground_express/
*2. Plug your Circuit Playground Express into your computer via a (data)USB cable.
*3. Double-click the small Reset button in the middle of the CPX(short for circuit playground express). you will see all of the LEDs turn green. If they turn all red, check the USB cable, try another USB port, etc. (If double-clicking doesn't do it, try a single-click!)
*4. You will see a new disk drive appear called CPLAYBOOT Drag the adafruit-circuitpython-etc...uf2 file onto it.
*5. The CPLAYBOOT drive will disappear and a new disk drive will appear called CIRCUITPY
That's it! You're done :)
2. Installing Mu Editor
Download Mu from(if you are using computers from school, the Mu Editor is already installed)
The first time you start Mu, you will be prompted to select your 'mode' -
you can always change your mind later. For now please select CircuitPython!
Mu attempts to auto-detect your board, so please plug in your CircuitPython device and make sure it shows up as a CIRCUITPY drive before starting Mu
3. Hello, World!
In the code area, where it says, # Write your code here :-), write the following code:
print( 8 )
Then click the Save button to save the code and run it on the CircuitPlayground.
You should see this message appear in the Serial Dialogue Panel:
code.py output: 8
Change the message to something else. If you want to print characters instead of numbers, you must use quotation marks.
print( "hello" )
Let's program the device to print two things, with a time delay in between the two print statements:
import time
print( "hello" )
time.sleep( 0.5 )
print( "World" )
Now let's make a loop, so the message will be running forever:
import time while True: print( "hello" ) time.sleep( 0.5 ) print( "World" )
Indentation and commenting options
Python programs are Space holder Character Sensitive.
To indent the three lines, I selected them all and then pressed the Tab key.
To unindent, select some lines and press Shift-Tab. For commenting: select lines and press command(control)-k
4. Interacting to the Serial Console
The REPL
The other feature of the serial connection is the Read-Evaluate-Print-Loop, or REPL.
The REPL allows you to enter individual lines of code and have them run immediately.
It's really handy if you're running into trouble with a particular program and can't figure out why.
It's interactive so it's great for testing new ideas.
To use the REPL, you first need to be connected to the serial console.
Once that connection has been established, you'll want to press Ctrl + C.
If there is code running, it will stop and you'll see Press any key to enter the REPL. Use CTRL-D to reload.
Follow those instructions, and press any key on your keyboard.
https://learn.adafruit.com/adafruit-circuit-playground-express/the-repl
5. Simple digital and analog inputs and output
1. Digital:
https://learn.adafruit.com/circuitpython-essentials/circuitpython-digital-in-out
2. Digital in and out With external electronic components:
After we followed the example, now let's try to connect an external Led.
let's connect the led to A1(D6). There is a list of pinout from Adafruit's website:
https://learn.adafruit.com/adafruit-circuit-playground-express/pinouts
instead of the onboard button, we can also use an external button
3. Analog input:
https://learn.adafruit.com/circuitpython-essentials/circuitpython-analog-in
4. Analog input and output with external electronic components:
import time import board import analogio
from adafruit_circuitplayground import cp
analogin = analogio.AnalogIn(board.A2)
def getVoltage(pin): return (pin.value * 3.3) / 300
while True: sensor = getVoltage(analogin) cp.play_tone(sensor,0.05) print("analog voltage:", sensor) time.sleep(0.01)
6. CircuitPython Made Easy
Libraries:
Libraries are codes other people have written for you. The libraries we will be using are included as part of the CircuitPython code, but you still must import them into your program. Other libraries, for many uses, must be downloaded and placed in a folder named lib on the CIRCUITPY drive.
CircuitPython Libraries already included in CPX, But if you are using CPB, (CPX = Circuit Playground Express, CPB = Circuit Playground Bluefruit)
you need to install CircuitPython Libraries by download the file and put them in the folder named lib on the CIRCUITPY drive.
we will use circuit playground libraries, a simple way to include the Circuit Playground functionality in our code.
more reference and examples:
https://learn.adafruit.com/circuitpython-made-easy-on-circuit-playground-express
Examples with CPX On-Board sensors:
SAVE AS:
if you want to save the code.py file in different names as well as in different locations:
double click on the file name in Mu editor.
1. Red LED:
https://learn.adafruit.com/circuitpython-made-easy-on-circuit-playground-express/red-led
2. Slide Switch:
https://learn.adafruit.com/circuitpython-made-easy-on-circuit-playground-express/slide-switch
3. Smiley face
4. light sensor:
https://learn.adafruit.com/circuitpython-made-easy-on-circuit-playground-express/light
5. play tone:
https://learn.adafruit.com/circuitpython-made-easy-on-circuit-playground-express/play-tone
Exercise:
combine the code from examples "Play tone" and "light sensor". and use light sensor input to generate tones.
Conditionals:
Evaluate a condition and react. If True, do this. If False, do this other thing. "while" creates a repeated action. while this condition is true, continue to do this thing over and over. when the condition is false, go on to the rest of the program. "if" creates a single action. if this condition is true, do this thing. if false, don't do it. "else" creates a single action, when the if the condition is false. There is a CircuitPython reference PDF: File:CircuitPython Reference Sheet.pdf please read it after this lesson.
6. Capacitive Touch & NeoPixels:
7. Accelerometer: Tap
https://learn.adafruit.com/circuitpython-made-easy-on-circuit-playground-express/tap
8. Play audio file
https://learn.adafruit.com/circuitpython-made-easy-on-circuit-playground-express/play-file
9. Drum machine
download the drum sample file here:
https://learn.adafruit.com/adafruit-circuit-playground-express/playground-drum-machine
since we are using the build-in Library, instead of using the code from the link, our code should look like this:
7. CPX external inputs and outputs Wiring
Connect to external speakers:
Connect to servo motors:
single motor:
multiple motors:
you can find the library and code for the servo motors here:
https://learn.adafruit.com/adafruit-circuit-playground-express/circuitpython-servo
ultra-sonic sensor:
https://learn.adafruit.com/jack-o-theremin/circuit-python-code