Lecture 10: Peripherals Bryan Burlingame 04 November 2015.

Slides:



Advertisements
Similar presentations
Khaled A. Al-Utaibi Interfacing an LED The Light Emitting Diode (LED) Applications DC Characteristics & Operation Interfacing to.
Advertisements

EMS1EP Lecture 4 Intro to Programming Dr. Robert Ross.
EMS1EP Lecture 8 Pulse Width Modulation (PWM)
EMS1EP Lecture 9 Analog to Digital Conversion (ADC) Dr. Robert Ross.
Lab7: Introduction to Arduino
Anurag Dwivedi & Rudra Pratap Suman.  Open Source electronic prototyping platform based on flexible easy to use hardware and software.
Programming Microcontrollers B. Furman 19MAR2011.
Re-programming the Simon Says with Arduino Linz Craig, Brian Huang.
Introduction to Arduino Programming January MER-421:Mechatronic System Design.
Introduction.
Embedded Programming and Robotics Lesson 2 C Programming Refresher C Programming1.
Introduction to Arduino Prepared by R. Lamond.  “Arduino is an open-source electronics prototyping platform based on flexible, easy- to-use hardware.
ARDUINO PROGRAMMING Working with the Arduino microcontroller.
Working with Arduino: Lesson #1: Getting Acquainted with the Kit EGN1007.
Colorado Space Grant Consortium Gateway To Space ASEN 1400 / ASTR 2500 Class #12 Gateway To Space ASEN 1400 / ASTR 2500 Class #12 T-58.
Lecture 9: Microcontrollers – part 1 BJ Furman 29OCT2012.
chipKit Sense Switch & Control LED
Introduction to the Arduino
Khaled A. Al-Utaibi  The Push Button  Interfacing Push Buttons to Arduino  Programming Digital Inputs  Working with “Bouncy”
Arduino Week 2 Lab ECE 1020 Prof. Ahmadi. Objectives 1. Control the rotation of standard servo motor  A standard servo motor is limited in its rotation.
Pulse Width Modulation (PWM). 100% Pulse Width Modulation (PWM) 0% On the chipKIT there are 490 periods per second. Use analogWrite(pin, value) to control.
Mekatronika, STT Mandala
Lecture 7: Microcontrollers & I/O Bryan Burlingame 14 October 2015.
Good LED Circuit 5V0 GND. What Voltage Does Meter See? Answer: 5 V.
Week 6: Functions - Part 2 BJ Furman 01OCT2012. The Plan for Today Comments on midterm exam (next week in lab!) Review of functions Scope of identifiers.
Lecture 10: Modular Programming (functions) B Burlingame 13 April 2015.
ARDUINO 1. Basics  Comments  /* * Blink * * The basic Arduino example. Turns on an LED on for one second, * then off for one second, and so on... We.
SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,
Microcontrollers, Microcomputers, and Microprocessors
Lecture 15: Course Review BJ Furman ME 30 16MAY2011.
INTERNET OF EVERYTHING SDU 2016 Week 4. Simple Digital and Analog Inputs  The Arduino’s ability to sense digital and analog inputs allows it to respond.
Microcontroller basics Embedded systems for mortals.
Week 5: Microcontrollers & Flow Control Bryan Burlingame 2 March 2016.
Embedded systems and sensors 1 Part 2 Interaction technology Lennart Herlaar.
Microcontroller basics Embedded systems for mortals.
1 Introduction to Haptics Introduction to the Hapkit board Allison M. Okamura Stanford University.
Lecture 9: Introduction to Arduino Topics: Arduino Fundamentals, Bean Date: Mar 22, 2016.
ME 120: Arduino Programming Arduino Programming Part II ME 120 Mechanical and Materials Engineering Portland State University
Programming in Arduino Materials:Arduino Board Casperelectronics Pre Pres. Notes Photos from workshop?
Lecture 7: Variable Scope B Burlingame March 16, 2016.
Arduino “Getting Started” Instructor : Dr Matthew Miss Khin Yi Kyaw
Pulse-Width Modulation: Simulating variable DC output
Pulse Width Modulation Instructor Dr Matthew Khi Yi Kyaw.
Lecture 7: Modular Programming (functions) B Burlingame 05 October, 2016.
Week 4: Microcontrollers & Flow Control
Assist. Prof. Rassim Suliyev - SDU 2017
Microcontroller basics
Week 6: Style and Peripherals
Home automation using Arduino & ‘PIR sensor’
Val Manes Department of Math & Computer Science
Microcontroller basics
Microcontroller basics
Lecture 8: Variable Scope & Working with Files
Lab 1: Arduino Basics Topics: Arduino Fundamentals, First Circuit
Arduino Part 1 Topics: Microcontrollers Programming Basics
3.0 ARDUINO WORKSHOP PRESENTATION FOR STUDENTS IN 4º DEGREE OF COMPULSORY SECONDARY EDUCATION 3.0.
IR Control Materials taken from a variety of sources including IR Remote for the Boe-Bot by Andy Lindsay.
Week 5: Microcontrollers
Lecture 7: Functions Revisited / Analogue Signals
Week 6: Microcontrollers II
IoT Programming the Particle Photon.
Arduino Week 2 Lab ECE 1020 Prof. Ahmadi.
Working with Arduino: Lesson #1: Getting Acquainted with the Kit
Arduino : Introduction & Programming
Programming 2: The Arduino IDE & First Sketches
Arduino Practice: Photoresistors, PWM, Potentiometers, Motors
Sensors and actuators Sensors Resistive sensors
Arduino Uno circuit basics
Setting up a basic program with Arduino
Pulse-Width Modulation: Simulating variable DC output
Presentation transcript:

Lecture 10: Peripherals Bryan Burlingame 04 November 2015

The Plan for Today  Finish discussing variable scope  Discuss peripherals on the Experimenter & the Arduino

Announcements Homework due two weeks No office hours tonight No lecture on November 11 (Veteran’s Day)  Normal labs on 5, 6 & 10  Thursday Lab will meet November 12  Take home lab will be assigned on November 11  Friday Labs on November 13 & Tuesday Lab on November 17 are open to all for assistance Midterms back in 2 weeks

Passing Arguments into Functions How are the arguments passed into functions?  'Pass by value'  function arguments are expressions  In the function call: Expressions are evaluated and copies of their values are put into temporary memory locations The names of the corresponding parameters in the function definition are made to be the names of the copies  The values of the expressions in the function call are not changed #include double product(double x, double y); int main() { int a = 10; double var1 = 3.0, var2 = 5.0; double ans; ans = product(var1, var2); printf("var1 = %.2f\n" "var2 = %.2f\n",var1,var2); printf("var1*var2 = %g\n", ans); } /* function definition */ double product(double A, double B) { double result; result = A * B; return result; }

