HackerBoxes 0011: HAX0R TREAT

by HackerBoxes in Circuits > Microcontrollers

25009 Views, 26 Favorites, 0 Comments

HackerBoxes 0011: HAX0R TREAT

WholeBoxSk00l 0011.png

[cue theme music]

HAX0R TREAT: This month, HackerBox Hackers are exploring motion detection and wireless signaling between multiple microcontroller nodes to create pranks and tricks. This Instructable contains information for working with HackerBoxes #0011. If you would like to receive a box like this right to your mailbox each month, now is the time to subscribe at HackerBoxes.com and join the revolution!

Topics and Learning Objectives for this HackerBox:

  • Setting up multiple battery-powered microcontroller nodes
  • Loading and modifying programs in the Arduino IDE
  • Using passive infrared motion detectors
  • Transmitting and receiving wireless messages
  • Implementing wireless communication network topologies
  • Using Servo Motors and LEDs for simple animatronics
  • Accessing USB storage devices from your projects

HackerBoxes is the monthly subscription box service for DIY electronics and computer technology. We are hobbyists, makers, and experimenters. Hack the Planet!

HackerBoxes #0011: Box Contents

WholeBoxMarketing 0011.png
Front 6x4.png
  • HackerBoxes #0011 Collectable Reference Card
  • Three Arduino Pro Mini Modules
  • Three 9V Battery Clip Leads
  • Two 315Mhz Radio Frequency Data Transmitters
  • Two 315Mhz Radio Frequency Data Receivers
  • USB Serial Module (CH340)
  • Passive Infrared Motion Sensor (HC-SR501)
  • Two MAX7219 8x8 LED Matrix Modules
  • SG90 Micro Servo
  • Two-Channel Relay Module
  • 40 DuPont Female-Female Jumper Wires
  • USB Drive Read/Write Module (CH376S)
  • Headless Skeleton USB Flash Drive 4GB
  • Speaker (8ohm, 5cm)
  • Audio Jack 3.5mm
  • Two Passive Piezo Buzzers
  • Ten Mini-PCB Addressable RGB LEDs (WS2812B)
  • Exclusive BitHead Lapel Pin
  • Exclusive HAX0R TREAT Decal

Some other things that will be helpful:

  • Basic soldering and prototyping tools
  • Three 9V batteries
  • Electrical tape or heat-shrink tubing
  • Hot glue gun or other adhesive
  • Arduino Integrated Development Environment (IDE) [download]

Most importantly, you will need a sense of adventure, DIY spirit, and hacker curiosity. Hardcore hobbyist electronics isn't always easy, but when you persist and enjoy the adventure, a great deal of satisfaction may be derived from persevering and getting your projects working. Just take each step slowly, mind the details, and don't hesitate to ask for help.

Tricks, Pranks, and Ad Hoc Wireless Signaling

Network Orange.jpg
Screen Shot 2016-09-30 at 12.34.37 AM.png

Halloween, or related Fall harvest festivals, are celebrated in different ways among various cultures around the world. Wikipedia has an amazingly detailed entry on the subject, which lists among typical Halloween activities: playing pranks. Even if you do not celebrate Halloween, these projects should provide plenty of fodder for pranking or surprising your friends and family for a birthday, other celebration, or just for fun!

The technology behind this month's pranking includes a simple platform for a peer-to-peer or ad hoc wireless signaling network. We will be working with three identical Arduino Pro Mini microcontroller "nodes" as well as two RF transmitters and two matching RF receivers for setting up wireless signaling between the processor nodes.

Preparing the Arduino Pro Mini

Pro Mini plus Pins.jpg
Pro Mini Schem.png
Battery Leads.jpg

The Arduino Pro Mini is a microcontroller board based on the ATmega328. We are using the 5V/16MHz variety. The Pro Mini was originally designed by SparkFun Electronics and has become quite popular thanks to its tiny footprint. While less than one square inch in area, the Pro Mini features 14 digital input/output pins, six analog inputs, a crystal oscillator, a voltage regulator, and a reset button.

Because the Arduino Pro Mini was intended for integration into other systems, it arrives without pre-mounted headers, allowing the use of various types of connectors or direct soldering of wires. For our purposes here, we will be interfacing the Pro Mini using female DuPont jumper wires, so we will start by vary carefully soldering in the provided header pins.

