Halloween Sentry-Bot with CRICKIT for CPX
2018-10-17 | By Adafruit Industries
License: See Original Project Circuit Playground
Courtesy of Adafruit
Guide by Dano Wall
Overview
This sentry robot will wait quietly for someone to pass by, then it springs to life! A lot of fun anytime or make it especially for Halloween.
Using MakeCode drag and drop blocks you will make a scary monster that can protect all your precious, precious candy.
In addition to basic programming concepts, this guide also demonstrates the construction of a simple mechanical movement, translating rotary motion into linear motion.
The Electronics
This project uses the following items from the Adafruit store:
- Circuit Playground Express
- Adafruit CRICKIT for Circuit Playground Express
- Micro Servo
- DC Gearbox Motor - "TT Motor" - 200RPM - 3 to 6VDC
- Eccentric Hub for TT Motor
- USB cable - A/MicroB - 3ft
- Alkaline AA batteries - 3 pack
- 3xAA holder with DC jack
Tools & Materials
For this project you will need these materials:
- Clear plastic cup
- Scrap cardboard
- Double sided tape
- Pipe cleaners
- Googly eyes
And these tools:
- Hobby knife
- Tweezers (or something pointy)
- Screwdriver
- Scissors
- Hot glue gun
Build the Circuit
The circuit for this bot requires connecting a servo motor and a DC motor to CRICKIT, the all-in-one motor controller that pairs with the best microcontroller anywhere: the Adafruit Circuit Playground Express.
A standard size plastic drinking cup fits almost perfectly over the CRICKIT. We will be using this cup as a housing for the electronics as well as a form upon which to build.
Here is a diagram showing the wiring for the motor, servo, and Crickit
Connect the DC motor
Bend pins on DC motor. This will allow the cable to fit comfortably inside the rim of the cup.
Plug into motor terminals.
Use a screwdriver to tighten down connections.
Connect the Servo Motor
Plug servo into servo port 1 on CRICKIT.
Make sure ground wire (brown) is facing inwards.
Test the Code
It's a good idea to test out your code before moving on to the final build. This page will lead you through a dry-run.
We will be using Microsoft MakeCode for Adafruit, a web-based code editor. It provides a block editor, similar to Scratch or Code.org, and also a JavaScript editor for more advanced users.
If you haven't used MakeCode before, this guide is a good place to start.
Getting into Bootloader Mode
To make your board work with MakeCode we need to put it into bootloader mode. All that's required to do this is to connect the board to your computer with a micro USB cable and click the small reset button in the center of the board.
Now we're ready for MakeCode!
Click this link or the button below to enter the portal to interact with the code for this project.
Upload the MakeCode file for this project
How to Upload the Code
To upload code, connect you Circuit Playground Express to your computer using the micro USB cable, click the Download button to download the .uf2 file to your computer, and drag 'n drop it onto the CPLAYBOOT drive.
The drive will automatically eject itself. (Your computer may give you a "failed to eject drive correctly" error, you can ignore this.) The code is now on your Circuit Playground Express and ready to run!
Testing the Code
This sketch in MakeCode uses the light sensor on Circuit Playground Express to control the motors.
In the on start block, we can see three startup commands. The threshold for what is considered a "loud sound" is lowered to 10, the volume level is set to almost full volume, and the servo motor is initialized, so it will always start in the same position.
In the three forever blocks, there are if / else statements, which gives the following instructions:
- If sound level goes above the specified value, move the servo back and forth 4 times.
- If light level goes below the specified value (in this case, 50), run the DC motor.
- If acceleration strength goes above 1022, turn all neopixels white.
Microphone
Once powered on, you should be able to test your circuit by snapping or clapping. In response, the servo arm should twitch back and forth.
Circuit Playground Express has a small microphone on it, allowing it to detect sounds.
Light Sensor
The light sensor is used to detect when the light level falls below a specified value. When you cast a shadow over the board you should see the DC motor start spinning in response.
The component that makes this possible is the tiny analog light sensor in the top left part of the board. This is used to detect ambient light with similar spectral response to the human eye.
With MakeCode we are able to use the readings coming from this light sensor to control the motors, but it's also possible, with some clever code, to use this as a color sensor or even a pulse sensor!
Accelerometer
Finally, your board should now be sensitive to being moved, even small vibrations. You can test this by tapping your board or thumping the table it's on, and the NeoPixels LEDs should flash white in response.
The small but very accurate accelerometer on the board makes it possible to detect slight movement. Acceleration is read by CPX as a value between 0 - 1023.
Making Changes
If you'd like to play with this code, click Edit and a new window will open in which you can create your own version.
When you want to try out your new code, connect Circuit Playground Express to your computer with a micro USB cable, download your adapted code, and drag the new .uf2 file onto your CPLAYBOOT drive.
Troubleshooting
Problem: My motors aren't working!
Solution: Make sure that the small slide switch on the CRICKIT is set to "ON".
Problem: My motors still aren't moving!
Solution: Make sure you've updated your Circuit Playground Express with the latest special 'seesaw' version of the CPX firmware. This guide will show you how.
Problem: My Circuit Playground Express doesn't show up as CPLAYBOOT!
Solution: Your Circuit Playground Express board comes ready to work with CircuitPython, and will show up as a flash drive named CIRCUITPY the first time it's connected to your computer. To switch over to work with MakeCode, connect the board to your computer with a micro USB cable and click the small reset button in the center of the board.
When Circuit Playground Express is in Bootloader mode, all the LEDs will flash red briefly, then turn green. Your computer should now show removable drive called CPLAYBOOT. Now you can copy the MakeCode file to the CPLAYBOOT flash drive.
Constructing the Monster
Make the Leg Gantry
Cut out a T-shaped piece of cardboard like the one pictured.
Crease cardboard in two places so it can bend around the cup.
Cut two slits down the side.
Fold into shape and use hot glue to attach a cardboard brace to hold in place.
Using hot glue guns can cause burns if the gun tip or glue touches exposed skin. Be very careful.
Mount the DC Motor
Position DC motor inside cup and mark placement of holes with a marker.
Poke holes in the cup using tweezers (or something else pointy).
Cut out a thin rectangle of cardboard and poke two holes, matching the spacing of the two holes in the cup.
Attach DC motor to inside and bar to outside of cup using two screws or a twist-tie.
Stick the eccentric hub on the motor.
Test Fit
Test the fit of the leg gantry on the cup, it should be able to slide easily up and down.
If the gantry feels sticky, now is the time to reposition the brace or widen the slits so that it can move up and down freely.
Add Spider Legs
Use hot glue to stick pipe cleaners to the cardboard.
A spider has eight legs!
Cut the Cable Holes
Use a hobby knife to cut a hole in the top of the cup for the servo cable.
Add a cutout for the DC jack at the rim of the cup.
This cutout will allow you to easily plug/unplug your power cable.
Add a Face
Two jagged-cut pieces of cardstock glued to a popsicle stick make a scary looking face.
Use tape to affix servo motor arm to the base of a popsicle stick.
Hot glue or glue sticks work well to attach googly eyes.
A short piece from a straw also works to attach eyes to the servo motor arm.
Boo!
Sentry on Duty
Now it's time to have some fun.
Place your finished bot in a place with ample bright light. The bright light will keep its light sensor from being triggered and setting it off.
Your sentry should be sensitive to different indications of people passing by.
Sound Sensitivity
Vibration Sensitivity
Light Sensitivity
Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.
Visit TechForum