Physical Computing

Physical Computing Course Page

 

Hello, and welcome to the course page for the Physical Computing class! I will be updating this page every day we have class, with explanations of what we’re doing, links to helpful resources, and some sample code. Be sure to follow along on this page during class, and look at it outside of our scheduled class hours to review the material.

 

 

Feel free to email me if you have any questions at michael@ilabliberia.org

 

Thanks!

Michael Madaio

 

Helpful Resources:

Links from last year’s offering of this course:

These are materials from the 2013 Physical Computing course.

 

 

Day 12 (7/10/14)

Today is the last day for the course!

You will have until 3:00 to finish your projects, and then at 3, we will take a brief post-course survey, and the link to your test will be given to you when you complete the survey.

 

You may, of course, continue to work on your projects outside of class, before the Presentation next Wednesday night (July 16th, 6:00-7:00), where you will show off your finished projects to members of the community.

 

Here is the survey link.

 

 

 

Day 11 (7/9/14)

 

Yesterday, you came up with your top 3 ideas, and I have to say… I was impressed!

 

If you haven’t yet, see the section on Day 9, yesterday.

 

 

You then started to predict what some of the challenges might be for each of those ideas, and ranked them in order of most likely to be feasible, to least likely.

 

Well, if you chose your project concept, today you will begin to create it, and tomorrow, you will finish! Plan your time accordingly.

 

Sketch out the circuit, then create it with the Arduino, and think about what code you can use from our previous projects, and how you would have to modify it for this project. If you want, find code online, but beware the trap of spending hours searching for the “perfect” code, when it would have taken you 10 minutes to write on your own.

 

You should have a working version of your project by the time you leave today.

 

 

Remember our steps:

 

-What information do you need to keep track of? (or, what do you (and the Arduino) need to KNOW?)

-What does it need to DO? (think about the process from INPUT of information, into the Arduino, then out of the OUTPUT).

-How will you do that? (feel free to find circuit diagrams and schematics online, especially when considering which resistor to use, how many, and where they need to be placed in the circuit).

 

 

 

 

Day 10 (7/8/14)

 

This is the last week of the course! Today you will be meeting in groups to sketch out and plan your projects. You should create 3 different project ideas, sketched out on paper, then we can talk about their relative merits, drawbacks, and feasibility to complete in a week.

 

 

Think first about a problem you might want to solve, or something you might want to make more convenient for you. Keep this limited to your house. Try thinking about each of your rooms, or go through your daily routine. What are issues, ranging from simple annoyances, to larger problems, that could be solved by using a processor taking in input and giving out an output?

 

 

 

Consider the possible inputs and outputs listed below. Think logically about how the Arduino might process the data and what possible actions it could take.

 

We will brainstorm and choose our project concept today, and start figuring out how to use the required inputs/outputs, and looking at sample code to modify to meet our needs.

 

Then, tomorrow, you should create the circuit and write the code, and Thursday, you should finish it, show it to other groups, get feedback, and revise. We will also take a short test at the end of class to assess your successful completion and mastery of the material.

 

 

 

Finally, next Wednesday, July 16th, we will have our course showcase, demoing our projects for members of the community (after a guest lecture by myself on Hacking, Tinkering, and the Maker movement).

 

 

Inputs:

  • Buttons
  • Switches
  • Potentiometers (dials)
  • Photocells (light sensors)
  • Temperature sensors
  • Vibration sensors (piezoelectric)
  • Flex sensors (for bending… a soft potentiometer)
Outputs:
  • Print line
  • Red and yellow LED’s
  • RGB (red, green, blue) LED’s
  • 1 speaker
  • 2 accelerometers
  • Servo Motors

 

 

 

 

Day 9 (7/3/14)

 

No Internet today, so no daily update:

 

However, we got our photocells to work. See this link for an explanation if you were not here.

 

Between today and next week, think of ideas for your project. We will spend all 3 days next week building them, then we will show them off at a Demo night the following Wednesday, July 16th.

 

Remember the components we have at our disposal:

  • Red and yellow LED’s
  • RGB (red, green, blue) LED’s
  • Buttons
  • Switches
  • Potentiometers (dials)
  • Photocells (light sensors)
  • Temperature sensors
  • Vibration sensors (piezoelectric)
  • Flex sensors (for bending… a soft potentiometer)
  • 1 speaker
  • 2 accelerometers
  • Servo Motors
Look at project ideas on Arduino websites and think about what in your house you would want to hack, or use the Arduino to modify and control.

 

 

 

 

