Introduction to Arduino

F3 ECA

2024-2025

Floor 4 - Physic Lab

Mr. Peter

Outline

Outline

Basic concepts of Arduino programming

1

Arduino Exercises

2

How to hold the Arduino

Arduino Structure

Arduino Programming Blocks

void setup() {

}

void loop() {

}

Arduino Programming Blocks

pinMode( pin, mode )

Pin Number

  1. 0 ~ 19 (14 ~ 19 can be represented as A0 ~ A5)
  2. On Arduino, the pin “~” indicates an analog signal
  3. LED_BUILTIN (Built-in LED Pin)

Mode

  1. INPUT                                                                           
  2. OUTPUT
  3. INPUT_PULLUP

 

digitalWrite( pin, signal )

Pin Number

  1. 0 ~ 19 (14 ~ 19 can be represented as A0 ~ A5)
  2. On Arduino, the pin “~” indicates an analog signal
  3. LED_BUILTIN (Built-in LED Pin)

Mode

  1. HIGH or 1                                                                      
  2. LOW or 0

 

delay( milliseconds )

Delay Milliseconds

  1. One thousandth of a second

  2. Input parameter 1000 represents a delay of one second

Arduino Programming Blocks

pinMode( pin, mode )

Pin Number

  1. 0 ~ 19 (14 ~ 19 can be represented as A0 ~ A5)
  2. On Arduino, the pin “~” indicates an analog signal
  3. LED_BUILTIN (Built-in LED Pin)

Mode

  1. INPUT                                                                           
  2. OUTPUT
  3. INPUT_PULLUP

 

digitalWrite( pin, signal )

Pin Number

  1. 0 ~ 19 (14 ~ 19 can be represented as A0 ~ A5)
  2. On Arduino, the pin “~” indicates an analog signal
  3. LED_BUILTIN (Built-in LED Pin)

Mode

  1. HIGH or 1                                                                      
  2. LOW or 0

 

delay( milliseconds )

Delay Milliseconds

  1. One thousandth of a second

  2. Input parameter 1000 represents a delay of one second

Arduino Programming Blocks

pinMode( pin, mode )

Pin Number

  1. 0 ~ 19 (14 ~ 19 can be represented as A0 ~ A5)
  2. On Arduino, the pin “~” indicates an analog signal
  3. LED_BUILTIN (Built-in LED Pin)

Mode

  1. INPUT                                                                           
  2. OUTPUT
  3. INPUT_PULLUP

 

digitalWrite( pin, signal )

Pin Number

  1. 0 ~ 19 (14 ~ 19 can be represented as A0 ~ A5)
  2. On Arduino, the pin “~” indicates an analog signal
  3. LED_BUILTIN (Built-in LED Pin)

Mode

  1. HIGH or 1                                                                      
  2. LOW or 0

 

delay( milliseconds )

Delay Milliseconds

  1. One thousandth of a second

  2. Input parameter 1000 represents a delay of one second

Ex01 - blink the builtin led

Ex01 - blink the builtin LED

Light up 2 times every second and light down for 1 second

Arduino - white board

Ex02 - blink the LED

Light up 3 times every second and light down for 1 second

Ex03 - Two LEDs

Ex04 - Two LEDs

Light up 3 times every second and light down for 1 second

Ex05 - Three LEDs

Three LEDs light up one by one.

Ex06 - Button Control

Light Up if Button is pressing, otherwise  light down

digitalRead( pin )

Pin Number

  1.  0 ~ 19 (14 ~ 19 can be represented as A0 ~ A5)
  2.  On Arduino, the pin “~” indicates an analog signal
  3.  LED_BUILTIN (Built-in LED Pin).                                           

Ex06 - Button Control

Light Up if Button is pressing, otherwise  light down

digitalRead( pin )

Pin Number

  1.  0 ~ 19 (14 ~ 19 can be represented as A0 ~ A5)
  2.  On Arduino, the pin “~” indicates an analog signal
  3.  LED_BUILTIN (Built-in LED Pin).                                           

Light on if Button is pressed, and light off if pressed again

Ex07 - Button Control

digitalRead( pin )

Pin Number

  1.  0 ~ 19 (14 ~ 19 can be represented as A0 ~ A5)
  2.  On Arduino, the pin “~” indicates an analog signal
  3.  LED_BUILTIN (Built-in LED Pin).                                           
bool ledState = false;

The code above shows how to define a true or false variable in Arduino

delay( milliseconds )

To prevent the code within the `loop()` function from repeatedly executing while a button is held down, a `delay()` function can be implemented after the button press is detected.

Light on if Button is pressed, and light off if pressed again

Ex07 - Button Control

digitalRead( pin )

Pin Number

  1.  0 ~ 19 (14 ~ 19 can be represented as A0 ~ A5)
  2.  On Arduino, the pin “~” indicates an analog signal
  3.  LED_BUILTIN (Built-in LED Pin).                                           
bool ledState = false;

The code above shows how to define a true or false variable in Arduino

delay( milliseconds )

To prevent the code within the `loop()` function from repeatedly executing while a button is held down, a `delay()` function can be implemented after the button press is detected.

Use Analog pin to light up a LED from low brigness to high brigness

Ex08 - Analog LEDs

analogWrite(pin, voltage);

Pin Number

  1. 14 ~ 19 or A0 ~ A5                                                       
  2. Analog pins only

Voltage

  1. 0 ~ 255                                                                       

 

  • Use two buttons to increase and decrease LED brightness
  • Implement PWM to control LED intensity
  • Display current brightness level on a series of LEDs

Ex09 - Analog LEDs Controls

analogWrite(pin, voltage);

Pin Number

  1. 14 ~ 19 or A0 ~ A5                                                       
  2. Analog pins only

Voltage

  1. 0 ~ 255                                                                       

 

  • Use two buttons to increase and decrease LED brightness
  • Implement PWM to control LED intensity
  • Display current brightness level on a series of LEDs

Ex09 - Analog LEDs Controls

analogWrite(pin, voltage);

Pin Number

  1. 14 ~ 19 or A0 ~ A5                                                       
  2. Analog pins only

Voltage

  1. 0 ~ 255                                                                       

 

  • Implement Exercise 05 (three LEDs), controlling the blinking speed.
  • Use two buttons to increase and decrease the LED blinking speed.

Ex10 - LEDs speed Controls

F3 ECA - Arduino Ex01/Ex02

By Mr Peter

F3 ECA - Arduino Ex01/Ex02

  • 78