ARDUINO: ANIMATED STUFFED ANIMAL

87

1

Posted in TechnologyArduino

Introduction: ARDUINO: ANIMATED STUFFED ANIMAL

Students will animate their own toy to stimulate their creativity and ability to think through a process as a series of smaller detailed steps. The Arduino board will be introduced and students will explore the parts that are included in their kits. The first lesson ends with a “Hello World” activity where the students connect their Arduino board to the computer and learn how to upload an example program that causes an LED light to blink on and off.

Teams build their system, program and test it, reflect on the challenge, and present their experiences to their class. Students plan and build a toy that ideally has 2 inputs and 2 outputs from what they learned in steps 1-7.

Step 1: COMPUTER SCIENCE INTRO

SET UP:

1. Double click the Arduino program to open it.

2. Plug in your USB cord to the Arduino board and the USB end into the computer. Note in the picture how far the cord plugs into the Arduino board, it will be sticking out about 1/4 inch.

3. Click on tools: make sure you connect the right board. Go down to “Board”. Take a look at your board. What word does it say on it? It says Uno, so click on "Arduino Uno”.

4. Click on tools, hover over or put your mouse over “Port”.

a. If you are using a PC computer: click on the port ending in com 2 or com 7

b. If you are using a Mac computer: click on the port ending in cu.USBmodem (Arduino Uno)

5. Now hands off your mouse, attention up here. After I go through the rest of the steps, you will have a turn to do them.

NOTE: A lot of Arduino boards come with the blink option already built in, so no code is needed. May need to check and reprogram the boards to not blink as soon as you plug it in. Students will enjoy the lesson more if you have them plug in an LED bulb rather than using only the orange LED already affixed to the Arduino. TO DO: Cut open package (if not already open), upload the default (blank) Arduino sketch to wipe the board clean, and place boards in anti-static bags.

Have students write down the word “code, upload, Arduino board” and their definition in their notepad.

Use laptops or computers. Use red and blue cups so kids can put somewhere visible if they have a question (blue means everything is O.K. and red means there is a technical difficulty). Need to figure out where students can store a folder electronically: USBs, electronic folder.

No one touch anything. First you will watch me go through each step, and then you will follow the steps.

BLINK STEPS:

1. You should have your cord plugged into your board and computer.

2. With the Arduino program open, go to the file menu. Hover over the examples. We are just starting out so hover over “Basics”. Click on “Blink”. It will open a new sketch.

3. Hover over the second icon, the word “upload” will pop up. When you click, it will upload the program to your board. Be patient at this point, it may take up to 10 seconds to upload or to “teach” the board to blink the light.

4. The light on your board should now be blinking. Show your shoulder partner when you get to this point.

BLINK STEPS PART 2:

1. Remember one of our rules. Do not make any changes when your board is still plugged in. Go ahead and unplug your board from the computer. Once your board is unplugged, we will come around to pass out your LED.

2. You need to plug your LED in to the right place. Look at your board. Your LED has a short leg and a long leg.

3. Look at your board. Find the pin where it says 13 and the pin that says GND, which means ground. Now go ahead and count starting at the bottom with the zero. Start counting with zero. You want to put the short leg wire into the GND and the longer leg into the 13. We will come around and check that your bulb is correct before you plug it in your computer again. Similar to batteries, the LED has different wire ends.

4. Once your LED connection has been approved, you may now plug your Arduino board back into your computer.

5. Look at the code on the computer. Do you see anything that is related to your board and what we connected? Go ahead and read the code to your partner. The gray part of the code is a translation for us. The other colored text is for the computer.

6. Wait for further instructions.

Step 2: Blink

EXPLORE

Pass out LED Breadboard Diagram

To connect your LED, you will have to use a resistor. A resistor is something that reduces the flow of electricity. In your kits, there are two resistors, a red and a black one. For this lesson, we will be using the black resistor.

Go ahead and follow the steps that are up on the doc cam with your kits to recreate the diagram. Once you think you have the right setup, call one of us over to check it so you can upload the code.

1. Find the ground (GND) next to the 5V on your Arduino board.

2. Take a black or white wire from your kit and connect it into this hole (GND next to PIN 13 or next to 5V).

3. Orient your breadboard so the negative (blue line) is at the top of the breadboard.

4. Now connect the other end of the black wire to a hole along the negative row of the breadboard.

5. Connect the red wire to the breadboard as shown in the diagram.

6. Now connect the other end of the red wire to pin 13 and follow this diagram.

7. Notice, in the diagram there is one wire on the LED that is slightly bent. This is used to represent the longer pin on the LED, please no not bend the legs of the LED.

Negative

(GND) Positive (Pin 13)

8. Now the computer’s power is directly connected to the breadboard.

9. The breadboard is now connected to the Arduino board, so when we plug it into the computer, our breadboard will also have electricity.

10. Now take out an LED light from your kit. Just like a battery and our breadboard, an LED has a positive and a negative end.

➢ To remember which end is positive and negative: What do you do when you see a negative or a minus sign? Subtract. Right, so the negative wire on an LED is the shorter one and the positive wire is the longer one.

11. Pick a place on the groundside of your breadboard near the other wires where you want the LED horizontally so it is on the same letter row but different columns. Remember the inside holes connect vertically.

(Have students pull out the 220 ohm resistor and explain how to fold the legs)

The black resistors are 220 ohms, the red resistors are 10k ohms, and blue resistors are 1k ohms.

12. Put the resistor next to the longer end of the LED so they are connected.

Why would we want the positive or power end of the breadboard connected to the resistor then connected to the positive end of the LED?

➢ Because the positive goes with positive.

Right. We want the positive end of the breadboard to be flowing through the resistor to the positive end of the LED. Then it will flow through the negative end of the LED to the ground.

DEFINITION (RESISTOR): The resistor reduces the amount of current flowing through your circuit because if there is too much current, it could burn out your LED.

13. Then connect the other resistor end to the power. Remember the power is connected horizontally across the breadboard.

Note: When having students place the LED in the breadboard based on the diagram, it is important to point out to the students NOT to bend the LED so it is “flat” like the diagram.

Now we have to go back and make sure the computer knows what it is powering.

Let’s look at the 0-13 numbers. These are called INPUTS. (Make the students remember what they did the previous week lesson “Blink”, what was the INPUT? What was the OUTPUT?)

