Week 15 Microcontrollers BJ Furman 28NOV2009 The Plan for Today Microcontrollers for engineering applications What is a microcontroller How are microcontrollers used The Arduino hardware platform The Spartronics I experimenter board Programming the Arduino Learning Objectives Explain what a microcontroller is Explain where microcontrollers are used Describe the Arduino prototyping platform Describe the Spartronics I experimenter board Program the Arduino What is a Microcontroller 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 Where are Microcontrollers Used Everywhere Car Phone Toothbrush Microwave oven Copier Television PC keyboard Appliances http ecomodder com wiki index php MPGuino The Arduino Platform Atmel ATmega328 microcontroller 14 digital I O pins 6 with PWM 6 analog I O pins 32 kB 2 kB Flash memory 2 kB RAM 1 kB EEPROM 16 MHz clock Rx Tx LEDs Pin 13 LED Digital Pins Power LED USB jack Voltage regulator Reset Button FTDI USB chip Microcontroller power jack Pwr GND Pins Analog Pins ICSP Header The Spartronics I Experimenter Board Potentiometer pot Momentary SPST pushbutton switch Piezo speaker RGB LED Connector for piezo speaker RGB LED Momentary Switch Pot R G B Cathode http www sparkfun com commerce images products 00105 03 L i ma jpg 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 Program sketch Must have setup loop setup configures pin modes and registers loop runs the main body of the program forever like while 1 Where is main Arduino simplifies things Does things for you Blink turns on an LED for 1 sec then off for 1 sec and repeats const byte ledPin 13 LED on digital pin 13 setup method runs once when the sketch starts void setup initialize the digital pin as an output pinMode ledPin OUTPUT loop method runs forever as long as the Arduino has power void loop digitalWrite ledPin HIGH set the LED on delay 1000 wait for a second digitalWrite ledPin LOW set the LED off delay 1000 wait for a second Using setup const byte ledPin 13 LED on digital pin 13 Digital pins can either be inputs or outputs Input the world outside the microcontroller determines the voltage applied to the pin we want to take information in void setup initialize the digital pin as an output pinMode ledPin OUTPUT sets whether a pin is an input or an output Output your program determines what the voltage on a pin is we want to send information out pinMode ledPin byte constant assigned the value of 13 OUTPUT is a macro defined constant for the value 1 INPUT where can you find out about the commands etc http arduino cc en Reference Extended Blinking the LED in loop digitalWrite Causes the voltage on the indicated pin to go HIGH or LOW Note must first configure the pin to be an output void loop digitalWrite ledPin HIGH turn LED on delay 1000 wait for a second digitalWrite ledPin LOW turn LED off delay 1000 wait for a second To make pin go to 5V high digitalWrite pin 1 To make pin go to 0V low digitalWrite pin 0 delay Causes the program to wait for a specified time in milliseconds http arduino cc en Reference Extended Spartronics Experimenter I Pinout Digital Pins 3 RGB LED red anode 4 Speaker 5 RGB LED green anode 6 RGB LED blue anode 7 Momentary push button switch Analog Pin 0 Pot digitalRead and digitalWrite Example 1 Write a program to turn on the LED on the Arduino connected to digital pin 13 when the button is pressed off otherwise Pseudocode define pin assignments configure pins which are input which are output loop forever if button is pressed make pin 13 high else make pin 13 low digitalRead and digitalWrite Example 2 Refine the pseudocode define pin assignments const byte ArduinoLEDpin 13 const byte pinButton 7 configure pins in function setup ArduinoLEDpin make it an OUTPUT pinButton INPUT make it an turn on pull up resistor on push button pin pin will read high until pulled low see schematic void setup pinMode ArduinoLEDpin OUTPUT pinMode pinButton INPUT digitalWrite pinButton HIGH digitalRead and digitalWrite Example 3 Refine the pseudocode cont loop forever use function loop If button is not pressed high 5V voltage on button pin 7 will be make pin 13 low LED will go off or stay off If button is pressed low 0V voltage on button pin 7 will be make pin 13 high LED will go on or stay on void loop if digitalRead pinButton LOW digitalWrite ArduinoLEDpin HIGH else digitalWrite ArduinoLEDpin LOW digitalRead and digitalWrite Example 4 Arduino program Suppose a change to the specifications LED on until button pressed then off Contrast mechatronic approach vs nonmechatronic re wire or re program separation of sensing elements from control elements LED button cntrl turns on Arduino LED when button on Experimenter board is pressed off otherwise pin assignments const byte ArduinoLEDpin 13 const byte pinButton 7 configure pins void setup pinMode ArduinoLEDpin OUTPUT pinMode pinButton INPUT digitalWrite pinButton HIGH loop forever void loop if digitalRead pinButton LOW digitalWrite ArduinoLEDpin HIGH else digitalWrite ArduinoLEDpin LOW digitalRead and digitalWrite Example 5 Arduino program Now want LED on until button pressed then off How to modify Pin assignments setup Need to turn on the LED loop Swap values of second argument in digitalWrite calls turns Arduino LED when LED button cntrl 2 LED button cntrl turns onoff Arduino LED when button button on on Experimenter Experimenter board board is on otherwise otherwise is pressed pressed off pin pin assignments assignments const byte ArduinoLEDpin const byte ArduinoLEDpin 13 13 const byte pinButton 7 const byte pinButton 7 configure configure pins pins void setup void setup pinMode ArduinoLEDpin pinMode ArduinoLEDpin OUTPUT OUTPUT pinMode pinButton INPUT pinMode pinButton INPUT digitalWrite pinButton digitalWrite pinButton HIGH HIGH digitalWrite ArduinoLEDpin HIGH turn on LED loop forever loop forever void loop void loop if digitalRead pinButton LOW if digitalRead pinButton LOW digitalWrite ArduinoLEDpin HIGH digitalWrite ArduinoLEDpin HIGH else elsedigitalWrite ArduinoLEDpin LOW digitalWrite ArduinoLEDpin LOW Analog In with Serial Out Read the pot Blink the LED on the Arduino at a rate proportional to the pot voltage Output the pot voltage to the serial monitor 0 0V 1023 5 V Initialize
View Full Document