MACUL 2016Arduino Workshop Version 1.1 Corrected on 3/13/2016

MACUL 2016Arduino Workshop Version 1.1 Corrected on 3/13/2016

MACUL 2016Arduino WorkshopVersion 1.1 Corrected on 3/13/2016

Resources/References

Workshop help: Using a Mac

THE ARDUINO

SAFE HANDLING:

Activity 1Setup: Blink and C code modifications: No Breadboard!

Activity 2Breadboarding (using Blink)

Activity 3: Many LEDs -- KnightRider

Activity 4: FADE – using PWM to effect analog output

Activity 5:Using trim potentiometer to adjust blink rate

Activity 6: Using a push button (“tactile button”) to light single LED

Final fun: free web-based circuit simulator:

Resources/References

(1)Arduino website:

  1. Learning:
  2. Language resource:
  3. Libraries:
  4. Playground (a wiki; Arduino users/developers can contribute and look at example projects):
  5. Download: Windows, Linux, Mac OSX:
  6. Products: Boards, Kits
  7. Forums
  8. Et cetera

(2)Nice 15 step tutorial for beginning Arduino users:

(3)Books:

  1. Banzi, Getting Started with Arduino, 978-596-15551-3

start from scratch, using IDE, elementary sketches and wiring, basic electronics, basic C programming). Sketches and wiring are extremely simple – basically from arduino website

  1. Schmidt, Arduino: A Quick Start Guide, 978-1-934356-66-1

Start from scratch, assumes more facility in C programming, projects are much more interesting/complicated)

  1. Nussey, Arduino for Dummies, 978-1-118-44637-9

Typical Dummies book – very readable. Two chapters on basic electronics. Looks useful for robotics club.

  1. Blum, Exploring Arduino, 978-1-118-54948-3

Start from beginning, but moves quickly. Looks like it would be a good choice for technical school/community college course. Covers more of the engineering/scientific background.

  1. Monk, 30 Arduino Projects for the Evil Genius, 978-0-07-174133-0

Start from beginning but moves very quickly. Fun projects.

(4)KITS

  1. Arduino Starter Kit: $99.90
  2. SparkFun Inventor’s Kit : $99.95
  3. Autodesk’s 123d circuits – Arduino Basic Kit: $84.00

(5)Other resources

  1. Free web-based circuit simulator (with arduino sketch and simple debugger):
  2. Fritzing (version 0.9.2b, in beta) – for drawing circuits, schematics, can order PCB board made from Fritzing sketch:

(6)Where to buy parts:

  1. Adafruit:
  2. Sparkfun:
  3. Amazon:
  4. Ebay:
  5. Jameco:
  6. Digi-key:
  7. Mouser:

(7)This handout:http://emunix.emich.edu/~haynes/Papers/MACUL2016/

Workshop help: Using a Mac

Turn on computer and login / Follow verbal instructions
Logoff and turn off computer / Follow verbal instructions
Open an application / Finder | Application | ... | Double Left Click
Close a document / File | Close or close window with click on red radio button left side of title bar
Quit an application / File | Quit
Select text / Left click - hold - drag - release
Copy text to clipboard / Right click | copy (command-c)
Delete text / Right click | cut (command-x)
Paste text / Right click | paste (command-v)
Undo / command-z
Force quit / Apple symbol (top menu bar, far left) | Force Quit | Choose app

THE ARDUINO

SAFE HANDLING:

(1) Disconnect Arduino from power (computer) while breadboarding.

(2) Components (LEDs, resistors, …) are cheap. If one starts smoking, cut power, throw away the damaged component, check the wiring diagram carefully before repeating.

(3) Arduino is approximate $20. You don’t want to fry it. The most common way to ruin the Arduino is to accidently create a short circuit between power (+5V or 3.3V) and GND. This could happen on the breadboard or with Arduino connections. Supply power after you have carefully checked wiring.