What number did we plug in our LED last time or in your review earlier? Which spot does the computer know to power?

➢ Port 13

14. Connect a red wire into the 13-hole on the Arduino board.

15. Put in the LED with short end connected where the blue wire is horizontally connected.

16. Go ahead and plug in your Arduino board using the USB plug in your kit.

Remember, you may not make any changes to either the breadboard or Arduino board while it’s plugged in.

17. Open up the Arduino Program.

18. Upload the Blink example.

a. Click on File

b. Hover the mouse over Examples until the menu pops up to the right.

c. Hover the mouse over Basics until the next menu pops up to the right.

d. Click on Blink.

e. Upload the program by clicking the arrow pointing to the right at the top of the window.

19. Your LED should now be blinking. Put your red cup up if it is not.

20. Change the speed of the blink like you did last week.

Look at your LED. How often do you think it’s blinking? How can you change it? Let the students play with the numbers on the code to make the connection that 1000 milliseconds are 1 second.

21. Unplug your Arduino board.

As students get their Blink working on the Arduino, encourage them to begin changing the numbers next to the “delay(1000)” in the code of the program. The following challenges can be used (or modified at the instructor’s discretion)

Challenge 1

1. Make the light blink every half-second. NOTE: “HIGH” is on and “LOW” is off

2. Make the light blink every 2.5 seconds.

Challenge 2

1. Make the light blink for 1 second on and 2 seconds off.

2. Make the light blink for 3 seconds on and half a second on.

REMINDER: If a student makes a syntax error while modifying the blink code it will not upload to the Arduino and they will see an error message in orange. They can always reload the code by going Files → Examples → 01.Basics → Blink to start with a fresh set of code.

SEE LESSON EXTENSION AT THE END OF DOCUMENT FOR ADDITIONAL ACTIVITY OF ADDING THREE LEDs (apx. 15-30 minutes)

EXPLAIN

How many different possibilities did we have with the LED?

Two, there was an on state and an off state. What words to did use for on and off in the programming code?

Low = Off

High = On

This gives us two options: On and Off. This is called binary.

In binary we have a total of two possibilities. Just like the light switch on the wall, we can either turn something on or off. All computers work with binary using a series of zeros to represent off and ones to represent on.

Can anybody think of something like the light switch that has two possible combinations?

NOTE: Students may come up with various possibilities. One likely suggestion will be “a button” at which point ask the student to find the button in their kit.

Transition to Elaborate

Please find the button that is in your kit, I will place an example of one under the document camera so you know what it looks like.

Please examples button under the document camera.

ELABORATE

Much like our last activity, we also need a resistor to use a button. However, this activity uses a different resistor. Place your 220 ohm resistor from the Blink activity back its bag and find the resistor labeled 10K (this may be the resistor colored with red Sharpie).

Just like earlier, go ahead and fold the legs of your resistor so it is in the shape of a lowercase ‘u’.

What do you think is the purpose of a resistor?

➢ To resist.

To resist what?

➢ Power or electricity.

Think of a resistor like the faucet on your sink at home. If I open the faucet all the way, what happens to the water?

➢ If comes rushing out.

Then what happens when I turn the knob only half-way?

➢ The flow of the water decreases.

This resistor is much like that the knob on your sink faucet, except instead of water it works with the current of electricity. When there is no resistor the electricity flows at a higher rate, but as we add higher values of resistors we can slow the current which decreases the amount of voltage.

On your computer, please find the folder labeled “Arduino Code Library”. Go to Lesson 2 and find the diagram for Lesson 2C – Button.

Teacher pulls up diagram on front projector.

Please note, that in the diagram the button crosses the middle of the breadboard. Although the button looks square, the legs are slightly different depending on the side. If you have the button facing the wrong direction you may need to rotate it 90 degrees if the legs don’t seem to fit.

Also, remember, it is okay to use either the black or white wires as your ground wires.

NOTE: Sometimes the button legs have difficulty making good contact on the breadboard. Ask students to try a different button and have them check that it is firmly in place.

Monitor and assist students as needed while they try to get their button circuit to work. Once a student has their button working, challenge them to modify the code so the button reverses whether it turns on or off the LED.

Challenge 1

1. Make the button turn the light off.

Challenge 2

1. Make the light blink while the button is depressed.

Step 3: Potentiometer

EXPLORE

Now let’s look at this diagram.

Project diagram on the screen.

Before you open your kits please take a look at this setup. You will be recreating this diagram. Other than what you were just using, make sure you have the rest of these items with you:

● One LED Bulb

● One 220 ohm resistor (black)

● 3 Wires

Also, please take the breadboard out of your kits and make sure you place it with the negative, or blue, rail at the top. Where is ground on the breadboard? Where is the power? How are the dots on the breadboard connected? The rows and columns?

Point to the power row. Point to the ground row. Where do we connect our circuit?

Students should give you answers. Hold breadboard the same direction.

Before you begin, make sure your Arduino is unplugged.

Give students time to wire the diagram. (3 min)

Raise your RED cup if you need help.

Now, just like last week we are going to transfer the LED to the breadboard. Using the diagram found in the folder FILES-EXAMPLES-BASICS-FADE, create this circuit. Note, there is a black resistor in this diagram (220 ohms.)

Once you have this circuit diagram created, you can plug your Arduino back in to the computer so it will blink on the new pin where we have connected the LED on the breadboard.

This Arduino diagram setting uses AnalogWrite to read your changes. We’ll explain in a bit what this means.

What would you need to change to make it brighter? What would you need to change to make it fade? You can keep playing with it now. Make sure everyone’s light is blinking.

Students follow instructions while teachers circulate the room to answer individual questions and help with errors.

EXPLORE (understanding the code)

Horns up! Go ahead and unplug your Arduino.

We are going to take a moment and we are going to look at the code. I see some of you are playing around and experimenting with the code, and this code does a few things that we have never seen before.

Show fade code.

Now there’s a name for all of those colons and semi-colons, and brackets, and parentheses that we use while we’re programming, these are called Syntax.

Now we have “int LED 9.” What do you think this is telling our program to do?

➢ Tells it which light to use. (specifically, you are associating the value 9 with the symbol "led". You could use any name, like "myLed" or "thing"; we just choose a name that makes sense and tells us what that number is going to be used for -- the port where the led is connected)

