If you would like to try out CircuitPython, you will find it rather easy to use compared to other programming environments. If you like, you can read about everything in the Adafruit Welcome to CircuitPython Guide.
First, since we're going to use sound in the CircuitPython version of Hello World, hook a speaker up to the Crickit speaker terminals (to the right in the diagram below). If you have the AdaBox 008 mini-speaker, the white wires can go in either terminal, there is no red/black polarity. If you have a 4 or 8 ohm metal speaker, you'll get better sound. The Adafruit shop has a nice selection of speakers.
Writing Code With Mu
Adafruit recommends the Mu editor to type your code into and interact with your project. You can find out how to install Mu on mac, PC, and linux in this guide page on the Adafruit Learning System.
The advantage to using Mu is it has an editor and added features like direct save to Circuit Playground Express and access to the Express' REPL interactive command line and print output. Mu can even plot values for you.
The Program
The code below will use the adafruit_crickit
library which provides powerful, high level access to the Crickit features.
Just like MakeCode, the goal is to move the servo to wave our arm.
Also, CircuitPython does have the ability to play real WAV audio file sounds. Adafruit has created a simple WAV file for this project, Adabot saying "Hello World"!
The goal will be to wave the flag and say Hello World.
The code for this is shown below:
# SPDX-FileCopyrightText: 2018 Anne Barela for Adafruit Industries # # SPDX-License-Identifier: MIT import time import audioio import audiocore import board import neopixel from adafruit_crickit import crickit # Set audio out on speaker speaker = audioio.AudioOut(board.A0) # Start playing the file (in the background) def play_file(wavfile): audio_file = open(wavfile, "rb") wav = audiocore.WaveFile(audio_file) speaker.play(wav) while speaker.playing: pass # NeoPixels on the Circuit Playground Express Light Blue pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=0.3) # Fill them with our favorite color "#0099FF light blue" -> 0x0099FF # (see http://www.color-hex.com/ for more colors and find your fav!) pixels.fill(0x0099FF) while True: print("Hello world!") play_file("hello.wav") # play Hello World WAV file crickit.servo_1.angle = 75 # Set servo angle to 75 degrees time.sleep(1.0) # do nothing for a 1 second crickit.servo_1.angle = 135 # Set servo angle to 135 degrees time.sleep(1.0) # do nothing for a 1 second
Using Mu (or your text editor), copy this code and save it to your computer (wherever you save things for later, perhaps a Documents folder or a flash drive you brought to class), naming it code.py
Also download the Hello World WAV file named hello.wav by clicking the green button below, downloading and saving the file into the same place as your code. The code.py and hello.wav will go together in the root directory of your CIRCUITPY flash drive.
Installing Code On CircuitPlayground
At this point, make sure your Crickit and Circuit Playground Express are both plugged in per the Wiring It Up page. The Crickit should have battery or wall power and a green light next to the smiley face. The round Circuit Playground Express should have a USB cable connected from its micro-B silver connector to your computer.
Now, on your desktop or laptop computer, navigate to where you saved your code.py and hello.wav files.
If you have correctly connected your Circuit Playground Express to your computer, you should have a drive named CIRCUITPY listed among the disk drives. That is the Circuit Playground Express flash memory.
Copy (usually by dragging the files and dropping them) to the CIRCUITPY drive. First hello.wav, second code.py.
As soon as the code.py is detected on the CIRCUITPY drive, CircuitPython will run your code. If it doesn't, you can press the Circuit Playground Express Reset button once to get it going or open the Mu REPL by pressing the REPL button and typing the Ctrl-D key..
When running, you should hear Adabot say "Hello World" once and the arm should start waving! You've made your first robotics project!!
Text editor powered by tinymce.