Difference between revisions of "Kaylee Prototyping with Microcontrollers, Sensors, and Materials"

From EG1004 Lab Manual
Jump to: navigation, search
(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...")
 
Line 57: Line 57:
Pins 2-12: Digital pins that can be switched between HIGH states and LOW states<br />
Pins 2-12: Digital pins that can be switched between HIGH states and LOW states<br />
Pin 13: Connected to the on-board LED, use it only as an input pin
Pin 13: Connected to the on-board LED, use it only as an input pin
=== Activity 0: Building Circuits on a Breadboard ===
 
<b><i>Heat </i></b>is a form of energy. <b><i>Heat transfer</i></b> is thermal energy that is transferred
from one body to another as a result of a temperature difference. <b><i>Temperature </i></b>is the measure of
the average kinetic energy of atomic motion. The faster the atoms are moving, the higher the temperature.
In this lab, we will focus on the ways heat is transferred.
 
The specific mechanisms that convey energy from one location to another are
<b><i>conduction</i></b>, <b><i>convection</i></b>, and <b><i>radiation</i></b>. If you put
any two objects with different temperatures together, heat will be transferred from the object
with the higher temperature to the one with the lower temperature until the system reaches
equilibrium. Let's say you placed a hot bowl of soup on the kitchen table. What would happen?
What if you placed a glass of ice water on the same table? Our job is to understand
how this heat transfer occurs and how to slow the process down.
 
<p>When there is a temperature difference within a solid body or between two solid bodies in contact with each
other, energy (heat) will flow from the region of higher temperature to the region of lower temperature. This
is known as <b><i>conduction</i></b>. The formula to determine the conductivity in a rod of material is:</p>
 
<math>\frac{Q}{t} = k \frac{A \left ( T_{hot} - T_{cold} \right )}{d} \,</math>
 
<p>In this equation, <i>Q </i>is heat transferred in time <i>t</i>, k is the thermal conductivity
constant, <i>A </i>is area, <i>T </i>is temperature and <b><i>d </i></b>is the thickness of
the barrier. Therefore, increasing <i>d </i>or decreasing <i>A </i>would reduce conduction. </p>
 
<p>Imagine a metal rod that is heated at one end. The atoms of the rod collide at the point where
the temperature differs, transferring heat until the temperature of the rod becomes uniform.</p>
 
<p><b><i>Convection</i></b> is the transfer of heat within a fluid medium (fluids consist of gasses and liquids).
Convection can occur in two different ways. The first way is known as <b><i>natural convection</i></b>. Natural
convection occurs when the main mechanism for heat transfer is due to the temperature difference either within
the fluid or at its boundaries. This difference in temperature causes the molecules in the higher temperature
region to expand and rise due to buoyant forces. See Figure 1 for a diagram of natural convection. The second
way in which convection occurs is called <b><i>forced convection</i></b>. Forced convection occurs when the main
mechanism for heat transfer is due to the forced flow or motion of the fluid.</p>
 
[[Image:lab_hegg_2.jpg|frame|center|Figure 1: Air circulation diagram]]
 
<p>Today we heat our homes through radiators using natural convection. Air flows through a heating element and
then is dispersed through the room. Would it be helpful to put bigger fins on the heating element? To decide, use
the formula for heat transferred through convection:</p>
 
<math>\frac{Q}{t} = hA \Delta T\,</math>
 
<p>In this equation, <i>Q </i>is the heat transferred in time <i>t</i>, &#8710;<i>T </i>is the
difference in temperature, <i>h </i>is the convection coefficient, and <i>A </i>is the
surface area.</p>
 
<p>Suppose you wanted to disperse the heat even faster. In that case you could blow a fan past the heating
elements.  This is forced convection; it is one of the principles used in air conditioners to have a relatively
small unit cool a large space.</p>
 
<p>According to the Encyclopedia Britannica, <b><i>radiation </i></b>is the process by which energy,
in the form of electromagnetic radiation, is emitted by a heated surface in all directions.
It travels to its point of absorption at the speed of light and does not require an intervening
medium to carry it. The heating of the Earth by the Sun is an example of heat transfer by radiation.
The formula for heat transferred through radiation:</p>
 
<math>\frac{Q}{t} = \varepsilon \sigma A \left ( T_s^4 - T_{sur}^4 \right )\,</math>
 
<p>In this equation, <i>Q </i>is the heat transferred in time <i>t</i>, <i>T<sub>s</sub> </i>is the
surface absolute temperature, <math>\varepsilon\,</math>is the constant of emissivity, <i>T<sub>sur</sub> </i>is
the surrounding absolute temperature, <i>A </i>is the surface area, and
&#963; is the Stefan-Boltzmann constant.</p>
 
<p>Electromagnetic radiation is a mean of energy transfer that occurs when an atom absorbs energy. As
we have seen earlier when heated atomic motion within a body increases. The energy absorbed by the
atom causes some of its electrons to move around. For sake of stability, the electrons eventually
come to its original state or position creating an electromagnetic wave. This EM wave can propagate
as heat, light, ultraviolet, or other electromagnetic waves depending on the type of atom and the
amount of energy absorbed.</p>
 
<p>Color is a property of light which is based on the frequency of the wave. When we see a specific
color, our eyes literally receive an electromagnetic wave of a specific frequency. Materials that
appear lighter (i.e. yellow) reflect more light (EM waves of a specific frequency) than darker ones.
Following the same logic, when an object appears white, it virtually reflect all the EM waves coming
to it while a "black" object absorbs it all.</p>
 
<p>Therefore, color (reflectivity) is to be considered when choosing materials for thermal
insulation. </p>
 
<p>How would you feel out in the sun on hot day in a black t-shirt? How would you feel if your
shirt were white? A black surface is an excellent absorber of heat, while a white surface is not.
Silver is a poor heat absorber and reflects radiated energy; that is why rooftops of apartment houses
are often painted silver.</p>
 
<p>Your container is a <b>thermodynamic system</b> – a part of the Universe separated from the
surroundings by an imaginary boundary. There are three types of systems: open systems, closed
systems and isolated systems. </p>
 
[[Image:lab_hegg_5.png|frame|center|Figure 2: Thermodynamic system with a boundary.]]
 
<p><b>Open systems</b> are systems where transfer of mass and heat is possible. For example, an open pot
of boiling water is an open system – it exchanges heat with the air around it and water vapor. If
collected, the water vapor can be condensed back to liquid water which has some mass.</p>
 
<p><b>Closed systems</b> are systems where only heat can be transferred to the surroundings. A
hermetically sealed bottle of soda is a closed system. If placed in a hot environment it absorbs
energy in form of heat but the amount of liquid within does not change.</p>
 
<p><b>Isolated systems</b> are systems which do not interact with the surroundings at all. No exchange
is possible - neither heat nor mass. An ideal Thermos® is an isolated system. If hot chocolate is poured
within, you will get the same amount back at the same temperature. No such system can be fabricated but
can be approximated.</p>
 
<p>Now that we understand how heat is transferred, we must consider how to slow it down. Understanding
how to minimize heat loss is the key to designing a successful insulating container.</p>
 
<p>The first consideration is the materials you choose. Using materials that are poor conductors of
heat will minimize heat loss. Glass is one of these. Fiberglass insulation uses spun glass for its
insulating effect.</p>
 
<p>Plastic is also a poor conductor of heat. Foam cups are made of plastic which has tiny air bubbles
suspended in it. Air is among the poorest conductors of heat. It turns out that a vacuum, or the
complete lack of air, is the best insulator of all. This is the principle employed in the Thermos
design.</p>
 
<p>The other important consideration in creating your container is its cost. Minimal design uses the
fewest resources while maintaining the safety and efficacy of your product. Engineers almost never have
the luxury of designing products or systems where cost is not a factor. Given unlimited resources,
you could most likely come up with a container that would maintain the egg's temperature almost
indefinitely, but no one could afford to buy it from you. Balancing cost and effectiveness into your
plans is a critical skill you will need later.</p>
 
<p> Another thing to consider is the method of data collection, because the way data is collected can cause many issues including providing incorrect data. To calculate changes in temperature, some devices that can be used are thermometers, thermistors, thermocouples, and RTDs. </p>
 
<p> <b><i> Thermocouples </i></b>  consist of two wires of dissimilar metals and are usually welded together or attached on a junction. When the temperature at a junction changes, it generates thermoelectric potential (emf) which is proportional to the temperature difference between the two junctions. This voltage difference can be sued to measure the temperature at that junction. </p>
 
<p> <b><i> Thermistors </i></b> are thermally sensitive resistors that display changes in electrical resistance when subjected to a temperature change. Thermistors, like a thermocouple, utilize resistance to read temperature but do not require two different types of metal wire connected at a junction. </p>
 
<p> <b><i> Thermometers </i></b> are devices used to measure temperature or a temperature gradient utilizing a temperature sensor and some other means to convert the physical change to a digital change. Most digital thermometers utilize thermistors or lasers to calculate temperature. </p>
 
<p> In this lab, a thermocouple, thermistor, and thermometer will be used to collect data. Each of the different devices utilizes different technologies to gather the temperature data from the egg. The thermocouple collects data through the NI-ELVIS II+ board as it's built into the board itself. The thermistor operates through an Arduino. The thermometer is operated manually. This is done to check the accuracy of different types of devices and sensors as well as to determine the efficiency in the data collection method.
 
= Activity 0: 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.  
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:
Change circuit to simpler circuit and HAVE students follow along to understand the concepts of:

Revision as of 23:24, 4 July 2018

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 Project Design Ratio (PDR) will be declared the winner.

Electricity

To put simply, electricity is the movement of electrons. Electrons flow through a conductive wire when there is a difference in charge between two points in the wire. This flow of electrons is called electrical current and it is measured in Amperes (A). Due to convention, electrical current flows opposite of the electrons. The difference in charge is called electrical voltage and it is measured in Volts (V). Another way to think about electrical voltage is to picture it as “electrical pressure,” analogous to water pressure. If there is a tank full of water (electrons) and a hole is poked in it, water will flow (electrical current flowing), due to the water pressure (electrical voltage) inside the tank. Finally, there are certain materials that resist that flow of electrons. This property is called electrical resistance and it is measured in Ohms (Ω). Resistors are electronic devices that are specifically designed to resist the flow of electrical current. There exists a mathematical relationship between current, voltage and resistance which is characterized by Ohm’s Law. This relationship is detailed below, where V is the voltage across a resistor, I is the current flowing through a resistor and R is the resistance of the resistor.

Electronic Components

There are several basic electronic components used to build simple circuits. Some of these components are polarized which means the way they are connected matters! Another way of thinking about it is that some components are symmetrical while others are not.

DC (Direct Current) Voltage Sources

DC Voltage Sources are used to power circuits because they have a voltage difference across their terminals. DC Power Sources are usually batteries (AA, AAA, etc). Arduino boards can be powered by a battery, a USB cable, or an AC adapter. When the Arduino is powered, it can be used as a 5V DC voltage source. They ARE polarized.

Resistors

Resistors are components that reduce the amount of current flowing through a circuit. Resistors convert the excess current to thermal energy. Resistors can be used to control the voltages and currents of circuits. Resistors are color coded with what resistance they are. They are NOT polarized. File:Resistor.jpg

Capacitors

Capacitors are components that can store energy in an electrical field and then dissipate it at a later time. Capacitance is a measure of how much charge a capacitor can store and it is measured in Farads (F). Capacitors resist voltage changes by supplying or drawing current. They are SOMETIMES polarized. Capacitors.jpg

Inductors

Inductors are components that can store energy in a magnetic field and then dissipate it at a later time. Inductance is a measure of how much energy an inductor can store and it is measured in Henrys (H). Inductors resist current changes by dropping or increasing the voltage across itself. They are NOT polarized. Inductor.jpg

Push-buttons and Switches

Push-buttons and switches are mechanical devices that interrupt or divert current running through them. Basic push-buttons and are polarized while basic switches are not. 375px

Diodes and Transistors (BJT/MOSFETS)

Diodes are components that allow current to only pass in one direction. MOSFETs are electric components that act as electrically controlled switches. They can also be used to amplify signals. They ARE polarized. Diode transistor.jpg

Light Emitting Diodes

LEDs are small electric lights which use low voltages and currents. The orientation of the LED is important since it acts like a diode and only allows current to flow in one direction. Most LEDS also require a resistor (typically 470 Ω) in series with them because they will burn out almost instantly when they encounter high current. They ARE polarized. LED.jpg

Microcontrollers

A microcontroller is a cheap, programmable computer without any of the peripherals such as a mouse, keyboard, or screen. Microcontroller boards have direct access to the input and output pins of its processing chip so that the user can directly read from sensors and perform actions. Microcontrollers are present in many electrical appliances like microwaves. Arduino boards were designed to be easily programmed and assembled into larger projects. These boards come in many shapes and sizes and some contain additional features such as WiFi or Bluetooth connectivity. Different boards can also have different features such as processing speed and memory size. Arduino.jpg This lab will be using an Arduino UNO board created by SparkFun called a RedBoard. Redboard.jpg

Arduino Hardware

Redboard info.jpg Reset Button: Restarts the Board
USB Connector: Provides power and connect it to the computer
Pin 13 LED: Usable LED without making an LED circuit
Serial LEDS: Shows if the Arduino is transmitting or receiving data from pins 0, 1 or the USB connection Redboard pins.jpg Power Pins
3.3V: Usually used to power low-voltage sensors
5V: Most common power pin used to power circuits
GND: Ground pin which is 0V
VIN: Voltage-In can be used to power the board using a battery Redboard IO.jpg I/O Pins
A0-A5: Identical analog pins that can be used to read sensors or control analog devices. Pins A0-A3 are more stable than A4-A5
Pins 0-1: Transmit and Receive pins, don’t use these pins for this lab
Pins 2-12: Digital pins that can be switched between HIGH states and LOW states
Pin 13: Connected to the on-board LED, use it only as an input pin

Heat is a form of energy. Heat transfer is thermal energy that is transferred from one body to another as a result of a temperature difference. Temperature is the measure of the average kinetic energy of atomic motion. The faster the atoms are moving, the higher the temperature. In this lab, we will focus on the ways heat is transferred.

The specific mechanisms that convey energy from one location to another are conduction, convection, and radiation. If you put any two objects with different temperatures together, heat will be transferred from the object with the higher temperature to the one with the lower temperature until the system reaches equilibrium. Let's say you placed a hot bowl of soup on the kitchen table. What would happen? What if you placed a glass of ice water on the same table? Our job is to understand how this heat transfer occurs and how to slow the process down.

When there is a temperature difference within a solid body or between two solid bodies in contact with each other, energy (heat) will flow from the region of higher temperature to the region of lower temperature. This is known as conduction. The formula to determine the conductivity in a rod of material is:

In this equation, Q is heat transferred in time t, k is the thermal conductivity constant, A is area, T is temperature and d is the thickness of the barrier. Therefore, increasing d or decreasing A would reduce conduction.

Imagine a metal rod that is heated at one end. The atoms of the rod collide at the point where the temperature differs, transferring heat until the temperature of the rod becomes uniform.

Convection is the transfer of heat within a fluid medium (fluids consist of gasses and liquids). Convection can occur in two different ways. The first way is known as natural convection. Natural convection occurs when the main mechanism for heat transfer is due to the temperature difference either within the fluid or at its boundaries. This difference in temperature causes the molecules in the higher temperature region to expand and rise due to buoyant forces. See Figure 1 for a diagram of natural convection. The second way in which convection occurs is called forced convection. Forced convection occurs when the main mechanism for heat transfer is due to the forced flow or motion of the fluid.

Figure 1: Air circulation diagram

Today we heat our homes through radiators using natural convection. Air flows through a heating element and then is dispersed through the room. Would it be helpful to put bigger fins on the heating element? To decide, use the formula for heat transferred through convection:

In this equation, Q is the heat transferred in time t, ∆T is the difference in temperature, h is the convection coefficient, and A is the surface area.

Suppose you wanted to disperse the heat even faster. In that case you could blow a fan past the heating elements. This is forced convection; it is one of the principles used in air conditioners to have a relatively small unit cool a large space.

According to the Encyclopedia Britannica, radiation is the process by which energy, in the form of electromagnetic radiation, is emitted by a heated surface in all directions. It travels to its point of absorption at the speed of light and does not require an intervening medium to carry it. The heating of the Earth by the Sun is an example of heat transfer by radiation. The formula for heat transferred through radiation:

In this equation, Q is the heat transferred in time t, Ts is the surface absolute temperature, is the constant of emissivity, Tsur is the surrounding absolute temperature, A is the surface area, and σ is the Stefan-Boltzmann constant.

Electromagnetic radiation is a mean of energy transfer that occurs when an atom absorbs energy. As we have seen earlier when heated atomic motion within a body increases. The energy absorbed by the atom causes some of its electrons to move around. For sake of stability, the electrons eventually come to its original state or position creating an electromagnetic wave. This EM wave can propagate as heat, light, ultraviolet, or other electromagnetic waves depending on the type of atom and the amount of energy absorbed.

Color is a property of light which is based on the frequency of the wave. When we see a specific color, our eyes literally receive an electromagnetic wave of a specific frequency. Materials that appear lighter (i.e. yellow) reflect more light (EM waves of a specific frequency) than darker ones. Following the same logic, when an object appears white, it virtually reflect all the EM waves coming to it while a "black" object absorbs it all.

Therefore, color (reflectivity) is to be considered when choosing materials for thermal insulation.

How would you feel out in the sun on hot day in a black t-shirt? How would you feel if your shirt were white? A black surface is an excellent absorber of heat, while a white surface is not. Silver is a poor heat absorber and reflects radiated energy; that is why rooftops of apartment houses are often painted silver.

Your container is a thermodynamic system – a part of the Universe separated from the surroundings by an imaginary boundary. There are three types of systems: open systems, closed systems and isolated systems.

Figure 2: Thermodynamic system with a boundary.

Open systems are systems where transfer of mass and heat is possible. For example, an open pot of boiling water is an open system – it exchanges heat with the air around it and water vapor. If collected, the water vapor can be condensed back to liquid water which has some mass.

Closed systems are systems where only heat can be transferred to the surroundings. A hermetically sealed bottle of soda is a closed system. If placed in a hot environment it absorbs energy in form of heat but the amount of liquid within does not change.

Isolated systems are systems which do not interact with the surroundings at all. No exchange is possible - neither heat nor mass. An ideal Thermos® is an isolated system. If hot chocolate is poured within, you will get the same amount back at the same temperature. No such system can be fabricated but can be approximated.

Now that we understand how heat is transferred, we must consider how to slow it down. Understanding how to minimize heat loss is the key to designing a successful insulating container.

The first consideration is the materials you choose. Using materials that are poor conductors of heat will minimize heat loss. Glass is one of these. Fiberglass insulation uses spun glass for its insulating effect.

Plastic is also a poor conductor of heat. Foam cups are made of plastic which has tiny air bubbles suspended in it. Air is among the poorest conductors of heat. It turns out that a vacuum, or the complete lack of air, is the best insulator of all. This is the principle employed in the Thermos design.

The other important consideration in creating your container is its cost. Minimal design uses the fewest resources while maintaining the safety and efficacy of your product. Engineers almost never have the luxury of designing products or systems where cost is not a factor. Given unlimited resources, you could most likely come up with a container that would maintain the egg's temperature almost indefinitely, but no one could afford to buy it from you. Balancing cost and effectiveness into your plans is a critical skill you will need later.

Another thing to consider is the method of data collection, because the way data is collected can cause many issues including providing incorrect data. To calculate changes in temperature, some devices that can be used are thermometers, thermistors, thermocouples, and RTDs.

Thermocouples consist of two wires of dissimilar metals and are usually welded together or attached on a junction. When the temperature at a junction changes, it generates thermoelectric potential (emf) which is proportional to the temperature difference between the two junctions. This voltage difference can be sued to measure the temperature at that junction.

Thermistors are thermally sensitive resistors that display changes in electrical resistance when subjected to a temperature change. Thermistors, like a thermocouple, utilize resistance to read temperature but do not require two different types of metal wire connected at a junction.

Thermometers are devices used to measure temperature or a temperature gradient utilizing a temperature sensor and some other means to convert the physical change to a digital change. Most digital thermometers utilize thermistors or lasers to calculate temperature.

In this lab, a thermocouple, thermistor, and thermometer will be used to collect data. Each of the different devices utilizes different technologies to gather the temperature data from the egg. The thermocouple collects data through the NI-ELVIS II+ board as it's built into the board itself. The thermistor operates through an Arduino. The thermometer is operated manually. This is done to check the accuracy of different types of devices and sensors as well as to determine the efficiency in the data collection method.

Activity 0: 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.

  1. Circle all the wire nodes in the schematic, in this case they are color coded. Every node will be a row used on the bread board.
    Example.jpg
  2. Connect the 5V and GND pins to the power rails.
    Step1.jpg
  3. Start with components connected to Vcc (5V) (the red node). In this example, the transistor and the diode(#1) have to connect to 5V so plug one end of each to the same row and then connect a wire from the power rail to that row.
  4. Since the other ends of the diode (#1) and transistor (color coded in the schematic in red) connect together as well, put them both into a second row. The middle pin of the transistor goes into a row by itself because it need to go to the Arduino later on.
    Step2.jpg
  5. The other components that connect to that second node are (color coded in the schematic in pink): the inductor and another diode (#2). Plug one end of those components into the second row. Then, plug the other ends of the inductor and diode(#2) into separate new rows since they connect to different nodes.
    Step3.jpg
  6. The only components remaining are the capacitor and resistor (color coded in the schematic in yellow) which plug into the rows that are designated for those nodes.
    Step4.jpg
  7. Finally, connect a wire to the row that connects the diode(#2), capacitor, and resistor (color coded in the schematic in dark blue) and connect A0 to the appropriate row (color coded in the schematic in light blue).
    Step5.jpg

As seen in the figure, there are 5 colored connections in the original circuit and only 5 rows were used on the breadboard. Note how the diodes are oriented!

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.