Yes! This code is telling us that the number we put in this line can only be an integer. Can you raise your hand and tell me what does integer mean?

➢ A number!

An integer is the word you would use to describe whole numbers. Can you give me an example of an integer?

➢ 1!

➢ 20!

Yes, all of these are integers.

Notice the part of the code that says pinMode(led, OUTPUT). Inside of the parenthesis is letting you know what pin you’re using and what your OUTPUT is. Do you know what your output is? If students don’t know it’s ok.

Now take a look at the part of the code analogWrite(led, brightness), notice how they’re in the same order as the parenthesis above. Now, what is your output? Students should say “Brightness”.

Yes! Brightness is your output, and as you can see it is “gradually” giving you the brightness output. This means Arduino is writing in analog form what you should see as the output.

Now, looking at your code, you know brightness changes how much your light fades.

However, what is the restriction on your brightness? It goes from what number to what number?

➢ It goes from 0 to 255.

So just to recap, AnalogWrite is giving you an OUTPUT, such as the light brightness, and it has 256 possible values.

Possible Elaborate (in case you have time to go over this)

Does anyone have any idea where that number is coming from?

Students participate.

Remember from our last class where we went over blinking a light? What were the possible options for turning it ON or OFF in the delay? Students should mention HIGH and LOW. So remember, digital means you can have only 2 options, like HIGH and LOW.

Have you heard the word byte before? Byte is just another word for 8 bits. The Analog to Digital Converter on the Arduino works with 8-bits and each bit has 2 possible values, either 0 or 1 (because its digital). How many possible values in total can this have?

Give students time to experiment with the value.

Explain the example below, or write it on the board.

How many options did you have with one bit? So then, what about 2 bits? Here you can draw a "decision tree" where each branch splits two ways for the two options of each bit, yielding 2^#bits options)

Computers often store information in chunks called bytes. In a byte, or 8-bits, you have 8 different fields to fill: _ _ _ _ _ _ _ _ like 01001100

(Below you can write 2x2x2x2x2x2x2x2 = 256)

There are 2 different possibilities (0 and 1) for each of these fields. That is 2^8, or 256. So, an analog output sends one byte of information, and that can only represent 256 different values.

Now that we have seen an analogWrite, we can find out what happens when we try an analogRead. If AnalogWrite is an OUTPUT what do you think AnalogRead is?

Students should say INPUT.

Yes! Your Arduino will now read analog INPUTS.

In your kits find the metal/brass thing that looks like a knob. When you find it hold it up in the air. Turn to your shoulder partner and confirm that they have the potentiometer in hand.

Visually confirm students have found the right part.

This metal knob has a special term called a potentiometer. Everybody say.

➢ Potentiometer.

The potentiometer allows us to change the resistance without the need for hundreds of different values of resistor.

What do you think we can make the potentiometer do with our circuit?

➢ Change the brightness of the light

Yes! Unlike digital where we can only have whole numbers, for analog we use a knob so we can cover all possible values.

We’re now going to try and create the circuit that you can find under Lesson 3B – Analog Input.

You may go ahead and start assembling your circuit, you have 3 minutes. Raise your RED cup if you need help.

Give students 3 minutes to assemble the circuit. As they finish they are welcome to upload the code from the Lesson 3B AnalogInput folder.

Once the whole class had their circuit working.

So, what did the potentiometer did to the light?

➢ Changed the rate at which it blinks.

In the upper right hand corner of your Arduino software there is a button that looks a little like a magnifying glass. If you click that button it will load something called the Serial Monitor. Go ahead and load the serial monitor and try adjusting your potentiometer.

This will open a new window that shows what value the potentiometer is adjusted to. As the students turn the knob, they will see that the lowest number they can reach is 0 and the highest is 1023.

*If a student’s serial monitor is blank or showing random characters, verify they opened the code from the library. Also, make sure they have the baud rate set to 9600.

Adjust your potentiometer and try to find out the range of values.

What is the lowest number?

➢ Zero.

What is the highest number?

➢ 1023.

How many total possibilities is that?

➢ 1024.

By using the AnalogRead ( )function, we can read the voltage applied to one of the pins. This function returns a number between 0 to 1023, which represents voltages between 0 and 5 volts.

Show Image.

0------------1------------2------------3------------4------------5

0-----------204--------408--------612--------816---------1024

Challenge 1

Let’s try this challenge question. A adjust your potentiometer so it looks like the light is on all the time.

Why do you think it looks solid?

➢ It is blinking but our eyes can’t keep up with it so we just see it turned on.

Great, I have another challenge for you.

Challenge 2

Using the values you see in the serial monitor, discuss with your shoulder partner at your table and try to make the light blink every half-a-second.

Give the students 1 minute to discuss and adjust their lights. Since the value is listed in milliseconds there will likely be conversion issues with students trying the value 2 or 50 as “twice a second.”

These values in the serial monitor are in milliseconds. How many milliseconds are in a whole second?

➢ 1000 ms

Remember, this is very similar to liters and milliliters, how many milliliters are in a liter?

➢ 1000 ml

How many milliliters are in half a liter?

➢ 500 ml

So, what value did you use to make the light blink every half a second?

➢ 500 ms

So to summarize, AnalogRead was reading Analog INPUTS, like your potentiometer and it has a total of 1024 possible values.

EXPLAIN

Possible Elaborate (only if you have time to do during class)

Let’s now understand where the 1024 came from. Our analog input had 1024 possibilities, right? This is because it works in 10 bits, that is 2^10. Just like the byte was 2^8 and it had 256 possible values.

How many possibilities does our button have? (Which is digital)

➢ 2!

Yes! Your computer understands digital. Digital is much more limited than analog. It just uses 0 or 1, which is called binary. Repeat after me: BINARY.

Microcontrollers (like Arduino) are capable of detecting binary signals: is the button pressed or not? These are digital signals. So we need an Analog to Digital Converter (ADC) for your computer to understand (or read) the INPUT.

Incoming signals that vary, like the ones from your potentiometer, are called analog signals.

An Analog to Digital Converter (which is what you see in your code as AnalogWrite) is converts an analog voltage on a pin to a digital number.

By converting from the analog world to the digital world, we can begin to use electronics to interface to the analog world around us.

Not every pin has the ability to do analog to digital conversions. Did you notice how instead of using the connections on the right of the Arduino Board (like we did with the Blink and Fade) we are now using the ones on the left?