Identifiers and Scope Identifier  The name of a variable, function, label, etc. int my_var1;/* a variable */ pow_table();/* a function */ start:/* a label */ Question:  Does it make a difference where in a program an identifier is declared? YES! --> concept of ‘scope’

Scope of Identifiers Scope of a declaration of an identifier  The region of the program that the declaration is active (i.e., can access the variable, function, label, etc.) Five types of scope:  Program (global scope)  File  Function prototype  Function  Block (“between the { } scope”)

Scope of Identifiers - Program (Global) Scope Program (global) scope  if declared outside of all functions  "Visible" to all functions from point of declaration  Visible to functions in other source files  Use only when necessary and then very carefully!!  ex. from Ch var_scope.c #include int a = 10; double product(double x, double y); int main() { double var1 = 3.0, var2 = 5.0; double ans; ans = product(var1, var2); printf("var1 = %.2f\n" "var2 = %.2f\n",var1,var2); printf("var1*var2 = %g\n", ans); } /* function definition */ double product(double x, double y) { double result; result = x * y; return result; }

Function scope  Applies only to labels start: * goto start;  Active from the beginning to the end of a function  Ex. Statement labels in a switch selection structure Scope of Identifiers - Function Scope #include int main() { int user_sel; /* prompt user for entry */ /* get user entry */ switch( user_sel ) { case 1: printf("\n message..."); /* call game function1 here */ break; case 2: printf("\n message..."); /* call game function2 here */ break; default: printf("Error"); break; }

