Académique Documents
Professionnel Documents
Culture Documents
Microcontrollers part 1
BJ Furman
29OCT2012
What is a microcontroller?
How are microcontrollers used?
The Arduino hardware platform
The Spartronics Experimenter board
Programming the Arduino
Basic steps
Digital I/O
Analog I/O
Learning Objectives
Digital I/O
Analog I/O
What is a Microcontroller?
http://www.amazon.com/AVR-Pin-20MHz32K-ATMega328/dp/B004G5AVS6
Everywhere!
Car
Phone
Toothbrush
Microwave oven
Copier
Television
PC keyboard
Appliances
http://ecomodder.com/wiki/index.php/MPGuino
Atmel ATmega328
microcontroller
14 digital I/O pins
6 with PWM
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
Momentary SPST
push-button switches
Red LEDs
Piezo speaker
Potentiometer (pot)
Temperature sensor
Light sensor
Dual 7-segment display
RGB LED
RGB LED
speaker
Light sensor
R
G
Cathode
http://www.sparkfun.com/commerce/images/products/00105-03-L_i_ma.jpg
Pot
setup()
setup()
loop()
configures pin modes and
registers
loop()
like while(1) {}
Where is main() ?
Using setup()
Output
Information is taken in
pinMode()
Input
void setup()
{
// initialize the digital pin as an output:
pinMode(ledPin, OUTPUT);
}
http://arduino.cc/en/Reference/Extended
digitalWrite()
void setup()
{
// initialize the digital pin as an output:
pinMode(LED_PIN, OUTPUT);
}
To make pin go to 5V (high):
void loop()
{
digitalWrite(pin_num,HIGH);
Best to #define pin num. digitalWrite(LED_PIN, HIGH); // turn LED on
delay(DELAY_ON); // wait for DELAY_ON ms
To make pin go to 0V (low):
digitalWrite(LED_PIN, LOW); // turn LED off
digitalWrite(pin_num,LOW);
delay(DELAY_OFF); // wait for DELAY_OFF ms
}
delay()
12
Pull-up resistor ON
ATmega328
ATmega328
Pull-up resistor
VTG= +5V
PD3
0
VTG= +5V
PD3
0
11 - LED0 (red)
9 - LED1 (red) or RGB (green)
6 - LED2 (red) or RGB (blue)
3 - LED3 (red) or RGB (red)
13 - LED on Arduino
Assignments
13
12
11
10
SCK
MISO
MOSI
SS
OC1
ICP
AIN1
AIN0
T1
T0
INT1
INT0
TXD
RXD
LED
LED
LED
pwm
pwm
LED0
pwm
pwm
pwm
pwm
LED1
LED2
LED3
green
blue
red
piezo
servo
SW0
SW1
SW2
SW3
Assignments
7
photocell
POT
temp sensor
Two steps:
1.
2.
pinMode()
12;
8;
7;
4;
// button SW0
// button SW1
// button SW2
// button SW3
void setup()
{
pinMode(SW0, INPUT); // make SW0 an INPUT
digitalWrite(SW0, HIGH); // turn on pullup resistor
etc.
}
digitalWrite()
a 1 to the pin
Pseudocode:
define pin assignments
configure pins (which are input, which are output)
loop forever
RGB_blue_pin
SW0_pin
make it an OUTPUT
_______
INPUT
make it an ______
void setup()
{
pinMode(RGB_blue_pin, OUTPUT);
pinMode(SW0_pin, INPUT);
digitalWrite(SW0_pin, HIGH);
}
high (5V)
voltage on button pin 12 will be _______
make pin 6 voltage low (LED will go off or stay off)
If button is pressed:
low (0V)
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);
}
}
Arduino program
Suppose a change to the
specifications:
re-wire, or
re-program
the mechatronics
approach separates the
sensing elements from
the control elements
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
/* pin assignments */
const byte RGB_blue_pin = 6;
const byte SW0_pin = 12;
/* 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);
}
/* configure pins */
void setup()
{
pinMode(RGB_blue_pin, OUTPUT);
pinMode(SW0_pin, INPUT);
digitalWrite(SW0_pin, HIGH);
digitalWrite(RGB_blue_pin, HIGH);
}
/* loop forever */
void loop()
{
if(digitalRead(SW0_pin) == LOW)
digitalWrite(RGB_blue_pin, HIGH);
else
digitalWrite(RGB_blue_pin, LOW);
}
/* loop forever */
void loop()
{
if(digitalRead(SW0_pin) == LOW)
digitalWrite(RGB_blue_pin, LOW);
else
digitalWrite(RGB_blue_pin, HIGH);
}
Analog In with
Serial Out
0V0
5 V 1023
Initialize with
Serial.begin()
Map voltage to delay
Write a line with
Serial.print or
Serial.println
Front Panel
30% duty
cycle
Block Diagram
Arduino analogWrite( )
analogWrite(pin, value);
0 value 255
0% duty cycle --> 0 V --> analogWrite(pin, 0);
100% duty cycle --> 5 V --> analogWrite(pin, 255);
duty cycle is
incremented
then
decremented
256 steps
0% to 100%
Review
References
Microcontroller. (2009, November 20). In
Wikipedia, the free encyclopedia.
Retrieved November 21, 2009, from
http://en.wikipedia.org/wiki/Microcontroller
Arduino Home Page. (2009, November
21). Retrieved November 21, 2009, from
http://arduino.cc/