What is different on these 5 bottom left?

➢ There’s an A in front of them!

Exactly. On the Arduino board, these pins have an ‘A’ in front of their label (A0 through A5) to indicate these pins can read analog voltages.

A couple of other things you might have noticed, like the two equal signs next to each other. Why do you think there are two of them there?

Students give out different answers.

When we see a double equal sign (==) like this it means, “is equivalent to.”

“And” is represented by a double ampersand (&&).

ELABORATE - Use for students who finish early

Unplug your USB cord from the computer. Put back your potentiometer and your black resistor in your kit. Confirm with your shoulder partner that you’re putting away the right parts.

Give students a few seconds to put them away.

So far you have used a touched based analog source because as you turn the knob the analog values change.

For this activity you’ll need a red resistor (10k) and a photoresistor from your kit. The photoresistor is the red circle with the squiggly on the top. Confirm with your shoulder partner that you have the right parts.

Wait for students to be ready.

So let’s talk about the photoresistor. Does anyone where the word photo comes from?

➢ Light?

Exactly. The word photo stands for light, so your photoresistor is sensitive to light.

Can anyone think of examples of things or places that are light sensitive?

➢ Lights turn on when you go in a room.

➢ The light on my phone!

➢ The light on the car when it goes off when you’re not using it.

Those are great examples! Sensors are a very important part of our everyday lives. Right now we will learn how to program exactly the same.

For this activity you can find the diagram and the code in your Arduino code library called Lesson 3C Night Light. Make sure your Arduino is unplugged before you start.

Give students 3 minutes to get their night light working. Once they assemble it the light will likely stay on solid until they have mapped the values.

As you can see, our night light is not working yet. Go ahead and open the serial monitor and cover your photoresistor to see if the value changes.

Students will acknowledge the value changes.

In your notebooks, write down the lowest and highest values that you see. There is no need to be perfectly exact since the number will be constantly change. (Example, low 600, high 950).

Look at VOID LOOP. Now find the line of code with the word sensor value after the “if”.

if (sensorValue > 500)

In my example I found the low to be 600 and high to be 950. What number is approximately in the middle?

750

So, I would have to change my line to say if (sensorValue > 750)

Give students 2 minutes to adjust their values and get their lights working.

Go ahead and cover your lights with your hands like this, what do you notice?

Yes! It is working!

Challenge

Your light now works, but it is on when it is bright and turns off then it is dark. Try to find a way to modify your code the night light is off during the day and on at night.

Give students 1 minute to figure this out.

Solution #1: (Preferred) Change the greater than sign to a less than sign.

Solution #1 is preferred since it demonstrates an understanding of inequalities rather than toggling a value on/off.

Solution #2: Swap the words LOW and HIGH.

Time pending, have students share out both methods and discuss the benefits of using the inequality in terms of efficiency.

Step 4: Loops

ENGAGE

Let’s watch the following video.

https://www.youtube.com/watch?v=7nI57gjhVd8 (show only 35 seconds of it)

Turn to your shoulder partner and talk about what is happening in the video. What light comes on after red? What light comes on after yellow? What light comes on after green? What happens after that?

Lights are changing from red to green, green to yellow and yellow to red. And what happens when it goes back to red?

➢ It starts again!

Exactly! Everyday we see traffic lights that keep changing and then they start all over again. Why do you think this happens?

➢ Because they’re programmed to do that.

Yes! Lights are programmed and today you will learn how to do the same.

EXPLORE

Before we start opening our kits let’s look at the following diagram.

For this activity you will need the resistor marked black or the 220 ohm resistor from your Arduino kits. Using the diagram found in the folder Lesson 4A – Blink Three Times, please setup your Arduino.

Once you have assembled the diagram please upload Blink from the examples menu. What could you do to make it blink three times then pause?

➢ Copy and paste it three times

Upload the Lesson 4A – Blink Three Times code. What is the main difference between the “blink” code and the “blink Three times” code?

➢ It has High/on and Low/off three times.

The only difference is that the code repeats itself 3 times. What could you do to make the light blink three times in the sample blink code?

➢ Copy and paste the HIGH, LOW, and DELAYS two additional times to make it blink three times.

Please unplug your Arduino. Using the exact same circuit, I would like you to open the program Lesson 4B – Blink Count Times. Please do not upload it to your Arduino yet.

In the program find the line that starts with the word “for”. After you plug in your Arduino, upload the code as is, then start experimenting with the numbers in the “for” line and see if you can figure out what it does.

Monitor class and assist as needed as students experiment with the for loop. Give the class 2-3 minutes.

What do you think the for statement does?

➢ Tells it how many times to blink.

The for statement tells it how many times to blink. Now this is a really, really, really important concept that we have in programming. When we did our first Blink program, if I wanted to repeat the code, I could just copy and paste it as many times as I wanted, right?

➢ Yes!

Now, what if I wanted it to blink 100 times? What would you do?

➢ Copy and paste 100 times.

If I want to write really long programs that do a lot of different complicated things, I don’t want to have to write 200 lines of code, especially if it’s doing the same thing multiple times. When that happens, we use something that’s called a “for loop.” Just like the programming done behind the traffic lights.

EXPLAIN

Let’s unplug our Arduinos.

What do you think the program does with “loop?”

➢ It does it over and over again.

This program goes through everything and then it hits this looping point and starts over. A “for” statement is a loop inside of a loop. It loops until it hits a stopping point. This “for loop” has 3 different parts to its syntax and it’s separated by colons. The first one tells me the variable. What’s our variable called? (right in front of =0)

➢ Count!

What is “count” starting at?

➢ Zero!

Then it’s telling me a rule for when to stop. When does it stop?

➢ At less than 3!

Specifically, when count is less than 3. “3” seems like a small number, but it just makes it easier and faster to code. Notice the “++”. This means add 1. So when we go through the loop, it will start at 0, and then follow the guidelines. Is 0 less than 3?

➢ Yes.

Good then you do the command, and add 1. What’s the count now?

➢ 1!

Is 1 less than 3?

➢ Yes!

Good then you do the command, and add 1. What’s the count now?

➢ 2!

Is 2 less than 3?

➢ Yes!

Good, so we go through the code again, and so on until we get to 3. Is 3 less than 3?

➢ No!

So does what does the program do?

➢ Stops the loop.

It stops the loop and moves on to the rest of the program. So how many times did it actually blink?

