Class 2

Key : 🔹 discussion     🔸 lecture        demo/activity      🍕 lunch 

  Arduino 101

Intro to Hardware

  • what is an Arduino?
  • different kinds of Arduinos

Terminology

Intro to the software

Digital Input and Output

  • blink
  • button (how to make a soft switch)

🍕

🔹 Get into groups for Ability Now project and discuss case situations

🔹 Prep for “observe and interview” field trip to Ability Now

 

 

Arduino 101

Arduino is an open software and hardware platform that has excellent tutorials, support and resources. Built for prototyping interactive projects with endless possibilities.

Getting Started

Download Arduino IDE

FTDI Drivers for the FT232RL Chip

Arduino boards communicate with your computer through a USB port. You also program the Arduino board through the USB port. Not all Arduinos are the same and use the same method to talk to your computers. Some use a FT232RL chip which requires you to download a driver. The FT232RL chip is created by a company called Future Technology Devices International so the driver is called an FTDI driver. The FT232RL chip converts USB signals to UART signals (also known as serial).

If you are using a LilyPad Simple or LilyPad Main board you will need drivers. Download the FTDI driver here. Click the links below based on your computer’s operating system to follow Sparkfun‘s excellent tutorials.

Windows     Mac

If you are using an Arduino Uno with a Windows machine you will need additional drivers. Install drivers 

 

Intro to Hardware

There are many ways that Arduino compatible boards are packaged. Here are some that will be used in class.

Components of the ProtoSnap Board

DevBoard_a

LilyPad Simple Board

 

lilyPadSimple_a

Parts of the LilyPad Simple board

 

LilyPad Programer FTDI Breakout Board

Additional hardware needed for the LilyPad Simple and Main boards.

LilyPadThumbs-10

 

LilyPad USB

lilypadUSB

9 digital : Pins 2, 3, 9, 10, 11, A2/12, A3/13, A4/14 and A5/15
4 PWM  : Pins 3, 9, 10 and 11
4 Analog Inputs : A2 – A5

**reference LilyPad Arduino website

A pinout diagram for the LilyPad USB, what you will be using in class.

Print

9 Digital : Pins 2, 3, 9, 10, 11, A2, A3, A4 and A5
4 PWM/Analog Output : 3, 9, 10, 11
4 Analog Inputs : A2, A3, A4 and A5

Terms we will be using a lot:
Digital
Analog
Input
Output
PWM

Let’s explore what they mean as they apply to a microcontroller ——>
Digital Input – When you want to sense something in the world, sometimes the only thing you need to know if whether something is true or false. Did the cat hit the toy? Did I clap my hands? Did the door open? These are yes or no questions, binary in logic. Digital inputs are read through a pin and this yes or no (0 or 1) can then be read and used to trigger other events.

Digital Output – Much like how a digital input can sense an action with two states, a digital output can control an action with two states. It can turn on and off something, like a LED.

Analog Input – Sometimes reading two states is not enough, there needs to be some shades of gray between the black and the white.  Instead of knowing whether the arm is straight or bent, you can know to what degree it is bent. An analog input on a microcontroller is an input pin that can read variable voltage, typically 0V to 3.3V or 5V.

Analog Out – Analog output can control a component to varying degrees, rather than only 2 states. Think of a light switch being digital on/off and a fader knob as analog. Not only can you switch the light on or off, you can change it’s brightness with analog output.

PWM – This stands for Pulse Width Modulation. This is how a microcontroller, which can really only output digital, tricks us into thinking that it’s outputting analog values. PWM pins swing 0V (off) to it’s highest, let’s say 5V, (on). This on/off pulse creates a square wave. Look for the digital pins on the Arduino board that have a ‘~’ by them, this means they are PWM pins and can be programmed to be analog out.

**references

ITP Physical Computing Lab

Arduino PWM

 

Intro to Software

Once you have Arduino and the FTDI drivers installed, plug your FTDI breakout board into your LilyPad and the USB cable into the board and into your computer.

Two things need to happen before you can upload a sketch to the board.

  1. Tell Arduino what kind of board you have.
    Tools > Board > LilyPad Arduino w/ATmega 328 (or whatever board you are using)
  2. Choose the USB to serial port that you computer and board will talk to each other through.
    Tools > Serial Port > /dev/tty.usbserial-*******
    In Windows, it will be listed as COM*

If nothing is showing up under Serial Port, it means the FTDI drivers did not install properly, close down Arduino, install and reopen.

board 

 

 

Use Example Sketches!

Arduino has a bunch of built-in example sketches to get you started. Sketches are programs that you upload to the Arduino to perform a specific task. They are open-source and can be altered and built upon to your liking. The language of Arduino is nicely wrapped C/C++, this means all C/C++ syntax and constructs will work when writing an Arduino sketch.

** Arduino’s FAQ Page

To get to an example sketch go to File > Examples.

There is a long list to choose from. This where we will be working from so we get familiar with Arduino’s environment.

 

Digital Output

Materials

  • microcontroller
  • alligator leads
  • LED
  • USB cord

Build Circuit

Hook up LED to pin 3 using alligator clips.

IMG_9687

Open Arduino Software

Once you open the Arduino software a window will pop up.

This is a sketch window. Copy and paste the code below into a sketch window and save.

Change LED pin number from 13 -> 3 (or whatever pin you put the LED on)

Screen Shot 2016-01-21 at 11.14.20 AM

Change comment to reflect pin change. Get used to commenting code! You and others will be thankful for it when trying to figure out what a sketch is programmed to do later on.

