H2 Introduction To Arduino
H2 Introduction To Arduino
1
Introduction to Arduino
• Arduino is a computation tool for sensing and controlling
signals
• It is more convenient and cost effective than using a personal
computer PC.
• It's an open-source system in terms of hardware and
software.
• You can download the Integrated Development Environment
(IDE) for your own OS from
http://arduino.cc/en/Main/Software
• Follow the instruction to install the IDE
• Useful Reference:
https://forum.arduino.cc/t/pdf-version-of-the-project-book/999501
Stem1: Introduction to Arduino v.0.a
2
(230522a)
Embedded system comparison
https://en.wikipedia.org/wiki/STM32
• ARM7, LPC2100, LPC2131, runs at 11Mz
• Arduino UNO ATmega328 Runs at 20Mz
• STM32F1 (M3) ,¥7.5
• Cortex M4, NuMicro family M4, STM32F4 ,180 MHz
STM32F4,@¥19.40,
• Raspberry Pi 3:1.1GHz Cortex-A53 550MHz
• Raspberry Pi 4:uses 64/32-bit quad-core ARM Cortex-
A72, runs at 1.5 GHz
• Apple A13,ARMv8.3-A,instruction A64,7µm,6
(2xLightning+ 4xThunder)2.65 GHz
Stem1: Introduction to Arduino v.0.a (2305 3
22a)
Download Arduino IDE, from
• https://www.arduino.cc/en/software. Pick “Just download” if
asked, it is ok if you don’t want to donate money.
• Pick your choice
Status Message
void loop()
{
// loops the content consecutively
// allowing the program to change and respond
}
Stem1: Introduction to Arduino v.0.a
16
(230522a)
Simple experiment
Try in the main Arudino IDE window:
Step1: Click File Examples 01. basics Blink
•Step2: Click Sketch upload (will run automatically)
or Click on the tick will has the effect
The program code (The LED on File 01Basics
the Arduino -Uno board will
blink) Blink
• // the setup function runs once when you press reset or power
Example
the board
• void setup() {
• // initialize digital pin LED_BUILTIN as an output.
• pinMode(LED_BUILTIN, OUTPUT);
• }
// the loop function runs over and over again forever
• void loop() {
• digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH
is the voltage level)
• delay(1000); // wait for a second
• digitalWrite(LED_BUILTIN, LOW); // turn the LED off by
making the voltage LOW
• delay(1000); // wait for a second
• } Stem1: Introduction to Arduino v.0.a (2305 17
22a)
Basic software functions
• Hardware related
– pinMode(), setup the functions of hardware pins
– digitalWrite() , set a pin to a digital level : either HIGH or LOW
– Digitalread(), read the digital level of a pin: either HIGH or LOW
– delay()
• Software related
– If-then-else
– For
– Switch-case
• Additional useful functions: Analog read/write
– analogRead() , read the level of an analog pin (any one of A0-A5), data value is from
0 to 1024 (equivalent to 0 - 5V)
– analogWrite(), set an analog pin (any one of A0-A5) to 0-5V
Stem1: Introduction to Arduino v.0.a
18
(230522a)
System setup procedures
• (Step 1) Setup the direction of the pins:
– using pinMode(),
• (Step 2) Then you can set a pin to : HIGH or
LOW
– (Step 2a) digitalWrite() //set pin to : HIGH ‘1’ or
LOW ‘0’
– or
– (step 2b) digitalRead(), //read state of pin: HIGH
‘1’ or LOW ‘0’
Stem1: Introduction to Arduino v.0.a
19
(230522a)
Basic Function (step1) – pinMode()
• pinMode() is used to configure the specified pin to behave
either as an input or output, or input_pullup
• Syntax Pin =0,..,13, or A0,A1,..,A5
Write comment for you to read
for Digital I/O, or
pinMode(pin, mode) // comment
– pin: the index number of the pin whose mode you wish to set
– mode: INPUT, OUTPUT, INPUT_PULLUP
– Example:
• pinMode(1, OUTPUT)//setup pin1 =digital out
• pinMode(3, INPUT)//setup pin3 =digital in
• pinMode(A3, INPUT)//setup A3 for digital in
• pinMode(A3, OUTPUT)//setup A3 for digital out
• If no PinMode applied to A0->A5, they are analog_in by default.
HIGH(5V) or LOW(0V)
• INPUT: Arduino
• OUTPUT:
HIGH(5V) or LOW (0V) Arduino
High(5V))
• INPUT_PULLUP:
1KΩ
When the pin is not Arduino
connect to HIGH(5V) or LOW)
or
anything, it is HIGH not_connected_to_anything
– pin: the number of the pin whose value you wish to set
– value: HIGH (5 V) or LOW (Ground)
– Example:
• digitalWrite(pin, value) // comment
• E.g
• digitalWrite(1, HIGH)//set pin1 to HIGH
Stem1: Introduction to Arduino v.0.a
22
(230522a)
Basic Function(step2b) – digitalRead()
• digitalWrite() is used to read the value from a
specified digital pin, either HIGH or LOW
digitalRead(pin)
• Syntax
– pin: the number of the pin whose mode you want to read
(integer)
– Example:
• digitalRead(pin)// read the state of the
• // it can be “HIGH” or “LOW”