Innovation Fellows Program - Lectures & Resources · with the standard Arduino Libraries, producing...
Transcript of Innovation Fellows Program - Lectures & Resources · with the standard Arduino Libraries, producing...
![Page 1: Innovation Fellows Program - Lectures & Resources · with the standard Arduino Libraries, producing a single hex file (.h). Means to Upload – Transferring the hex file to the Arduino](https://reader031.fdocuments.in/reader031/viewer/2022013010/5f5661384c809533c7553159/html5/thumbnails/1.jpg)
Innovation Fellows Program
Arduino Uno Microcontroller Overview Prof. Steven S. Saliterman, http://saliterman.umn.edu/ Department of Biomedical Engineering, University of Minnesota
![Page 2: Innovation Fellows Program - Lectures & Resources · with the standard Arduino Libraries, producing a single hex file (.h). Means to Upload – Transferring the hex file to the Arduino](https://reader031.fdocuments.in/reader031/viewer/2022013010/5f5661384c809533c7553159/html5/thumbnails/2.jpg)
Prof. Steven S. Saliterman
Arduino Uno
![Page 3: Innovation Fellows Program - Lectures & Resources · with the standard Arduino Libraries, producing a single hex file (.h). Means to Upload – Transferring the hex file to the Arduino](https://reader031.fdocuments.in/reader031/viewer/2022013010/5f5661384c809533c7553159/html5/thumbnails/3.jpg)
Prof. Steven S. Saliterman
Power & Interface…
USB Interface
Digital I/O Pins Reset Button
ATMega Microcontroller
ICSP Connector
Power & Auxiliary Pins Analog –to - Digital Converter Pins
Debug LED
7 to 12 VDC Input
![Page 4: Innovation Fellows Program - Lectures & Resources · with the standard Arduino Libraries, producing a single hex file (.h). Means to Upload – Transferring the hex file to the Arduino](https://reader031.fdocuments.in/reader031/viewer/2022013010/5f5661384c809533c7553159/html5/thumbnails/4.jpg)
Prof. Steven S. Saliterman
USB Connection to Computer…
![Page 5: Innovation Fellows Program - Lectures & Resources · with the standard Arduino Libraries, producing a single hex file (.h). Means to Upload – Transferring the hex file to the Arduino](https://reader031.fdocuments.in/reader031/viewer/2022013010/5f5661384c809533c7553159/html5/thumbnails/5.jpg)
Prof. Steven S. Saliterman
Example Modules – Sensors…
Ultrasonic Sensor - HC-SR04 IR Sensor 2Y0A21 Sharp Digital Distance Sensor
MAX6675 Module + K Type Thermocouple Temperature Sensor
Diymall Bme280 Pressure Temperature Sensor Module with IIC I2c
SunFounder Accelerometer ADXL335
![Page 6: Innovation Fellows Program - Lectures & Resources · with the standard Arduino Libraries, producing a single hex file (.h). Means to Upload – Transferring the hex file to the Arduino](https://reader031.fdocuments.in/reader031/viewer/2022013010/5f5661384c809533c7553159/html5/thumbnails/6.jpg)
Prof. Steven S. Saliterman
9-Axis Absolute Orientation Sensor…
Absolute Orientation (Euler Vector, 100Hz) Three axis orientation data based on a 360° sphere.
Absolute Orientation (Quaterion, 100Hz) Four point quaternion output for more accurate data manipulation.
Angular Velocity Vector (100Hz) Three axis of 'rotation speed' in rad/s.
Acceleration Vector (100Hz) Three axis of acceleration (gravity + linear motion) in m/s^2.
Magnetic Field Strength Vector (20Hz) Three axis of magnetic field sensing in micro Tesla (uT).
Linear Acceleration Vector (100Hz) Three axis of linear acceleration data (acceleration minus gravity) in m/s^2.
Gravity Vector (100Hz) Three axis of gravitational acceleration (minus any movement) in m/s^2.
Temperature (1Hz) Ambient temperature in degrees celsius.
Courtesy of Adafruit
Sensor “fusion” combines the following measurements…
![Page 7: Innovation Fellows Program - Lectures & Resources · with the standard Arduino Libraries, producing a single hex file (.h). Means to Upload – Transferring the hex file to the Arduino](https://reader031.fdocuments.in/reader031/viewer/2022013010/5f5661384c809533c7553159/html5/thumbnails/7.jpg)
Prof. Steven S. Saliterman
Actuators…
2-Channel 5 V Solid State Relay Module
Standard Size - High Torque - Metal Gear Servo Stepper Motor
DC Motor with Gear
![Page 8: Innovation Fellows Program - Lectures & Resources · with the standard Arduino Libraries, producing a single hex file (.h). Means to Upload – Transferring the hex file to the Arduino](https://reader031.fdocuments.in/reader031/viewer/2022013010/5f5661384c809533c7553159/html5/thumbnails/8.jpg)
Prof. Steven S. Saliterman
User Interface…
Adafruit 1.8” Color TFT LCD Display with MicroSD Card Breakout – ST77 35R1.8" Color TFT LCD display with MicroSD Card Breakout - ST7735R
7-Segment Serial Display - Red
Diymall 0.96" Inch I2c IIC Serial 128x64 Oled LCD LED White Display Module
Mini ISD2548 Digital Voice Recorder
![Page 9: Innovation Fellows Program - Lectures & Resources · with the standard Arduino Libraries, producing a single hex file (.h). Means to Upload – Transferring the hex file to the Arduino](https://reader031.fdocuments.in/reader031/viewer/2022013010/5f5661384c809533c7553159/html5/thumbnails/9.jpg)
Prof. Steven S. Saliterman
Consider this Device Concept Task: Make a digital thermometer consisting of an enclosure, microcontroller board, thermocouple sensor, digital display, sound alert, slide switch, pushbutton and battery….
http://store-usa.arduino http://www.globalsources.com http://www.thermometricscorp.com
![Page 10: Innovation Fellows Program - Lectures & Resources · with the standard Arduino Libraries, producing a single hex file (.h). Means to Upload – Transferring the hex file to the Arduino](https://reader031.fdocuments.in/reader031/viewer/2022013010/5f5661384c809533c7553159/html5/thumbnails/10.jpg)
Prof. Steven S. Saliterman
Formulate an Algorithm…
1. When the pushbutton is pressed… Measure the temperature, Beep when the reading is good, Display the value for ten seconds, and finally Save the value to memory.
2. Start all over again. 3. Now flowchart this…
![Page 11: Innovation Fellows Program - Lectures & Resources · with the standard Arduino Libraries, producing a single hex file (.h). Means to Upload – Transferring the hex file to the Arduino](https://reader031.fdocuments.in/reader031/viewer/2022013010/5f5661384c809533c7553159/html5/thumbnails/11.jpg)
Prof. Steven S. Saliterman
Flowchart the Algorithm…
Display Temperature
& Beep
Start
Delay 10 Sec.
Monitor for Button Press
Button Pressed
?
Read Temperature
Valid Reading
?
Clear Display
Store Value
Stop
No
Yes
Yes
No
![Page 12: Innovation Fellows Program - Lectures & Resources · with the standard Arduino Libraries, producing a single hex file (.h). Means to Upload – Transferring the hex file to the Arduino](https://reader031.fdocuments.in/reader031/viewer/2022013010/5f5661384c809533c7553159/html5/thumbnails/12.jpg)
Prof. Steven S. Saliterman
Software & Programming
1. Software is the smart in your “smart device.” 2. An algorithm displayed as a flowchart, transforms your problem
into various input, processing, decision and output steps 3. Lines of code are written to implement your algorithm. 4. Code may be written in assembly language and/or higher level
languages such as C, C++, and C#. 5. A compiler converts your code into machine language that the
microcontroller understands. 6. The compiled code is then uploaded into a board containing the
microcontroller, memory and various interface circuits. 7. Errors are then fixed by debugging. 8. You may write your own code and/or incorporate code that has
been written by others (“sketches”).
![Page 13: Innovation Fellows Program - Lectures & Resources · with the standard Arduino Libraries, producing a single hex file (.h). Means to Upload – Transferring the hex file to the Arduino](https://reader031.fdocuments.in/reader031/viewer/2022013010/5f5661384c809533c7553159/html5/thumbnails/13.jpg)
Prof. Steven S. Saliterman
Integrated Development Environment (IDE)
Editor – To write your code in C (.c) and/or assembly (.a) language. A finished program is called a “sketch.”
Compiler – Turns your code into machine readable instructions or object files (.o). A Linker combines this code with the standard Arduino Libraries, producing a single hex file (.h).
Means to Upload – Transferring the hex file to the Arduino board program memory. This is done via the USB or serial connection with the aid of the bootloader.
Means to Run – Executing the Program Means to Debug – Finding & Correcting Errors
![Page 14: Innovation Fellows Program - Lectures & Resources · with the standard Arduino Libraries, producing a single hex file (.h). Means to Upload – Transferring the hex file to the Arduino](https://reader031.fdocuments.in/reader031/viewer/2022013010/5f5661384c809533c7553159/html5/thumbnails/14.jpg)
Prof. Steven S. Saliterman
1. Structures A. Setup & loop B. Control statements C. Syntax D. Arithmetic operators E. Comparison operators F. Bitwise operators G. Compound operators
Arduino Programming Components
2. Variables A. Constants B. Data Types C. Variable Scope D. Qualifiers E. Conversion F. Utilities
3. Functions A. Digital I/O B. Analog I/O C. Due & Zero only D. Advanced I/O E. Time F. Math G. Trigonometry H. Characters I. Random numbers J. Bits and bytes K. External interrupts L. Interrupts M. Communication N. USB
We will discuss today only the items above in blue.
![Page 15: Innovation Fellows Program - Lectures & Resources · with the standard Arduino Libraries, producing a single hex file (.h). Means to Upload – Transferring the hex file to the Arduino](https://reader031.fdocuments.in/reader031/viewer/2022013010/5f5661384c809533c7553159/html5/thumbnails/15.jpg)
Prof. Steven S. Saliterman
1. Structures: Setup() Example
1) Initialize variables
2) Assign pins 3) Runs once, after
powerup or reset.
int buttonPin = 3; void setup() { Serial.begin(9600) ; //serial baud rate pinMode(buttonPin), INPUT); //assign pin 3 to be an input } void loop() { … }
![Page 16: Innovation Fellows Program - Lectures & Resources · with the standard Arduino Libraries, producing a single hex file (.h). Means to Upload – Transferring the hex file to the Arduino](https://reader031.fdocuments.in/reader031/viewer/2022013010/5f5661384c809533c7553159/html5/thumbnails/16.jpg)
Prof. Steven S. Saliterman
Loop() Example
1. Occurs after setup.
2. Loops consecutively
3. Initialize variables 4. Assign pins 5. Runs once, after
powerup or reset. 6. Variations: if, if-
else; if-else-if
int buttonPin = 3; void setup() { Serial.begin(9600) ; //serial baud rate pinMode(buttonPin), INPUT); //assign 3 to be an input } void loop() { if(digitalRead(buttonPin) == HIGH) Serial.write(‘H’); else serial.write(‘L’); delay(1000); }
![Page 17: Innovation Fellows Program - Lectures & Resources · with the standard Arduino Libraries, producing a single hex file (.h). Means to Upload – Transferring the hex file to the Arduino](https://reader031.fdocuments.in/reader031/viewer/2022013010/5f5661384c809533c7553159/html5/thumbnails/17.jpg)
Prof. Steven S. Saliterman
Control Statements
Loop Statements For While Do-While
Decision Statements Break and Continue If If-Else, if-else-if Switch-Case
Directional Goto Return
![Page 18: Innovation Fellows Program - Lectures & Resources · with the standard Arduino Libraries, producing a single hex file (.h). Means to Upload – Transferring the hex file to the Arduino](https://reader031.fdocuments.in/reader031/viewer/2022013010/5f5661384c809533c7553159/html5/thumbnails/18.jpg)
Prof. Steven S. Saliterman
Arrays…
1. The first element is indexed with zero, e.g. a[3] has 3 elements, a[0], a[1], and a[2].
2. Declare as usual, e.g. int a[3], float a[3], and char a[3]. 3. Initialize: int a[3] = {2, 6, 1}. 4. Ok to initialize using a “for” loop. 5. If number of elements is not stated, the initialization will
determine it, e.g. int a[ ] = {2, 6, 1} – elements will be three. 6. Arrays may be multidimensional, e.g. a[3, 5]. 7. Two dimensional (rows and columns) can also be written, e.g.
int M[4] [5] (remember there is a zero row and column). 8. Number of elements may be determined by variable – in which
case range check first.
![Page 19: Innovation Fellows Program - Lectures & Resources · with the standard Arduino Libraries, producing a single hex file (.h). Means to Upload – Transferring the hex file to the Arduino](https://reader031.fdocuments.in/reader031/viewer/2022013010/5f5661384c809533c7553159/html5/thumbnails/19.jpg)
Prof. Steven S. Saliterman
“For” Statement (a Loop)…
for (initialization; condition; increment) {program statement(s);} Example – What is the value of the a[49] element? … int a[100]; for (int n = 0; n < 100; n = n + 1) { a[n] = n * 2; } …
Statement Format
Example Code
![Page 20: Innovation Fellows Program - Lectures & Resources · with the standard Arduino Libraries, producing a single hex file (.h). Means to Upload – Transferring the hex file to the Arduino](https://reader031.fdocuments.in/reader031/viewer/2022013010/5f5661384c809533c7553159/html5/thumbnails/20.jpg)
Prof. Steven S. Saliterman
“While” Statement (a Loop)…
while (expression – a boolean that is true or false) {program statement(s);} Example – What is the value of a[30] element? … int a[100]; int n = 0; while (n < 100) { a[n] = n * 3; n = n + 1; // Could also use “++n” } …
![Page 21: Innovation Fellows Program - Lectures & Resources · with the standard Arduino Libraries, producing a single hex file (.h). Means to Upload – Transferring the hex file to the Arduino](https://reader031.fdocuments.in/reader031/viewer/2022013010/5f5661384c809533c7553159/html5/thumbnails/21.jpg)
Prof. Steven S. Saliterman
“Do-while” Statement (a Loop)…
do {program statement(s)} while (test condition); Example – What is the value of a[75] element? … int a[100]; int n = 0; do { a[n] = n * 4; n = n + 1; } while n < 100; …
![Page 22: Innovation Fellows Program - Lectures & Resources · with the standard Arduino Libraries, producing a single hex file (.h). Means to Upload – Transferring the hex file to the Arduino](https://reader031.fdocuments.in/reader031/viewer/2022013010/5f5661384c809533c7553159/html5/thumbnails/22.jpg)
Prof. Steven S. Saliterman
“If” Statement (a Decision)…
if (expression) {program statement(s);} Example – What is the value of n? … int a = 4, n = 0; if a <= 5 { n = n + 50; } …
![Page 23: Innovation Fellows Program - Lectures & Resources · with the standard Arduino Libraries, producing a single hex file (.h). Means to Upload – Transferring the hex file to the Arduino](https://reader031.fdocuments.in/reader031/viewer/2022013010/5f5661384c809533c7553159/html5/thumbnails/23.jpg)
Prof. Steven S. Saliterman
“If-Else” Statement (a Decision)…
if (expression) {program statement(s);} else {program statement(s)}; Example – What is the value of n? … int a = 10, n = 0; if a <= 5 { n = n + 50; } else { n = n + 25; } …
![Page 24: Innovation Fellows Program - Lectures & Resources · with the standard Arduino Libraries, producing a single hex file (.h). Means to Upload – Transferring the hex file to the Arduino](https://reader031.fdocuments.in/reader031/viewer/2022013010/5f5661384c809533c7553159/html5/thumbnails/24.jpg)
Prof. Steven S. Saliterman
“Switch – Case” Statement (a Decision…)
switch (expression) { case label1: program statements; break; case label2: program statements; break; default: program statements; break; }
For example: int a; Bool buy; … a = 2; switch (a) { case 1: // if a =1 buy = true; break; case 2: // if a =2 buy = false; break; } …
![Page 25: Innovation Fellows Program - Lectures & Resources · with the standard Arduino Libraries, producing a single hex file (.h). Means to Upload – Transferring the hex file to the Arduino](https://reader031.fdocuments.in/reader031/viewer/2022013010/5f5661384c809533c7553159/html5/thumbnails/25.jpg)
Prof. Steven S. Saliterman
Syntax
; Used to end a statement { } Enclose statements, keep balanced // Start comment until end of line /* … */ Multi-line comment
#define Assigning a value to a constant name Follows C rules and no semicolon afterwards Use const type variable = value (e.g. const float pi = 3.14) when able instead.
#include To include outside libraries
![Page 26: Innovation Fellows Program - Lectures & Resources · with the standard Arduino Libraries, producing a single hex file (.h). Means to Upload – Transferring the hex file to the Arduino](https://reader031.fdocuments.in/reader031/viewer/2022013010/5f5661384c809533c7553159/html5/thumbnails/26.jpg)
Prof. Steven S. Saliterman
Arithmetic & Boolean Operators
= assignment operator + addition - subtraction * multiplication / division % modulo
&& and || or ! not
![Page 27: Innovation Fellows Program - Lectures & Resources · with the standard Arduino Libraries, producing a single hex file (.h). Means to Upload – Transferring the hex file to the Arduino](https://reader031.fdocuments.in/reader031/viewer/2022013010/5f5661384c809533c7553159/html5/thumbnails/27.jpg)
Prof. Steven S. Saliterman
Comparison & Pointer Operators
== equal to != not equal to < less than > greater than <= less than or equal to >= greater than or equal to
* dereference & reference
![Page 28: Innovation Fellows Program - Lectures & Resources · with the standard Arduino Libraries, producing a single hex file (.h). Means to Upload – Transferring the hex file to the Arduino](https://reader031.fdocuments.in/reader031/viewer/2022013010/5f5661384c809533c7553159/html5/thumbnails/28.jpg)
Prof. Steven S. Saliterman
2. Variables: Constants
1. true | false (typed in lower case) 1. false is defined as zero 2. true is defined as one, or any boolean test of an
integer that is non-zero. 2. Integer constants:
Decimal 123 Binary B11110000 (leading B) Octal 0173 (leading zero) Hexadecimal 0x7B (leading 0x)
![Page 29: Innovation Fellows Program - Lectures & Resources · with the standard Arduino Libraries, producing a single hex file (.h). Means to Upload – Transferring the hex file to the Arduino](https://reader031.fdocuments.in/reader031/viewer/2022013010/5f5661384c809533c7553159/html5/thumbnails/29.jpg)
Prof. Steven S. Saliterman
5. Floating point constants: Constant Evaluates to Also 10.0 10 2.34E5 2.34 * 10^5 234000 67e-5 67.0 * 10^-5 .00067
![Page 30: Innovation Fellows Program - Lectures & Resources · with the standard Arduino Libraries, producing a single hex file (.h). Means to Upload – Transferring the hex file to the Arduino](https://reader031.fdocuments.in/reader031/viewer/2022013010/5f5661384c809533c7553159/html5/thumbnails/30.jpg)
Prof. Steven S. Saliterman
Data Types
1. boolean (8 bit) - simple logical true/false (1 byte = 8 bits) 2. byte (8 bit) - unsigned number from 0-255 3. char (8 bit) - signed number from -128 to 127. The compiler
will attempt to interpret this data type as a character in some circumstances, which may yield unexpected results
https://learn.sparkfun.com/tutorials/data-types-in-arduino
4. unsigned char (8 bit) - same as ‘byte’; if this is what you’re after, you should use ‘byte’ instead, for reasons of clarity
5. word (16 bit) - unsigned number from 0-65535 (1 word = 2 bytes)
6. unsigned int (16 bit)- the same as ‘word’. Use ‘word’ instead for clarity and brevity.
![Page 31: Innovation Fellows Program - Lectures & Resources · with the standard Arduino Libraries, producing a single hex file (.h). Means to Upload – Transferring the hex file to the Arduino](https://reader031.fdocuments.in/reader031/viewer/2022013010/5f5661384c809533c7553159/html5/thumbnails/31.jpg)
Prof. Steven S. Saliterman
9. long (32 bit) - signed number from -2,147,483,648 to 2,147,483,647
10. float (32 bit) or double- signed number from -3.4028235E38 to 3.4028235E38. Floating point on the Arduino is not native; the compiler has to jump through hoops to make it work. If you can avoid it, you should.
https://learn.sparkfun.com/tutorials/data-types-in-arduino
7. int (16 bit) - signed number from -32768 to 32767. This is most commonly what you see used for general purpose variables in Arduino example code provided with the IDE.
8. unsigned long (32 bit) - unsigned number from 0-4,294,967,295. The most common usage of this is to store the result of the millis() function, which returns the number of milliseconds the current code has been running.
![Page 32: Innovation Fellows Program - Lectures & Resources · with the standard Arduino Libraries, producing a single hex file (.h). Means to Upload – Transferring the hex file to the Arduino](https://reader031.fdocuments.in/reader031/viewer/2022013010/5f5661384c809533c7553159/html5/thumbnails/32.jpg)
Prof. Steven S. Saliterman
Variable Scope
1. A global variable is one that can be seen by every function in a program. Local variables are only visible to the function in
which they are declared. In the Arduino environment, any variable declared
outside of a function (e.g. setup(), loop(), etc. ), is a global variable.
“For “ loop variables are local. 2. Static - the static keyword is used to create
variables that are visible to only one function.
![Page 33: Innovation Fellows Program - Lectures & Resources · with the standard Arduino Libraries, producing a single hex file (.h). Means to Upload – Transferring the hex file to the Arduino](https://reader031.fdocuments.in/reader031/viewer/2022013010/5f5661384c809533c7553159/html5/thumbnails/33.jpg)
Prof. Steven S. Saliterman
3. Functions: Digital I/O (Digital Pins)
Digital pins on the Arduino can be defined as being Inputs or Outputs using the function pinMode().
The state of a digital pin can be determined with the function digitalRead().
The state of an output can be set as High or Low with the function digitalWrite().
![Page 34: Innovation Fellows Program - Lectures & Resources · with the standard Arduino Libraries, producing a single hex file (.h). Means to Upload – Transferring the hex file to the Arduino](https://reader031.fdocuments.in/reader031/viewer/2022013010/5f5661384c809533c7553159/html5/thumbnails/34.jpg)
Prof. Steven S. Saliterman
digitalWrite()
digitalWrite() writes a HIGH or a LOW value to a digital pin. If the pin has been configured as an OUTPUT with pinMode(),
its voltage will be set to the corresponding value: 5V (or 3.3V on 3.3V boards) for HIGH, 0V (ground) for LOW.
If the pin is configured as an INPUT, digitalWrite() will enable (HIGH) or disable (LOW) the internal pullup on the input pin. It is recommended to set the pinMode() to INPUT_PULLUP to enable the internal pull-up resistor. See the digital pins tutorial for more information. e.g. digitalWrite(ledPin, HIGH)
![Page 35: Innovation Fellows Program - Lectures & Resources · with the standard Arduino Libraries, producing a single hex file (.h). Means to Upload – Transferring the hex file to the Arduino](https://reader031.fdocuments.in/reader031/viewer/2022013010/5f5661384c809533c7553159/html5/thumbnails/35.jpg)
Prof. Steven S. Saliterman
digitalRead()
digital read() reads the value from a specified digital pin, either HIGH or LOW.
e.g. digitalRead(inPin);
![Page 36: Innovation Fellows Program - Lectures & Resources · with the standard Arduino Libraries, producing a single hex file (.h). Means to Upload – Transferring the hex file to the Arduino](https://reader031.fdocuments.in/reader031/viewer/2022013010/5f5661384c809533c7553159/html5/thumbnails/36.jpg)
Prof. Steven S. Saliterman
Example: Blinking an LED const int LED = 10; int blinks = 5; // blink 5 times; bool done = false; void setup() { pinMode(LED, OUTPUT); //set pin 10 as an OUTPUT digitalWrite(LED, LOW); // Initialize off } void loop() { while (done != true) { for (int i = 1; i<= blinks; ++i) // ++i same as i = i+1 { digitalWrite(LED, HIGH); // Turn on LED delay(500); //Pause digitalWrite(LED, LOW); // Turn off LED delay(500); //Pause } done = true; } }
![Page 37: Innovation Fellows Program - Lectures & Resources · with the standard Arduino Libraries, producing a single hex file (.h). Means to Upload – Transferring the hex file to the Arduino](https://reader031.fdocuments.in/reader031/viewer/2022013010/5f5661384c809533c7553159/html5/thumbnails/37.jpg)
Prof. Steven S. Saliterman
Schematic…
![Page 38: Innovation Fellows Program - Lectures & Resources · with the standard Arduino Libraries, producing a single hex file (.h). Means to Upload – Transferring the hex file to the Arduino](https://reader031.fdocuments.in/reader031/viewer/2022013010/5f5661384c809533c7553159/html5/thumbnails/38.jpg)
Prof. Steven S. Saliterman
Example: Debouncing a Pushbutton const int LED = 9, BUTTON = 2; bool lastState = LOW, currentState = LOW, lit = false; void setup() { pinMode(LED, OUTPUT); pinMode(BUTTON, INPUT); } boolean debounce(boolean last) //function { boolean state = digitalRead(BUTTON); if(last != state) // has button settled down { delay(5); //delay if not state= digitalRead(BUTTON); //and read again } return state; }
void loop() { currentState = debounce(lastState); //call function if (lastState == LOW && currentState == HIGH) { lit = !lit; //toggle LED } lastState = currentState; digitalWrite(LED, lit); //update LED }
![Page 39: Innovation Fellows Program - Lectures & Resources · with the standard Arduino Libraries, producing a single hex file (.h). Means to Upload – Transferring the hex file to the Arduino](https://reader031.fdocuments.in/reader031/viewer/2022013010/5f5661384c809533c7553159/html5/thumbnails/39.jpg)
Prof. Steven S. Saliterman
Debouncing a Pushbutton…
![Page 40: Innovation Fellows Program - Lectures & Resources · with the standard Arduino Libraries, producing a single hex file (.h). Means to Upload – Transferring the hex file to the Arduino](https://reader031.fdocuments.in/reader031/viewer/2022013010/5f5661384c809533c7553159/html5/thumbnails/40.jpg)
Prof. Steven S. Saliterman
analogReference()
analogReference() - configures the reference voltage used for analog input (i.e. the value used as the top of the input range). The options are: DEFAULT: the default analog reference of 5 volts (on 5V
Arduino boards) or 3.3 volts (on 3.3V Arduino boards) INTERNAL: an built-in reference, equal to 1.1 volts on the
ATmega168 or ATmega328 and 2.56 volts on the ATmega8 (not available on the Arduino Mega)
INTERNAL1V1: a built-in 1.1V reference (Arduino Mega only) INTERNAL2V56: a built-in 2.56V reference (Arduino Mega
only) EXTERNAL: the voltage applied to the AREF pin (0 to 5V
only) is used as the reference.
![Page 41: Innovation Fellows Program - Lectures & Resources · with the standard Arduino Libraries, producing a single hex file (.h). Means to Upload – Transferring the hex file to the Arduino](https://reader031.fdocuments.in/reader031/viewer/2022013010/5f5661384c809533c7553159/html5/thumbnails/41.jpg)
Prof. Steven S. Saliterman
analogRead()
analogRead() reads the value from the specified analog pin. The Arduino board contains a 6 channel (8 channels on the Mini
and Nano, 16 on the Mega), 10-bit analog to digital converter. This means that it will map input voltages between 0 and 5 volts into integer values between 0 and 1023. This yields a resolution between readings of: 5 volts / 1024 units or, .0049 volts (4.9 mV) per unit. The input range and resolution can be changed using analogReference().
It takes about 100 microseconds (0.0001 s) to read an analog input, so the maximum reading rate is about 10,000 times a second
![Page 42: Innovation Fellows Program - Lectures & Resources · with the standard Arduino Libraries, producing a single hex file (.h). Means to Upload – Transferring the hex file to the Arduino](https://reader031.fdocuments.in/reader031/viewer/2022013010/5f5661384c809533c7553159/html5/thumbnails/42.jpg)
Prof. Steven S. Saliterman
analogWrite()
analogWrite() writes an analog value (PWM wave) to a pin.
Can be used to light a LED at varying brightnesses or drive a motor at various speeds. After a call to analogWrite(), the pin will generate a steady square wave of the specified duty cycle until the next call to analogWrite() (or a call to digitalRead() or digitalWrite() on the same pin).
The frequency of the PWM signal on most pins is approximately 490 Hz. On the Uno and similar boards, pins 5 and 6 have a frequency of approximately 980 Hz. Pins 3 and 11 on the Leonardo also run at 980 Hz.
![Page 43: Innovation Fellows Program - Lectures & Resources · with the standard Arduino Libraries, producing a single hex file (.h). Means to Upload – Transferring the hex file to the Arduino](https://reader031.fdocuments.in/reader031/viewer/2022013010/5f5661384c809533c7553159/html5/thumbnails/43.jpg)
Prof. Steven S. Saliterman
analogReadResolution()
analogReadResolution() is also an extension of the Analog API for the Arduino Due and Zero.
Sets the size (in bits) of the value returned by analogRead(). It defaults to 10 bits (returns values between 0-1023) for backward compatibility with AVR based boards.
The Due and the Zero have 12-bit ADC capabilities that can be accessed by changing the resolution to 12. This will return values from analogRead() between 0 and 4095.
![Page 44: Innovation Fellows Program - Lectures & Resources · with the standard Arduino Libraries, producing a single hex file (.h). Means to Upload – Transferring the hex file to the Arduino](https://reader031.fdocuments.in/reader031/viewer/2022013010/5f5661384c809533c7553159/html5/thumbnails/44.jpg)
Prof. Steven S. Saliterman
Example: Reading a Potentiometer
Igoe, Tom. If statement tutorial. Arduino 2012
![Page 45: Innovation Fellows Program - Lectures & Resources · with the standard Arduino Libraries, producing a single hex file (.h). Means to Upload – Transferring the hex file to the Arduino](https://reader031.fdocuments.in/reader031/viewer/2022013010/5f5661384c809533c7553159/html5/thumbnails/45.jpg)
Prof. Steven S. Saliterman
const int analogPin = A0; const int ledPin = 13; const int threshold = 400; void setup() { pinMode(ledPin, OUTPUT); Serial.begin(9600); } void loop() { int analogValue = analogRead(analogPin); if (analogValue > threshold) { digitalWrite(ledPin, HIGH); } else { digitalWrite(ledPin, LOW); } Serial.println(analogValue); delay(1); }
Reading a Potentiometer…
Igoe, Tom. If statement tutorial. Arduino 2012
// pin that the potentiometer is attached to // pin that the LED is attached to on UNO // an arbitrary threshold level that's in the range of the analog input // initialize the LED pin as an output // initialize serial communications // read the value of the potentiometer // if the analog value is high enough, turn on the LED // print the analog value // delay in between reads for stability
![Page 46: Innovation Fellows Program - Lectures & Resources · with the standard Arduino Libraries, producing a single hex file (.h). Means to Upload – Transferring the hex file to the Arduino](https://reader031.fdocuments.in/reader031/viewer/2022013010/5f5661384c809533c7553159/html5/thumbnails/46.jpg)
Prof. Steven S. Saliterman
I2C or “Inter-Integrated Circuit” Serial Interface
SDA SCL Only two bus lines are required; a serial data line (SDA) and a serial clock line
(SCL). Each device connected to the bus is software addressable. Serial, 8-bit oriented, bidirectional data transfers can be made.
![Page 47: Innovation Fellows Program - Lectures & Resources · with the standard Arduino Libraries, producing a single hex file (.h). Means to Upload – Transferring the hex file to the Arduino](https://reader031.fdocuments.in/reader031/viewer/2022013010/5f5661384c809533c7553159/html5/thumbnails/47.jpg)
Prof. Steven S. Saliterman
Summary
Arduino Uno, sensors and actuator examples. Using an IDE, programs are typically written in C and assembly
language, compiled, linked with libraries and uploaded onto the Arduino board memory as hexadecimal code.
Structures, variables and functions comprise an embedded program.
Digital pins are defined as INPUT or OUTPUT and having levels of HIGH and LOW.
Digital and analog functions. Examples: blinking light, debouncing a pushbutton and reading a
potentiometer. I2C Serial Interface.