ARDUINO PART 1
Topics:
Microcontrollers
Programming Basics: structure and variables
Digital Output
What is a Microcontroller
• A small computer on a single
chip
• containing a processor,
memory, and input/output
• Typically "embedded" inside
some device that they control
• A microcontroller is often small
and low cost
What is a Development Board
A printed circuit board
designed to facilitate
work with a particular
microcontroller.
• Typical components include:
• power circuit
• programming interface
• basic input; usually buttons and LEDs
• I/O pins
The Arduino Development Board
Making-robots-with-arduino.pdf
What is the Arduino
What is Arduino?
• Open-source physical computing
platform based on a simple
microcontroller board and a
development environment for writing
software for the board
• Arduino comes with its own open-
source Integrated Development
Environment (IDE)
Getting Started
1. Download & install the Arduino environment (IDE)
2. Connect the board to your computer via the UBS cable
3. If needed, install the drivers (not needed in lab)
4. Launch the Arduino IDE
5. Select your board
6. Select your serial port
7. Open the blink example
8. Upload the program
Try It: Connect the USB Cable
todbot.com/blog/bionicarduino
Arduino Integrated Development Environment (IDE)
Parts of the IDE
• Compile –Before the program “code” can be sent to the
board, it needs to be converted into instructions that the
board understands. This process is called compiling.
• Stop - This stops the compilation process.
• Create new Sketch - This opens a new window to create a
new sketch.
• Open Existing Sketch - This loads a sketch from a file on
your computer.
• Save Sketch- This saves the changes to the sketch you are
working on.
• Upload to Board - This compiles and then transmits over
the USB cable to your board.
• Serial Monitor
• Tab Button - This lets you create multiple files in your
sketch.
• Sketch Editor - This is where you write or edit sketches
• Text Console - This shows you what the IDE is currently
doing and is also where error messages display if you make
a mistake in typing your program. (often called a syntax
error)
• Line Number -This shows you what line number your
cursor is on. It is useful since the compiler gives error
messages with a line number
Select Serial Port and Board
Status Messages
Add an External LED to pin 13
• File > Examples > Digital > Blink
• LED’s have polarity
– Negative indicated by flat side of the housing
and a short leg
www.instructables.com
A Little Bit About Programming
• Code is case
sensitive
• Statements are
commands and
must end with a
semi-colon
Our First Program
// Example 01
/*
Blinking Onboard LED
Turns on the onboard LED on for one second, then off for one second, repeatedly.
*/
// Digital Pin 13 has an LED connected on your Tinkduino.
int LED = 13;
// the setup routine runs once when you press reset:
void setup()
{
// initialize the digital pin as an output.
pinMode(LED, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop()
{
digitalWrite(LED, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
Setup and Loop
Each program must contain at least two functions.
A function is a series of programming statements
that can be called by name.
1. setup() which is called once when the program
starts.
Setting pin modes or initializing libraries
are placed here
2. loop() which is called repetitively over and over
again as long as the Arduino has power.
Syntax and Symbols
Comments
Variables
Variables
Variables
Terminology
Digital I/0
www.mikroe.com/chapters/view/1
pinMode(pin, mode)
Sets pin to either INPUT or OUTPUT
digitalRead(pin)
Reads HIGH or LOW from a pin
digitalWrite(pin, value)
Writes HIGH or LOW to a pin
Electronic stuff
Output pins can provide 40 mA of current
Writing HIGH to an input pin installs a 20KΩ pullup
Arduino Timing
• delay(ms)
– Pauses for a few milliseconds
• delayMicroseconds(us)
– Pauses for a few microseconds
Digital? Analog?
• Digital has two values: on and off
• Analog has many (infinite) values
• Computers don’t really do analog, they quantize
• Remember the 6 analog input pins---here’s how
they work
todbot.com/blog/bionicarduino
Bits and Bytes
Variables
www3.ntu.edu.sg
Putting It Together
• Complete the sketch
(program) below.
• What output will be
generated by this program?
• What if the schematic were
changed?
www.ladyada.net/learn/arduino