All 13 digital pins and all 6 analog pins (A0 – A5) will supply power.

Note: On the Arduino, GND is right next to pin 13, and GND is right next to +5V!

Activity 1Setup: Blinkand C code modifications: No Breadboard!

(1) Connect the Arduino to the computer, set up, and run Blink.

See The instructions are replicated here:

Set up:

(1) Plug printer cable to computer’s USB port and to Arduino’s printer port.

 notice the power ON light on Arduino board

(2) Launch Arduino app

(3) Select board (default is ok) from Tools > Board > Arduino Uno

(4) Select serial port from Tools > Serial Port > /dev/cu.usbmodemFA1311

The port will almost certainly be named something different, but using ‘usbmodem’.

(5) Open LED Blink example: In File > Examples > 0.1 Basics > Blink

(6) Verify (compile) the Blink program (Check icon upper left of Sketch menu). Repair any errors before proceeding!

(7) Upload the Blink program (will automatically compile if necessary) (Right arrow icon upper left of Sketch menu). Repair any errors before proceeding – this may require:

a new upload, or

disconnecting Arduino from power, restoring power, then upload.

(8) Observe blinking yellow LED (labeled L, next to GND and pin 13).

(2) Online description of Blink:

(3) We walk through the Blink code.

/*

Blink

Turns on an LED on for one second, then off for one second, repeatedly.

Most Arduinos have an on-board LED you can control. On the Uno and

Leonardo, it is attached to digital pin 13. If you're unsure what

pin the on-board LED is connected to on your Arduino model, check

the documentation at

This example code is in the public domain.

modified 8 May 2014

by Scott Fitzgerald

*/

// the setup function runs once when you press reset or power the board

void setup() {

pinMode(13, OUTPUT);// configure pin 13 as an output

}

// the loop function runs over and over again forever

void loop() {

digitalWrite(13, HIGH); // turn the LED on (voltage HIGH)

delay(1000); // wait for a second

digitalWrite(13, LOW); // turn the LED off (voltage LOW)

delay(1000); // wait for a second

}

(3) Do this: Modify Blink to turn LED on for 2 seconds, then off for ½ second.

Open new sketch: File > New

Repeat as required:// program development cycle

Edit the sketch.

Verify, fix any errors

Upload, observe behavior

Compare my solution to yours:

/*

Blink

This example code is in the public domain.

modified 8 May 2014

by Scott Fitzgerald

modified: smh on 2/12/2016

*/

void setup() {

// initialize digital pin 13 as an output.

pinMode(13, OUTPUT);

}

void loop() {

digitalWrite(13, HIGH); // LED on (voltage HIGH)

delay(2000); // wait for two seconds

digitalWrite(13, LOW); // LED off (voltage LOW)

delay(500); // wait 1/2 second

}

(4) Modify Blink to have LED flash ‘SOS’ in Morse code ( . . . - - - . . .)

(1) Open new sketch.

(2) Repeat program development cycle to flash ‘S’ – debug this

(3) Repeat program development cycle to flash ‘S O’ – debug this

(hint: Copy and paste ‘S’ code, then modify it to make ‘O’ code)

(4) Repeat program development cycle to flash ‘S O S’

Compare my solution to yours:

/* Blink

This example code is in the public domain.

modified 8 May 2014

by Scott Fitzgerald

modified: smh on 2/12/2016

*/

void setup() {

// initialize digital pin 13 as an output.

pinMode(13, OUTPUT);

}

