Overview
Are you familiar with block-based programming and trying to make the jump to Python?
You're in luck! EduBlocks is a web-based programming interface that is making the transition from block-based programming languages like Scratch to the syntax-based language Python easier.
If you've never programmed before and are new to block-based programming altogether, check out the block-based language MakeCode first. There's a helpful guide on that here.
In this guide we will go through the EduBlocks editor interface and different code blocks.
Next, we'll see how to upload code to the Circuit Playground Express and troubleshoot any issues. The Circuit Playground Express (CPX) is a great introduction to coding and electronics. Click here to learn more!
Last, we will go over different applications and examples of how to program the CPX to blink an LED, switch NeoPixel colors with buttons, and lastly make a touch-based drum machine!
- A speaker or amplifier with an 1/8" or 1/4" input
If using a speaker with 1/8" input:
If using an amp with 1/4" input:
To trigger drum sounds:
- Spare coins or other capacitive touch materials like aluminum foil or even fruit!
Page last edited March 08, 2024
Text editor powered by tinymce.
The quickest way to get started with EduBlocks is to jump right in! The programming language is web-based, which means there is no downloading necessary. Just head to the website below.
After entering the site, you will be taken to the EduBlocks project editor. This is where all the coding happens!
Page last edited March 08, 2024
Text editor powered by tinymce.
User Interface
The EduBlocks user interface is the area on your screen where the CircuitPython program exists. The screen is divided into multiple sections, which have different functions, from picking blocks to code with, to downloading a CircuitPython file. We will go through the different elements of the interface
-
On the right side of the screen is our editing space. This is where we'll drag and drop blocks onto to create our program.
-
The left column is where we'll find all the different blocks with which to choose from.
-
The toolbar on the top allows us to create new programs, open old ones, save current ones, add extensions, load sample programs, change themes and lastly switch between CircuitPython syntax code and blocks.
- The CircuitPlayground logo at the top will allow you to change modes. EduBlocks supports other platforms like the micro:bit, Raspberry Pi and also Core Python. We won't need to use it in this tutorial, however, you can explore the different platforms if you like.
Page last edited March 08, 2024
Text editor powered by tinymce.
Block Types
EduBlocks is a "block-based" programming language. This means instead of writing lines of code to program, we use "blocks". Each block has a specific category that describes what its purpose is. In EduBlocks, each block type corresponds to a library in CircuitPython.
Each CircuitPython program you run needs to have a lot of information to work. The reason CircuitPython is so simple to use is that most of that information is stored in other files and works in the background. These files are called libraries.
For example the NeoPixel library allows us to control the NeoPixels on the board. Without importing the library, we cannot access the functionality.
- Basic blocks are core functionality of CircuitPython. With these blocks, we can create loops, conditional statements, functions, and more.
- This section allows you to create variables. To create a variable, you simply click the Create Variable.. button, give the variable a name and then two blocks will appear which will allow you to use the variable in your code
- This library lets us manage digital inputs and outputs. For example this library lets us access the buttons on the CPX and use them to trigger outputs like changing the colors of NeoPixels. Read more about the library here.
- This library allows us to control the properties of NeoPixels LEDs such as brightness, color and more. Read more here.
- With touch blocks, we can access the capacitive touch capabilities of different inputs. On the CPX there are multiple capacitive touch inputs we can use to trigger different events, for example, playing a sound. It's all done with our fingers! Learn more here.
- This library allows us to access and control any connected servo motors. Read more here.
- Audio blocks allow us to create our own musical tones as well as play sound files. More here.
- The PWM (Pulse-Width Modulation) library allows us to control components like leds and servos in a more precise way than just on or off. For example we can fade LEDs and change speed of servos. More here.
- Control brightness, color and other attributes of another type of led. Dotstars are great for light painting! More here.
- By clicking "Extentions" on the top right of the interface, we can add one more library entitled "Circuit Playground Easy"
- This library allows us to perform tasks that the other libraries allow with much simpler code. These blocks are great for first timers. More here.
Page last edited March 08, 2024
Text editor powered by tinymce.
Tips and Tricks
- To save a program, click "Save" in the top toolbar. This will automatically place a file entitled "main.xml" into your downloads folder. Rename and save this file somewhere you will remember.
- To load old programs, click open in the toolbar and navigate to the .xml file you saved previously.
- When ready to download programs in CircuitPython format, click Download in the toolbar. This may prompt you to tell your browser if the file is safe to download. Go ahead and click keep and a file called main.py will be downloaded and placed into your downloads folder. Put this file somewhere you will remember. This is the file that will later be put onto the CPX and run the code we wrote.
- To switch to CircuitPython syntax-based code, click the Blocks button on the top right of the toolbar.*
- This will convert the block code into CircuitPython code.
- Click Python on the right side of the toolbar to go back to code blocks.
*This example uses the code from the Analog Demo from Samples in the toolbar.
Copy and Paste
- Need multiple blocks that are the same? Speed up your workflow by selecting blocks then copy and pasting them!
EduBlocks is still in Beta mode, so it unfortunately doesn't have NeoPixel capabilities yet in the CPX Easy library. Thus to program NeoPixels, we must use the NeoPixel library.
Knowing this, when we import both the CPX Easy and NeoPixel libraries, we get a "Value: pin in use" error because both libraries are trying to access the NeoPixels.*
To avoid errors like these when using the CPX Easy library, refrain from also using the NeoPixel library.
*This error would show up in the REPL if using a code editor such as Mu. See Troubleshooting section.
Page last edited March 08, 2024
Text editor powered by tinymce.
Connecting to the CPX
Now we'll set up the CPX for CircuitPython mode to be able to upload the code we write to the board.
If your CPX is already in CircuitPython mode, double check it is updated to the latest version. Check on the circuitpython.org site to see the latest version. You can find the current version by opening up the file boot_out.txt on the root directory of the CIRCUITPY drive.
You will know you are in CircuitPython mode if the drive CIRCUITPY shows up on your computer when connected via micro-B USB cable to your computer.
If your board doesn't have CircuitPython installed
If this is your first time using your CPX with CircuitPython or you see a drive entitled CPLAYBOOT, your board may not have CircuitPython installed yet. No worries. Follow the steps and extended guide below to switch to CircuitPython mode.
- First install or update CircuitPython. The latest file from circuitpython.org will be a .uf2 file.
- Next, plug the CPX to your a computer via micro-B USB cable and double click the reset button on the Circuit Playground Express (the CPX NeoPixels should turn green).
- Now drag the recently downloaded .uf2 file onto the CPLAYBOOT drive oon your computer.
- The drive should disappear from your File Explorer or Finder (depending on your operating system) and come back as CIRCUITPY.
Here is an extended guide on how to follow the above steps with images and pictures to help.
Running Your Program
Once you have CircuitPython installed, when you want to run your main.py file, copy it via your computer to the root directory of the CIRCUITPY drive.
Problems?
If issues are still arising while getting into CircuitPython mode, check out this troubleshooting guide.
Page last edited March 08, 2024
Text editor powered by tinymce.
Lesson 1: Blinky LED
Your Circuit Playground Express has a little red LED next to the USB port. It's labeled D13.
In EduBlocks, we are going to turn it on for half a second, then off for half a second, then back on etc. forever!
- First, go ahead and add the CPX Easy block extension if you haven't already. Directions on how to do that are back on the "Block Types" page in the "Navigating EduBlocks" section of this guide.
- From the
Basicblock category, drag inimport time. - From
CPX Easy, import the necessary library by dragging it over. - Next in
Basic, drag over awhile True:loop. This is the main loop of the program and will run forever until the CPX is disconnected from power or reset. - Now in
CPX Easy, drag in acpx.red_ledblock. Set the value in the white bubble toTrue. - Now drag in a
time.sleep()block and set the value to0.5which will be half a second. - Now drag in another
cpx.red_ledblock and set its value toFalse. - Lastly, copy and paste the
time.sleep()block from earlier by clicking on it, copying and pasting and dragging under the last block.
Once the blocks look good, click Download in the EduBlocks toolbar.
You may have to tell your browser you want to keep the file if asked.
A file named main.py should show up in downloads.
With the CPX plugged into the computer, drag main.py to the CIRCUITPY drive.
The drive should now have the file on it and the code should be running!
Is the LED blinking on and off on your CPX?
Issues? Troubleshooting help here.
Here are the code files if you want to upload directly to EduBlocks or the CPX.
import time from adafruit_circuitplayground.express import cpx while True: cpx.red_led = True time.sleep(0.5) cpx.red_led = False time.sleep(0.5)
Page last edited March 08, 2024
Text editor powered by tinymce.
Lesson 2: NeoPixels
The Circuit Playground Express has two buttons. Button A is on the left and button B is on the right. These buttons can be used as inputs, which means you can use them to tell your board to do something when you press them.
In this example, we'll use the buttons of the CPX to change the colors of the NeoPixels. Button A will turn them red, and B will turn them blue.
- First
import digitalio,board, andneopixel. - Next from the NeoPixel category, drag in an
np = neopixel.NeoPixel()block and set it equal toboard.NEOPIXEL, 10, brightness = 0.2. This sets up the NeoPixels and determines brightness (can be between0.0and1.0). - Now drag in an
np = fill()block and set it to(0,0,0). This sets the NeoPixels to the color to black or off.
- In order to control the buttons, we need to create some variables to assign to button Aa and button B.
- To do this, drag in a
varblock inBasic. - In the drop down menu click Rename variable
- Next name the variable as "button_a" and hit ok.
- Now you have a variable for button_a!
- Repeat for button_b
- To set up the buttons, drag in a
DigitalInOut()block from theDigitalblock category. Choose the variable from the drop down menu asbutton_a. Set the block equal toboard.BUTTON_A. - Now drag in a
.direction =block and set it equal toDirection.INPUTchoosingbutton_afrom the drop down menu. - Then drag in a
.pull =block and set it equal toPull.DOWN. - Repeat the above steps for
button_b.
- Drag in a
while: Trueloop. - Next drag in an
ifstatement with the condition set asbutton_a.value(this block can be found inDigital) - From
Neopixeldrag innp.fill()and set it to(255,0,0)which is Red. (R,G,B) - Then drag in an
np.show()block. - Now put an
elif(meaning else if) block setting the condition tobutton_b. - Repeat steps above but set the color to
(0,0,255)for blue.
Download the file and drag to the CPX.
Try it out!
When button A is pressed, NeoPixels should turn red.
When button B is pressed they should turn blue!
Here are the code files if you want to upload directly to EduBlocks or the CPX.
np = None
button_a = None
button_b = None
from digitalio import *
import board
import neopixel
np = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness = 0.2)
np.fill((0,0,0))
np.show()
button_a = DigitalInOut(board.BUTTON_A)
button_a.direction = Direction.INPUT
button_a.pull = Pull.DOWN
button_b = DigitalInOut(board.BUTTON_B)
button_b.direction = Direction.INPUT
button_b.pull = Pull.DOWN
while True:
if button_a.value:
np.fill((255,0,0))
np.show()
elif button_b.value:
np.fill((0,0,255))
np.show()
Page last edited March 08, 2024
Text editor powered by tinymce.
Lesson 3: Drum Machine!
This lesson is a spin off of John Park's Drum Machine.
We can use the capacitive touch pads on the Circuit Playground Express as triggers and small .wav files for our drum sounds!
Until now, we needed to code in CircuitPython to play sound files. Now with EduBlocks, all we need to do is drag a couple of blocks and we're good to go!
First, download the .zip file below, which contains all of the drum samples we'll be using. Save the file to your desktop or somewhere else easy to find, and then unzip it.
You can plug in your Circuit Playground Express, and then drag the drum files onto it. It shows up as the CIRCUITPY drive.
- Import
time, board, touchio, digitalio,andcpx. - Create a variable called
BPMand set it to.125. This is how fast the drum sounds will play in between each other. In John Park's project the BPM is set to 120 then later on divided by 960 to get the time in seconds that 120 beats per minute is. Unfortunately some math functions are not available in EduBlocks as it is in beta, so we must hard code the value of 120/960 in for our BPM variable. This happens to be .125. Feel free to mess around with this number to try out different drum speeds.
- Create 7 variables named
touch1, touch2, touch3, ... etc. - Drag in 7
=touchioTouchIn()blocks fromTouch. - Via the drop down menus change to
touch1, touch2, touch3, ... etc. - Each block should =
(board.A1), (board.A2), (board.A3), ... etc.
- Drag in a
while: Trueloop. - Next drag in an
ifstatement with the condition set astouch1.value(this block can be found inTouch). - From
CPX Easy, drag incpx.play_file()and type bd_tek.wav in for the value. - Drag in
time.sleep()block. - Drag variable
BPMin astime.sleep()value. - Repeat above steps until all 7 sound files are accounted for.
Download the main.py file and drag to the CPX.
Try it out!
Sounds should be triggering when each coin is touched or held!
If you want to use your own sound files, you can! Record, sample, remix, or simply download files from a sound file sight, such as freesample.org. Then, to make sure you have the files converted to the proper specifications, check out this guide here that'll show you how! Spoiler alert: you'll need to make a small, 22Khz (or lower), 16 bit PCM, mono .wav file!
Want to listen to your Drum Machine at body movin' volumes? No problem! Hook up an 1/8" or 1/4" phono output to the GND and A0 pads, then plug in to an amp or speaker! I tried it on a small speaker with an aux input and it sounded great!
Here are the code files if you want to upload directly to EduBlocks or the CPX.
touch1 = None
touch2 = None
touch3 = None
touch4 = None
touch5 = None
touch6 = None
touch7 = None
BPM = None
import time
import board
import touchio
from digitalio import *
from adafruit_circuitplayground.express import cpx
BPM = .125
touch1 = touchio.TouchIn(board.A1)
touch2 = touchio.TouchIn(board.A2)
touch3 = touchio.TouchIn(board.A3)
touch4 = touchio.TouchIn(board.A4)
touch5 = touchio.TouchIn(board.A5)
touch6 = touchio.TouchIn(board.A6)
touch7 = touchio.TouchIn(board.A7)
while True:
if touch1.value:
cpx.play_file("bd_tek.wav")
time.sleep(BPM)
elif touch2.value:
cpx.play_file("elec_hi_snare.wav")
time.sleep(BPM)
elif touch3.value:
cpx.play_file("elec_cymbal.wav")
time.sleep(BPM)
elif touch4.value:
cpx.play_file("elec_blip2.wav")
time.sleep(BPM)
elif touch5.value:
cpx.play_file("bd_tek.wav")
time.sleep(BPM)
elif touch6.value:
cpx.play_file("bass_hit_c.wav")
time.sleep(BPM)
elif touch7.value:
cpx.play_file("drum_cowbell.wav")
time.sleep(BPM)
Page last edited March 08, 2024
Text editor powered by tinymce.
Troubleshooting
If the code does not work when uploaded to the CPX, you may have to use a code editor and REPL to figure out what's going wrong.
This is a common process in programming known as "troubleshooting" or "debugging".
A code editor and REPL will help you determine which part of your code may be causing errors.
Follow directions on how to download a code editor and troubleshoot issues with the REPL here.
Page last edited March 08, 2024
Text editor powered by tinymce.