Scope of Identifiers - Block Scope Block (local) scope  A block is a series of statements enclosed in braces { }  The identifier scope is active from the point of declaration to the end of the block ( } )  Nested blocks can both declare the same variable name and not interfere  ex. from Ch var_scope_block.c scope_nested_blocks.c #include double product(double x, double y); int main() { int a = 10; double var1 = 3.0, var2 = 5.0; double ans; ans = product(var1, var2); printf("var1 = %.2f\n" "var2 = %.2f\n",var1,var2); printf("var1*var2 = %g\n", ans); } /* function definition */ double product(double x, double y) { double result; result = x * y; return result; }

Storage Duration How long the identifier exists in memory Static storage class  Identifier exists when program execution begins For variables:  Storage allocated and variable is initialized once  Retains their values throughout the execution of the program #include void just_count(void); /* proto */ int main() { int i; for(i=0;i<10;i++) { just_count(); } return 0; } void just_count(void) { static int count_a; int count_b; count_a = count_a + 1; count_b = count_b + 1; printf("count_a== %d\n", count_a); printf("count_b== %d\n", count_b); } just_count.c

For functions:  function name exists when execution begins For variables with global scope:  i.e., declared outside of all functions and uses static keyword  "Visible" to all functions from point of declaration in this source file only  Keeps data ‘private’ to this file only Storage Duration, cont. #include static int a = 10; double product(double x, double y); int main() { double var1 = 3.0, var2 = 5.0; double ans; ans = product(var1, var2); printf("var1 = %.2f\n" "var2 = %.2f\n",var1,var2); printf("var1*var2 = %g\n", ans); } /* function definition */ double product(double x, double y) { double result; result = x * y; return result; }

Recall: Definition of a Microcontroller A microcontroller is an integrated circuit which combines:  Microprocessor  Memory  Input/Output Peripherals Ex: Serial console, digital to analog converters

Arduino Peripherals (Uno) Serial (TTL) 6 PWM modules (Digital to Analog (DAC)) 6 ADC (Analog to Digital) SPI bus TWI (I 2 C) bus

Experimenter Red/Green/Blue (RGB) Light Emitting Diode (LED) 4 x Red LEDs 4 x Active low switches Servoelectric motor header (servo) Piezo-electric speaker (piezo) Potentiometer (POT) Photocell resistor Temperature sensor

SCKMISOMOSISSOC1ICPAIN1AIN0T1T0INT1INT0TXDRXD LED pwm LED0LED1LED2LED3 redgreenblue piezo servo SW0SW1SW2SW3 Spartronics Experimenter Digital Pin Assignments

photocellPOTtemp sensor Spartronics Experimenter Analog Pin Assignments

Digital Signals Most basic form of sending information to the world On or Off (1 or 0)  Active High Logic  Think of a light