Warning: If you are new to soldering, really take your time with this. Go very, very slowly and take great care. There are a lot of guides and videos online about soldering. Here is one example. You might want to find an old junk PCB for some practice (un)soldering. If you feel that you need additional assistance, consider joining a local makers group or hacker space in your area. Also, amateur radio clubs are always excellent sources of electronics experience.

Battery Power: The Pro Mini has an on-board regulator allowing it to be run from a 5-12VDC unregulated power source. An easy example of which is a 9V battery. To make a header-pin friendly 9V battery, female connector ends from a DuPont jumper can be soldered onto the leads of a 9V battery clip. The connections can be covered with tape or heat-shrink tubing.

The red battery lead should be connected to the RAW pin of the Pro Mini. The RAW pin is the input to the onboard regulator. The black lead should be connected to any of the GND pins. There is some code preloaded onto the Pro Mini. This code will start running a few seconds after power is applied. The code will slowly blink an LED on the Pro Mini.

CH340 USB Serial Module

CH340.png
CH340 schem.png

A USB/Serial module allows a computer USB port to talk to the serial interface on a hardware device such as an Arduino microcontroller board. There are many different types of USB/Serial modules using various USB/Serial bridge chips. The CH340 chip from WCH is increasingly common and is relatively easy to use.

Your computer's operating system requires a Device Driver to communicate with the CH340 USB/Serial chip. The specific device driver that is needed depends upon both the OS version and also the type of USB/Serial chip. For the CH340 USB/Serial chips, there are drivers available for many operating systems (UNIX, Mac OS X, or Windows). WCH (the maker of the CH340) supplies those drivers here. The Google translate version may be helpful.

The CH340 USB/Serial module may be used in either 5V mode or a 3.3V mode. The jumper block on the module's header connector is used to select which of these modes is being used. Since our microcontroller modules are 5V and the jumper usually defaults to the 3.3V configuration, we will need to switch the jumper to 5V mode by shorting between the 5V pin and the VCC pin.

When the CH340 USB/Serial module is plugged into a PC, the power LED on the module should light up and the operating system should notice the connection of the USB/Serial chip. Within the Arduino IDE, a PORT item should appear indicating the USB/Serial module. If it does not, be sure that the appropriate driver is installed.

Once the CH340 PORT is selected within the IDE, the CH340 USB/Serial Module may be further tested by connecting the module's TX (transmit) and RX (receive) pins together using a female/female DuPont wire. Next, open the serial monitor in the Arduino IDE. Anything typed into the serial monitor will go out through the CH340 module's TX pin and right back into the RX pin to be displayed on the serial monitor. This is called an echo or loop-back test. Notice the operation of the three LEDs (power, tx, and rx) on the CH340 module.

Loading Code Onto the Arduino Pro Mini

ProMini with CH340.jpg

Once we've had the Arduino Pro Mini powered up and running and we have also seen the CH340 module working, we can put them together. This will let us program the Pro Mini from the Arduino IDE software running on your computer.

We can wire up the Arduino Pro Mini, CH340 USB/Serial Module, and 9V Battery Clip as shown. Note that the grounds of the Pro Mini, the CH340 Module, and the 9V Battery are all tied together. This is important so that all of the signals and supplies are referenced to the same 0V ground. For now, do not worry about the unused red 5V line in the circuit diagram. That is related to the power source footnote below.

Now that everything is wired up, we can plug the CH340 into a USB port on the computer, launch the Arduino IDE software, select the appropriate tools>port in the IDE for our USB port, and select "Arduino Pro or Pro Mini" in the IDE for tools>board.

Finally, load up a piece of example code:

File->Examples->Basics->Blink

This is actually the code that was preloaded onto the Pro Mini and should be running right now to blink the LED. Accordingly, if we load this code as it stands nothing will happen. Instead, let's modify the code a little bit. Looking closely, you can see that the program turns the LED on, waits 1000 milliseconds (one second), turns the LED off, waits another second, and then does it all again - forever.

Modify the code: change both of the "delay(1000)" statements to "delay(100)"

This mod will cause the LED to blink ten times faster, right?