void loop() {

// flash . . . ‘S’

digitalWrite(13, HIGH);

delay(100);

digitalWrite(13, LOW);

delay(500);

digitalWrite(13, HIGH);

delay(100);

digitalWrite(13, LOW);

delay(500);

digitalWrite(13, HIGH);

delay(100);

digitalWrite(13, LOW);

delay(1500); // inter-char pause

// flash - - - ‘O’

digitalWrite(13, HIGH);

delay(1000);

digitalWrite(13, LOW);

delay(500);

digitalWrite(13, HIGH);

delay(1000);

digitalWrite(13, LOW);

delay(500);

digitalWrite(13, HIGH);

delay(1000);

digitalWrite(13, LOW);

delay(1500); // inter-char pause

// flash . . .

digitalWrite(13, HIGH);

delay(100);

digitalWrite(13, LOW);

delay(500);

digitalWrite(13, HIGH);

delay(100);

digitalWrite(13, LOW);

delay(500);

digitalWrite(13, HIGH);

delay(100);

digitalWrite(13, LOW);

delay(1500); // inter-char pause

}

You had to make some decisions:

length of dot (300 ms),

length of dash (1000 ms) ,

length of space between dot and/or dish (intra-character – 500 ms), and

length of space between characters (inter-character – 1500ms).

(5) There are two characteristics of the code given in #4 that are highly offensive to code developers:

(a) The magic numbers 300, 500, 1000 and 1500. We hate magic numbers.

(b) The replication of the code to flash – the code to flash ‘S’ is repeated verbatim twice.

While copy and paste makes it easy to generate code, it is too verbose for a human reader.

The fix for (a) is to make variables that hold the values of the magic numbers. Give the variables names that have meaning for a human reader.

The fix for (b) is to put duplicated code in a function. Then call the function when you want to execute that code.

These “fixes” are not to repair broken code. The code is not broken. However, the code is inelegant and unnecessarily difficult for a human to read. If it is difficult to read, then it is difficult to maintain, extend, modify.

Here is my improved code. :

/*Blink

This example code is in the public domain.

modified 8 May 2014

by Scott Fitzgerald

modified: smh on 2/12/2016

*/

int dotOn = 100;

int dashOn = 1000;

int intraChar = 500;

int interChar = 1500;

void setup() {

// initialize digital pin 13 as an output.

pinMode(13, OUTPUT);

}

void loop() {

flashS();

delay(interChar); // inter-char pause

flashO();

delay(interChar); // inter-char pause

flashS();

delay(interChar); // inter-char pause

}

// function to flash ‘S’

void flashS() {

// flash . . .

digitalWrite(13, HIGH);

delay(dotOn);

digitalWrite(13, LOW);

delay(intraChar);

digitalWrite(13, HIGH);

delay(dotOn);

digitalWrite(13, LOW);

delay(intraChar);

digitalWrite(13, HIGH);

delay(dotOn);

digitalWrite(13, LOW);

}

// function to flash ‘O’

void flashS() {

// flash - - -

digitalWrite(13, HIGH);

delay(dashOn);

digitalWrite(13, LOW);

delay(intraChar);

digitalWrite(13, HIGH);

delay(dashOn);

digitalWrite(13, LOW);

delay(intraChar);

digitalWrite(13, HIGH);

delay(dashOn);

digitalWrite(13, LOW);

}

Activity 2Breadboarding (using Blink)

This image shows wiring up the breadboard for Blink. I will be using the column/row specifiers on your breadboard, not those on the breadboard in the picture.

Orient your breadboard so the blue ‘-‘ row is along the top.

The board has grids of holes. You will plug components and wires into the holes.

In the above picture, there are two long rows of holes running along the upper edge and two long rows along the lower edge. These long rows are supply rails.These do not have to be used; when they are used, wiring is usually simplified. Each rail has all its holes electronically connected (under the plastic). If you supply power to any hole in one rail, all other holes in that rail also have power; if you connect ground to any hole in one rail, all other holes in the rail are connected to ground.

In the above picture, the bottom blue ‘-‘ rail is connected to GND (ground) on the Arduino. There is a wire from bottom ground rail to top ground rail. See the second black wire that goes from GND on the Arduino to the ground supply rail on the breadboard. Both of the blue ‘-‘ rails are now connected to GND.