Using setup() A digital pin can either be an output or an input  Output your program determines what the voltage on a pin is (either 0V (LOW or logic 0) or 5V (HIGH or logic 1)  Information is sent out  Input the world outside the microcontroller determines the voltage applied to the pin  Information is taken in const byte ledPin = 13; // LED on digital pin 13 void setup() { // initialize the digital pin as an output: pinMode(ledPin, OUTPUT); } where can you find out about the commands, etc? pinMode()  sets whether a pin is an input or an output ledPin  byte constant assigned the value of 13 OUTPUT is a macro defined constant Which has the value 1 INPUT is a macro …  ?

Blinking the LED in loop() digitalWrite()  Causes the voltage on the indicated pin to go HIGH (+5V) or LOW (0V)  Note: must first configure the pin to be an output To make pin go to 5V (high):  digitalWrite(pin_num,HIGH);  Best to #define pin num. To make pin go to 0V (low):  digitalWrite(pin_num,LOW); delay()  Causes the program to wait for a specified time in milliseconds #define LED_PIN 13 // LED on digital pin 13 #define DELAY_ON 500 // in ms #define DELAY_OFF 100 void setup() { // initialize the digital pin as an output: pinMode(LED_PIN, OUTPUT); } void loop() { digitalWrite(LED_PIN, HIGH); // turn LED on delay(DELAY_ON); // wait for DELAY_ON ms digitalWrite(LED_PIN, LOW); // turn LED off delay(DELAY_OFF); // wait for DELAY_OFF ms }

Spartronics Experimenter LED Pinout Pin and LED map  11 - LED0 (red) or RGB (red)  9 - LED1 (red) or RGB (green)  6 - LED2 (red) or RGB (blue)  3 - LED3 (red)  13 - LED on Arduino Jumper determines whether pins map to red LEDs or the RGB 11963

Pull-up Resistor Concept ATmega328 PD3 V TG = +5V 0 1 ATmega328 PD3 V TG = +5V 0 1 Pull-up resistor OFFPull-up resistor ON Pull-up resistor

Code to Set Up Button Pins Two steps: 1. Make the pin an INPUT pinMode() 2. Turn the pull-up resistor on digitalWrite() a 1 to the pin const byte SW0 = 12; // button SW0 const byte SW1 = 8; // button SW1 const byte SW2 = 7; // button SW2 const byte SW3 = 4; // button SW3 void setup() { pinMode(SW0, INPUT); // make SW0 an INPUT digitalWrite(SW0, HIGH); // turn on pullup resistor etc. } (See full_test.pde for a more elegant approach to setting up button pins)

Digital I/O Example - Problem Statement Write a program to turn on the blue of the RGB LED (connected to digital pin 6) when SW0 is pressed (off otherwise)  Pseudocode: define pin assignments configure pins (which are input, which are output) loop forever  if SW0 button is pressed  make pin 6 high  else  make pin 6 low

Digital I/O Example - Pin Assignment and Configuration Refine the pseudocode:  define pin assignments const byte RGB_blue_pin = 6; const byte SW0_pin = 12;  configure pins (in function setup()) RGB_blue_pin  make it an _______ SW0_pin  make it an ______ turn on pull-up resistor on SW0 pin  pin will read high (1) until pulled low (0)  see schematic void setup() { pinMode(RGB_blue_pin, OUTPUT); pinMode(SW0_pin, INPUT); digitalWrite(SW0_pin, HIGH); } OUTPUT INPUT

Digital I/O Example - loop() Algorithm Refine the pseudocode, cont. :  loop forever (use function loop()) If button is not pressed:  voltage on button pin 12 will be _______  make pin 6 voltage low (LED will go off or stay off) If button is pressed:  voltage on button pin 12 will be _______  make pin 6 voltage high (LED will go on or stay on) void loop() { if(digitalRead(SW0_pin) == LOW) { digitalWrite(RGB_blue_pin, HIGH); } else { digitalWrite(RGB_blue_pin, LOW); } high (5V) low (0V)

Digital I/O Example - Arduino Program Arduino program Suppose a change to the specifications:  LED is on until button pressed, then off  Contrast mechatronic approach vs. non- mechatronic re-wire, or… re-program the mechatronics approach separates the sensing elements from the control elements /* Blue_LED_button_cntrl1 - turns on blue LED when SW0 on Experimenter board is pressed, off otherwise */ /* pin assignments */ const byte RGB_blue_pin = 6; const byte SW0_pin = 12; /* configure pins */ void setup() { pinMode(RGB_blue_pin, OUTPUT); pinMode(SW0_pin, INPUT); digitalWrite(SW0_pin, HIGH); } /* loop forever */ void loop() { if(digitalRead(SW0_pin) == LOW) digitalWrite(RGB_blue_pin, HIGH); else digitalWrite(RGB_blue_pin, LOW); }

/* Blue_LED_button_cntrl1 - turns on blue LED when SW0 on Experimenter board is pressed, off otherwise */ /* pin assignments */ const byte RGB_blue_pin = 6; const byte SW0_pin = 12; /* configure pins */ void setup() { pinMode(RGB_blue_pin, OUTPUT); pinMode(SW0_pin, INPUT); digitalWrite(SW0_pin, HIGH); } /* loop forever */ void loop() { if(digitalRead(SW0_pin) == LOW) digitalWrite(RGB_blue_pin, HIGH); else digitalWrite(RGB_blue_pin, LOW); } Digital I/O Example - Modification Modify Arduino program, so that LED is on until button is pressed, then turns off  How? Pin assignments?  setup()?  Need to turn on the LED!  loop()?  Swap values of second argument in digitalWrite calls

Practice - 1 Pair up with someone next to you that you do not know Develop an algorithm for:  Finding and printing out the largest of two numbers (3 min) One person work on the pseudocode, the other on a flowchart (1 min) Compare pseudocode and flowchart (3 min) Write out the algorithm in C

Serial console Allows the microcontroller to provide text feedback to the programmer

Arduino Text Output No screen Serial output  RS232 over USB  Console built into the Arduino IDE

Example: Serial Console void setup() { // initialize serial comm at 9600 bits per second: Serial.begin(9600); } // the loop routine runs over and over again forever: void loop() { Serial.println("Wax on"); delay(100); Serial.println("Wax kinda"); delay(100); Serial.println("Wax off"); delay(100); // place at least a 1 millisecond delay for stability }

Analog Signals Real world systems tend to be analog Covers the range from 0V to 5V Recall: Microcontrollers work in binary, so how does this work We approximate!  Step-wise approximation of a continuous function

Resolution All analog to digital and digital to analog systems have a minimum detectable change. This is the resolution of the system On these Arduinos  8 BIT DAC (digital to analog conversion) 5V / 2 8  10 BIT ADC (analog to digital conversion) 5V / 2 10

Analog In with Serial Out Read the POT NNote: analog voltage! 0 V  0 5 V  1023 Blink an LED at a rate proportional to the pot voltage Output the pot voltage to the serial monitor Initialize with Serial.begin() Map voltage to delay Write a line with Serial.print or Serial.println #define MAX_DELAY_TIME 1000 // max delay in ms #define MIN_DELAY_TIME 10 // min delay in ms #define MAX_POT_VALUE 855 // max pot reading #define MIN_POT_VALUE 0 // min pot reading const byte potPin = 1; // pot output on pin 1 const byte ledPin = 6; // blue LED on pin 6 unsigned int potVoltage = 0; // value of pot voltage unsigned int delay_ms; void setup() { pinMode(ledPin, OUTPUT); pinMode(potPin, INPUT); Serial.begin(9600); // init serial comm at 9600 bps } void loop() { potVoltage = analogRead(potPin); // read pot delay_ms = map(potVoltage,MIN_POT_VALUE,MAX_POT_VALUE,MIN _DELAY_TIME,MAX_DELAY_TIME); Serial.print("sensor = " ); // print to monitor Serial.print(potVoltage); Serial.print(" delay, ms = " ); Serial.println(delay_ms); // print delay and linefeed digitalWrite(ledPin, HIGH); // turn the LED on delay(delay_ms); // wait for delay_ms digitalWrite(ledPin, LOW); // turn the LED off: delay(delay_ms); // wait for delay_ms } POT_input_Serial_Out.pde

Analog Out (PWM) Concept No facility exists on most microcontrollers to directly output an analog voltage (i.e., a voltage that varies continuously over the range of 0 to 5V)  Use Pulse Width Modulation (PWM) to approximate Digital outputs are capable of 0V or 5V Over a fraction (t on ) of a time period t cycle, keep pin at 5V, the rest of the time, at 0V  Average voltage is proportional to t on /t cycle, called the ‘Duty Cycle’ 5V time

Arduino analogWrite( ) analogWrite (pin, value);  0  value  255 0% duty cycle --> 0 V --> analogWrite (pin, 0); 100% duty cycle --> 5 V --> analogWrite (pin, 255);  Must be a PWM pin

SCKMISOMOSISSOC1ICPAIN1AIN0T1T0INT1INT0TXDRXD LED pwm LED0LED1LED2LED3 redgreenblue piezo servo SW0SW1SW2SW3 Spartronics Experimenter Digital Pin Assignments

Analog Output Example Fade the red LED in, then out  duty cycle is incremented then decremented  256 steps 0% to 100% const byte ledPin = 3; // red RGB LED on Experimenter const byte FADE_MAX = 255; // max value for setting duty cycle const byte FADE_INC = 5; // increment for changing duty cycle void setup() { pinMode(ledPin, OUTPUT); } void loop() { int fadeValue; // PWM value // fade in from min to max in increments of 5 points: for(fadeValue = 0 ; fadeValue <= FADE_MAX; fadeValue +=FADE_INC) { analogWrite(ledPin, fadeValue); // sets the value (range from 0 to 255): } // fade out from max to min in increments of 5 points: for(fadeValue = FADE_MAX; fadeValue >= 0; fadeValue -=FADE_INC) { analogWrite(ledPin, fadeValue); // sets the value (range from 0 to 255): } fade_example.pde

References Modular Programming in C math.h /xsh/math.h.html /xsh/math.h.html Arduino Home Page. (2009, November 21). Retrieved November 21, 2009, from