Let's load the modified code into the Pro Mini and check:

  1. Press and Hold the reset button on the Pro Mini
  2. In the IDE, Click the UPLOAD button (the arrow icon) just above your modified code
  3. Watch below the code for the status info: "compiling" and then "uploading"
  4. As soon as it switches to "uploading" release the reset button on the Pro Mini
  5. The IDE should now say "Uploading Complete" and your LED should be blinking faster

If so, congratulations! You have just hacked your first code. If not, try again, sometimes the timing of the reset release is a little tricky. The transmit LED on the CH340 module should light up steady for a second or two while the upload occurs. When you don't see this (followed by an error on the IDE), the reset release is generally to blame. Just try it again until you get used to the proper timing.

Once your fast-flash version of blink is loaded and running, why not see if you can you change the code again to cause the LED to blink fast twice and then wait a couple of seconds before repeating? Give it a try! How about some other patterns? Once you succeed at visualizing a desired outcome, coding it, and observing it to work as planned, you have taken an enormous step in hardware hacking. Wear your new HackerBoxes BitHead pin with pride!

A note about the power source: While we want our project to be battery powered, it is actually possible to power the Pro Mini from the CH340 USB module (while the CH340 is plugged into a computer or USB power supply). Since the 5V pin on the CH340 Module is blocked by the configuration jumper on the header, you will have to solder a wire to the base of the 5V pin close to where it meets the circuit board (or even on the rear). This is illustrated by the unused red 5V line in the circuit diagram. When using this 5V CH340 source, the battery should be completely removed and the 5V line from the CH340 modules should connect to a VCC pin on the Pro Mini (not the RAW pin).

Motion Activated Frightful Sci-Fi Sounds

PIR and Buzzer.png
ProMini PIR Buzzer.jpg

The HC-SR501 (tutorial) is a motion detector based on a passive infrared (PIR) sensor. PIR sensors measure infrared (IR) radiation from objects in their field of view. All objects (at normal temperatures) emit heat energy in the form of radiation. This radiation is not visible to the human eye because it is mostly at infrared wavelengths. However, it can be detected by electronic devices such as PIR sensors.

Wire up the components as shown and load the example code to feast your ears on a simple demonstration of motion activated creepy noises. The activating motion also causes the example code to toggle the LED on the Pro Mini and output status to the serial monitor.

Downloads

Adding a Radio Frequency Transmitter

PIR and Buzzer and TX.png
RF TX schem.png

We can extend our motion-activated buzzer project to also transmit the motion detection event to another Arduino Pro Mini using a radio frequency (RF) wireless message. We have two 315MHz RF transmitters and two RF receivers. The operating frequency 315MHz is used in the United States for a lot of simple unlicensed RF devices, such as garage door openers. In some other countries, different frequencies may be allowed for these types of devices. If you are not a licensed radio operator, make sure you are complying with the rules in your jurisdiction.

We can start by wiring one of the RF transmitters (the square one) into the project as shown in the wiring diagram. Note that you will run short on VCC (5V) and GND (0V) pins on the Arduino Pro Mini for wiring up all these peripherals at once. This can be mitigated by making a DuPont "splitter". Cut two female-female DuPont jumpers near their middles, strip the four resulting wire ends, solder them together, and then seal with tape or shrink tubing. You can now connect one of the four female connectors to a VCC (or GND) pin of the Pro Mini and then use the other three connectors for the additional modules.

The attached example code simply extends the motion-activated buzzer project to also transmit a message "A" over the 315MHz RF link using the VirtualWire Library (datasheet). VirtualWire is an Arduino library that provides features to send short messages over wireless using ASK (amplitude shift keying) modulation. The messages are sent without addressing, retransmit, or acknowledgment - a bit like UDP. VirtuaWire can be used with a number of simple radio transmitters and receivers. All that is required is one pin to transmit data and then one pin to receive data.

The VirualWire Library can be downloaded here. Start installing the library by extracting the entire folder from the downloaded zip file. Place the entire folder into your Arduino Libraries folder. Within the Arduino IDE, going into "manage libraries" will cause the IDE to scan the Libraries folder and grab any new libraries that have been added. This library will need to be added before the example code will compile.

Set Up an RF Receiver on Another Arduino Pro Mini

ProMini and RX.png
RF RX schem.jpg

Wire up one of the 315MHz RF receivers (the rectangular one) up to another Pro Mini as shown in the wiring diagram. This will serve as the receive node for the message transmitted by our motion-detection circuit.