Day 8 (7/2/14)

 

Let’s start by making sure our Button State Change program works. If you stuck around yesterday, you saw that we realized that instead of analogRead(buttonPin), it should have been digitalRead(buttonPin). That was a transcription error on my part, as I was typing the code from my laptop to the projector. Good exercise in troubleshooting and debugging, though! In retrospect: duh, of course, it HAS to be a digitalRead function, because we want a DIGITAL input (either 0 or 1), and not the continuous input from an analogRead (checking a series of values between 0 and 1023, or so), which we WILL be using today for the potentiometer and photocell.

 

 

 

Today we will begin to use the potentiometer, which we will be measuring with the analogRead(potPin) function (making sure it’s plugged into one of the analog pins (A0, etc). If it’s lying on its “back”, with the knob facing upwards, the top pin is GND, the middle is data, and the bottom should be 5V. If you’re unsure, google Arduino potentiometer, and look at a picture.

 

 

So, we’ll have some value, let’s call it val = analogRead(potPin). We can then use this to increase delay, fade the light (if the ledPin is plugged into a PWM… to simulate an analog fade by blinking very rapidly), or anything else, really.

 

 

Fading an LED with PWM and a Potentiometer

 

 

Finally, we will be using the photocell, so plug that in to 5V, with the negative going to an analog In pin, and through a 10k resistor, into GND. Then, plug in an LED into a 330k resistor, into a digital pin (with PWM), and into the GND.

 

 

For the code, we will create a variable photocellReading, which we will set = to the analogRead(photocellPin). This gives us a value for the resistance on the photocell from 0-1023, but this is the inverse of what we want, so we have to subtract this value from 1023 to get it to read from 1023 (high resistance… or very bright) to 0… low resistance, or very dark.

 

photocell = 1023 – photocell;

 

 

Then, because we have values from 0 to 1023, and we want values from 0 to 255 for the brightness of our light, we must use the map function to map all of these values. The 5 parameters map takes are the input variable and the range of it first set of values, and the range of the 2nd set of values that we want it to be.

 

LEDbrightness = map(photocellReading, 0, 1023, 0, 255);

 

Then we simply analogWrite(ledPin, LEDbrightness);

 

And, voila!

 

 

https://learn.adafruit.com/photocells/using-a-photocell

 

 

 

Finally, form a group of 2-3, each with an Arduino. Begin to brainstorm 3 or 4 different ideas for your project. Remember that we have 2 weeks to make these, and we will be demoing them for people on Wednesday, July 16th.

 

 

 

Day 7 (7/1/14)

 

Today we will be tackling 3 different things –

 

  • Using a counter to track whether the button has been pressed or not (by counting how many times it has been pressed, and using a modulo “%” to see whether the count is even or odd (or any other combination you want).  With this, we will also learn how to “print” to the screen to keep track of numerical values and give us debugging and error reports.
  • We will learn to use the potentiometer, which is an analog method of increasing resistance, to manually increase the delay time (or fadeAmount), or, really, any other numerical element we want to manually control with an increasing or decreasing numerical value.
  • We will use the photocell light sensor to measure the ambient light nearby and translate that into resistance, to dim or brighten an LED.

 

 

First, plug in a 10k resistor to the button, and a 5V in to the button so that it is a complete circuit, even when the button is not pressed. See the attached diagram. Then, attach a wire to a designated input pin, on the other side of the button, so that it is only connected when the button is pressed.

 

 

http://arduino.cc/en/Tutorial/ButtonStateChange

 

We will then be using the Serial.print function to print the count variable of how many times the button is pressed. We have to initialize this in our setup() with a call to Serial.begin(9600); and we can then later in our loop call Serial.print or Serial.println to print various strings and variables to the monitor (the magnifying glass on the top right of the Arduino IDE).

 

 

We will use modulo to figure out whether the count is even (meaning turn on) or odd (meaning turn off… or whatever you tell it to do). Modulo divides one value by another and gives you the remainder. So 8%2 = 0, 8%3 = 2, 8%4 = 0, 8% 5 = 3, 8%6 = 2, 8%7 = 1, 8%8 = 1, etc. We will be using buttonCount % 2 == 0 (or whatever variable we have designated) to check to see if the buttonCount is even. If that statement above is true, then do something, “else”… do something else.

 

 

 

Day 6 (6/26/14)

Review and catch up day.

 

 

Day 5 (6/25/14)

 

Before we start for today, please take 5 minutes to take a brief survey that I’m giving as part of the research I’m doing on the iLab. If you’ve already taken it for another class, you don’t need to take it again.

Let’s begin by finishing our challenge from yesterday.

 

Can you have multiple LED’s blink at different times? What would the circuit look like? What would the code look like?

 

We will learn how to use buttons to control whether a circuit is connected or not. Buttons, like switches, are either normally open (NO), or normally closed (NC). If the button is NO, then the default is for power to not go through, until it is pressed and the circuit is closed, allowing power to go through.
Let’s put a button into our circuit, with only LED to start. Where would the button go in that simple circuit? What do you think the LED will look like when it is not pressed? When it is?
Now let’s reconnect 2 LED’s to the circuit, and let’s try to control them manually, using the button, rather than through digital output. How would we do that?
Let’s say you wanted LED #1 to be on the first time, and LED #2 to be off, and for those to switch when you pressed the button. How would the circuit “know” which one to send power to?

 

Day 4 (6/24/14)

 

Let’s begin by reviewing what we ended with on Thursday. Use this Morse Code chart and create any 3 or 4 letter word using blinking lights to create each letter. Copy and paste code to save time.

 

 

 

 

 

Now let’s try having our LED fade. Create a new sketch, and call it lastName_Fade.

 

 

When you copy the code into your Arduino IDE, don’t copy the comments (the /* symbol denotes beginning a multi-line comment, and its opposite, */ denotes ending a multi-line comment).

 

 

/*
Fade

This example shows how to fade an LED on pin 9
using the analogWrite() function.

This example code is in the public domain.
*/

 

int led = 9; // the pin that the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by

 

// the setup routine runs once when you press reset:
void setup() {
// declare pin 9 to be an output:
pinMode(led, OUTPUT);
}

 

// the loop routine runs over and over again forever:
void loop() {
// set the brightness of pin 9:
analogWrite(led, brightness);

 

// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;

 

// reverse the direction of the fading at the ends of the fade:
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);
}

 

 

Now, let’s try adding multiple LED’s. How do you think that would change the circuit or the results?

 

 

/

Day 3 (6/19/14)

When we left yesterday, we had built our first simple circuit, and written the code to have our light blink, though we have not yet loaded it onto the Arduino to see it work.

 

Today, we will start by debugging our code and loading it onto the Arduino. Then, we can change the parameters to see how else the code might work differently, if the values are different.

 

Remember, there are 2 main functions, within which all of our code will be running, with the exception of some global variables that you might want to use.

void setup() {

pinMode(13, OUTPUT);

}

 

void loop() {

digitalWrite(13, HIGH);

delay(1000);

digitalWrite(13, LOW);

delay(1000);

}

 

 

There are some conventions in code which programmers obey to make it human-readable, and then there are some rules we must follow for our code to be machine-readable, or else it will not run.

 

First, the syntax of the code includes the punctuation, capitalization, the way we use white space, the way we indent, and how we create variables, call functions, and use parameters. For instance, if you want the name of something to include two words, if you put a space between them, the compiler will read those as two separate terms, and try to act on them differently. So, if you want to say pin mode, you would write pinMode. The capitalization is both a human-readable convention, so we can see the difference between the two words (not pinmode), but also a code requirement, since the computer distinguishes capital letters from other like terms. (pinmode is taken to be a different thing than pinMode). We typically start all functions and variables with lower case letters, and write all constants as all upper-case letters (OUTPUT, INPUT, HIGH, LOW). Constants are programmatic terms that contain predefined variables. See the Arduino Reference page for more information.

 

Functions, as we said yesterday, a set of instructions to take in input (also known as parameters), act on them in a series of statements, and return some output.

They are always written as follows:

 

void setup() {

}

 

 

// void is the value of the output being returned (in this case, nothing is returned, not even 0, which is a value).

 

// setup is the name of the function, and () would contain the parameters, if it took any.

 

// the curly braces { and } enclose all of the statements that this function would do when it runs. Everything contained within the braces must be indented (by pushing tab, not hitting spacebar), or else the program won’t know they are contained in the function.

 

All Arduino sketches (programs) will include the setup() function, which runs once when you turn the Arduino on, and the loop() function, which runs on a continuous loop while the Arduino is plugged into a power source. Consider wisely which functions will be contained within each of those particular functions.

 

 

Arduino has several more built-in functions like this that we can use, such as pinMode(), digitalWrite(), and delay(). To see the complete list… consult the Reference. (This is a good habit to get into when learning ANY new software.)

 

 

Now that our blink program is working, try changing the values of the delay() function. Try changing the pin number and plugging the wire into different pins. Let’s create a global ledPin variable to make this change easier. That way, if we happen to refer to this pin number multiple times in our sketch, we need only change it once. At the very beginning of the sketch, outside of the setup() function, declare a new variable, and let’s call it ledPin.

 

int ledPin = 13;        // int, for integer, then the variable name you’ve just created (you can call it anything, so long as it makes sense to you and others reading your code), then = the number pin you want it to be assigned to.

 

Try repeating those 2 functions (digitalWrite() and delay() to have it create an SOS Morse Code distress signal. (dot dot dot, dash dash dash, dot dot dot)

 

How would you do that? What else can you spell with Morse Code?

 

Each “blink” will have either a short or a long delay between turning on and turning off, and between turning off and back on. You could, if you wanted, write 12 lines of code for each “letter” (S = dot dot dot, and O = dash dash dash).

void loop() {

digitalWrite(13, HIGH);

delay(500);

digitalWrite(13, LOW);

delay(500);

 

Repeat this 2 more times for the S, then repeat it 3 times in a row with 1000 for the delay for the O (dashes are longer), then repeat the 12 lines of the S again. However

 

However, there’s an easier way. Let’s create a dot() function and a dash() function to represent the 4 lines of each individual dot or dash blink.

 

OUTSIDE of our void loop() function, let’s create a new void dot() function. Remember to indent the statements that run within that function, even though they don’t show up here on this website.

 

void dot() {

digitalWrite(13, HIGH);

delay(500);

digitalWrite(13, LOW);

delay(500);

}

 

 

If we create another function for dash(), (also outside of any other function), then in our loop() function, we can just call these other functions. Like so:

 

void loop() {

dot();

dot();

dot();

 

dash();

dash();

dash();

 

dot();

dot();

dot();

 

delay(2000);    // This is so there is a pause before the SOS cycle repeats itself.

}

 

 

Day 2 (6/18/14)

Congratulations, you made it through day 1, and you’re back! Today we will be working with the Arduino hardware and software, and starting to build and program our first circuit.

 

Mentally review what we discussed yesterday, with a circuit needing a power supply, and possibly having an input sensor to “read” some aspect of the environment, as well as possibly having a visible output, such as a light, or a sound, or motion, etc. If it is an electronic circuit, and not just electric, the input will run through a processor, which will have some sort of program, or logic, that dictates how it operates.

 

Our first circuit will be a blinking light, and you will be programming it to blink on and off at a prerecorded interval. First, look at the Arduino processor. Find the header pins, both Digital (for binary signals), or Analog (for analog signals).  Find GND, for ground. This is where our circuit will terminate, so all of the excess current will not build up and create a charge. Find the 3.3V and 5V pins (V stands for volts), and is a measure of the charge in the circuit. Find the TX and RX lights, which stand for Transmitting Data (TX), and Receiving Data (RX). Find the USB plug, which will send both power and data from the computer, and find the round 9V wall plug, which will send power from either the wall, or a 9V battery. This will be enough to get us started. If you’re interested in the rest of the pieces of the board, then read further in the Manual I linked to above.

 

Find your white breadboard, with 30 rows of horizontal pins (1-30), and 10 columns (A-F). You should also see a positive (+) column, and a negative (-) column on either side. This is where we will plug in our lights, resistors, jumper wires, and other elements of our circuits.

 

 

 

 

Day 1 (6/17/14)

Welcome to the first day of the Physical Computing class. In this class, you will be using the Arduino hardware and software to understand more about how electronic microprocessors use sensors to receive input from the outside world and engage in computation to then act on that input with a visible (or audible) output.

 

Think for a second of any electronic device that you interact regularly in your lives. If you need help, open up Google and conduct an image search.

  • What is the input it receives?
  • What, if any, computation does it engage in?
  • What is the output?

 

All electric and electronic devices are comprised of a circuit – a loop that begins with a power source, that passes through the various other components (or, elements) of the circuit.

 

The most basic example is a light – power enters from the electric socket on the wall, or a battery, passes through the light filament, and then terminates in what is called the “ground” (even if you never see it go into the actual ground).

 

 

More advanced circuits use either information from the external environment to tell it whether or not, and how to activate, or information from an internal program to determine whether or not, and how to activate. The most simple version of this is a button, where the button, when pressed, “opens” or “closes” the circuit, enabling current to flow and a light to be activated.

 

Alternatively, you can write a software program telling the circuit when and how to activate. The most simple version of this, a blinking light, we will begin today.

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *