Create a small Arduino circuit to shock dad on Father’s day
What you’ll learn:
How to incorporate Arduino circuits into a Father’s day message.
Key takeaways:
There are two parts to building an Arduino circuit: the wiring and circuit components, and the programming.
We have the ultimate Father’s day card for you to make that incorporates really bad Dad jokes (because who says he shouldn’t be the recipient of such ridiculous jokes), Arduino circuit building, and a little bit of programming.
Jumping into the world of Arduino can seem daunting. But I have great news for you! We can build a quick little circuit together and you can choose just how hard the circuit components will be, as well as how much programming you want to do!
Thanks to Tinkercad’s newest Arduino simulator addition you can even learn how to wire and code without any supplies.
If you are on a tight budget, or a tight time frame, building and showing Dad his cool card Father’s day card is the ticket that will save the day.
There are two versions of this Star Wars themed Father’s day card:
- A motion activated musical card: This card is a much simpler circuit, with much simpler programming code. However, the trade off is that this circuit does not use a small LCD screen to print out a Father’s day message to dad (or a card message in general)
- A motion activated musical card that displays text: As you can imagine, adding in an LCD display adds in a lot more circuit building. It also adds in a little programming code, which means it will take a bit longer. However, the motion activated card that prints a message is super cool and everyone will be ecstatic when it works!
Project Ingredients:Circuit starter kit (This one comes with a generic Arduino Uno board)PIR Motion detector (Some kits have it all, but they are pricier)
-OR-
Arduino Uno (you can reuse this board for many projects)PIR Motion detectorElectronics components kit
How to make a Star Wars themed card with Arduino for Father’s day
Before we start, a note to all the Moms out there:
Alright, Moms, you can do this! It will be a TON of fun and the pay off of quality time with your kids building an amazing Father’s day card is HUGE.
I know you might be fretting right now. Circuits? Programming? You might think to yourself that your kids would love it but you just can’t do it. But you can.
You can do this.
I have the wiring schematics laid out for you, the code is written and just needs to be put on the computer board. There is no soldering required, just plug and play. If you need help, I would love to help you. Just email me 🙂 Seriously, you can do this. Dad will think you are all rock stars after this (not that he doesn’t already).
OK, now that we have that out of the way, let’s learn how to make our Star Wars musical cards that are motion activated!
Star Wars themed musical card (the easier version)
1. Gather your supplies.
You will need your Arduino Uno, two LEDs, two resistors, a PIR detector (motion detector), a piezo buzzer, and jumper cables.
2. Build your circuit
You can do this before or after you program your Arduino. Once the circuit is built you can connect the Arduino to your computer for power (or to push the code) and run it! If you want to be able to have the Arduino detached from the computer you will need a 9V DC cap to power it.
You can see this circuit execute the code in a simulation on Tinkercad. Once you have started the simulation you will need to click on the PIR detector (it looks like a large white ball). Doing so will show the zone that the PIR can detect motion in with a blue dot. Click on the blue dot to signal motion in that zone to the simulation.
Power your breadboard using the +5V and Ground pins of your Arduino. We are using black wires for wires that go to ground, and red for those that are “hot” or have +5V through them. Note that we put these wires into the bottom two rows of the breadboard. These rows are connected horizontally, along with the top two rows of the breadboard. The other holes in the breadboard are actually connected vertically, as columns.
Connect the PIR motion detector to three pins on the left of your breadboard. connect the GND pin to ground in the breadboard, the +5 pin to the +5 power of the breadboard and the SGNL (signal) pin to pin 7 of your Arduino.
Connect your piezo buzzer. Connect one pin to the ground of the breadboard and one to pin 8 of your Arduino. If your piezo buzzer has a polarity there will be a plus sign by the pin that needs to be connected to pin 8 of the Arduino.
Connect the first LED. LEDs have a polarity, the long leg of the LED needs to go into a 1kOhm resistor and into pin 13 of the Arduino. The short leg is connected to the ground of your breadboard.
Connect the second LED, long leg to pin 12 of your Arduino through a 1kOhm resistor, and the short leg to the ground on your breadboard.
3. Program your Arduino
Plug your Arduino into your desktop computer and head over to Arduino’s code editor. If you have a Chromebook you can use CodeBender’s free 30-day trial to get the code pushed to your Arduino chip.
To program your Arduino Uno:
- In the Arduino IDE (the code editor) click on “New Sketch”
- Copy paste our code below into your “New Sketch”
- Click the checkmark in the upper left to verify the code
- Choose the Arduino Uno as your board and click push to board
The Code
Copy and paste the code below including the “//” in the beginning and the “}” in the end. Learn about the code and the coding process below.
//Father’s Day motion activated Star Wars song.//Supplies: Arduino Uno, 2 LEDs, 2 1kOhm resistors, //piezo buzzer, PIR motion detector, jumper wires//www.rosieresearch.com
//create musical notes//Buzzer code via Nick Sort at //https://gist.github.com/nicksort/4736535
const int c = 261;const int d = 294;const int e = 329;const int f = 349;const int g = 391;const int gS = 415;const int a = 440;const int aS = 455;const int b = 466;const int cH = 523;const int cSH = 554;const int dH = 587;const int dSH = 622;const int eH = 659;const int fH = 698;const int fSH = 740;const int gH = 784;const int gSH = 830;const int aH = 880; //define pin connectionsconst int buzzerPin = 8;const int ledPin1 = 12;const int ledPin2 = 13;const int pir=7;
//define countersint counter = 0;
// the setup routine runs once when you press reset:void setup() { // setup pin modes pinMode(ledPin2, OUTPUT); pinMode(ledPin1, OUTPUT); pinMode(buzzerPin, OUTPUT);pinMode(pir, INPUT_PULLUP);
}
// the loop routine runs over and over again forever:void loop() { //Check for motion if(digitalRead(pir)==HIGH) { //If motion is detected, turn on LED and play music digitalWrite(ledPin2, HIGH);
// Play the song firstSection();
//Play second section secondSection(); //Variant 1 beep(f, 250); beep(gS, 500); beep(f, 350); beep(a, 125); beep(cH, 500); beep(a, 375); beep(cH, 125); beep(eH, 650); delay(500); //Repeat second section secondSection(); //Variant 2 beep(f, 250); beep(gS, 500); beep(f, 375); beep(cH, 125); beep(a, 500); beep(f, 375); beep(cH, 125); beep(a, 650); delay(650); } else { digitalWrite(ledPin2, LOW); // turn the LED off by making the voltage LOW delay(200); } }
void beep(int note, int duration){
//Play tone on buzzerPin tone(buzzerPin, note, duration); //Play different LED depending on value of ‘counter’ if(counter % 2 == 0) { digitalWrite(ledPin1, HIGH); delay(duration); digitalWrite(ledPin1, LOW); }else { digitalWrite(ledPin2, HIGH); delay(duration); digitalWrite(ledPin2, LOW); } //Stop tone on buzzerPin noTone(buzzerPin); delay(50); //Increment counter counter++;} void firstSection(){ beep(a, 500); beep(a, 500); beep(a, 500); beep(f, 350); beep(cH, 150); beep(a, 500); beep(f, 350); beep(cH, 150); beep(a, 650); delay(500); beep(eH, 500); beep(eH, 500); beep(eH, 500); beep(fH, 350); beep(cH, 150); beep(gS, 500); beep(f, 350); beep(cH, 150); beep(a, 650); delay(500);}
void secondSection(){ beep(aH, 500); beep(a, 300); beep(a, 150); beep(aH, 500); beep(gSH, 325); beep(gH, 175); beep(fSH, 125); beep(fH, 125); beep(fSH, 250); delay(325); beep(aS, 250); beep(dSH, 500); beep(dH, 325); beep(cSH, 175); beep(cH, 125); beep(b, 125); beep(cH, 250); delay(350);}
Learning the code
Let’s take a look at some important items in the code to learn how Arduino programming works.
//
The “//” creates a comment line. Anything after the “//” the computer will ignore. Consider this your handwritten notes in the margins of a book – they are for you or anyone else that will be looking at your program. Why do we have so many “//” in the beginning? You need to add the “//” into the beginning of each line, so unless we wanted the text to go off the screen we had to hit enter. If we didn’t add in another “//” the computer would start paying attention!
const int or int
These define variables for the computer. In the beginning, we define a whole bunch of const int values. The int means we are dealing with integer numbers (whole numbers), the const means our integers are constant. That is, it will stay the same for the whole program. The initial list of const int values define the frequencies of musical notes.
Below is another list of const int values. Here we define the locations we will be putting the wires on the Arduino board. When we say const int buzzer Pin = 8; we are telling the computer that we will attach the buzzer to pin 8, and that it will always be at pin 8 (it’s a constant).
void name {}
You will see “void name {}” a few places in the program where name will be substituted for setup, loop, beep, etc.
These are all mini-programs that can run within your larger program.
void setup{} will only run once at the very beginning of turning on your Arduino (or when you hit the reset button). This program is used to define variables that will be used everywhere. For example, in our void setup{} we defined how each of the pins on the Arduino board will need to act. Will they wait to receive a signal or will the put out a signal?
Let’s take a look at some lines of code in the void setup{} piece:
pinMode(ledPin2, OUTPUT);
We could rewrite this aspinMode(13, OUTPUT); since ledPin2 is a constant integer that is set to 13.
Why would we want to write it as (ledPin2, OUTPUT) instead?There are two main reasons to define your pins with words instead of just numbers. First, it allows you to quickly and easily change the pin location in your Arduino with only changing one location of code in your program. If you wrote 13 everywhere you wanted to refer to ledPin2 you would have to search through your code of every instance. Here, if we want to connect the LED to pin 3 we can, we just change the line of code const int ledPin2 = 13; to const int ledPin2 = 3;
Second, it makes our code easier to understand. When we use ledPin2 in the code we know exactly what is going on. We don’t have to search around for what the ’13’ refers to each time.
void loop{} is the bulk of your program. This will essentially loop over and over again until you turn the circuit off.
In our void loop {} we first check for motion, and if a motion is detected we play music. You will notice that the music plays when it hits “firstsection()“. What is this “firstsection()“?
Scroll down a little in the code and you will find a new program defined in void first section(){}. This program runs through a series of beep commands that tells the program what notes to play and how long to play them. Beep is also its own void beep{} program that teaches the computer how to play the music.
How do you start writing such a program?
It might seem difficult to imagine writing a program, but programming always starts from a desire to make a computer do something and then fleshing out the specifics.
An example of this process can be seen here:
We want the computer to first check for motion if we find motion we want to play a song. We will define the song as a bunch of musical notes. Since some parts of the song will repeat we can write a sub-program to define sets of beeps as portions to our song. We want beeps to be a certain frequency and last a determined amount of time. After a beep we want the buzzer to turn off.
Knowing that this was our plan, can you look through the code and see how it is working?
3 thoughts on “Star Wars Motion Sensors: Father’s Day Fun”
Comments are closed.