Microcontroller Basics Lesson Stand Alone (1)
Microcontroller Basics Lesson Stand Alone (1)
Resistor LED
Capacitor Transistor
Inductor
Breadboard
Terminals with blue and red lines are called power
busses and are connected together horizontally.
void setup()
{
pinMode(ledPin, OUTPUT); //set ledPin as output
digitalWrite(ledPin, LOW); //Set the default state of the pin to GND(0 Volts)
}
void loop()
{
digitalWrite(ledPin, HIGH); //LED ON
delay(100); //Wait 100ms (=0,1s)
digitalWrite(ledPin, LOW); //LED OFF
delay(1000); //Wait 1000ms (=1s)
}
Uploading the program
1. Click Verify
2. Click Upload
3. Plug in and
reset Teensy
Arduino C – Basic functions
void setup() Setup function is run once, when the microcontroller
{}
boots up or resets.
void loop() After setup function the processor moves to run code
{}
inside the loop function. Code inside loop function will be
run over and over until the microcontroller is shut down.
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:
• It’s required to have both setup() and loop() functions in the code
• Depending whether the pin is set as an OUTPUT or INPUT the actual effect of
digitalWrite() is different
Example 2 - Setup
Reading digital input (Using push button)
Example 2 - Code
Reading digital input (Using push button)
byte ledPin = 6;
byte buttonPin = 12;
void setup()
{
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
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
}
}
P=U*I M U=R*I
• Microcontrollers typically operate on low
voltages (0-5V) →You must be careful when
connecting devices
• Know the electrical limits of the
microcontroller: Teensy can handle max
5V/40mA per 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
𝑈 5𝑉
• 𝑈 =𝑅∗𝐼 →𝑅 = = ≈ 220Ω
𝐼 0,023𝐴
Example 3 - Schematic
Example 3 – Breadboard Setup
Example 3 - Code
Controlling external LED with push button
byte ledPin = 24; //new ledPin value
byte buttonPin = 12;
void setup()
{
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
pinMode(buttonPin, INPUT);
digitalWrite(buttonPin, HIGH);
}
void loop()
{
if(digitalRead(buttonPin) == LOW)
{
digitalWrite(ledPin, HIGH); //LED ON
}
else
{
digitalWrite(ledPin, LOW); //LED OFF
}
}
You have just entered
the Matrix