➢ 3! From 0 to 2 is 3 times!

Go ahead and take a few more minutes with the code and see if you can make it blink 10 times in a row and then pause. While experiment discuss with your shoulder partner how a for loop works, and be prepared to share-out.

Give the students 1 minute or two to experiment with the code. Monitor the class.

Call on one or two students to explain the following:

What did you have to change in the program?

➢ count < 10

What number did the loop start with?

➢ 0

What does the i++ mean?

➢ To add 1 to i or count.

Transition to Explore Part 2.

We are going to be wiring up a piece that we have not wired up before. Unplug your boards from your computer, and put away your wires, resistor and LED back in your kits. You should only have out the Arduino, breadboard, and USB.

EXPLORE PART 2

So far we have used for loops to change the number of times that a light blinks. But, they can be used for other things. Please look in your kits to find the speaker. Hold up the speaker once you find it. Verify with your shoulder partner that you are holding the right part.

Wait for students to hold-up their speaker.

Take a look at the top of the speaker. What symbol to do you see?

➢ Plus sign.

This is just like an LED light. It has a positive and a negative end.

Which wire is the positive? The longer or the shorter wire?

➢ The longer one is positive.

Right, negative means subtract so the negative is the shorter end and the positive is the longer end.

Go ahead and find Lesson 4C – Siren Passive in your Arduino code library and follow the diagram then upload the code.

Work with your shoulder partner or put your cup up if you need help. Once you get yours working as well, you can start adjusting the numbers in the code and discover what sounds you can create.

Note: Students may have trouble with no sound if the speaker’s pins do not line-up with the wires. Check the pins since the plus sign on the top may have to be slightly misaligned for it to work.

Once you hear the first siren go off.

Looks like somebody got it going.

Give the students about 5 minutes to experiment with their speakers.

Everyone unplug your Arduinos.

Wait until all sounds stop.

I heard a lot of people, when they got the speaker to make the siren noise, saying, “it only goes once.” Since we just went over loops, take a look at the code. What do you see? Void loop. Void setup. So why isn’t this thing looping over and over?

➢ The code is only in the void setup.

The void setup() tells the Arduino to only run that code one time. After that, it continues to the void loop(). When this program gets to the void loop() it has nothing in it. Does that mean it stops?

➢ Yes/No.

Actually, it doesn’t stop. It just loops “nothing” forever. It’s still running to program, but it has no commands to follow unless you change the number of “maxloops”.

I’m going to give you 3 minutes to experiment with creating interesting sounds with your speakers. Be sure to share with your shoulder partner. You may begin.

Monitor and assist students as needed.

Now, lets go to your code library: Lesson 5A- Mario theme and upload the program while you have your speaker plugged in.

What song is this?

➢ Nintendo!

Ok you can play with this while everyone is done finishing this part. Now let’s do something more exciting using our potentiometer. Remember the knob we were using last time? What do you think it’s going to do? Talk to your shoulder partner about what you think it’s going to do.

ELABORATE

For this part you will use an LED, the rest of your wires and your potentiometer, the knob you used last week.

Follow the diagram to connect the potentiometer and the speaker. Call one of us over to look at your setup and make sure it’s good to go. Once we give you the O.K., go ahead and upload Lesson 5A – Mario Theme from the code library into your Arduino.

What do you guys notice is happening? How can you make the song go faster? How can you make the song go slower? How is your potentiometer doing this?

If there is still time left after this, let students follow the diagram and upload Lesson 5B – Nyan Cat Theme.

Step 5: Servo

ENGAGE

Now think back to our first day together and the steps that we went through to program the teacher to turn on and off the light switch. What were some of the problems we encountered?

➢ They walked in to the wall.

➢ We had to be specific about the number of steps

➢ We had to specify degrees.

Great! Keeping that in mind I’d like to show you a video of a bunch of robots that were programed for a competition. This competition was designed to have some of the greatest minds at the best universities, such as MIT and Carnegie Melon, get together to build search and rescue robots.

Show Video: DARPA Robot Competition Fails - https://www.youtube.com/watch?v=g0TaYhjpOfo

EXPLORE

Lets think back to the video we watched earlier. What were some things that you saw in the video?

➢ The robots fell down.

Those robots were programmed the exact same way that you are programming your Arduino right now. In fact, if you think about their arms and legs, they are controlled by dozens of servos just like the one you have in front of you. But, in order to have more control over the motor we wouldn’t want to have to upload a new program each time we want to set it to a new degrees.

Now let’s understand the motors like the ones you saw on the video. We are going to introduce you to a new component. Go ahead and take out the little blue thing.

Show servo on doc cam and hold it up

This is called a servomechanism, but for short, people often just refer to it as a servo. Repeat this with me, SERVO. It works just like a little motor.

Knowing everything you’ve learned so far, do you think this servo is an OUTPUT or an INPUT?

➢ An output!

Yes, this is a new output that we are going to be learning about. You can take the servo out of its plastic bag. Now hold your servo up and confirm with your shoulder partner that you have the right part.

Students hold up their servo.

Servos are more fragile than a lot of the other pieces so we have to go over some rules on how to handle them. Take out one of the little white things, called horns, that are in the bag with the servo and place it on top of the servo. Look at your shoulder partner and make sure you have the right part on your hands. Go ahead and and put it on the servo like this.

Show how to put the horns on the servo on the doc cam.

RULE 1: never try to force the horns on the servo, so always make sure you are patient when piecing them together because this is the easiest way to break the servo.

RULE 2: only the computer should rotate the horns, never by hand because that’s when they break the gears inside.

Without shouting out, I’d like everybody to think for a moment about what each one of the wires might do?

Give the students about 10 seconds to think.

Now, turn to your shoulder partner and share what you think each wire does.

Give students about 1 minute to discuss the wires. Then call on students for examples.

what did your could think the wires might do, and why?

➢ The red wire is power since we’ve always used red for power, the brown is probably ground, and the orange is for data.

Yes! You’re right. Also, take a look at the connector for the servo. You will need to connect a wire to each of the holes so you can connect it to the Arduino.

Since we have always been using blue for data, go ahead and connect the blue directly to the yellow. The middle one should be power so let’s go with red, and the last one should be ground so connect the brown with the black.

Demonstrate plugging a male-male wire in to the servo.