In the middle of the breadboard, from left to right, there is a gutter. Above and below the gutter are short columns of 5 holes each. Each hole in a short column is electronically connected to the other 4 holes: all holes with the same integer label are connected. On your breadboard, e.g., holes a4, b4, c4, d4, and e4 are all connected. The holes above the gutter and below the gutter are not connected.

(0)Disconnect Arduino from computer.

(1) Take out your materials:

  1. 1 LED
  2. 1 330 Ω resistor (orange, orange, brown)
  3. wires: 2 black, 1 red, 1 gree

(2) Set up the board for general use:

Connect the two power rails,

Connect the two ground rails.

Do not connect to the Arduino yet (this will be the last thing you do before supplying power to the Arduino).

(3) Install the LED. The short leg of the LED goes in hole g4, the long leg goes in hole g5. Orientation of LED matters.

(4) Install the resistor. One lead goes in the GND rail. The other lead goes in hole i4. Orientation of resistor does not matter.

(5)Observe the LED and the resistor. All five holes on row 4 are electronically connected: the LED short leg is connected to the resistor; the other end of the resistor is connected to GND.

(6) Connect the signal (power) to the LED. The green wire goes from digital pin 13 on the Arduino to hole e5: the long leg of LED is connected to pin 13.

(7) Connect the board to GND. The black wire goes from ground rail to pin GND on Arduino (there are 3 GND pins on Arduino).

To run Blinky on this board, first carefully check the connections.

Componentholehole

LEDg5 (+)g4 (-)

330Ω resistori4GND

green wireg5pin 13

(8) Then plug Arduino back into the computer. You should not upload a fresh copy of Blink – it is already on-board!

You are running the same code as in Activity 1 (Blink – the original), the on-board LED should blink in unison with the breadboard

Blink code is duplicated here:

/*

Blink

Turns on an LED on for one second, then off for one second,

repeatedly.

Most Arduinos have an on-board LED you can control. On the Uno and

Leonardo, it is attached to digital pin 13. If you're unsure what

pin the on-board LED is connected to on your Arduino model, check

the documentation at

This example code is in the public domain.

modified 8 May 2014

by Scott Fitzgerald

*/

// the setup function runs once when you press reset or power the board

void setup() {

pinMode(13, OUTPUT);// configure pin 13 as an output

}

// the loop function runs over and over again forever

void loop() {

digitalWrite(13, HIGH); // turn the LED on (voltage HIGH)

delay(1000); // wait for a second

digitalWrite(13, LOW); // turn the LED off (voltage LOW)

delay(1000); // wait for a second

}

While running, test the connections! Unplug the specified wire, observe effect, then plug back in.

Wire to pin 13 on Arduino?LED should stop blinking. Why?

Wire to GND on Arduino?LED should stop blinking. Why?

There is a serial connection between GND, through LED, through resistor, and back to D13 (“digital pin 13).

What is going on here?

When the voltage is HIGH on D13, then current can flow from power (D13, through the LED, then resistor to GND). When the voltage is LOW on D13, then there is no current flow – the LED will not light up.

Activity 3: Many LEDs -- KnightRider

This code is slightly modified from

Similar code can be found in

(1) Wire up breadboard, with three sets of LED + resistor on the bread board. Connect the long leg of one LED pin2, one to pin3, one to pin 4.

(1)Verify and upload KnightRider1. Observe the pattern of on/off for the LEDs.

(2)Walk through the first version of the code: KnightRider1

/* Knight Rider 1

* ------

*

* Basically an extension of Blink_LED.

*

* (cleft) 2005 K3, Malmo University

* @author: David Cuartielles

* @hardware: David Cuartielles, Aaron Hallborg

* slightly modifed: smh 3/6/2016

*/

int pin2 = 2;

int pin3 = 3;

int pin4 = 4;

int timer = 200;

void setup(){

pinMode(pin2, OUTPUT);

pinMode(pin3, OUTPUT);

pinMode(pin4, OUTPUT);

}

