Professional Documents
Culture Documents
USB port
12 V input
IMPORTANT TO NOTICE:
Depending whether the pin is set as an OUTPUT or INPUT the actual effect of
•
digitalWrite() is different
map(var1,var2,var3,var4,var5) Maps the value of var1 linearly form the range var2,
var3 to the range of var4, var5. Works with negative
numbers as well.
Arduino C – Basic functions
attachInterrupt(var1,var2,var3) Attaches an interrupt to a pin. Var1 is determined
by digitalPinToInterrupt(pin). Var2 is the function
to be ran. Var3 is the trigger mode of the interrupt.
detachInterrupt(var1) Detaches an interrupt. Var1 is again determined by
digitalPinToInterrupt(pin).
void setup()
{
pinMode(ledPin, OUTPUT); //set ledPin as output
}
void loop()
{
digitalWrite(ledPin, HIGH); //LED ON
delay(1000); //Wait 1000ms (=0,1s)
digitalWrite(ledPin, LOW); //LED OFF
delay(1000); //Wait 1000ms (=1s)
}
Uploading the program
1. Click Verify
The program is
checked 2.
1.
2. Click Upload
The program is
uploaded to the
Arduino mCu
board
Breadboard
Terminals with blue and red lines are called power
busses and are connected together horizontally.
Resistor LED
Capacitor Transistor
Inductor
Example 1 – Push Button
Reading digital input
Example 1 - Polling
Reading digital input (Using push button)
byte ledPin = 13;
byte buttonPin = 2;
void setup()
{
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW); //set ledPin off as default
pinMode(buttonPin, INPUT); //set buttonPin as input
digitalWrite(buttonPin, HIGH); //set the default state of the pin to HIGH (+5V)
}
void loop()
{
if(digitalRead(buttonPin) == LOW)
{
digitalWrite(ledPin, HIGH); //LED ON
}
else
{
digitalWrite(ledPin, LOW); //LED OFF
}
}
Example 1 - Interrupt
Using an interrupt for the same job
byte ledPin = 13;
byte buttonPin = 2;
bool ledState = 0;
void switchLED()
{
ledState = !ledState;
digitalWrite(ledPin, ledState);
}
void setup()
{
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW); //set ledPin off as default
pinMode(buttonPin, INPUT_PULLUP); //turn on the internal pullup
attachInterrupt(digitalPinToInterrupt(buttonPin), switchLED, CHANGE); //creates
the interrupt
}
void loop()
{
//waiting for interrupt
}
• Microcontrollers typically operate on low
voltages (0-5V). Even so you must be careful
when connecting devices.
• Know the electrical limits of the
microcontroller: Uno can handle max
5V/40mA per I/O pin
• Always double check the wiring! If you see
smoke it’s already too late!
P=U*I M U=R*I
• To prevent overloading a pin or a component
with excessive current you need to use a
resistor
• Example: Using an LED – Calculating the
required resistor size
• Operation voltage for LED: 5V
• Recommended current 23mA
PWM (Pulse Width Modulation)
• Basic terms
• 75 %
• 25 %
• 0%
PWM Applications
• Servomotor angle control
• DC motor speed control
• LED Dimming
• Audio generation
• Digitally generating analog voltages (requires
filtering)
PWM with Uno
• Total of 6 pins with PWM capability (~ symbol)
• PWM frequency
– Pins 5 & 6: default: 980Hz ~1kHz
(62.5kHz base with 64 as prescaler)
– Pins 3,9,10,11: default 490Hz ~0.5kHz
(31.25 kHz base with 64 as prescaler)
• Can be adjusted (30Hz-62.5kHz) with setPwmFrequency();
http://playground.arduino.cc/Code/PwmFrequency
CAUTION: Affects the delay() millis() and Servo functions
Example 2 – Control PWM LED
Using an analog input to control a PWM LED
Longer pin
Example 2 - Analog
Potentiometer controlled PWM LED
I/O
int ledPin = 3; //Variable to store the pin number
int potPin = A0; //Variable to store the pin number
void setup()
{
pinMode(ledPin, OUTPUT); //set ledPin as output
pinMode(potPin, INPUT); //set potPin as input
}
void loop()
{
int potValue = analogRead(potPin); //Read the value of the potentiometer
int ledValue = map(potValue, 0, 1023, 0, 255); //map to correct range
analogWrite(ledPin, ledValue); //Read the value of the potentiometer
delay(10); //To run the loop ~100 times a second
}
In the next exercise
• USB communication
• Sensors