Now notice that your data is going into 9 on your Arduino board, does this mean is digital or analog?

➢ Digital

Yes! Your inputs to move your servo will be digital.

Using the diagram in Lesson 5C – Servo Sweep I would like you to go ahead and build this circuit.

Once you have it wired up, we’re going to go plug in our Arduino and go into the software. Go to File → Examples → Servo → Sweep. Remember to check your port and make sure your Arduino is connected.

Monitor class as students build the circuit.

Alternatively the code has also been included in the Lesson 5C – Servo folder.

If you work your servo too hard it may start getting warm. If you notice it getting warm unplug your Arduino from the computer and give it a break to cool down.

NOTE: the servos are only designed to go between 0 and 180 degrees. These cannot go smaller than 0 or greater than 180.

If your servo is working, you can go ahead and change the numbers. However, the numbers must be between 0 and 180 so we don’t damage the servo.

I want you to play with the program and I want you to experiment, but make sure you stay within the (0,180) bounds. Remember to unplug your Arduino before you make any changes to your code and then upload the program again.

Let the students play with the code and the servos while teacher circulates room to answer questions.

So what do you think “pos” stands for?

➢ Position.

Yes, that’s right! Your Arduino understands where to move the arm in by changing the degrees in the position. Notice how it starts at 0 and it goes up to 90.

What does the plus sign next to the equal sign mean (+=)?

➢ It means it’s going up by one degree.

Exactly, notice how next to the code where you have the parallel lines it is telling you it is going up by 1 degree.

What about the delay? What happens if you change this number?

➢ It goes faster or slower!

Yes, depending on the number you put, it lets Arduino know how long to wait for the next round.

You can keep playing with the code for a couple more minutes before we start going over what we have learned today.

Walk around the room and let them know they have to have their journals ready.

EXPLAIN

Go ahead and write servo in your journals. What is a servo?

➢ A motor.

A servo is a motor that you can control. You can tell it precisely where to go. Go ahead and write what a servo is in your own words and draw a picture too if you’d like so you can remember it.

Where do you think this could come in handy in everyday life?

➢ A fan, windshield wipers, automatic doors.

What happened when we change the numbers in the code?

➢ It changed the degrees in which the servo is turning.

➢ The delay changes the speed at which the servo turned.

Last week we were introduced to the idea of For Loops. Take a look at you code and see if you can identify a for loop. What does it look like the for loop is telling the servo to do?

➢ The loop tells it which degree to move the servo to.

If you are controlling anything, you need to make sure you give the mechanical thing you are using has enough time to do whatever you are telling it. It doesn’t take long to tell the computer to do it, but the thing that is doing it needs time to do it. (LEDS are a bit faster.) If I tell you to do your homework, how long did that take me? Okay now do your homework. Are you done yet? So it takes you some time to actually do it.

The servo is already doing things, it has been programmed to do these automatic things. We want to be able to control what it does.

ELABORATE

Is it possible to control the servo using any one of these other inputs we’ve used?

➢ Use the potentiometer!

How can we use the potentiometer?

➢ To make it slow down and speed up.

Can you think of any other ways we can use the potentiometer?

➢ Use it to turn the servo; change the angle of the sweep.

That’s a great idea, let’s go ahead and try to build that.

Using the diagram in Lesson 5D – Servo Potentiometer I’d like you to try and construct a circuit that will allow you to manually adjust the angle of the servo arm. Keep in mind that you are going to have to rewire your servo so that it connects to the breadboard for power. Remember to have it with the negative bus up.

Give students about 5 minutes to wire up the diagram.

Once you get the wiring done and you’re ready to try it out with the program, we are using the built in program called Knob. So go to File → Examples → Servo → Knob. Go ahead and play with the code to see what you can make the servo do, but remember to stay between 0 and 180 degrees.

Notice how your potentiometer is connected to the Arduino board. Is this input digital or analog?

➢ Analog!

Go ahead and unplug your Arduinos from the computers. Last week we were talking about the fact that analog read between 0 and 1023. Does anyone see those numbers here?

➢ Yes, in the value!

We have a value, called a map, and it is setting a low and a high number. So when the value maps to 0, it is at 0 degrees, and when it maps to 1023, it goes to 180 degrees.

I want you to change the program so that it only maps the servo between 0 and 90 degrees.

Give students time to try and change their program. This is best done by changing the 180 to 90.

NOTE: Some students will try to change the 1023 to 500, this will cause the servo to go haywire once they turn the potentiometer past halfway. Simply have them reload the example code and try again.

Ok go ahead and unplug your Arduinos at this time.

Think about what we’ve done so far in this course, where do you think the servos are actually useful in the real world, where it’s necessary to move between 0 and 90 degrees?

➢ On airplanes; robot joints.

Knowing everything you can use servos for, is it useful to learn how to make them work?

➢ Yes!

Great job. Next time we are going to learn how we can control these servos wirelessly with the remote and start thinking about how we can combine multiple inputs and outputs to create our own inventions.

Step 6: IR Remote

ENGAGE

How are TVs different now than how they were 50 years ago? Think about it for a few seconds. Ok, raise your hand and share.

➢ They are flat!

➢ They are better.

But what accessory do they use now that they didn’t use before?

➢ Remote controls!

Yes! Today we use remote controls all the time, to open the garage door, to play movies, to play music, and overtime, we have improved the way remote controls work to even create awesome toys. Let’s see this video:

http://www.cnet.com/videos/coolest-remote-controlled-toys-of-2014/

Show video until the end.

How do you think this can be done?

➢ The control sends a signal to whatever you are trying to move.

Sure, and how does the receiver know how to read this information?

➢ It has a sensor.

It has a sensor, right. Today we are going to learn more about how remote controls work. We want to start our lesson today with where we left off last time. We are also going to use the servo and potentiometer again today.

Before we build our first project let’s talk about the rules for handling the servo.

● No twisting/turning the servo by hand

● If it gets too warm, unplug it and let it cool down

You can go ahead and open your kits and begin assembling and uploading the code for Lesson 5D – Servo Potentiometer from your library.

Give students 3 minutes to get their circuit working.

NOTE: If a student’s servo is vibrating at 0 or 180 degree have them reduce the range to 10 or 20 and 160 or 170.

Horns up! Great job! It’s awesome to see how much everybody has improved.

Now that we have learned how to control our servo we can use them for different things. But, how have we been controlling the servos so far?

