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.
This project uses the following items from the Adafruit store:
This little servo can rotate approximately 180 degrees (90 in each direction). Works just like standard servos you're used to but smaller.
Eccentric Hub for TT Motor
This eccentric motor hub has 2 hexagonal holes and 2 circular holes which allow you to quickly and easily mount custom wheels, cardboard & wood, or other mechanisms to your robot project.
Alkaline AA batteries - 3 pack
These batteries are good quality at a good price, and work fantastic with any of the kits or projects that use AAs.
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)
- Hot glue gun
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 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.
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.
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.
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.
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:
sound level goes above the specified value, move the servo back and forth 4 times.
light level goes below the specified value (in this case,
50), run the DC motor.
acceleration strength goes above
1022, turn all neopixels white.
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.
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!
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.
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 CPLAYBOOTdrive.
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.
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 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 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 motor.
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.
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 base of popsicle stick.
Hot glue or glue stick work well to attach googly eyes.
A short piece from a straw also works to attach eyes to the servo motor arm.
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.