You are on page 1of 26

Microcontroller basics

Starting with Arduino


Exercise 1

Working with Arduino IDE

Basic electric components

Your first program

Digital input and output

Analog input and output

Interrupts
Arduino IDE
• Download from:
http://arduino.cc/en/Main/Software
About Arduino Uno
Reset button 14 Digital I/O pins (6 PWM)

USB port

12 V input

6 Analog Input pins


3.3 V and 5 V outputs
Setting up Arduino IDE
Structure of an Arduino code

Before going to the setup function constant


int pin = 1; 1. Define Variables
variables should be defined

void setup() 2. Setting up Setup function is run once, when the


{} functions microcontroller boots up or resets.

After setup function the processor moves to


void loop() run code inside the loop function. Code inside
{} 3. Eternal loop
loop function will be run over and over until
the microcontroller is shut down.
• It’s required to have both setup() and loop()
functions in the code
Arduino C – Basic functions
pinMode(var1, var2) pinMode functions sets the mode of given pin. Var1 is the
number of the pin and var2 is the mode (INPUT,
INPUT_PULLUP, OUTPUT)
digitalWrite(var1, var2) digitalWrite changes the status of the pin. Var1 is the
number of the pin and var2 is the status (LOW, HIGH).

IMPORTANT TO NOTICE:
Depending whether the pin is set as an OUTPUT or INPUT the actual effect of

digitalWrite() is different

digitalRead(var1) digitalRead returns the current status (LOW, HIGH) of the


pin. Var1 is the number of the pin.
Arduino C – Basic functions
analogRead(var1) AnalogRead returns a 10-bit (by default) value equal to
the voltage of the pin relative to the analog reference
voltage.
analogWrite(var1, var2) AnalogWrite sets a pin (var1) to a voltage relative to
the analog reference voltage equal to an 8-bit (by
default) value.
analogReadResolution(var1)/ Changes the resolution of the corresponding function
analogWriteResolution(var1) to var1.

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).

noInterrupts() Disables interrupts. Used when you are running


time sensitive code for example

interrupts() Enables interrupts disabled by noInterrupts().


Writing your first program
Basic blinking LED
int ledPin = 13; //Variable to store the pin number

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.

Terminals in the middle are connected together


vertically. The gap in the middle separates the two
sides.
Basic Components
Passive components Active components

Resistor LED

Capacitor Transistor

Push button Integrated circuit (IC)

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

• Duty cycle = Pulse Width / Wave Period


Duty cycle
• 100 %

• 75 %

• 50 % ←Ideal Square Wave

• 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

You might also like