Prototyping with Micro-controllers, Sensors, and Materials

From EG1004 Lab Manual
Revision as of 21:34, 5 July 2018 by Kdunnigan (talk | contribs) (Created page with "= Objectives = The first objective of this lab is to utilize the basics of electronics, the Arduino board, and the Arduino IDE (Integrated Development Environment). The Arduin...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Objectives

The first objective of this lab is to utilize the basics of electronics, the Arduino board, and the Arduino IDE (Integrated Development Environment). The Arduino IDE will be used to program the Arduino board. These skills will be used for several hands-on tasks including programming the Arduino to control an LED with a button, take temperature readings, and design a basic prototype for a product.

The prototyping will focus on designing and building a thermal insulation device that utilizes a variety of materials, this design will be tested and the resulting data analyzed. Students will be able to see how arduino can be applied. All designs will be tested with a heated object. The team with the lowest Product Design Ratio (PDR) will be declared the winner.

The Arduino IDE

The Arduino IDE is a program that can be used to edit, compile, and upload code to a supported microcontroller. Figure 2 is a screenshot of the program:

Figure 2: Arduino Software Sketch

Verify: checks code for errors and points to where the errors occurred after it finishes. Upload: verifies code and then uploads it to the Arduino board if there are no errors.
Console: shows any errors the software found in hardware.
Serial Monitor: a tool used to see how a program is running. It’s like a multimeter for the program. Programs written in Arduino are called sketches. A basic sketch can be broken up into 3 different areas: global, setup and loop; these areas are pictured below.

Sketchareas.jpg

Global: constants and imported libraries go here.
Setup: activate the pins and sensors used. This code only runs once.
Loop: the code that runs continuously such as reading sensors and turning pins HIGH or LOW.

Arduino Programming

The Arduino programming language is based on C/C++, but it is designed to be simpler and easier to learn. The most intuitive way to think about programming is like building with LEGO blocks: certain rules must be followed and different building blocks can be used to build bigger parts. General

  • Every line must either end with a semicolon ‘;’ unless it’s a conditional, loop, or function
  • Comments start with a //
    • Comments are text that the program ignores
    • Used to label and explain code

Datatypes Datatypes are the different kinds of data values that can be used, manipulated and stored using C++. The table below includes the most basic and widely used datatypes.

Datatype What it stores (examples) Default value Notes
Boolean A true value (1, TRUE, HIGH) or
a false value (0, FALSE, LOW)
0, FALSE, LOW -
int An integer number (-5, 15, 1047, etc.) 0 Can be positive or negative
double A decimal number (-0.5, 123.77, etc.) 0 Can be positive or negative
char A single character (‘c’, ‘A’, ‘5’, ‘?’, etc.) Indeterminate Must be enclosed in single quotes
string A sequence of characters (“Hello World!”,
“10”, “157+5”, etc.)
Empty (“”) Must be enclosed in double quotes

Operators Operators perform operations on variables and constants. The results of these operations are usually stored in a variable. The table below includes common operators.

Operator What it does Notes
= Assigns a value to a variable
+ Adds two or more values
- Subtracts two or more values
* Multiplies two or more values
/ Divides two or more values
++ Increment by 1 Usually used in loops
-- Decrement by 1 Usually used in loops
== Checks if two value are equal Usually used in conditionals
!= Checks if two value are not equal Usually used in conditionals
> or < Less than/Greater than comparison Usually used in conditionals
<= or >= Less than/greater than or equal to comparison Usually used in conditionals
&& or || Boolean AND or Boolean OR Used to cascade multiple Boolean operations Usually used in conditionals

Constants and Variables Constants and variables hold data according to their datatype. They need to be given a name so they can be referred to later. Constants hold data that will NOT change while a program is running. Constants usually contain pin numbers or sensor threshold values. Variables contain data that WILL change while a program is running. Variables usually contain sensor values and other values that need to have mathematical operations done on them. Below is an example of how to create different constants and variables.

Variables.png

Conditional Statements Conditional statements run code enclosed by their curly brackets when a condition is met.

Conditionals.jpg

Loops Loops run the code enclosed by their curly brackets a specific amount of times or until a condition is met. While-loop While-loops are used to perform a task until a condition is met For-loop For-loops are used when you want something to run a specific number of times. Although they seem complicated at first, the structure of most for-loops is the same. In the parenthesis, the first part sets a variable (usually ‘i’ for ‘index’) to a value used to begin a count, the middle is the condition when the loop stops, and the third part is where you increment or decrement the counting variable.

While.jpg

Commonly Used Arduino Functions

Function What it does
pinMode(pin,mode) Sets a pin as an input or output
digitalWrite(pin, value) Sets a digital output pin to HIGH or LOW
digitalRead(pin) Reads an digital input pin as HIGH or LOW
analogWrite(pin, value) Sets an analog output pin to a value 0-1023
analogRead(pin) Reads an analog output pin as a value 0-1023
delay(milliseconds) Pauses the program for a certain amount of time
Serial.print(value) Prints the value (variable) to the Serial Monitor.

Design Considerations

Carefully consider the inconsistencies of the sensors that are used in the lab and the needs of a reliable product.

  • What is the accuracy and precision of the temperature sensor?
  • How could this sensor be incorporated into a product?

Materials and Equipment

  • Arduino UNO microcontroller and USB cable
  • Computer with Arduino IDE
  • Breadboard and jumper wire
  • Resistors
    • 220 Ohms
    • 2.2k Ohms
  • LED
  • Pushbutton
  • TMP36

Procedure

Starting a new sketch in Arduino

  1. Open the Arduino IDE
  2. Plug the Arduino/RedBoard into the computer
  3. In the Arduino IDE toolbar, go to Tools > Board and select “Arduino/Genuino Uno”
  4. Also in the Tools toolbar, select the correct port

Building Circuits on a Breadboard

Breadboards are small boards that are commonly used for circuit prototyping. They allow the connection of components that were discussed previously without making permanent connections. Change circuit to simpler circuit and HAVE students follow along to understand the concepts of: Which rows are powered How the break in rows stop current (needed for the button)

Figure 1: Breadboard Connections

As seen in the Figure 1, the internal connections of a breadboard are very specific. Sections A and D depict how the power rails stretch the length of the board. This is where the 5V and GND connections from the Arduino can be used to power the circuits. Sections B and C are where circuits are built. Below is an example of how to wire a breadboard from a schematic. Do NOT build this circuit, just follow along. See the lab 5 NI ELVIS Tutorial Video on the EG manual for more information on how to use breadboards.

Activity 1: Building an LED Circuit

  1. The first activity will be making a simple LED blinking circuit. The programming flowchart and circuit diagram are in Figure 3.
    Figure 3: LED Circuit Diagram and Flowchart
  2. First, wire the LED to the breadboard like in Figure 4. Remember, since LEDs are polarized, their orientation matters. The shorter leg of the LED should be connected to the same row as GND. The resistor is NECESSARY, otherwise too much current would flow and the LED will burn out! Make sure to wire power to the power (red) rail and the signal from 7 to the LED directly
    Figure 4: Arduino Wiring
  3. Now type the following code into a new sketch.
    LED code.jpg
    needs to define type - Marcus make screenshot
  4. The flowchart uses Digital Pin 7 on the Arduino as an output. Therefore, create a constant that holds the number 7 and in the setup area set Pin 7 as an output using pinMode. Then, turn the LED on by using digitalWrite, have a delay of one second, turn the LED off by using digitalWrite and then set another delay of one second.
  5. The LED circuit will be used in the next two activities. Do not deconstruct it.

Activity 2: Using a Button

  1. Activity 2 adds a button to the circuit from activity 1 and requires conditionals (think if-statement). The LED should be on when the button is pressed and off when the button isn’t pressed.
  2. Before you breadboard the circuit look at the bottom side (pin side) of the button to examine which pins are connected.

There is a line connecting the pins that are wired together internally on each side. See the schematic below, pins 1 and 2 are connected, and pins 3 and 4 are connected. Make sure the button straddles the break in the breadboard

  1. First, breadboard the circuit diagram in Figure 5 and sketch a flowchart of the program needed. Have a TA verify the flowchart.
    Figure 5: Button Circuit Diagram
  2. Write the Arduino program to implement the flowchart. Use an if statement
  3. Remember to create a constant that holds the pin number to which the button is connected and that the button will be a digital input. After the button constant, create an integer that will hold the button state:
    Button read.jpg
  4. Within the loop function, you must check the state of the button (whether it is pressed or not pressed), using the following code:
    Button state.jpg