On Github mhellar / physcomp16
Slides are here: http://mhellar.github.io/iot16/week1
“The two most important introductions for art in the past 20 years have been the Arduino and Processing,” - Paola Antonelli, senior curator in the Department of Architecture and Design at the Museum of Modern Art..
Most of us know what a computer looks like. It usually has a keyboard, monitor, CPU (Central Processing Unit), printer, and a mouse. These types of computers, like the Mac or PC, are primarily designed to communicate (or “interface”) with humans.
There are also little computers all around us, running program and quietly doing calculations, not interacting with humans at all. These computers are in your car, on the Space Shuttle, in your kid brother’s toy, and maybe even inside your hairdryer.
We call these devices “microcontrollers”. Micro because they’re small, and controller because they “control” machines, gadgets, whatever ! They’re cool because, you can build a machine or device, write programs to control it and then let it work for you automatically. And they are cheap!
Arduino is an open source physical computing platform based on a simple input/output (I/O) board and a development environment that implements the Processing language (www.processing.org). Arduino can be used to develop standalone interactive objects or can be connected to software on your computer (such as Flash, Processing, NodeJS or Max/MSP). !
The Arduino board is a microcontroller board, which is a small circuit (the board) that contains a whole computer on a small chip (the micro- controller). This computer is at least a thousand times less powerful than the MacBook I’m using to write this, but it’s a lot cheaper and very useful to build interesting devices. !
The Arduino design team have placed on this board all of the components that are required for this micro controller to work properly and to communicate with your computer. There are many versions of this board; the one we’ll use throughout this book is the Arduino Uno, which is the simplest one to use and the best one for learning on.
These can be inputs or outputs, which is specified by the sketch you create in the IDE.
These dedicated analogue input pins take analog values (i.e., voltage readings from a sensor) and convert them into a number between 0 and 1023 we’ll look at this next class
These are actually six of the digital pins that can be reprogrammed for analog output we’ll look at this in the next class
These power pins are capable or providing 5.5 volts or 3.3 volts of power. Also there are two pins that are capable of providing ground any components that you attach to the controller.
The USB connector connects the controller to you computer allowing you to program it. The USB connection also provides power.
The power connector allows you to power your controller independently either with a battery or a power supply.
The IDE (Integrated Development Environment) is a program running on your computer that allows you to write sketches for the Arduino board in a simple language modeled after the Processing (www.processing.org) language.
The magic happens when you press the button that uploads the sketch to the board: the code that you have written is translated into the C language (which is generally quite hard for a beginner to use), and is compiled into a language that the micro controller understands. This last step is quite important, because it’s where Arduino makes your life easy by hiding away as much as possible of the complexities of programming microcontrollers.
This first lesson won't really teach programming the Arduino. It’s meant to introduce you to building a basic electrical circuit. It will also get us prepped for building the rest of the exercises throughout the class.
1 resistor (bands are red,red,violet, gold)
1 resistor (bands are red,red,violet, gold)
3 jumper wires. 2 Long(yellow,green), 1 short(white)
1 breadboard
1 USB Cable
1 Arduino
Solderless Breadboard
Step 1: connect Arduino to computer with USB cable
Step 2: connect one side green wire to the ground pin(marked GND)
Connect the other side of the green wire to the ground strip on the breadboard marked with a minus sign -
Step 3: connect one side of the yellow wire to the 5 volt pin(marked 5V)
Connect the other side of the green wire to the ground strip on the breadboard market with a plus sign +
Step 4: connect one side of the short white to any socket along the red strip
Connect the other side of the short wire to any socket in the row numbered 11
Step 5:Place the resistor on one socket in the strip marked with a negative sign -
Place the other end of the resistor in row 10
Step 6: The LED has a long wire and a short wire place that in the same row as the short white wire #11
Place short end of the LED in the same row as the resistor #10
You should have Light!
An electric circuit is a closed loop where electric current can flow. Electricity flows like water from Ground to Positive.
As the current passes though a component such as an LED it causes it to light up.
The Arduino is acting as our battery providing 5 volts of electricity.
An electric circuit is a closed loop where electric current can flow. Electricity flows like water from Ground to Positive.
As the current passes though a component such as an LED it causes it to light up.
5 Volts is too much electricity for the LED and will burn it out!
So before we send electricity to it we need to place a resistor in front of it to limit the electrical current.
LED Resistor calculator: http://led.linear1.org/1led.wiz
This lesson will basically get you up and running using the Arduino software and uploading a sketch to the Arduino board. Once you've completed this step we can continue to the really interesting stuff, which is when we start writing our own sketches!
Once the LED is connected, you need to tell Arduino what to do. This is done through code - that is, a list of instructions that we give the micro- controller to make it do what we want.
Open the Arduino IDE from the Applications folder.
// Example 01 : Blinking LED int LED = 13; // LED connected to digital pin 13 void setup() { pinMode(LED, OUTPUT); // sets the digital // pin as output } void loop() { digitalWrite(LED, HIGH); // turns the LED on delay(1000); // waits for a second digitalWrite(LED, LOW); // turns the LED off delay(1000); // waits for a second }
Arduino expects two functions to exists—one called setup() and one called loop(). setup() is where you put all the code that you want to execute once at the beginning of your program loop() contains the core of your program, which is executed over and over again. When you power up the board, the code runs; when you want to stop, you just turn it off.
// Example 01 : Blinking LED() A comment is a useful way for us to write little notes. The preceding title comment just reminds us that this program, Example 01, blinks an LED.
void setup() This line tells Arduino that the next function will be called setup().
digitalWrite(LED, HIGH); // turns the LED on As the comment says, digitalWrite() is able to turn on (or off) any pin that has been configured as an OUTPUT. The first argument (in this case, LED) specifies which pin should be turned on or off (remember that LED is a constant value that refers to pin 13, so this is the pin that’s switched). The second argument can turn the pin on (HIGH) or off (LOW).
Imagine that every output pin is a tiny power socket, like the ones you have on the walls of your apartment. American ones are 110 V, and Arduino works at a more modest 5 V. The magic here is when software becomes hardware. When you write digitalWrite(LED, HIGH), it turns the output pin to 5 V, and if you connect an LED, it will light up. So at this point in your code, an instruction in software makes something happen in the physical world by controlling the flow of electricity to the pin. Turning on and off the pin at will now let us translate these into something more visible for a human being; the LED is our actuator
delay(1000); // wait for a second Arduino has a very basic structure. Therefore, if you want things to happen with a certain regularity, you tell it to sit quietly and do nothing until it is time to go to the next step. delay() basically makes the processor sit there and do nothing for the amount of milliseconds that you pass as an argument. Milliseconds are thousands of seconds; therefore, 1000 milliseconds equals 1 second. So the LED stays on for one second here.
digitalWrite(LED, LOW); // turns the LED off This instruction now turns off the LED that we previously turned on. Why do we use HIGH and LOW? Well, it’s an old convention in digital electronics. HIGH means that the pin is on, and in the case of Arduino, it will be set at 5 V. LOW means 0 V. You can also replace these arguments mentally with ON and OFF.
delay(1000); // wait for a second Here, we delay for another second. The LED will be off for one second. } This closing curly bracket marks end of the loop function.
Before we move on to the next section, I want you to play with the code. For example, reduce the amount of delay, using different numbers for the on and off pulses so that you can see different blinking patterns. In particular, you should see what happens when you make the delays very small, but use different delays for on and off . . . there is a moment when something strange happens; this “something” will be very useful when you learn about pulse-width modulation on Thursday.
//Random Delay // ! int LED = 13; // LED connected to// digital pin 13 ! int i; // Variable to hold our random value void setup() { pinMode(LED, OUTPUT); // sets the digital pin as output } ! void loop() { i = random(1, 1000); // generate a random value every loop cycle digitalWrite(LED, HIGH); // turns the LED on delay(i); // pause for the duration of variable i digitalWrite(LED, LOW); // turns the LED off delay(i); // waits for a second the duration of variable i ! }
/// Blink with for loop // ! int LED = 13; // LED connected to digital pin 13 ! int i; // Variable to hold our random value void setup() { pinMode(LED, OUTPUT); // sets the digital // pin as output } ! void loop() { // set I to 1; while i is less that 100 increment i by one ! for(int i = 1;i <=100; i++){ digitalWrite(LED, HIGH); // turns the LED on delay(i); // set delay based on the value of i digitalWrite(LED, LOW); // turns the LED off delay(i); // set delay based on the value of i } }