➢ Manually, turning the knob.

Is that efficient?

➢ No!

How would you rather control the servo?

➢ With a remote!

Excellent, let’s try to wire this circuit so we can control the servo with the remotes in our kits.

EXPLORE

We are going to need a few things to do this. First, pull out the remote. Some of your remotes may have a small clear plastic tab at the bottom. This needs to be removed for the battery to work. So those who have that plastic tab please go ahead and remove them.

The other thing you need from your kit is the IR receiver, the one with the 3 legs that reminds us of Wolverine. . IR stands for “Infrared Receiver” and this is the object that received the infrared signals from the remote control. Confirm with your shoulder partner that you have the right part.

Hold up part so everyone is on the same page

Now I want your attention please. Please put everything down.

It is important to note that the one thing in our kits that can potentially hurt you is the IR receiver. The reason is that if you plug this in incorrectly, the metal part will heat up so that the middle doesn’t get damaged. It’s called a heat sink and it happens when your wires are backwards. You won’t be able to see anything, but it will be heating up and when you go touch it, it can burn you.

On your computer in the Arduino code library, go to lesson 6. Under lesson 6, there is an IR receiver file, which will open a diagram. Take a look at the diagram.

Follow the diagram for the correct wiring to make sure no one gets burned.

If you notice, this is really similar to the potentiometer circuit you already have wired to your board. At this time I would like you to remove the potentiometer from your board along with the three wires attached to it. We want to remove those wires because this new diagram has them in a different order. Notice how we are now using pin 11 in your Arduino board. Also notice how you will be using 3.3V and 5V.

Give students time to make those changes.

The two things we don’t want to get backwards are the red and the blue. So, before you plug this into the computer, make sure you have one of the instructors look at your set up and confirmed it is wired correctly.

NOTE: As students assemble their circuit some will likely try to upload the code and start using the remote. This won’t work since they have not included the IR Remote library. Just ask them to wait a few moments since it is important that circuit gets wired correctly for safety reasons.

As a few of you have discovered, you will get an error when you try to upload the code we from this lesson. The error says something about a missing file with the IR remote modified.h. These ‘.h’ files are referred to as libraries that have already been written and we can take that code and stick it somewhere else to use it over and over again without writing the code for the remote every time.

What we need to do is add a library.

Make sure everyone is paying attention, has the code up on their screen, and is ready to follow along with creating a library.

Steps: Go to Sketch → Include Library → Add Zip Library. Select “Desktop,” then select “Arduino Code Library,” then “Lesson 6A.” Then choose the zip file called “Arduino-IRremote.zip” Once you do that, you should be able to upload the program.

Teacher circulates room to answer question and resolve technical difficulties

➢ What button do I press to make it work?

Look at the code and see if you can figure out which buttons have been programmed.

➢ The minus and the plus!

Yes. You can keep playing with it.

EXPLAIN

To make the remote work we had to add a library to this program. What is a library?

➢ A place with books

Great, now the library that we use when we program is a lot like an individual book that tells it own story. We could write the story each time we wanted to read a book, but libraries allow us to reuse the information that other have created to make our programs better and easier to use.

The IR Remote is an item that is complex to program by itself, but with the library we can easily use it with a lot less programming.

Now, open up your serial monitor (the magnifying glass on the top right of your code) and try pressing the buttons on your remote.

Give students time to view the serial monitor.

What do you see?

➢ I see letters and numbers

➢ FFFFFF

Does anybody have an idea about what those letters and number actually mean?

➢ It’s the signal from the remote.

Correct, when the signal hits the IR Receiver it sends a message to the Arduino. That message is in a system we call HEX. If you remember a few weeks ago we learned a little about binary, how many possibilities were there in binary?

➢ 2 possibilities, 0 or 1

Go ahead and try to figure out how many possibilities there are for each digit in HEX.

➢ 16 possibilities, 0-9 and A-F.

Great! HEX is in a number system that we call Base 16. Base 16 is similar to the system that we normally use called Base 10. Imagine if you were counting and had sixteen fingers. You would count all the up to 16 before you ran out of digits. So, if your first finger was zero you’d eventually count up until you had fingers A B C D E and F. Once you ran out of fingers you could start again and place a 1 in the “sixteens” column.

HEX is also very useful since it is how colors are often determined in web design. If you ever want to program your own website you will likely see some HEX.

Using the serial monitor I would like you to try and detect the value for the power button on the remote.

Give students time to figure out it says FFA25D

Look at the part of your code that says #define. Let’s add this new button to our code.

#define BUTTON_POWER 0xFFA25D

Challenge

I have a challenge for you. Based on this new variable called BUTTON_POWER I would like you to try to make the power button move the servo to 90 degrees. Remember, you can copy and paste code with either CTL-C and CTRL-V on PC, or Command-C and Command-V on Mac.

if (currIRvalue == BUTTON_MINUS) {

Serial.println("write 0");

myservo.write(0);

}

else if (currIRvalue == BUTTON_PLUS) {

Serial.println("write 180");

myservo.write(180);

}

else if (currIRvalue == BUTTON_POWER) {

Serial.println("write 90");

myservo.write(90);

}

NOTE: Depending on time you may choose to introduce the final project, Lesson 6B – Ultrasonic Ping (15-25min) or Lesson 6C- RGB Fade (if modules are available, 5-10min)

ELABORATE PART 1 (15-20 min)

Ok, let’s try something different now, we’re going to learn how to use this new item. Take Ultrasonic Ping out. But first let’s talk about bats. Raise your hand if you can tell me when do bats come out.

➢ At night!

Yes, they come out at night. And who can tell me how do bats see at night?

Did you know that bats use their ears instead of their eyes to fly at night?

Let’s watch this video so that you get a better idea of what I’m talking about.

Show video up to 1:15

https://www.youtube.com/watch?v=vRHaFQo6fmk

So what do you think about the video and how bats see at night with their ears?

Raise your hand and tell me what is the name of the sounds that bats can hear?

➢ Ultrasonic sound.

Yes! The item I’m holding is an Ultrasonic Ping and it measures how far you’re away from it using echo, just like the bats. Today we are going to learn how this works. Go ahead and connect the following diagram. You have 2 minutes to set it up. Make sure you don’t fold the Ultrasonic ping please.

NOTE: Demonstrate how to put the ultrasonic sensor in to the breadboard.

IF YOU DO NOT DEMONSTRATE THIS THE STUDENTS WILL TRY TO BEND IT FLAT ON THE BOARD LIKE IT APPEARS IN THE DIAGRAM.

Once you have the setup ready, go ahead and import the following library: Newping.zip. It is located in your desktop lesson folders. Just click on Sketch → Include library → Add Zip library → choose your Desktop → Lesson 6 → Lesson 6B → Newping.Zip

Once you have the setup ready, go ahead and upload the program in the same folder called Lesson 6b Ultrasonic Ping. Open your serial monitor and select the 115200 baud.

What do you notice the ping is doing?

➢ It’s measuring the distance!

Yes, it is reading the echo of any item placed in front of it. It can actually reach up to 400 cm! Can you think of any other examples of technology similar to this?

➢ The new cars that stop when they’re too close to objects.

➢ The doors that open automatically at the mall.

➢ Ultrasounds to see babies.

Yes, all of these are great examples.

Since we have a few minutes left, let’s try another new item.

ELABORATE PART 2 (5 minutes)

So the last part of this lesson is just showing you how to create cool lighting. This last part will take us 5 minutes, it is the easiest one we’ve done so far. We will no longer need any wires or breadboard. So please put away everything except your Arduino board and your USB connector. Take a look at this part I’m holding.

Show the Color changing RGB module.

Please take it out and confirm with your shoulder partner you have the right part.

Go ahead and insert it into 11, 12, 13, and GND on the Arduino board. Then you can upload the code from your library. The code is included in Lesson 6C – RGB Fade.

Notice the code that is programmed to make the light change. Can you name which color is next on your light? You can play with the code too. You can change the order of each color.

We are done for today. Please put everything away and let’s review what we learned by answering the following 3 questions on your notebook.

Step 7: Animation

ENGAGE

Introduce Final Project

Let’s begin brainstorming by looking at a set of projects all done with arduino kits similar to your own

https://www.instructables.com/id/Getting-started-with-Arduino-Potentiometer-Pong/ (start at 0:30)

https://www.instructables.com/id/RainBoard-RGB-LED-Rainbow-Fader/

(whole video)

https://www.instructables.com/id/Necomimi-Arduino-Cat-Ears/

(only show 5 or 10 seconds of the video)

https://www.instructables.com/id/Secret-Knock-Detecting-Door-Lock/

(start video at 0:50 seconds end at 1:15)

Did you like the toys? Do you think you now understand how these toys function?

What about the things that you’ve learned with us? Do you think you can animate a stuffed animal or make a small gizmo as well?

➢ Yes!

Yes! You will! Today you will begin putting together everything you’ve learned to make your own toy. But first, let’s go over what we have learned so far.

So far we have learned a lot about inputs and outputs.

I want you to pull out your notebooks. Go ahead and write the words INPUTS and OUTPUTS on the top. We are going to generate a list of all the different ones we’ve explored during our lessons.

Call on students and generate list. You list may be slightly different depending on the projects completed in class.

Try to remember what we used on the second lesson. What item did we program?

➢ A button!

Yes, a button. Is a button an INPUT to the computer or was that an OUTPUT from the computer?

➢ INPUT!

Yes. And why is that?

➢ Because we used the button to tell the computer what to do.

Exactly! What else did we explore on that same class?

➢ The LED light.

And is that an INPUT or OUTPUT?

➢ An OUTPUT because it’s what the computer was giving us.

Exactly!

Now in your journals make a list of all the inputs and outputs we've observed so far. Work together to try to get a list of all of them.

Walk around the classroom making sure they’re completing the list and helping them out with similar probing questions. Give them TWO minutes to finish the chart. And then go over.

Inputs

Outputs

Button

IR Receiver

Photo-resistor

Potentiometer

Tilt sensor

Ultrasonic sensor

IR Remote

LED (light)

Servo

Speaker (sound)

Show students your own project you have been working on and what it does. An example of this is below but you do not have to build a cow.

I have two example projects I would like to share with you. Here I have a little cow that is plugged in to an Arduino. As you can see on the breadboard there is an IR Receiver just like we learned how to use last week. If I take my remote and point it at the receiver (press power) I can make his horn light up.

Also, if I press the plus sign I can move his hand one way (press plus), minus will move his hand back (press minus), and if I press EQ I can make him wave (press EQ).

With your remote, let’s see if you can bring him to life from across the room.

Allow students a minute or two to activate the cow with their remotes.

What inputs and outputs did we use with the cow?

➢ Input: Remote (multiple buttons)

➢ Output: LED (light), Servo (motion)

For your final project you will be encouraged to include at least two input and two outputs.

One last part we have to introduce is the female wire (or extender wire). These wires are very useful for connecting things that will be far away from your breadboard. For example if you wanted to connect an LED you would put a female wire on each end then connect a male wire to each female end and plug it into the breadboard.

Explore

At your table begin to work on a few ideas for what you would like to create with arduino. It can be animating a stuffed animal or creating a light up hat or anything you could do with the materials we have and a little bit of time. Write the ideas down in your journal and when you think you've picked which one you want to do get it checked by a teacher and begin working. When you're thinking of something try to come up with a plan and a list of materials that you will use. If you can't think of anything try to use your project to tell a story or think of something you could use in your everyday life. When thinking up your project try to think of something that will use at 2 inputs and 2 outputs.

After you have a teachers approval begin to collect the materials you will need and work on your project.

The following will be good requirements to follow when approving projects.

No traveling ( walking or rolling robots)

No putting a single circuit that you've already done in the animal

School appropriate

Nothing requiring a servo to turn more than 180

Playing songs is very complicated- if they do this everything else can be simple

Project must be thought provoking

Give students time to discuss. Have them share some ideas. You can also have them look at instructables.com to see their possibilities and get them excited about animating an object. When they come to you with their ideas remember our limited time and try to only green light things that will be achievable.

Additional resources

Distance Sensor:

Must download and import the following library: http://playground.arduino.cc/Code/NewPing

The above code is for turning on a LED if the sensor detects an object in front it.

Share

    Recommendations

    • Microcontroller Contest

      Microcontroller Contest
    • Space Challenge

      Space Challenge
    • Science of Cooking

      Science of Cooking
    user

    We have a be nice policy.
    Please be positive and constructive.

    Tips

    Questions

    Comments