The attached example code also uses the VirtualWire Library to receive wireless messages from the 315MHz ASK link. If the message matches the one sent by the other Pro Mini ("A"), then the example code will turn on the LED to show that the message was received. In other words, when the motion detector is triggered on the other board, this one will light up its LED - even from a distance of ten meters or more.

The RF receivers can be used to remotely trigger servo motors, lights, noises, and various other prank-worthy animatronics as shown in the remainder of this instructable.

Antennas: The working distance between the transmitter and receiver is quite usable by default, but it can be extended by soldering some wire into the antenna ports on each of the 315MHz modules. How long should the wire be for a quarter-wavelength antenna? Different lengths, structures, and orientations of antennas may be fun to experiment with or encourage a young hacker to test as a science fair project. An amateur radio operator will be happy to talk to you at length (wavelength?) on the topic of antennas. Also, here are a couple of fun antenna textbooks:

Joseph Carr "Practical Antenna Handbook" [pdf]
Thomas Milligan "Modern Antenna Design" [pdf]

Downloads

Wireless Network Topologies

topologies.jpg

Messages may be sent over wireless links or broadcast channels in order to realize various wireless network configurations, generally referred to as topologies.

In a simple point-to-point example, as we have already demonstrated, messages are sent from one node to another node.

In an unaddressed broadcast example, messages are sent from one node and then received at multiple other nodes. Keep in mind that one node (e.g. having the motion sensor) can broadcast a message that is received by a second node causing the second node to perform some action. At the same time, the same message, is also received by a third node triggering the third node to complete some other action. Either receiving node can use a delay() after message detection in order to allow the other receiving node to complete its action first. Such timing can cause the wireless relaying of messages to appear much more complex than it actually is.

In an addressed broadcast example, messages from a first node include a specific indicator as to which other node(s) should react to the message.

In a multiple point-to-point example, message contents can indicate which node the message originated at and then possibly also which node(s) should react to the message.

Servo Motors

servo SG90.jpg
servo schem.jpg
servo phase.png

This type of servo motor is a rotary actuator. A servo is generally controlled by a PWM (pulse width modulation) signal. These signals are generally a series of repeating pulses of variable width. These servos are connected through a standard three-wire connection: two wires for a DC power supply and one for control, carrying the pulses.

To get started with your servo motor, wire it up as shown. Pin9 is the PWM output from the Arduino that supplies the control pulses. One of the small arms may be temporarily pressed onto the servo (without a screw) just to see how it works. Once the servo is installed into a final application, the correct arm can be screwed on at the desired angle.

The example servo microcontroller combo can be combined with one of the RF receivers to make the servo radio controlled.

Downloads

RGB LED Pixel Mini PCBs - WS2812B

pixel_chain.png

Ready for some cool colored lights? The WS2812B (datasheet) is an individually-addressable red-green-blue (RGB) LED with an integrated controller and driver. We have ten of them on little round PCBs for easy soldering as needed. They are manufactured in a large sheet but can be individually separated from their neighbors with a gentle snap. As shown in the wiring diagram, these pixels can be easily chained together such that they only require a single I/O pin to control a large number of LEDs.

Click onto the github for Pololu's "Addressable RGB LED Strip" Arduino Library. Hit the "clone or download" button and download the library zip file. Extract the folder into your Library folder and tickle the IDE to go find the new library.

From the examples folder, load the "LedStripColorTester" project and change these two lines:
PololuLedStrip<6> ledStrip; // the RGB LEDs are on pin 6
#define LED_COUNT 2 // only two RGB LEDs connected

Download the code to the Pro Mini, launch the serial monitor, set the data rate to 115,200 baud and type R, G, B "triples" into the serial monitor to set the LED colors. For example:

  • "10, 0, 0" to get dim red
  • "200, 0, 0" to get bright red
  • "100, 0, 100" for purple
  • "0, 100, 0" for green
  • and so on

Fright Skull With Servo Jaw and Firey LED Eyes

skull wiring.png
SKULL led eyes.jpg
SKULL servo.jpg
SKULL operating.jpg

The FRIGHT SKULL is an example of an animatron that can be triggered by a motion sensor or over a wireless RF link. This example skull was modified from a plastic toy to use a servo motor, some RGB LEDs, and a buzzer to put on quite a show.

