Lecture 9 Microcontrollers part 1 BJ Furman 29
Lecture 9: Microcontrollers – part 1 BJ Furman 29 OCT 2012
The Plan for Today n Microcontrollers for engineering applications ¨ ¨ ¨ What is a microcontroller? How are microcontrollers used? The Arduino hardware platform The Spartronics Experimenter board Programming the Arduino Basic steps n Digital I/O n Analog I/O n
Learning Objectives n Explain what a microcontroller is n Explain where microcontrollers are used n Describe the Arduino prototyping platform n Describe the Spartronics Experimenter board n Explain what is meant by a pin being an input or an output n Write programs for the Arduino that can do: ¨ Digital I/O ¨ Analog I/O
What is a Microcontroller? n A small computer usually implemented on a single IC that contains a central processing unit (CPU), some memory, and peripheral devices such as counter/timers, analog-todigital converters, serial communication hardware, etc. ATmega 328 the ‘brain’ of the Arduino http: //www. amazon. com/AVR-Pin-20 MHz 32 K-ATMega 328/dp/B 004 G 5 AVS 6
Where are Microcontrollers Used? n Everywhere! n Car ¨ Phone ¨ Toothbrush ¨ Microwave oven ¨ Copier ¨ Television ¨ PC keyboard ¨ Appliances http: //ecomodder. com/wiki/index. php/MPGuino ¨
The Arduino Platform n n Atmel ATmega 328 microcontroller 14 digital I/O pins ¨ n n n 6 with PWM 6 analog I/O pins 32 k. B (-2 k. B) Flash memory 2 k. B RAM 1 k. B EEPROM 16 MHz clock $22 - $30 built ¨ Rx + Tx LEDs Pin 13 LED Digital Pins Power LED USB jack Reset Button FTDI USB chip Voltage regulator Microcontroller power jack $13 ‘breadboardable’ http: //arduino. cc/ Pwr/GND Pins Analog Pins ICSP Header
The Spartronics Experimenter Board n n n n Momentary SPST push-button switches Red LEDs Piezo speaker Potentiometer (pot) Temperature sensor Light sensor Dual 7 -segment display RGB LED speaker Light sensor R G Cathode B http: //www. sparkfun. com/commerce/images/products/00105 -03 -L_i_ma. jpg Pot
Handling the Arduino - How NOT to Do It! Improper Handling - NEVER!!!
Handling the Arduino - The Proper Way Proper Handling - by the edges!!!
Programming the Arduino n An arduino program == ‘sketch’ ¨ Must have: n n ¨ setup() loop() configures pin modes and registers loop() n runs the main body of the program forever ¨ ¨ like while(1) {…} Where is main() ? n n Arduino simplifies things Does things for you /* Blink - turns on an LED for DELAY_ON msec, then off for DELAY_OFF msec, and repeats */ const byte led. Pin = 13; // LED on digital pin 13 const int DELAY_ON = 1000; const int DELAY_OFF = 1000; // setup() method runs once, when the sketch starts void setup() { // initialize the digital pin as an output: pin. Mode(led. Pin, OUTPUT); } // loop() method runs forever, // as long as the Arduino has power void loop() { digital. Write(led. Pin, HIGH); // set the LED on delay(DELAY_ON); // wait for DELAY_ON msec digital. Write(led. Pin, LOW); // set the LED off delay(DELAY_OFF); // wait for DELAY_OFF msec }
Using setup() n const byte led. Pin = 13; // LED on digital pin 13 A digital pin can either be an output or an input ¨ Output n your program determines what the voltage on a pin is (either 0 V (LOW or logic 0) or 5 V (HIGH or logic 1) ¨ ¨ Information is taken in pin. Mode() ¨ n n the world outside the microcontroller determines the voltage applied to the pin ¨ n Information is sent out Input n void setup() { // initialize the digital pin as an output: pin. Mode(led. Pin, OUTPUT); } n sets whether a pin is an input or an output led. Pin byte constant assigned the value of 13 OUTPUT is a macro defined constant n Which has the value 1 INPUT is a macro … ? where can you find out about the commands, etc? http: //arduino. cc/en/Reference/Extended
Blinking the LED in loop() n digital. Write() ¨ ¨ n #define LED_PIN 13 // LED on digital pin 13 #define DELAY_ON 500 // in ms #define DELAY_OFF 100 Causes the voltage on the indicated pin to go HIGH (+5 V) or LOW (0 V) Note: must first configure the pin to be an output void setup() { // initialize the digital pin as an output: pin. Mode(LED_PIN, OUTPUT); } n To make pin go to 5 V (high): void loop() ¨ digital. Write(pin_num, HIGH); { § Best to #define pin num. digital. Write(LED_PIN, HIGH); // turn LED on delay(DELAY_ON); // wait for DELAY_ON ms n To make pin go to 0 V (low): digital. Write(LED_PIN, LOW); // turn LED off ¨ digital. Write(pin_num, LOW); delay(DELAY_OFF); // wait for DELAY_OFF ms } delay() ¨ Causes the program to wait for a specified time in milliseconds http: //arduino. cc/en/Reference/Extended
Spartronics Experimenter Button Pinout To ATmega 328 n Pin and Button map 12 - SW 0 ¨ 8 - SW 1 ¨ 7 - SW 2 ¨ 4 - SW 3 How should the associated pins be configured: as INPUTS or as OUTPUTS? ¨ ‘Active LOW’ ¨ n n n Voltage on pin changes from 5 V to 0 V when switch is pressed Need to turn on internal ‘pull-up’ resistor, so that 5 V is supplied to pin 12 8 7 4
Pull-up Resistor Concept Pull-up resistor OFF Pull-up resistor ON ATmega 328 Pull-up resistor VTG= +5 V 1 PD 3 0
Spartronics Experimenter LED Pinout n Pin and LED map ¨ ¨ ¨ 11 - LED 0 (red) 9 - LED 1 (red) or RGB (green) 6 - LED 2 (red) or RGB (blue) 3 - LED 3 (red) or RGB (red) 13 - LED on Arduino Jumper determines whether pins map to red LEDs or the RGB 11 9 6 3
Spartronics Experimenter Digital Pin Assignments 13 12 11 10 9 8 7 6 5 4 3 2 1 0 SCK MISO MOSI SS OC 1 ICP AIN 1 AIN 0 T 1 T 0 INT 1 INT 0 TXD RXD LED LED pwm LED 0 pwm pwm LED 1 LED 2 LED 3 green blue red piezo servo SW 0 SW 1 SW 2 SW 3
Spartronics Experimenter Analog Pin Assignments 7 6 5 4 3 2 1 0 photocell POT temp sensor
Code to Set Up Button Pins n Two steps: 1. Make the pin an INPUT n 2. pin. Mode() Turn the pullup resistor on n const byte SW 0 = const byte SW 1 = const byte SW 2 = const byte SW 3 = 12; 8; 7; 4; // button SW 0 // button SW 1 // button SW 2 // button SW 3 void setup() { pin. Mode(SW 0, INPUT); // make SW 0 an INPUT digital. Write(SW 0, HIGH); // turn on pullup resistor etc. } digital. Write() a 1 to the pin (See full_test. pde for a more elegant approach to setting up button pins)
Digital I/O Example - Problem Statement n Write a program to turn on the blue of the RGB LED (connected to digital pin 6) when SW 0 is pressed (off otherwise) ¨ Pseudocode: define pin assignments n configure pins (which are input, which are output) n loop forever n if SW 0 button is pressed § make pin 6 high ¨ else § make pin 6 low ¨
Digital I/O Example - Pin Assignment and Configuration n Refine the pseudocode: ¨ define pin assignments n n ¨ const byte RGB_blue_pin = 6; const byte SW 0_pin = 12; configure pins (in function setup()) n RGB_blue_pin ¨ n SW 0_pin ¨ n make it an OUTPUT _______ INPUT make it an ______ turn on pull-up resistor on SW 0 pin ¨ ¨ pin will read high (1) until pulled low (0) see schematic void setup() { pin. Mode(RGB_blue_pin, OUTPUT); pin. Mode(SW 0_pin, INPUT); digital. Write(SW 0_pin, HIGH); }
Digital I/O Example - loop() Algorithm n Refine the pseudocode, cont. : ¨ loop forever (use function loop()) n If button is not pressed: ¨ ¨ n high (5 V) voltage on button pin 12 will be _______ make pin 6 voltage low (LED will go off or stay off) If button is pressed: ¨ ¨ low (0 V) voltage on button pin 12 will be _______ make pin 6 voltage high (LED will go on or stay on) void loop() { if(digital. Read(SW 0_pin) == LOW) { digital. Write(RGB_blue_pin, HIGH); } else { digital. Write(RGB_blue_pin, LOW); } }
Digital I/O Example - Arduino Program n n Arduino program Suppose a change to the specifications: ¨ ¨ LED is on until button pressed, then off Contrast mechatronic approach vs. nonmechatronic n n n re-wire, or… re-program the mechatronics approach separates the sensing elements from the control elements /* Blue_LED_button_cntrl 1 - turns on blue LED when SW 0 on Experimenter board is pressed, off otherwise */ /* pin assignments */ const byte RGB_blue_pin = 6; const byte SW 0_pin = 12; /* configure pins */ void setup() { pin. Mode(RGB_blue_pin, OUTPUT); pin. Mode(SW 0_pin, INPUT); digital. Write(SW 0_pin, HIGH); } /* loop forever */ void loop() { if(digital. Read(SW 0_pin) == LOW) digital. Write(RGB_blue_pin, HIGH); else digital. Write(RGB_blue_pin, LOW); }
Digital I/O Example - Modification n Modify Arduino program, so that LED is on until button is pressed, then turns off ¨ How? n Pin assignments? ¨ ¨ setup()? § Need to turn on the LED! loop()? § Swap values of second argument in digital. Write calls /* Blue_LED_button_cntrl 1 - turns on blue LED when SW 0 on Experimenter board is pressed, off otherwise */ /* pin assignments */ const byte RGB_blue_pin = 6; const byte SW 0_pin = 12; /* configure pins */ void setup() { pin. Mode(RGB_blue_pin, OUTPUT); pin. Mode(SW 0_pin, INPUT); digital. Write(SW 0_pin, HIGH); } /* loop forever */ void loop() { if(digital. Read(SW 0_pin) == LOW) digital. Write(RGB_blue_pin, HIGH); else digital. Write(RGB_blue_pin, LOW); }
Comparison of Digital I/O Programs /* Blue_LED_button_cntrl 1 - turns on blue LED when SW 0 on Experimenter board is pressed, off otherwise */ /* Blue_LED_button_cntrl 2 - turns off blue LED when SW 0 on Experimenter board is pressed, on otherwise */ /* pin assignments */ const byte RGB_blue_pin = 6; const byte SW 0_pin = 12; /* configure pins */ void setup() { pin. Mode(RGB_blue_pin, OUTPUT); pin. Mode(SW 0_pin, INPUT); digital. Write(SW 0_pin, HIGH); } /* configure pins */ void setup() { pin. Mode(RGB_blue_pin, OUTPUT); pin. Mode(SW 0_pin, INPUT); digital. Write(SW 0_pin, HIGH); digital. Write(RGB_blue_pin, HIGH); } /* loop forever */ void loop() { if(digital. Read(SW 0_pin) == LOW) digital. Write(RGB_blue_pin, HIGH); else digital. Write(RGB_blue_pin, LOW); } /* loop forever */ void loop() { if(digital. Read(SW 0_pin) == LOW) digital. Write(RGB_blue_pin, LOW); else digital. Write(RGB_blue_pin, HIGH); }
Analog In with Serial Out n Read the POT ¨ Note: analog voltage! n n 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 n n n 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 pot. Pin = 1; // pot output on pin 1 const byte led. Pin = 6; // blue LED on pin 6 unsigned int pot. Voltage = 0; // value of pot voltage unsigned int delay_ms; void setup() { pin. Mode(led. Pin, OUTPUT); pin. Mode(pot. Pin, INPUT); Serial. begin(9600); // init serial comm at 9600 bps } void loop() { pot. Voltage = analog. Read(pot. Pin); // read pot delay_ms = map(pot. Voltage, MIN_POT_VALUE, MAX_POT_VALUE, MIN _DELAY_TIME, MAX_DELAY_TIME); Serial. print("sensor = " ); // print to monitor Serial. print(pot. Voltage); Serial. print(" delay, ms = " ); Serial. println(delay_ms); // print delay and linefeed digital. Write(led. Pin, HIGH); // turn the LED on delay(delay_ms); // wait for delay_ms digital. Write(led. Pin, LOW); // turn the LED off: delay(delay_ms); // wait for delay_ms } POT_input_Serial_Out. pde
Effect of Using delay() Leads to poor (slow) performance as delay time increases n Try to avoid long delays n ¨ ¨ ¨ Use millis() instead Check for time exceeding millis() + delay_time Ex. POT_in_Serial_Out. pde n n Note also the use of #ifdef for ‘conditional compilation’ Note how roll-over of millis() is handled
Analog Out (PWM) Concept n 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 5 V) 5 V time ¨ Use Pulse Width Modulation (PWM) to approximate an analog voltage Digital outputs are capable of 0 V or 5 V n Over a fraction (ton) of a time period tcycle, keep pin at 5 V, the rest of the time, at 0 V n The average voltage is proportional to ton/tcycle, which is called the ‘Duty Cycle’ ¨ See Lab View PWM_demo. vi ¨
Front Panel 30% duty cycle Block Diagram
Arduino analog. Write( ) n analog. Write(pin, value); ¨ 0 value 255 0% duty cycle --> 0 V --> analog. Write(pin, 0); n 100% duty cycle --> 5 V --> analog. Write(pin, 255); n n fade_example. pde (see next page)
Analog Output Example n Fade the red LED in, then out ¨ ¨ duty cycle is incremented then decremented 256 steps n 0% to 100% const byte led. Pin = 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() { pin. Mode(led. Pin, OUTPUT); } void loop() { int fade. Value; // PWM value // fade in from min to max in increments of 5 points: for(fade. Value = 0 ; fade. Value <= FADE_MAX; fade. Value +=FADE_INC) { analog. Write(led. Pin, fade. Value); // sets the value (range from 0 to 255): } // fade out from max to min in increments of 5 points: for(fade. Value = FADE_MAX; fade. Value >= 0; fade. Value -=FADE_INC) { analog. Write(led. Pin, fade. Value); // sets the value (range from 0 to 255): } } fade_example. pde
Review
References Microcontroller. (2009, November 20). In Wikipedia, the free encyclopedia. Retrieved November 21, 2009, from http: //en. wikipedia. org/wiki/Microcontroller n Arduino Home Page. (2009, November 21). Retrieved November 21, 2009, from http: //arduino. cc/ n
Spartronics Experimenter Board
- Slides: 33