Screen Shot 2016-01-21 at 11.15.31 AM

 

Upload Sketch

Blinky success!

Variables

Screen Shot 2016-01-21 at 11.26.36 AM

The number 3 is used a few times in our blinky sketch, what if we wanted to change the pin number later on? We would have to change all 3. The number 3 isn’t very descriptive either, we could be using 3 later on in a simple mathematical operation and it could get confusing. In this sketch, 3 is our LED pin, what if we could just name it that and make it easier to change later on. We can if we create a variable!

A variable can be thought of as a cup that a piece of data is stored in. A name can be written on the outside of the bucket, which we can then use to reference the piece of data it contains. Variables also tell the computer what kind of data it is dealing with, so it can act accordingly.

A variable has a type, name and value.

int is short for integer, which is the type of data.

ledPin is the variable’s name we gave it, make it descriptive!

= is how we assign the name a piece of data.

3 is the data we are assigning to the name.

There is another piece of code that needs to be used when declaring the ledPin variable.

const gets put in from of the variable when you know that the value will not be changed later on. This makes the data read-only. 

Exercise! Hook up a second LED and get that blinking. You will need to:

  1. create a variable for the second LED you need to name it something unique

**reference
const keyword
Arduino variables

IMG_9700

Try hooking up other components, like a vibe board.

 

Digital Input

Materials

  • microcontroller
  • alligator leads
  • switch (handmade)
  • USB cord

Build Circuit

To read digital input, we need a digital device. Let’s make a soft switch! If the link does not work make a free account with Instructables and click on it again.  Hook up one side of the switch to pin 2 and the other side to the ground (-) petal.

IMG_0094

In Arduino Software

Navigate to File > Examples > 01. Basics >  DigitalReadSerial example. Copy, paste and save.

Screen Shot 2016-01-21

A digital device is something that is either read as on/off, high or low, full voltage (5V) or none (0V). For Arduino to read whether something is high or low on a digital pin, it uses the function digitalRead().

DigitalRead() takes one parameter, which goes inside the parenthesis. This parameter is the pin number you want to read the device on. In this case, it’s going to be digital pin 2, which assigned to the variable pushButton.

 

 

Before moving on, take a look at the sketch where it says:

Serial.begin() opens the serial port which is essential to debugging. Debugging simply means the process of finding out what is wrong (finding the bugs) when the circuit and software are not behaving as you intend it to.  The microcontroller communicates with your computer over the USB port using serial communication, also known as UART. You can print out serial data and the results of your program when you open the serial port. To print out data use Serial.print() or Serial.println() which adds a carriage return after it prints.

Let’s see this in action. Open up the serial monitor.

Screen Shot 2016-01-21 at 12.59.54 PM

When opened you will see a bunch of 0s and 1s being printed out. What you want is either a 1 when the switch is pressed or a 0 clearly showing two different states that happen when the switch is either closed or open. Instead, there is no correlation to a press or release they 0s and 1s just come and go as they please. This is called a floating pin and needs to be fixed. When the switch is open, it doesn’t know what value to reference, the pin is left there hanging in the wind. A circuit needs to be closed or connected to power or ground somehow, so it always has a voltage reference. Right now, it doesn’t have one. What will help this situation and how do we give it a reference?

This happens because when open or closed the switch needs to be connected to power or ground. Right now when the switch is open, it is not connected to either one so doesn’t know what voltage to reference (oV or 5V). The pin is left there hanging in the wind. What will help this situation and how do we give it a voltage reference?

Build Circuit

There are two solutions to the problem of a floating switch pin: a pull-up or pull-down resistor. Let’s start with a pull-down resistor. Grab a 10K Ohm resistor and hook up the circuit below using a breadboard or alligator clips to connect your components with.

pullDownResistor-01

The 10K ohm resistor is connected between digital pin 6 and ground. When the switch is open, it pulls the voltage down to 0V, hence its name pull-down resistor. This means we will see a 0 in our serial monitor when the switch is open.

When the switch is closed, it swings to high to 5 volts that comes from the power pin on the arduino board. This will print a 1 in the serial monitor.

Now, let’s use a pull-up resistor. This is what the circuit looks like if we want the circuit to be pulled up to 5 volts. This is what a pull-up resistor circuit looks like. But wait! No need to add a pull-up resistor to your circuit your Arduino board has one built in. 

pullUpResistor-01

The LilyPad Arduino and almost every Arduino board have built-in pull-ups that can be enabled through software. This brings you back to the original circuit and simplifies it by taking out the need for an external resistor.  This is really helpful when you have lots of switches and want fewer wires and components to build and connect. Hook up the original circuit again by removing the resistor and connecting one side of the switch to ground and the other to pin 2. 

To enable the internal pull-up resistor on the Arduino change this piece of the sketch from:

to

Upload Sketch

Now when you open the serial window you will get a 1 for when it is open and a 0 for when the switch is closed. 

Screen Shot 2016-01-21 at 1.06.03 PM

Combining Input and Output

Let’s program the switch to turn the LED on and off!

Build Circuit

switchLEDlilyPad-01

In Arduino Software

Navigate to Examples > Digital > Button

Change pin 13 to the pin number your LED is on.

Open Serial Port and Print Button State

We want to read the serial data coming from the board to know whether our button is working properly. Enable serial port by add this line in setup():

and this line in loop():

Enable Internal Pull-up Resistor

Change:

to:

Upload Sketch

LED will be on when switch is open and off when closed.

 

Exercise: Adjust code to make it off when open and on with closed.