Downloads

LED Matrix Scary Eyes

Jack o Eyes.jpg

To make some scary eyes for another type of animatron, all you need is a couple MAX7219 8x8 LED Matrix Modules. The modules are wired together in a chain. The first MAX7219 module is wired to the Arduino Pro Mini and the second module is wired to the first module.

According to the sample code, the first MAX7219 should be wired like so:

  • Pin 10 of Pro Mini to the CS pin
  • Pin 11 of Pro Mini to the CLK pin
  • Pin 12 of Pro Mini to the DATA pin
  • VCC to VCC (5V)
  • GND to GND (0V)

Then just grab the .ino file from this LED Matrix Evil Eyes project.

You will also need the LedControl Library. The Library Manager in the Arduino IDE should be able to find that library and install it from within the IDE. All of the details about the library can be found here.

The RF receiver or IR motion sensor can be added to turn on the scary eyes or to trigger the motion of the pupils.

Generating Simple Aglorithmic Sounds

ProMini and Speaker.png

This very simple speaker circuit and tiny program can generate some really interesting sounds beyond the piezo buzzer clicks we've been using. Note that this program directly takes over some of the low-level timers and registers of the microcontroller, so it may not play nicely with normal Arduino function also expecting to use those chip features.

This page has some great background information about generating algorithmic symphonies from one line of code.

You can connect a 3.5mm audio jack in place of the speaker in order to plug headphones or amplified speakers. Connect the audio output (pin 6) from the Arduino into both channels on the audio jack to have the sound driven to both speakers. This is considered dual-channel-mono, not proper stereo, since both channels contain the same signal.

USB Drive Read/Write Module CH376S

CH376 Wiring Diag.jpg
Skeleton Flash 4GB.jpg

Have you ever wanted to read or write files to a USB drive from your microcontroller project? The CH376S module (datasheet) will let us do just that.

For a quick demo of how to read a file from a USB drive. Create a file on the drive called TEST.TXT and put some text information in it. Wire up the CH376S module as shown, load the sample code, set the Arduino IDE serial monitor to 9600 board. Enter the command "4" in the serial monitor to open a file on the USB flash and print the file contents out to the serial monitor.

Take a look here for the more detailed demo showing more features of the CH376S.

Note about wiring the CH376S: The reference card shows the pinouts for one version of the module, but many of the newer models have a different pinout and should be wired as shown in the wiring diagram here. Also, note that an optional parallel interface maybe used to get higher data transfer rates.

Downloads

Relay Module (Dual Channel)

relay symbol.jpg
Relay Module.png
holiday lights.jpg

A relay is basically a switch controlled by an electromagnet. As one example application, you can use a relay to switch a lamp on and off using the low voltage output from an Arduino as the control (example). Obviously this applies to other electrical appliances just as well as a lamp.

NOTICE: Using a relay to switch line voltages is obviously a very risky undertaking. In some jurisdictions, it may even be illegal to do so without the assistance of a licensed electrician. If you have any doubts about how to do this safely, how to put it into a proper housing, how to take all necessary precautions, if you are even allowed to do it, and so forth, then we strongly advise you to simply avoid the matter. Any experimentation with line voltages is against our advice and, like all maker activities, is at your own risk.

Since relays can be used to switch "low voltage" signals as well, there is plenty of fun that you can have experimenting with relays without taking the undue risk of messing around with line voltages. Looking at the example diagram for a set of commercial Halloween LED lights, we can see that there is a line voltage side and low voltage side. A relay could be used on either side, but obviously dealing with low voltage wires is a lot safer. There is no reason to cut the line voltage leads or to open the controller box. Again, if you are unsure about this sort of experiment, then we recommend steering clear.

Hack the Planet

Black CIRC ONLY.png

We hope you are hatching some great plans for pranks and tricks using motion sensing and wireless signaling. If you have enjoyed this Instrucable and would like to have a box of electronics projects like this delivered right to your mailbox each month, please join us by SUBSCRIBING HERE.

Please share your success in the comments below and/or on the HackerBoxes Facebook page. Certainly let us know if you have any questions or need some help with anything. Thank you for being part of the HackerBoxes adventure. Please keep your suggestions and feedback coming. HackerBoxes are YOUR boxes. Let's make something great!