void loop() {

digitalWrite(pin2, HIGH);

delay(timer);

digitalWrite(pin2, LOW);

delay(timer);

digitalWrite(pin3, HIGH);

delay(timer);

digitalWrite(pin3, LOW);

delay(timer);

digitalWrite(pin4, HIGH);

delay(timer);

digitalWrite(pin4, LOW);

delay(timer);

digitalWrite(pin4, HIGH);

delay(timer);

digitalWrite(pin4, LOW);

delay(timer);

digitalWrite(pin3, HIGH);

delay(timer);

digitalWrite(pin3, LOW);

delay(timer);

digitalWrite(pin2, HIGH);

delay(timer);

digitalWrite(pin2, LOW);

delay(timer);

delay(timer * 10);

}

(3)The code is wordy, which makes it ugly. The next version uses a 1D array to make the code more readable. The breadboard does not change.

Verify and upload KnightRider2. Observe the behavior of the LEDs.

This code is prettier – it is easier to read – because it uses a 1D array.

/* KnightRider2

* ------

*

* Blink LEDs on/off in order (pin 2, pin 3, pin 4)

* Reducing the amount of code using for(;;).

*

*

* (cleft) 2005 K3, Malmo University

* @author: David Cuartielles

* @hardware: David Cuartielles, Aaron Hallborg

* slightly modified: smh 3/6/2016

*/

int pinArray[] = {2, 3, 4};

int count = 0;

int timer = 200;

void setup(){

// we make all the declarations at once

for (count=0; count < 3; count++) {

pinMode(pinArray[count], OUTPUT);

}

}

void loop() {

//

for (count = 0; count < 3;count++) {

digitalWrite(pinArray[count], HIGH);

delay(timer);

digitalWrite(pinArray[count], LOW);

delay(timer);

}

for (count = 2; count >= 0;count--) {

digitalWrite(pinArray[count], HIGH);

delay(timer);

digitalWrite(pinArray[count], LOW);

delay(timer);

}

delay(timer * 10);

}

(4)Now, let’s change the code so the LED will turn on and off in a specified (hard-coded) order. We use a second 1D array to specify the order (two 1D arrays all together).

This is good place to point out that it is easy to change software as compared to changing hardware – that fact is very significant: the breadboard does not change.

/* Knight Rider DANCE!

* ------

*

* Blink LEDs on/off in hardcoded order

*

*

* (cleft) 2005 K3, Malmo University

* @author: David Cuartielles

* @hardware: David Cuartielles, Aaron Hallborg

* modified: smh 3/6/2016

*/

int pinArray[] = {2, 3, 4};// used to configure

int danceArray[] = {2, 3, 2, 4, 4, 2, 3};// dance sequence

int pix = 0;// index to pinArray

int dix = 0;// index to danceArray

int timer = 200;

void setup(){

// we make all the declarations at once

for (pix = 0; pix < 3; pix++) {

pinMode(pinArray[pix], OUTPUT);

}

}

void loop() {

//

for (dix = 0; dix < 7; dix++) {

digitalWrite(danceArray[dix], HIGH);

delay(timer);

digitalWrite(danceArray[dix], LOW);

delay(timer);

}

delay(timer * 10);

}

(5) Try these changes (no solutions given, but we can do this together later, given time)

-Wire up another 1 or 2 LEDs and modify KnightRider2 to use them.

-Change the hardcoded dance sequence in KnightRiderDance.

-Given a hardcoded dance sequence (eg [2, 4, 3, 3]) modify the loop() code so each LED is controlled on/off twice in a stutter: on short, off, on long, short.

Activity 4: FADE – using PWM to effect analog output

Online description of FADE:

(1)Wire-up Fade breadboard. N.B. You do not have to remove components already present! You can just add another LED/resistor pair, or you disconnect the unused LEDs from Arduino and hook up the signal wire from pin 9 to the LED. We will not be driving pin2, pin3 pin4 from the KnightRider activity in software.